From max at tyndur.org Mon Aug 17 17:14:26 2009 From: max at tyndur.org (Max Reitz) Date: Mon, 17 Aug 2009 17:14:26 +0200 Subject: [Cdi-devel] [PATCH] UHCI, USB, MSC Message-ID: <4A8973D2.3050204@tyndur.org> + UHCI/USB/MSC (MSDs erscheinen als CDI.storage), der Schreibsupport ist gro?e Gl?ckssache, Treiber nur unter QEMU erfolgreich getestet... Signed-off-by: Max Reitz --- usb/include/msd.h | 104 +++++++++ usb/include/uhci.h | 110 ++++++++++ usb/include/usb.h | 287 +++++++++++++++++++++++++ usb/main.c | 451 +++++++++++++++++++++++++++++++++++++++ usb/msd.c | 599 ++++++++++++++++++++++++++++++++++++++++++++++++++++ usb/uhci.c | 339 +++++++++++++++++++++++++++++ 6 files changed, 1890 insertions(+), 0 deletions(-) create mode 100644 usb/include/msd.h create mode 100644 usb/include/uhci.h create mode 100644 usb/include/usb.h create mode 100644 usb/main.c create mode 100644 usb/msd.c create mode 100644 usb/uhci.c diff --git a/usb/include/msd.h b/usb/include/msd.h new file mode 100644 index 0000000..99c796d --- /dev/null +++ b/usb/include/msd.h @@ -0,0 +1,104 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef _CDI__USB__MSD_H +#define _CDI__USB__MSD_H + +#include + +#include "cdi/storage.h" + +#include "usb.h" + + +//"USBC" +#define CBW_SIGNATURE 0x43425355 +struct command_block_wrapper +{ + uint32_t cbw_signature; + uint32_t cbw_tag; + uint32_t cbw_data_transfer_length; + uint8_t cbw_flags; + uint8_t cbw_lun; + uint8_t cbw_cb_length; + uint8_t cbw_cb[16]; +} __attribute__((packed)); + +//"USBS" +#define CSW_SIGNATURE 0x53425355 +struct command_status_wrapper +{ + uint32_t csw_signature; + uint32_t csw_tag; + uint32_t csw_data_residue; + uint8_t csw_status; +} __attribute__((packed)); + +struct msc_sense +{ + unsigned error : 7; + unsigned valid : 1; + uint8_t rsvd0; + unsigned sense_key : 4; + unsigned rsvd1 : 4; + uint32_t information; + uint8_t additional_length; + uint32_t rsvd2; + uint8_t additional_code; + uint8_t additional_code_qualifier; + uint32_t rsvd; +} __attribute__((packed)); + +struct msd_capacity +{ + uint32_t last_lba; + uint32_t block_length; +} __attribute__((packed)); + +struct cdi_msd +{ + struct cdi_storage_device cdi_device; + struct usb_device *usb_device; + uint32_t offset; //F?r Partitionen +}; + +struct part_table_entry +{ + uint8_t active; + uint8_t begin_chs[3]; + uint8_t type; + uint8_t end_chs[3]; + uint32_t start; + uint32_t size; +} __attribute__((packed)); + + +#define MSC_CMD_REZERO 0x01 +#define MSC_CMD_SENSE 0x03 +#define MSC_CMD_CAPACITY 0x25 +#define MSC_CMD_READ10 0x28 +#define MSC_CMD_WRITE10 0x2A +#define MSC_CMD_SEEK 0x2B +#define MSC_CMD_READ12 0xA8 +#define MSC_CMD_WRITE12 0xAA + +#endif diff --git a/usb/include/uhci.h b/usb/include/uhci.h new file mode 100644 index 0000000..0158bd2 --- /dev/null +++ b/usb/include/uhci.h @@ -0,0 +1,110 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef _CDI__USB__UHCI_H +#define _CDI__USB__UHCI_H + +#include + +#include "cdi/misc.h" +#include "cdi/pci.h" + +#include "usb.h" + + +#define UHCI_USBCMD 0x00 //w +#define UHCI_USBSTS 0x02 //w +#define UHCI_USBINTR 0x04 //w +#define UHCI_FRNUM 0x06 //w +#define UHCI_FRBASEADD 0x08 //l +#define UHCI_SOFMOD 0x0C //b +#define UHCI_RPORTS 0x10 +#define UHCI_PORTSC1 0x10 //w +#define UHCI_PORTSC2 0x12 //w + +#define MAXP 0x0080 +#define GRESET 0x0004 +#define HCRESET 0x0002 +#define USB_RUN 0x0001 + +#define RPORT_RESET 0x0200 +#define RPORT_LOSPD 0x0100 //Low speed device attached +#define RPORT_ENABLE 0x0004 +#define RPORT_CSC 0x0002 //Connect status change +#define RPORT_DEVICE 0x0001 + + +struct uhci +{ + struct hci gen_hci; + uint16_t pbase; + uintptr_t phys_frame_list; + uint32_t *frame_list; + int root_ports; +}; + +struct uhci_qh +{ + volatile uint32_t next; + volatile uint32_t transfer; +} __attribute__((packed)); + +struct uhci_td +{ + volatile uint32_t next; + + unsigned trans_len : 11; + unsigned rsvd0 : 6; + unsigned bitstuff_err : 1; + unsigned crc_time_err : 1; + unsigned nak : 1; + unsigned babble : 1; + unsigned buf_err : 1; + unsigned stalled_err : 1; + unsigned active : 1; + unsigned ioc : 1; + unsigned isochronous : 1; + unsigned low_speed : 1; + unsigned errors : 2; + unsigned spd : 1; + unsigned rsvd1 : 2; + + unsigned pid : 8; + unsigned device : 7; + unsigned endpoint : 4; + unsigned data_toggle : 1; + unsigned rsvd2 : 1; + unsigned maxlen : 11; + + uint32_t buffer; + + uint32_t user[4]; +} __attribute__((packed)); + +struct transfer +{ + void *virt; + uintptr_t phys; + volatile int error; +}; + +#endif diff --git a/usb/include/usb.h b/usb/include/usb.h new file mode 100644 index 0000000..2c435d8 --- /dev/null +++ b/usb/include/usb.h @@ -0,0 +1,287 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef _CDI__USB__USB_H +#define _CDI__USB__USB_H + +#include + +#include "cdi/misc.h" +#include "cdi/pci.h" + + +#define PACKET_SETUP 0x2D +#define PACKET_IN 0x69 +#define PACKET_OUT 0xE1 + +#define NO_DATA 0x01 +#define DEV_TO_HOST 0x80 +#define HOST_TO_DEV 0x00 +#define STD_REQUEST 0x00 +#define CLS_REQUEST 0x20 +#define VEN_REQUEST 0x40 +#define REC_DEVICE 0x00 +#define REC_INTERFACE 0x01 +#define REC_ENDPOINT 0x02 +#define REC_OTHER 0x03 + +#define GET_STATUS 0 +#define CLEAR_FEATURE 1 +#define SET_FEATURE 3 +#define SET_ADDRESS 5 +#define GET_DESCRIPTOR 6 +#define SET_DESCRIPTOR 7 +#define GET_CONFIGURATION 8 +#define SET_CONFIGURATION 9 +#define GET_INTERFACE 10 +#define SET_INTERFACE 11 +#define SYNC_FRAME 12 + +#define PORT_DEVICE 0x0001 +#define PORT_ENABLED 0x0002 +#define PORT_SUSPENDED 0x0004 +#define PORT_OVERCURRENT 0x0008 +#define PORT_RESET 0x0010 +#define PORT_POWER 0x0100 +#define PORT_LOWSPEED 0x0200 +#define PORT_HIGHSPEED 0x0400 +#define PORT_TEST 0x0800 +#define PORT_INDICATOR 0x1000 + +#define PORTF_CONNECTION 0 +#define PORTF_ENABLE 1 +#define PORTF_SUSPEND 2 +#define PORTF_OVERCURRENT 3 +#define PORTF_RESET 4 +#define PORTF_POWER 8 +#define PORTF_LOWSPEED 9 +#define PORTF_HIGHSPEED 10 +#define PORTF_C_CONNECTION 16 +#define PORTF_C_ENABLE 17 +#define PORTF_C_SUSPEND 18 +#define PORTF_C_OVERCURRENT 19 +#define PORTF_C_RESET 20 +#define PORTF_TEST 21 +#define PORTF_INDICATOR 22 + +#define DESC_DEVICE 1 +#define DESC_CONFIGURATION 2 +#define DESC_STRING 3 +#define DESC_INTERFACE 4 +#define DESC_ENDPOINT 5 +#define DESC_DEVICE_QUALIFIER 6 +#define DESC_OTHER_SPEED_CONFIGURATION 7 +#define DESC_INTERFACE_POWER 8 + +#define USB_NO_ERROR 0x00 +#define USB_STALLED 0x01 +#define USB_BUFFER_ERROR 0x02 +#define USB_BABBLE 0x04 +#define USB_NAK 0x08 +#define USB_CRC 0x10 +#define USB_TIMEOUT 0x20 +#define USB_BITSTUFF 0x40 +#define USB_TRIVIAL_ERROR 0x80 + +#define USB_TOD_SETUP 0x0001 +#define USB_TOD_SETUP_DATA_IN 0x0002 +#define USB_TOD_SETUP_DATA_OUT 0x0004 +#define USB_TOD_SETUP_ACK_IN 0x0008 +#define USB_TOD_SETUP_ACK_OUT 0x0010 +#define USB_TOD_COMMAND 0x0020 +#define USB_TOD_DATA_IN 0x0040 +#define USB_TOD_DATA_OUT 0x0080 +#define USB_TOD_STATUS 0x0100 + +#define USB_TODS_SETUP 0 +#define USB_TODS_SETUP_DATA_IN 1 +#define USB_TODS_SETUP_DATA_OUT 2 +#define USB_TODS_SETUP_ACK_IN 3 +#define USB_TODS_SETUP_ACK_OUT 4 +#define USB_TODS_COMMAND 5 +#define USB_TODS_DATA_IN 6 +#define USB_TODS_DATA_OUT 7 +#define USB_TODS_STATUS 8 + + +typedef enum +{ + HCI_UHCI, + HCI_OHCI, + HCI_EHCI +} hci_type_t; + +struct cdi_hci +{ + struct cdi_device cdi_device; + struct hci *hci; +}; + +struct usb_device; + +struct hci +{ + struct cdi_pci_device *pcidev; + hci_type_t type; + cdi_list_t (*find_devices)(struct hci *); + void (*activate_device)(struct hci *, struct usb_device *); + int (*do_packet)(struct hci *, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle); + int (*get_frame)(struct hci *); +}; + +struct setup_packet +{ + uint8_t request_type; + uint8_t request; + uint16_t value; + uint16_t index; + uint16_t length; +} __attribute__((packed)); + +struct device_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint16_t bcdUSB; + uint8_t class_id; + uint8_t subclass_id; + uint8_t protocol_id; + uint8_t max_packet_size0; + uint16_t vendor_id; + uint16_t device_id; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t num_configurations; +} __attribute__((packed)); + +struct config_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint16_t total_length; + uint8_t num_interfaces; + uint8_t config_value; + uint8_t iConfiguration; + uint8_t attributes; + uint8_t max_power; +} __attribute__((packed)); + +struct interface_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint8_t interface_number; + uint8_t alternate_setting; + uint8_t num_endpoints; + uint8_t interface_class; + uint8_t interface_subclass; + uint8_t interface_protocol; + uint8_t iInterface; +} __attribute__((packed)); + +struct endpoint_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint8_t endpoint_address; + uint8_t attributes; + uint16_t max_packet_size; + uint8_t interval; +} __attribute__((packed)); + +struct hub_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint8_t nbr_ports; + uint16_t hub_characteristics; + uint8_t pwron2pwrgood; + uint8_t hub_contr_current; +} __attribute__((packed)); + +typedef enum +{ + USBDC_COMPOSITE = 0x00, + USBDC_AUDIO = 0x01, + USBDC_COMM_CDC = 0x02, + USBDC_HID = 0x03, + USBDC_PID = 0x05, + USBDC_IMAGE = 0x06, + USBDC_PRINTER = 0x07, + USBDC_MSC = 0x08, + USBDC_HUB = 0x09, + USBDC_CDC_DATA = 0x0A, + USBDC_CHIP = 0x0B, + USBDC_SECURITY = 0x0D, + USBDC_VIDEO = 0x0E, + USBDC_HEALTH = 0x0F, + USBDC_DIAGNOSIS = 0xDC, + USBDC_WIRELESS = 0xE0, + USBDC_MISC = 0xEF, + USBDC_SOFT_SPEC = 0xFE, + USBDC_VENDOR_SPEC = 0xFF +} usb_class_t; + +struct class_data +{ + usb_class_t type; +}; + +struct usb_device +{ + struct hci *hci; + int id; + int port; + int low_speed; + struct device_desc *device; + struct config_desc *config; + struct interface_desc *interface; + struct class_data *classd; + int stalled; + int locked; + int expects; +}; + +struct msclass_data +{ + struct class_data gen_class; + struct endpoint_desc *bulk_ep_in; + struct endpoint_desc *bulk_ep_out; +}; + + +#include "uhci.h" + + +#define HCI_STRUCT_SIZE sizeof(struct uhci) + + +int do_packet(struct usb_device *device, int type, int endpoint, uintptr_t phys_data, int length, int datatoggle, int type_of_data); +void enumerate_hci(struct hci *); +struct cdi_driver *init_uhcd(void); +void init_msc_driver(void); +void register_msd(struct usb_device *usbdev); +void uhci_init(struct cdi_device *cdi_hci); + +#endif diff --git a/usb/main.c b/usb/main.c new file mode 100644 index 0000000..c06f15d --- /dev/null +++ b/usb/main.c @@ -0,0 +1,451 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include +#include + +#include "cdi/io.h" +#include "cdi/misc.h" +#include "cdi/pci.h" + +#include "usb.h" + +#define DEBUG + +#ifdef DEBUG +#include +#define dprintf(fmt, args...) printf("[usb] " fmt, ##args) +#define _dprintf(fmt, args...) printf(fmt, ##args) +#else +static int dprintf(const char *fmt, ...) +{ + return 0; +} +#define _dprintf(fmt, args...) dprintf(fmt, ##args) +#endif + +static struct usb_device *devices[128]; +static cdi_list_t ehci, ohci, uhci; +static void (*ccdriver_take_device[0x100])(struct usb_device *); +static int usb_dev_ids = 1; + +static void usb_init(void); +static void enumerate_hub(struct usb_device *usbdev); + +#ifdef CDI_STANDALONE +int main(void) +#else +int init_usb(void) +#endif +{ + cdi_init(); + + memset(devices, 0, 128 * sizeof(struct usb_device *)); + + usb_init(); + + cdi_run_drivers(); + + return 0; +} + +static const int next_data_type[9] = { + USB_TOD_SETUP_DATA_IN | USB_TOD_SETUP_DATA_OUT | USB_TOD_SETUP_ACK_IN, + USB_TOD_SETUP_ACK_OUT, + USB_TOD_SETUP_ACK_IN, + USB_TOD_SETUP | USB_TOD_COMMAND, + USB_TOD_SETUP | USB_TOD_COMMAND, + USB_TOD_DATA_IN | USB_TOD_DATA_OUT | USB_TOD_STATUS, + USB_TOD_STATUS, + USB_TOD_STATUS, + USB_TOD_SETUP | USB_TOD_COMMAND +}; + +/*static const char *tod_name[9] = { + "SETUP", + "SETUP DS IN", + "SETUP DS OUT", + "SETUP ACK IN", + "SETUP ACK OUT", + "COMMAND", + "DATA IN", + "DATA OUT", + "STATUS" +};*/ + +/** +* Verarbeitet ein USB-Paket. +* +* @param device Das USB-Ger?t +* @param type Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) +* @param endpoint Der gew?nschte Endpoint (0 bis 15) +* @param phys_data Die physische Adresse des zu verwendenden Datenpuffers +* @param length Die L?nge des Puffers +* @param datatoggle Gibt an, ob DATA0 (0) oder DATA1 (1) verwendet werden soll +* @param type_of_data Typ der Daten (zur Sicherheit, damit es kein doofes STALL gibt) +*/ + +int do_packet(struct usb_device *device, int type, int endpoint, uintptr_t phys_data, int length, int datatoggle, int type_of_data) +{ + int error; + int tod_short; + + if (!(device->expects & type_of_data)) { + dprintf("Expected 0x%04X, got 0x%04X...?\n", device->expects, type_of_data); + for (;;); + } + type_of_data >>= 1; + for (tod_short = 0; type_of_data; tod_short++) { + type_of_data >>= 1; + } + //dprintf("TOD: %s\n", tod_name[tod_short]); + device->expects = next_data_type[tod_short]; + endpoint &= 0x07; + type &= 0xFF; + if (device->stalled) //TODO: Irgendwie entstallen, evtl.? oO + { + dprintf("Zugriff auf stalled-Ger?t verweigert.\n"); + return USB_STALLED; + } + error = device->hci->do_packet(device->hci, + (device->hci->get_frame(device->hci) + 3) & 0x3FF, + type, device->id, endpoint, + device->low_speed, phys_data, length, + datatoggle); + if (error == USB_STALLED) + { + printf("[usb] ENDPOINT %i DES GER?TS %i STALLED!\n", endpoint, device->id); + device->stalled = 1; + } + return error; +} + +static void *do_control(struct usb_device *device, int direction, void *buffer, int length, int rtype, int recipient, int request, int value, int index) +{ + void *rbuf; + int rval, no_data; + struct setup_packet *setup; + uintptr_t pbuf, psetup; + + no_data = direction & NO_DATA; + if (!no_data && (cdi_alloc_phys_mem(length, &rbuf, (void **)&pbuf) == -1)) { + return NULL; + } + if (cdi_alloc_phys_mem(sizeof(struct setup_packet), + (void **)&setup, (void **)&psetup) == -1) { + return NULL; + } + direction &= 0x80; + if ((direction == HOST_TO_DEV) && length && (buffer != NULL)) { + memcpy(rbuf, buffer, length); + } + setup->request_type = direction | (rtype & 0x60) | (recipient & 0x1F); + setup->request = request; + setup->value = value; + setup->index = index; + setup->length = length; + if (do_packet(device, PACKET_SETUP, 0, psetup, sizeof(struct setup_packet), + 0, USB_TOD_SETUP) != USB_NO_ERROR) { + return NULL; + } + cdi_sleep_ms(20); + if (no_data) + { + rval = USB_NO_ERROR; + rbuf = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten + } + else + { + if (direction == DEV_TO_HOST) { + rval = do_packet(device, PACKET_IN, 0, pbuf, length, 1, + USB_TOD_SETUP_DATA_IN); + } else { + rval = do_packet(device, PACKET_OUT, 0, pbuf, length, 1, + USB_TOD_SETUP_DATA_OUT); + } + } + if (rval == USB_NO_ERROR) + { + cdi_sleep_ms(20); + if (no_data || (direction == HOST_TO_DEV)) { + rval = do_packet(device, PACKET_IN, 0, 0, 0, 1, USB_TOD_SETUP_ACK_IN); + } else { + rval = do_packet(device, PACKET_OUT, 0, 0, 0, 1, USB_TOD_SETUP_ACK_OUT); + } + } + return (rval == USB_NO_ERROR) ? rbuf : NULL; +} + +static void usb_init(void) +{ + struct cdi_pci_device *dev; + struct hci *hci; + struct cdi_hci *cdi_hci; + struct cdi_driver *uhcd; + char *dev_name; + int i; + + init_msc_driver(); + + memset(ccdriver_take_device, 0, sizeof(void (*)(struct usb_device *)) * 0x100); + ccdriver_take_device[8] = ®ister_msd; + ccdriver_take_device[9] = &enumerate_hub; + + ehci = cdi_list_create(); + ohci = cdi_list_create(); + uhci = cdi_list_create(); + + cdi_list_t pci_devices = cdi_list_create(); + cdi_pci_get_all_devices(pci_devices); + for (i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) + { + if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03)) { + cdi_pci_device_destroy(dev); + } else { + switch (dev->interface_id) + { + case 0x00: + hci = malloc(HCI_STRUCT_SIZE); + hci->pcidev = dev; + hci->type = HCI_UHCI; + cdi_list_push(uhci, hci); + break; + case 0x10: + hci = malloc(HCI_STRUCT_SIZE); + hci->pcidev = dev; + hci->type = HCI_OHCI; + cdi_list_push(ohci, hci); + break; + case 0x20: + hci = malloc(HCI_STRUCT_SIZE); + hci->pcidev = dev; + hci->type = HCI_EHCI; + cdi_list_push(ehci, hci); + } + } + } + + cdi_list_destroy(pci_devices); + + dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n", cdi_list_size(ehci), + cdi_list_size(ohci), + cdi_list_size(uhci)); + + uhcd = init_uhcd(); + + for (i = 0; (hci = cdi_list_pop(uhci)) != NULL; i++) + { + cdi_hci = malloc(sizeof(struct cdi_hci)); + cdi_hci->cdi_device.type = CDI_UNKNOWN; + dev_name = malloc(10); + sprintf(dev_name, "uhci%i", i); + cdi_hci->cdi_device.name = dev_name; + hci->find_devices = NULL; + cdi_hci->hci = hci; + //cdi_list_push(uhcd->devices, cdi_hci); + //^ Das ist zwar sehr sch?n, geht so aber leider nicht + dprintf("%s registriert.\n", dev_name); + cdi_hci->cdi_device.driver = uhcd; + uhci_init(&cdi_hci->cdi_device); + } + + cdi_list_destroy(ehci); + cdi_list_destroy(ohci); + cdi_list_destroy(uhci); +} + +static void enum_device(struct usb_device *usbdev) +{ + struct device_desc *dev_desc; + struct config_desc *conf_desc; + struct interface_desc *if_desc, *best_if; + char *name; + void *position; + int i, id; + + usbdev->stalled = 0; + usbdev->locked = 0; + usbdev->expects = USB_TOD_SETUP | USB_TOD_COMMAND; + dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_DEVICE << 8, 0); + if (dev_desc == NULL) { + return; + } + usbdev->device = dev_desc; + dprintf("0x%04X:0x%04X (%i) -> ", dev_desc->vendor_id, dev_desc->device_id, + dev_desc->class_id); + do_control(usbdev, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_ADDRESS, (id = usb_dev_ids++), 0); + usbdev->id = id; + _dprintf("%i\n", id); + if (dev_desc->iManufacturer) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iManufacturer, 0); + if (name == NULL) { + return; + } + dprintf(" -> Hersteller: "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf("\n"); + } + if (dev_desc->iProduct) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iProduct, 0); + if (name == NULL) { + return; + } + dprintf(" -> Produkt: "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf("\n"); + } + devices[id] = usbdev; + //TODO: Man kann doch nicht immer den ersten nehmen... + conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, + sizeof(struct config_desc), STD_REQUEST, REC_DEVICE, + GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return; + } + conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, conf_desc->total_length, + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return; + } + usbdev->config = conf_desc; + do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_CONFIGURATION, conf_desc->config_value, 0); + if (conf_desc->iConfiguration) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | conf_desc->iConfiguration, 0); + if (name == NULL) { + return; + } + dprintf("Verwende Konfiguration "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf(".\n"); + } + position = conf_desc; + position += sizeof(struct config_desc); + best_if = position; //Standard-IF + for (i = 0; i < conf_desc->num_interfaces; i++) + { + if_desc = position; + if (if_desc->interface_class == 8) //TODO: Mehr Klassencodes nat?rlich! + { + best_if = if_desc; + break; + } + position += sizeof(struct interface_desc) + if_desc->num_endpoints * sizeof(struct endpoint_desc); + } + do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_INTERFACE, best_if->interface_number, 0); + usbdev->interface = best_if; + if (best_if->iInterface) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | best_if->iInterface, 0); + if (name == NULL) { + return; + } + dprintf("Verwende Interface "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf(".\n"); + } + dprintf("Konfiguration: %i:%i (%i)\n", conf_desc->config_value, + best_if->interface_number, + best_if->interface_class); + if (ccdriver_take_device[best_if->interface_class] != NULL) { + ccdriver_take_device[best_if->interface_class](usbdev); + } +} + +static void enumerate_hub(struct usb_device *usbdev) +{ + struct hub_desc *hub_desc; + struct usb_device *down; + uint16_t *port_status; + int i; + + //Mehr als 8 Ports sollte es nicht geben, die Gr??e des Deskriptors wird + //daher wohl nicht gr??er als sizeof()+16 werden + hub_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*hub_desc) + 16, + CLS_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_DEVICE << 8, 0); + for (i = 0; i < hub_desc->nbr_ports; i++) + { + port_status = do_control(usbdev, DEV_TO_HOST, NULL, 4, CLS_REQUEST, + REC_OTHER, GET_STATUS, 0, i+1); + if (!(port_status[0] & PORT_DEVICE)) { + continue; + } + //Strom an + do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, + SET_FEATURE, PORTF_POWER, i+1); + cdi_sleep_ms(hub_desc->pwron2pwrgood * 2); + //Resetten + do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, + SET_FEATURE, PORTF_RESET, i+1); + //Reset sollte jetzt eigtl. schon beendet sein + //(do_control wartet ja 50 ms) + down = malloc(sizeof(struct usb_device)); + down->hci = usbdev->hci; + down->id = 0; + down->port = i; + down->low_speed = (port_status[0] & PORT_LOWSPEED) && 1; + enum_device(down); + } +} + +void enumerate_hci(struct hci *hci) +{ + struct usb_device *usbdev; + cdi_list_t usb_devices; + + if (hci->find_devices != NULL) + { + usb_devices = hci->find_devices(hci); + while ((usbdev = cdi_list_pop(usb_devices)) != NULL) + { + hci->activate_device(hci, usbdev); + cdi_sleep_ms(50); + enum_device(usbdev); + } + } +} diff --git a/usb/msd.c b/usb/msd.c new file mode 100644 index 0000000..11fbd6a --- /dev/null +++ b/usb/msd.c @@ -0,0 +1,599 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include +#include +#include + +#include "cdi/misc.h" +#include "cdi/storage.h" + +#include "msd.h" +#include "usb.h" + +//DEBUG definieren, um einige Debugmeldungen anzuzeigen +#define DEBUG +//FULLDEBUG definieren, um ALLE Debugmeldungen anzuzeigen +#define FULLDEBUG + +#if defined FULLDEBUG && !defined DEBUG +#define DEBUG +#endif + +#ifdef DEBUG +#define dprintf(fmt, args...) printf("[usb-msc] " fmt, ##args) +#define _dprintf(fmt, args...) printf(fmt, ##args) +#else +static int dprintf(const char *fmt, ...) +{ + return 0; +} +#define _dprintf(fmt, args...) dprintf(fmt, ##args) +#endif + +#ifdef FULLDEBUG +#define fdprintf(fmt, args...) printf("[usb-msc] " fmt, ##args) +#else +static int fdprintf(const char *fmt, ...) +{ + return 0; +} +#endif + +static uint32_t cbw_tag = 1; + +static const char *driver_name = "usb-msc"; + +static struct cdi_storage_driver cdi_driver; + +static void deinit_msd(struct cdi_device *cdi_msd); +static void get_partitions(struct cdi_storage_device *cdistrg); +static void init_msd(struct cdi_device *cdi_msd); +static void kill_driver(struct cdi_driver *cdi_msc_driver); +static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer); +static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer); +static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count); + +//TODO: CDI-Funktion w?re hier sicher n?tzlich... +#define CPU_IS_LITTLE_ENDIAN + +#define change_endianess_32(_) ((((_)&0xFF000000)>>24)|(((_)&0xFF0000)>>8)|(((_)&0xFF00)<<8)|(((_)&0xFF)<<24)) +#ifdef CPU_IS_LITTLE_ENDIAN +#define CPU2BE(_) change_endianess_32(_) +#define CPU2LE(_) (_) +#define BE2CPU(_) change_endianess_32(_) +#define LE2CPU(_) (_) +#else +#define CPU2BE(_) (_) +#define CPU2LE(_) change_endianess_32(_) +#define BE2CPU(_) (_) +#define LE2CPU(_) change_endianess_32(_) +#endif + +//Das muss definiert werden, um vor einem Befehl zu warten, bis das MSD fertig +//ist. Theoretisch ist das ?u?erst sinnvoll, praktisch h?ngt sich der Treiber +//bisweilen auf - das sollte allerdings mit dem Timeout gegessen sein +/** +#define WAIT_FOR_MSD_READY +*/ + +//TODO: Storage bietet eine SCSI-Klasse. Die k?nnte man durchaus benutzen. xD + +void init_msc_driver() +{ + struct cdi_driver *stddrv = (struct cdi_driver *)&cdi_driver; + stddrv->type = CDI_STORAGE; + stddrv->name = driver_name; + stddrv->init_device = &init_msd; + stddrv->remove_device = &deinit_msd; + stddrv->destroy = &kill_driver; + cdi_driver.read_blocks = &msd_cdi_read; + cdi_driver.write_blocks = &msd_cdi_write; + cdi_storage_driver_init(&cdi_driver); + cdi_storage_driver_register(&cdi_driver); +} + +void register_msd(struct usb_device *usbdev) +{ + struct cdi_storage_device *strgdev; + struct cdi_msd *cdimsd; + struct msclass_data *msc; + struct endpoint_desc *ep_desc; + void *address; + int i; + uint32_t bs, bc; + uint64_t size; + static int msdnum = 0; + + if (usbdev->interface->interface_protocol != 0x50) + { + dprintf("Es werden nur bulk-only-Interfaces unterst?tzt.\n"); + return; + } + cdimsd = malloc(sizeof(struct cdi_msd)); + if (cdimsd == NULL) { + return; + } + cdimsd->usb_device = usbdev; + cdimsd->offset = 0; + strgdev = (struct cdi_storage_device *)cdimsd; + strgdev->dev.type = CDI_STORAGE; + strgdev->dev.name = malloc(10); + if (strgdev->dev.name == NULL) + { + free(strgdev); + return; + } + sprintf((char *)strgdev->dev.name, "msd%i", msdnum++); + msc = malloc(sizeof(struct msclass_data)); + if (msc == NULL) + { + free((void *)strgdev->dev.name); + free(strgdev); + return; + } + usbdev->classd = (struct class_data *)msc; + msc->bulk_ep_in = NULL; + msc->bulk_ep_out = NULL; + address = (void *)usbdev->interface + sizeof(struct interface_desc); + for (i = 0; i < usbdev->interface->num_endpoints; i++) + { + ep_desc = address; + if ((ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (msc->bulk_ep_in == NULL)) { //BULK-IN + msc->bulk_ep_in = ep_desc; + } else if (!(ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (msc->bulk_ep_out == NULL)) { //BULK-OUT + msc->bulk_ep_out = ep_desc; + } + address += sizeof(struct endpoint_desc); + } + if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL)) + { + dprintf("Nicht gen?gend Endpoints gefunden.\n"); + return; + } + if (!msd_get_capacity(usbdev, &bs, &bc)) + { + strgdev->block_size = 0; + strgdev->block_count = 0; + dprintf("Konnte Gr??e f?r %s nicht ermitteln.\n", strgdev->dev.name); + } + else + { + strgdev->block_size = bs; + strgdev->block_count = bc; + size = bs; + size *= bc; + dprintf("%s: %i * %i B (ca. %i MB).\n", strgdev->dev.name, bc, bs, size >> 20); + } + cdi_storage_device_init(strgdev); + cdi_list_push(cdi_driver.drv.devices, strgdev); + get_partitions(strgdev); +} + +static void init_msd(struct cdi_device *cdi_dev) +{ + //Hier wird nichts gemacht, da die Partitionen schon in register_msd() + //erkannt werden M?SSEN. Somit l?uft die ganze Initialisierung dort ab. +} + +static void kill_driver(struct cdi_driver *cdi_msc_driver) +{ + //TODO: Man k?nnte doch noch irgendwie die Ger?te entfernen +} + +static void deinit_msd(struct cdi_device *cdi_msd) +{ + //TODO: Und gerade hier... +} + +static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + struct msd_capacity *cap; + uintptr_t pcbw, pcsw, pcap; + uint32_t expected_tag; + + if ((usbdev == NULL) || (block_size == NULL) || (block_count == NULL)) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct msd_capacity), + (void **)&cap, (void **)&pcap) == -1) { + return 0; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = sizeof(struct msd_capacity); + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_CAPACITY; + cbw->cbw_cb[1] = 0; //LUN: 0 + if (do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, pcbw, + 0x1F, 0, USB_TOD_COMMAND) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(20); + if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcap, + sizeof(struct msd_capacity), 0, USB_TOD_DATA_IN) + != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(5); + if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcsw, + 0x0D, 0, USB_TOD_STATUS) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(5); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) { + return 0; + } + *block_size = BE2CPU(cap->block_length); + *block_count = BE2CPU(cap->last_lba) + 1; + return 1; +} + +#ifdef WAIT_FOR_MSD_READY +static int msd_ready(struct usb_device *usbdev) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + uintptr_t pcbw, pcsw; + uint32_t expected_tag; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return 0; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = 0; + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" + if (do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, pcbw, + 0x1F, 0, USB_TOD_COMMAND) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(7); + if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcsw, + 0x0D, 0, USB_TOD_STATUS) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(4); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) { + return 0; + } + return 1; +} +#endif + +static inline int tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} + +static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length); + +static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer) +{ + void *vb; + uintptr_t pb; + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int j; +#endif + uint32_t i; + struct usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + + fdprintf("read(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) + { + dprintf("Blockspeicher konnte nicht allociert werden.\n"); + return -1; + } + if (!count) + { + dprintf("Leere Leseanfrage.\n"); + return 0; + } + while (tsl(&usbdev->locked)) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + for (i = 0; i < count; i++) + { +#ifdef WAIT_FOR_MSD_READY + for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { + cdi_sleep_ms(20); + } +#endif + error = msd_read(usbdev, start + i, 1, pb, bs); + if (error != USB_NO_ERROR) + { + dprintf("Lesefehler 0x%X bei Block %i.\n", error, i); + usbdev->locked = 0; + return -1; + } + memcpy(buffer + i*bs, vb, bs); + } + usbdev->locked = 0; + return 0; +} + +static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + uintptr_t pcbw, pcsw; + uint32_t expected_tag; + int error; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (!phys_buffer || !length) { + return USB_TRIVIAL_ERROR; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = length; + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_READ10; + cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; + cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; + cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; + cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; + cbw->cbw_cb[8] = sectors & 0x000000FF; + error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, + pcbw, 0x1F, 0, USB_TOD_COMMAND); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(20); + error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, + phys_buffer, length, 0, USB_TOD_DATA_IN); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(5); + error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, + pcsw, 0x0D, 0, USB_TOD_STATUS); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(4); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) + { + dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature, + csw->csw_tag, + expected_tag, + csw->csw_status); + return USB_CRC; //Watt wei? denn ich... + } + return USB_NO_ERROR; +} + +static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length); + +static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer) +{ + void *vb; + uintptr_t pb; + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int j; +#endif + uint32_t i; + struct usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + + fdprintf("write(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) + { + dprintf("Blockspeicher zum Schreiben konnte nicht allociert werden.\n"); + return -1; + } + while (tsl(&usbdev->locked)) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + for (i = 0; i < count; i++) + { + memcpy(vb, buffer + i*bs, bs); +#ifdef WAIT_FOR_MSD_READY + for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { + cdi_sleep_ms(20); + } +#endif + error = msd_write(usbdev, start + i, 1, pb, bs); + if (error != USB_NO_ERROR) + { + dprintf("Schreibfehler 0x%X bei Block %i.\n", error, i); + usbdev->locked = 0; + return -1; + } + } + usbdev->locked = 0; + return 0; +} + +static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + uintptr_t pcbw, pcsw; + uint32_t expected_tag; + int error; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (!phys_buffer || !length) { + return USB_TRIVIAL_ERROR; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = length; + cbw->cbw_flags = 0x00; //OUT + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_WRITE10; + cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; + cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; + cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; + cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; + cbw->cbw_cb[8] = sectors & 0x000000FF; + error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, + pcbw, 0x1F, 0, USB_TOD_COMMAND); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(20); + error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, + phys_buffer, length, 0, USB_TOD_DATA_OUT); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(5); + error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, + pcsw, 0x0D, 0, USB_TOD_STATUS); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(4); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) + { + dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature, + csw->csw_tag, + expected_tag, + csw->csw_status); + return USB_CRC; + } + return USB_NO_ERROR; +} + +static void get_partitions(struct cdi_storage_device *cdistrg) +{ + void *mbr; + struct part_table_entry *partition; + struct cdi_msd *cdimsd, *base_cdimsd = (struct cdi_msd *)cdistrg; + int i, j = 0; + + mbr = malloc(512); + if (msd_cdi_read(cdistrg, 0, 1, mbr) != USB_NO_ERROR) { + dprintf("MBR konnte nicht eingelesen werden.\n"); + return; + } + if (((uint16_t *)mbr)[255] != 0xAA55) { + return; + } + partition = mbr + 0x1BE; + dprintf("Partitionen auf %s:", cdistrg->dev.name); + for (i = 0; i < 4; i++) + { + if (partition[i].type && partition[i].size && (partition[i].start + partition[i].size < cdistrg->block_count)) + { + cdimsd = malloc(sizeof(struct cdi_msd)); + if (cdimsd == NULL) { + break; + } + cdimsd->offset = partition[i].start; + cdimsd->usb_device = base_cdimsd->usb_device; + cdimsd->cdi_device.dev.type = CDI_STORAGE; + cdimsd->cdi_device.dev.name = malloc(14); + if (cdimsd->cdi_device.dev.name == NULL) + { + free(cdimsd); + break; + } + sprintf((char *)cdimsd->cdi_device.dev.name, "%sp%i", cdistrg->dev.name, j++); + cdimsd->cdi_device.block_size = base_cdimsd->cdi_device.block_size; + cdimsd->cdi_device.block_count = partition[i].size; + _dprintf(" %s", cdimsd->cdi_device.dev.name); + cdi_storage_device_init((struct cdi_storage_device *)cdimsd); + cdi_list_push(cdi_driver.drv.devices, cdimsd); + } + } + _dprintf("\n"); +} diff --git a/usb/uhci.c b/usb/uhci.c new file mode 100644 index 0000000..c09075c --- /dev/null +++ b/usb/uhci.c @@ -0,0 +1,339 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include + +#include "cdi/io.h" +#include "cdi/lists.h" +#include "cdi/misc.h" +#include "cdi/pci.h" + +#include "uhci.h" +#include "usb.h" + +#define DEBUG + +#ifdef DEBUG +#include +#include +#define dprintf(fmt, args...) printf("[uhci] " fmt, ##args) +#define _dprintf(fmt, args...) printf(fmt, ##args) +#else +static int dprintf(const char *fmt, ...) +{ + return 0; +} +#define _dprintf(fmt, args...) dprintf(fmt, ##args) +#endif + +static const char *driver_name = "uhci"; + +static struct cdi_driver cdi_driver; +static cdi_list_t active_transfers; + +static void uhci_handler(struct cdi_device *dev); +static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle); +static cdi_list_t get_devices(struct hci *gen_hci); +static void activate_device(struct hci *gen_hci, struct usb_device *device); +static int get_current_frame(struct hci *gen_hci); +static void uhci_deinit(struct cdi_device *cdi_hci); +static void uhci_kill(struct cdi_driver *cdi_hcd); + + +struct cdi_driver *init_uhcd() +{ + active_transfers = cdi_list_create(); + cdi_driver.type = CDI_UNKNOWN; + cdi_driver.name = driver_name; + cdi_driver.init_device = &uhci_init; + cdi_driver.remove_device = &uhci_deinit; + cdi_driver.destroy = &uhci_kill; + cdi_driver_init(&cdi_driver); + cdi_driver_register(&cdi_driver); + return &cdi_driver; +} + +static void uhci_kill(struct cdi_driver *cdi_hcd) +{ + //TODO: Da geht doch noch was +} + +static void uhci_deinit(struct cdi_device *cdi_hci) +{ + cdi_outw(((struct uhci *)((struct cdi_hci *)cdi_hci)->hci)->pbase + UHCI_USBCMD, + MAXP); //HC anhalten + //TODO: Hier doch auch +} + +void uhci_init(struct cdi_device *cdi_hci) +{ + struct hci *gen_hci = ((struct cdi_hci *)cdi_hci)->hci; + struct uhci *uhci = (struct uhci *)gen_hci; + struct cdi_pci_resource *res; + int i, size = 0x14; + + //cdi_pci_alloc_ioports(gen_hci->pcidev); + uhci->pbase = 0; + for (i = 0; (res = cdi_list_get(gen_hci->pcidev->resources, i)) != NULL; + i++) + { + if (res->type == CDI_PCI_IOPORTS) + { + size = res->length ? res->length : 0x14; + if (cdi_ioports_alloc(res->start, size) == -1) + { + dprintf("I/O-Ports konnten nicht reserviert werden.\n"); + return; + } + uhci->pbase = res->start; + break; + } + } + if (!uhci->pbase) + { + dprintf("I/O-Basis nicht gefunden!\n"); + return; + } + if (cdi_alloc_phys_mem(4096, (void **)&uhci->frame_list, + (void **)&uhci->phys_frame_list) == -1) + { + dprintf("Frame List konnte nicht allociert werden!\n"); + return; + } + cdi_register_irq(gen_hci->pcidev->irq, &uhci_handler, cdi_hci); + uhci->root_ports = (size - 0x10) >> 1; + if (uhci->root_ports > 7) { //Laut Linuxkernel ist das so "weird", dass da was nicht stimmen kann... + uhci->root_ports = 2; + } + dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase, uhci->root_ports, gen_hci->pcidev->irq); + for (i = 0; i < 1024; i++) { + uhci->frame_list[i] = 1; //Invalid + } + dprintf("Resetten...\n"); + //HC zur?cksetzen + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); + for (i = 0; (cdi_inw(uhci->pbase + UHCI_USBCMD) & HCRESET) && (i < 50); i++) { + cdi_sleep_ms(10); + } + //Reset auf dem USB treiben + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | GRESET); + for (i = 0; (cdi_inw(uhci->pbase + UHCI_USBCMD) & GRESET) && (i < 50); i++) { + cdi_sleep_ms(10); + } + //Alle Interrupts senden + cdi_outw(uhci->pbase + UHCI_USBINTR, 0xF); + //Framelistadresse eintragen + cdi_outl(uhci->pbase + UHCI_FRBASEADD, uhci->phys_frame_list); + cdi_outw(uhci->pbase + UHCI_FRNUM, 0); //Frame zur?cksetzen + //Ports abschalten, damit selektiv eingeschaltet werden kann (Enumeration) + for (i = 0; i < uhci->root_ports; i++) { + cdi_outw(uhci->pbase + UHCI_RPORTS + i*2, RPORT_CSC); + } + dprintf(" Fertig\n"); + gen_hci->find_devices = &get_devices; + gen_hci->activate_device = &activate_device; + gen_hci->do_packet = &enqueue_request; + gen_hci->get_frame = &get_current_frame; + + enumerate_hci(gen_hci); +} + +static cdi_list_t get_devices(struct hci *gen_hci) +{ + struct uhci *uhci = (struct uhci *)gen_hci; + struct usb_device *dev; + cdi_list_t dlist = cdi_list_create(); + int i; + + dprintf("Ger?te finden: "); + cdi_outw(uhci->pbase + UHCI_USBCMD, USB_RUN); + for (i = 0; i < uhci->root_ports; i++) + { + if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_DEVICE)) + { + _dprintf("-"); + continue; + } + _dprintf("!"); + dev = malloc(sizeof(struct usb_device)); + dev->hci = gen_hci; + dev->id = 0; + dev->port = i; + dev->low_speed = + (cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_LOSPD) && 1; + cdi_list_push(dlist, dev); + } + _dprintf("\n"); + return dlist; +} + +static void activate_device(struct hci *gen_hci, struct usb_device *device) +{ + struct uhci *uhci = (struct uhci *)gen_hci; + + dprintf("Ger?t an Port %i wird aktiviert.\n", device->port); + cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, + RPORT_RESET | RPORT_ENABLE | RPORT_CSC); + cdi_sleep_ms(20); +} + +static int get_current_frame(struct hci *gen_hci) +{ + return cdi_inw(((struct uhci *)gen_hci)->pbase + UHCI_FRNUM); +} + +static inline int tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} +static volatile int locked = 0; + +static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle) +{ + struct uhci *uhci = (struct uhci *)gen_hci; + struct uhci_td *td; + struct uhci_qh *qh; + uintptr_t ptd, pqh; + struct transfer *addr; + + if (cdi_alloc_phys_mem(sizeof(struct uhci_td), + (void **)&td, (void **)&ptd) == -1) { + return USB_TRIVIAL_ERROR; + } + if (cdi_alloc_phys_mem(sizeof(struct uhci_qh), + (void **)&qh, (void **)&pqh) == -1) { + return USB_TRIVIAL_ERROR; + } + while (tsl(&locked)) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + qh->next = 1; //Invalid + qh->transfer = ptd; + memset(td, 0, sizeof(struct uhci_td)); + td->next = 1; //Invalid + td->active = 1; + td->ioc = 1; + td->data_toggle = datatoggle; + td->low_speed = low_speed; + td->errors = 1; + td->pid = type; + td->device = device; + td->endpoint = endpoint; + td->maxlen = length ? length-1 : 0x7FF; + td->buffer = phys_data; + addr = malloc(sizeof(struct transfer)); + addr->virt = td; + addr->phys = ptd; + addr->error = 0xFFFF; + cdi_list_push(active_transfers, addr); + uhci->frame_list[frame] = pqh | 2; +#ifdef CDI_STANDALONE + /*int cframe = get_current_frame(gen_hci), nframe; + nframe = (cframe > frame) ? frame + 1024 : frame; //?berl?ufe korrigieren + //Ann?hern, wann er wohl fertig ist + cdi_sleep_ms(nframe + 30 - get_current_frame(gen_hci));*/ +#else + while (!(qh->transfer & 1)) { + __asm__ __volatile__ ("hlt"); + } +#endif + while (addr->error == 0xFFFF) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + uhci->frame_list[frame] = 1; + locked = 0; + return addr->error; +} + +static void uhci_handler(struct cdi_device *cdi_hci) +{ + struct uhci *uhci = (struct uhci *)((struct cdi_hci *)cdi_hci)->hci; + int status = cdi_inw(uhci->pbase + UHCI_USBSTS), i; + struct transfer *addr; + struct uhci_td *td; + + if (!status) { //Also, von hier kommt der IRQ nicht. + return; + } + if (status & ~0x0001) { + dprintf("Unerwarteter IRQ 0x%04X von %s\n", status, cdi_hci->name); + } + if (status & 0x10) + { + printf("[uhci] SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n"); + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! + } + else + { + for (i = 0; (addr = cdi_list_get(active_transfers, i)) != NULL; i++) + { + td = addr->virt; + if (td->active) { + continue; + } + addr->error = USB_NO_ERROR; + cdi_list_remove(active_transfers, i--); + if (td->stalled_err) + { + dprintf("ENDPOINT STALLED\n"); + addr->error |= USB_STALLED; + } + if (td->buf_err) + { + dprintf("Puffer?berlauf oder Underrun\n"); + addr->error |= USB_BUFFER_ERROR; + } + if (td->babble) + { + dprintf("Puh, da war ein Ger?t wohl sehr gespr?chig: Babble.\n"); + addr->error |= USB_BABBLE; + } + if (td->nak) + { + dprintf("NAK empfangen\n"); + addr->error |= USB_NAK; + } + if (td->crc_time_err) + { + dprintf("CRC-Fehler oder Timeout\n"); + addr->error |= USB_CRC | USB_TIMEOUT; + } + if (td->bitstuff_err) + { + dprintf("Bitstufffehler\n"); + addr->error |= USB_BITSTUFF; + } + //TODO: free(td) - dazu w?re eine CDI-Funktion n?tzlich + } + } + cdi_outw(uhci->pbase + UHCI_USBSTS, status); +} -- 1.6.0.2 From max at tyndur.org Mon Aug 17 20:31:04 2009 From: max at tyndur.org (Max Reitz) Date: Mon, 17 Aug 2009 20:31:04 +0200 Subject: [cdi-devel] [PATCH] UHCI, USB, MSC Message-ID: <20090817203104.68513v2al0nikqnc@mail.famkaufmann.info> + UHCI/USB/MSC (MSDs erscheinen als CDI.storage), der Schreibsupport ist gro?e Gl?ckssache, Treiber nur unter QEMU erfolgreich getestet... Signed-off-by: Max Reitz --- usb/include/msd.h | 104 +++++++++ usb/include/uhci.h | 110 ++++++++++ usb/include/usb.h | 287 +++++++++++++++++++++++++ usb/main.c | 451 +++++++++++++++++++++++++++++++++++++++ usb/msd.c | 599 ++++++++++++++++++++++++++++++++++++++++++++++++++++ usb/uhci.c | 339 +++++++++++++++++++++++++++++ 6 files changed, 1890 insertions(+), 0 deletions(-) create mode 100644 usb/include/msd.h create mode 100644 usb/include/uhci.h create mode 100644 usb/include/usb.h create mode 100644 usb/main.c create mode 100644 usb/msd.c create mode 100644 usb/uhci.c diff --git a/usb/include/msd.h b/usb/include/msd.h new file mode 100644 index 0000000..99c796d --- /dev/null +++ b/usb/include/msd.h @@ -0,0 +1,104 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef _CDI__USB__MSD_H +#define _CDI__USB__MSD_H + +#include + +#include "cdi/storage.h" + +#include "usb.h" + + +//"USBC" +#define CBW_SIGNATURE 0x43425355 +struct command_block_wrapper +{ + uint32_t cbw_signature; + uint32_t cbw_tag; + uint32_t cbw_data_transfer_length; + uint8_t cbw_flags; + uint8_t cbw_lun; + uint8_t cbw_cb_length; + uint8_t cbw_cb[16]; +} __attribute__((packed)); + +//"USBS" +#define CSW_SIGNATURE 0x53425355 +struct command_status_wrapper +{ + uint32_t csw_signature; + uint32_t csw_tag; + uint32_t csw_data_residue; + uint8_t csw_status; +} __attribute__((packed)); + +struct msc_sense +{ + unsigned error : 7; + unsigned valid : 1; + uint8_t rsvd0; + unsigned sense_key : 4; + unsigned rsvd1 : 4; + uint32_t information; + uint8_t additional_length; + uint32_t rsvd2; + uint8_t additional_code; + uint8_t additional_code_qualifier; + uint32_t rsvd; +} __attribute__((packed)); + +struct msd_capacity +{ + uint32_t last_lba; + uint32_t block_length; +} __attribute__((packed)); + +struct cdi_msd +{ + struct cdi_storage_device cdi_device; + struct usb_device *usb_device; + uint32_t offset; //F?r Partitionen +}; + +struct part_table_entry +{ + uint8_t active; + uint8_t begin_chs[3]; + uint8_t type; + uint8_t end_chs[3]; + uint32_t start; + uint32_t size; +} __attribute__((packed)); + + +#define MSC_CMD_REZERO 0x01 +#define MSC_CMD_SENSE 0x03 +#define MSC_CMD_CAPACITY 0x25 +#define MSC_CMD_READ10 0x28 +#define MSC_CMD_WRITE10 0x2A +#define MSC_CMD_SEEK 0x2B +#define MSC_CMD_READ12 0xA8 +#define MSC_CMD_WRITE12 0xAA + +#endif diff --git a/usb/include/uhci.h b/usb/include/uhci.h new file mode 100644 index 0000000..0158bd2 --- /dev/null +++ b/usb/include/uhci.h @@ -0,0 +1,110 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef _CDI__USB__UHCI_H +#define _CDI__USB__UHCI_H + +#include + +#include "cdi/misc.h" +#include "cdi/pci.h" + +#include "usb.h" + + +#define UHCI_USBCMD 0x00 //w +#define UHCI_USBSTS 0x02 //w +#define UHCI_USBINTR 0x04 //w +#define UHCI_FRNUM 0x06 //w +#define UHCI_FRBASEADD 0x08 //l +#define UHCI_SOFMOD 0x0C //b +#define UHCI_RPORTS 0x10 +#define UHCI_PORTSC1 0x10 //w +#define UHCI_PORTSC2 0x12 //w + +#define MAXP 0x0080 +#define GRESET 0x0004 +#define HCRESET 0x0002 +#define USB_RUN 0x0001 + +#define RPORT_RESET 0x0200 +#define RPORT_LOSPD 0x0100 //Low speed device attached +#define RPORT_ENABLE 0x0004 +#define RPORT_CSC 0x0002 //Connect status change +#define RPORT_DEVICE 0x0001 + + +struct uhci +{ + struct hci gen_hci; + uint16_t pbase; + uintptr_t phys_frame_list; + uint32_t *frame_list; + int root_ports; +}; + +struct uhci_qh +{ + volatile uint32_t next; + volatile uint32_t transfer; +} __attribute__((packed)); + +struct uhci_td +{ + volatile uint32_t next; + + unsigned trans_len : 11; + unsigned rsvd0 : 6; + unsigned bitstuff_err : 1; + unsigned crc_time_err : 1; + unsigned nak : 1; + unsigned babble : 1; + unsigned buf_err : 1; + unsigned stalled_err : 1; + unsigned active : 1; + unsigned ioc : 1; + unsigned isochronous : 1; + unsigned low_speed : 1; + unsigned errors : 2; + unsigned spd : 1; + unsigned rsvd1 : 2; + + unsigned pid : 8; + unsigned device : 7; + unsigned endpoint : 4; + unsigned data_toggle : 1; + unsigned rsvd2 : 1; + unsigned maxlen : 11; + + uint32_t buffer; + + uint32_t user[4]; +} __attribute__((packed)); + +struct transfer +{ + void *virt; + uintptr_t phys; + volatile int error; +}; + +#endif diff --git a/usb/include/usb.h b/usb/include/usb.h new file mode 100644 index 0000000..2c435d8 --- /dev/null +++ b/usb/include/usb.h @@ -0,0 +1,287 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef _CDI__USB__USB_H +#define _CDI__USB__USB_H + +#include + +#include "cdi/misc.h" +#include "cdi/pci.h" + + +#define PACKET_SETUP 0x2D +#define PACKET_IN 0x69 +#define PACKET_OUT 0xE1 + +#define NO_DATA 0x01 +#define DEV_TO_HOST 0x80 +#define HOST_TO_DEV 0x00 +#define STD_REQUEST 0x00 +#define CLS_REQUEST 0x20 +#define VEN_REQUEST 0x40 +#define REC_DEVICE 0x00 +#define REC_INTERFACE 0x01 +#define REC_ENDPOINT 0x02 +#define REC_OTHER 0x03 + +#define GET_STATUS 0 +#define CLEAR_FEATURE 1 +#define SET_FEATURE 3 +#define SET_ADDRESS 5 +#define GET_DESCRIPTOR 6 +#define SET_DESCRIPTOR 7 +#define GET_CONFIGURATION 8 +#define SET_CONFIGURATION 9 +#define GET_INTERFACE 10 +#define SET_INTERFACE 11 +#define SYNC_FRAME 12 + +#define PORT_DEVICE 0x0001 +#define PORT_ENABLED 0x0002 +#define PORT_SUSPENDED 0x0004 +#define PORT_OVERCURRENT 0x0008 +#define PORT_RESET 0x0010 +#define PORT_POWER 0x0100 +#define PORT_LOWSPEED 0x0200 +#define PORT_HIGHSPEED 0x0400 +#define PORT_TEST 0x0800 +#define PORT_INDICATOR 0x1000 + +#define PORTF_CONNECTION 0 +#define PORTF_ENABLE 1 +#define PORTF_SUSPEND 2 +#define PORTF_OVERCURRENT 3 +#define PORTF_RESET 4 +#define PORTF_POWER 8 +#define PORTF_LOWSPEED 9 +#define PORTF_HIGHSPEED 10 +#define PORTF_C_CONNECTION 16 +#define PORTF_C_ENABLE 17 +#define PORTF_C_SUSPEND 18 +#define PORTF_C_OVERCURRENT 19 +#define PORTF_C_RESET 20 +#define PORTF_TEST 21 +#define PORTF_INDICATOR 22 + +#define DESC_DEVICE 1 +#define DESC_CONFIGURATION 2 +#define DESC_STRING 3 +#define DESC_INTERFACE 4 +#define DESC_ENDPOINT 5 +#define DESC_DEVICE_QUALIFIER 6 +#define DESC_OTHER_SPEED_CONFIGURATION 7 +#define DESC_INTERFACE_POWER 8 + +#define USB_NO_ERROR 0x00 +#define USB_STALLED 0x01 +#define USB_BUFFER_ERROR 0x02 +#define USB_BABBLE 0x04 +#define USB_NAK 0x08 +#define USB_CRC 0x10 +#define USB_TIMEOUT 0x20 +#define USB_BITSTUFF 0x40 +#define USB_TRIVIAL_ERROR 0x80 + +#define USB_TOD_SETUP 0x0001 +#define USB_TOD_SETUP_DATA_IN 0x0002 +#define USB_TOD_SETUP_DATA_OUT 0x0004 +#define USB_TOD_SETUP_ACK_IN 0x0008 +#define USB_TOD_SETUP_ACK_OUT 0x0010 +#define USB_TOD_COMMAND 0x0020 +#define USB_TOD_DATA_IN 0x0040 +#define USB_TOD_DATA_OUT 0x0080 +#define USB_TOD_STATUS 0x0100 + +#define USB_TODS_SETUP 0 +#define USB_TODS_SETUP_DATA_IN 1 +#define USB_TODS_SETUP_DATA_OUT 2 +#define USB_TODS_SETUP_ACK_IN 3 +#define USB_TODS_SETUP_ACK_OUT 4 +#define USB_TODS_COMMAND 5 +#define USB_TODS_DATA_IN 6 +#define USB_TODS_DATA_OUT 7 +#define USB_TODS_STATUS 8 + + +typedef enum +{ + HCI_UHCI, + HCI_OHCI, + HCI_EHCI +} hci_type_t; + +struct cdi_hci +{ + struct cdi_device cdi_device; + struct hci *hci; +}; + +struct usb_device; + +struct hci +{ + struct cdi_pci_device *pcidev; + hci_type_t type; + cdi_list_t (*find_devices)(struct hci *); + void (*activate_device)(struct hci *, struct usb_device *); + int (*do_packet)(struct hci *, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle); + int (*get_frame)(struct hci *); +}; + +struct setup_packet +{ + uint8_t request_type; + uint8_t request; + uint16_t value; + uint16_t index; + uint16_t length; +} __attribute__((packed)); + +struct device_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint16_t bcdUSB; + uint8_t class_id; + uint8_t subclass_id; + uint8_t protocol_id; + uint8_t max_packet_size0; + uint16_t vendor_id; + uint16_t device_id; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t num_configurations; +} __attribute__((packed)); + +struct config_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint16_t total_length; + uint8_t num_interfaces; + uint8_t config_value; + uint8_t iConfiguration; + uint8_t attributes; + uint8_t max_power; +} __attribute__((packed)); + +struct interface_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint8_t interface_number; + uint8_t alternate_setting; + uint8_t num_endpoints; + uint8_t interface_class; + uint8_t interface_subclass; + uint8_t interface_protocol; + uint8_t iInterface; +} __attribute__((packed)); + +struct endpoint_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint8_t endpoint_address; + uint8_t attributes; + uint16_t max_packet_size; + uint8_t interval; +} __attribute__((packed)); + +struct hub_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint8_t nbr_ports; + uint16_t hub_characteristics; + uint8_t pwron2pwrgood; + uint8_t hub_contr_current; +} __attribute__((packed)); + +typedef enum +{ + USBDC_COMPOSITE = 0x00, + USBDC_AUDIO = 0x01, + USBDC_COMM_CDC = 0x02, + USBDC_HID = 0x03, + USBDC_PID = 0x05, + USBDC_IMAGE = 0x06, + USBDC_PRINTER = 0x07, + USBDC_MSC = 0x08, + USBDC_HUB = 0x09, + USBDC_CDC_DATA = 0x0A, + USBDC_CHIP = 0x0B, + USBDC_SECURITY = 0x0D, + USBDC_VIDEO = 0x0E, + USBDC_HEALTH = 0x0F, + USBDC_DIAGNOSIS = 0xDC, + USBDC_WIRELESS = 0xE0, + USBDC_MISC = 0xEF, + USBDC_SOFT_SPEC = 0xFE, + USBDC_VENDOR_SPEC = 0xFF +} usb_class_t; + +struct class_data +{ + usb_class_t type; +}; + +struct usb_device +{ + struct hci *hci; + int id; + int port; + int low_speed; + struct device_desc *device; + struct config_desc *config; + struct interface_desc *interface; + struct class_data *classd; + int stalled; + int locked; + int expects; +}; + +struct msclass_data +{ + struct class_data gen_class; + struct endpoint_desc *bulk_ep_in; + struct endpoint_desc *bulk_ep_out; +}; + + +#include "uhci.h" + + +#define HCI_STRUCT_SIZE sizeof(struct uhci) + + +int do_packet(struct usb_device *device, int type, int endpoint, uintptr_t phys_data, int length, int datatoggle, int type_of_data); +void enumerate_hci(struct hci *); +struct cdi_driver *init_uhcd(void); +void init_msc_driver(void); +void register_msd(struct usb_device *usbdev); +void uhci_init(struct cdi_device *cdi_hci); + +#endif diff --git a/usb/main.c b/usb/main.c new file mode 100644 index 0000000..c06f15d --- /dev/null +++ b/usb/main.c @@ -0,0 +1,451 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include +#include + +#include "cdi/io.h" +#include "cdi/misc.h" +#include "cdi/pci.h" + +#include "usb.h" + +#define DEBUG + +#ifdef DEBUG +#include +#define dprintf(fmt, args...) printf("[usb] " fmt, ##args) +#define _dprintf(fmt, args...) printf(fmt, ##args) +#else +static int dprintf(const char *fmt, ...) +{ + return 0; +} +#define _dprintf(fmt, args...) dprintf(fmt, ##args) +#endif + +static struct usb_device *devices[128]; +static cdi_list_t ehci, ohci, uhci; +static void (*ccdriver_take_device[0x100])(struct usb_device *); +static int usb_dev_ids = 1; + +static void usb_init(void); +static void enumerate_hub(struct usb_device *usbdev); + +#ifdef CDI_STANDALONE +int main(void) +#else +int init_usb(void) +#endif +{ + cdi_init(); + + memset(devices, 0, 128 * sizeof(struct usb_device *)); + + usb_init(); + + cdi_run_drivers(); + + return 0; +} + +static const int next_data_type[9] = { + USB_TOD_SETUP_DATA_IN | USB_TOD_SETUP_DATA_OUT | USB_TOD_SETUP_ACK_IN, + USB_TOD_SETUP_ACK_OUT, + USB_TOD_SETUP_ACK_IN, + USB_TOD_SETUP | USB_TOD_COMMAND, + USB_TOD_SETUP | USB_TOD_COMMAND, + USB_TOD_DATA_IN | USB_TOD_DATA_OUT | USB_TOD_STATUS, + USB_TOD_STATUS, + USB_TOD_STATUS, + USB_TOD_SETUP | USB_TOD_COMMAND +}; + +/*static const char *tod_name[9] = { + "SETUP", + "SETUP DS IN", + "SETUP DS OUT", + "SETUP ACK IN", + "SETUP ACK OUT", + "COMMAND", + "DATA IN", + "DATA OUT", + "STATUS" +};*/ + +/** +* Verarbeitet ein USB-Paket. +* +* @param device Das USB-Ger?t +* @param type Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) +* @param endpoint Der gew?nschte Endpoint (0 bis 15) +* @param phys_data Die physische Adresse des zu verwendenden Datenpuffers +* @param length Die L?nge des Puffers +* @param datatoggle Gibt an, ob DATA0 (0) oder DATA1 (1) verwendet werden soll +* @param type_of_data Typ der Daten (zur Sicherheit, damit es kein doofes STALL gibt) +*/ + +int do_packet(struct usb_device *device, int type, int endpoint, uintptr_t phys_data, int length, int datatoggle, int type_of_data) +{ + int error; + int tod_short; + + if (!(device->expects & type_of_data)) { + dprintf("Expected 0x%04X, got 0x%04X...?\n", device->expects, type_of_data); + for (;;); + } + type_of_data >>= 1; + for (tod_short = 0; type_of_data; tod_short++) { + type_of_data >>= 1; + } + //dprintf("TOD: %s\n", tod_name[tod_short]); + device->expects = next_data_type[tod_short]; + endpoint &= 0x07; + type &= 0xFF; + if (device->stalled) //TODO: Irgendwie entstallen, evtl.? oO + { + dprintf("Zugriff auf stalled-Ger?t verweigert.\n"); + return USB_STALLED; + } + error = device->hci->do_packet(device->hci, + (device->hci->get_frame(device->hci) + 3) & 0x3FF, + type, device->id, endpoint, + device->low_speed, phys_data, length, + datatoggle); + if (error == USB_STALLED) + { + printf("[usb] ENDPOINT %i DES GER?TS %i STALLED!\n", endpoint, device->id); + device->stalled = 1; + } + return error; +} + +static void *do_control(struct usb_device *device, int direction, void *buffer, int length, int rtype, int recipient, int request, int value, int index) +{ + void *rbuf; + int rval, no_data; + struct setup_packet *setup; + uintptr_t pbuf, psetup; + + no_data = direction & NO_DATA; + if (!no_data && (cdi_alloc_phys_mem(length, &rbuf, (void **)&pbuf) == -1)) { + return NULL; + } + if (cdi_alloc_phys_mem(sizeof(struct setup_packet), + (void **)&setup, (void **)&psetup) == -1) { + return NULL; + } + direction &= 0x80; + if ((direction == HOST_TO_DEV) && length && (buffer != NULL)) { + memcpy(rbuf, buffer, length); + } + setup->request_type = direction | (rtype & 0x60) | (recipient & 0x1F); + setup->request = request; + setup->value = value; + setup->index = index; + setup->length = length; + if (do_packet(device, PACKET_SETUP, 0, psetup, sizeof(struct setup_packet), + 0, USB_TOD_SETUP) != USB_NO_ERROR) { + return NULL; + } + cdi_sleep_ms(20); + if (no_data) + { + rval = USB_NO_ERROR; + rbuf = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten + } + else + { + if (direction == DEV_TO_HOST) { + rval = do_packet(device, PACKET_IN, 0, pbuf, length, 1, + USB_TOD_SETUP_DATA_IN); + } else { + rval = do_packet(device, PACKET_OUT, 0, pbuf, length, 1, + USB_TOD_SETUP_DATA_OUT); + } + } + if (rval == USB_NO_ERROR) + { + cdi_sleep_ms(20); + if (no_data || (direction == HOST_TO_DEV)) { + rval = do_packet(device, PACKET_IN, 0, 0, 0, 1, USB_TOD_SETUP_ACK_IN); + } else { + rval = do_packet(device, PACKET_OUT, 0, 0, 0, 1, USB_TOD_SETUP_ACK_OUT); + } + } + return (rval == USB_NO_ERROR) ? rbuf : NULL; +} + +static void usb_init(void) +{ + struct cdi_pci_device *dev; + struct hci *hci; + struct cdi_hci *cdi_hci; + struct cdi_driver *uhcd; + char *dev_name; + int i; + + init_msc_driver(); + + memset(ccdriver_take_device, 0, sizeof(void (*)(struct usb_device *)) * 0x100); + ccdriver_take_device[8] = ®ister_msd; + ccdriver_take_device[9] = &enumerate_hub; + + ehci = cdi_list_create(); + ohci = cdi_list_create(); + uhci = cdi_list_create(); + + cdi_list_t pci_devices = cdi_list_create(); + cdi_pci_get_all_devices(pci_devices); + for (i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) + { + if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03)) { + cdi_pci_device_destroy(dev); + } else { + switch (dev->interface_id) + { + case 0x00: + hci = malloc(HCI_STRUCT_SIZE); + hci->pcidev = dev; + hci->type = HCI_UHCI; + cdi_list_push(uhci, hci); + break; + case 0x10: + hci = malloc(HCI_STRUCT_SIZE); + hci->pcidev = dev; + hci->type = HCI_OHCI; + cdi_list_push(ohci, hci); + break; + case 0x20: + hci = malloc(HCI_STRUCT_SIZE); + hci->pcidev = dev; + hci->type = HCI_EHCI; + cdi_list_push(ehci, hci); + } + } + } + + cdi_list_destroy(pci_devices); + + dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n", cdi_list_size(ehci), + cdi_list_size(ohci), + cdi_list_size(uhci)); + + uhcd = init_uhcd(); + + for (i = 0; (hci = cdi_list_pop(uhci)) != NULL; i++) + { + cdi_hci = malloc(sizeof(struct cdi_hci)); + cdi_hci->cdi_device.type = CDI_UNKNOWN; + dev_name = malloc(10); + sprintf(dev_name, "uhci%i", i); + cdi_hci->cdi_device.name = dev_name; + hci->find_devices = NULL; + cdi_hci->hci = hci; + //cdi_list_push(uhcd->devices, cdi_hci); + //^ Das ist zwar sehr sch?n, geht so aber leider nicht + dprintf("%s registriert.\n", dev_name); + cdi_hci->cdi_device.driver = uhcd; + uhci_init(&cdi_hci->cdi_device); + } + + cdi_list_destroy(ehci); + cdi_list_destroy(ohci); + cdi_list_destroy(uhci); +} + +static void enum_device(struct usb_device *usbdev) +{ + struct device_desc *dev_desc; + struct config_desc *conf_desc; + struct interface_desc *if_desc, *best_if; + char *name; + void *position; + int i, id; + + usbdev->stalled = 0; + usbdev->locked = 0; + usbdev->expects = USB_TOD_SETUP | USB_TOD_COMMAND; + dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_DEVICE << 8, 0); + if (dev_desc == NULL) { + return; + } + usbdev->device = dev_desc; + dprintf("0x%04X:0x%04X (%i) -> ", dev_desc->vendor_id, dev_desc->device_id, + dev_desc->class_id); + do_control(usbdev, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_ADDRESS, (id = usb_dev_ids++), 0); + usbdev->id = id; + _dprintf("%i\n", id); + if (dev_desc->iManufacturer) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iManufacturer, 0); + if (name == NULL) { + return; + } + dprintf(" -> Hersteller: "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf("\n"); + } + if (dev_desc->iProduct) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iProduct, 0); + if (name == NULL) { + return; + } + dprintf(" -> Produkt: "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf("\n"); + } + devices[id] = usbdev; + //TODO: Man kann doch nicht immer den ersten nehmen... + conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, + sizeof(struct config_desc), STD_REQUEST, REC_DEVICE, + GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return; + } + conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, conf_desc->total_length, + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return; + } + usbdev->config = conf_desc; + do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_CONFIGURATION, conf_desc->config_value, 0); + if (conf_desc->iConfiguration) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | conf_desc->iConfiguration, 0); + if (name == NULL) { + return; + } + dprintf("Verwende Konfiguration "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf(".\n"); + } + position = conf_desc; + position += sizeof(struct config_desc); + best_if = position; //Standard-IF + for (i = 0; i < conf_desc->num_interfaces; i++) + { + if_desc = position; + if (if_desc->interface_class == 8) //TODO: Mehr Klassencodes nat?rlich! + { + best_if = if_desc; + break; + } + position += sizeof(struct interface_desc) + if_desc->num_endpoints * sizeof(struct endpoint_desc); + } + do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_INTERFACE, best_if->interface_number, 0); + usbdev->interface = best_if; + if (best_if->iInterface) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | best_if->iInterface, 0); + if (name == NULL) { + return; + } + dprintf("Verwende Interface "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf(".\n"); + } + dprintf("Konfiguration: %i:%i (%i)\n", conf_desc->config_value, + best_if->interface_number, + best_if->interface_class); + if (ccdriver_take_device[best_if->interface_class] != NULL) { + ccdriver_take_device[best_if->interface_class](usbdev); + } +} + +static void enumerate_hub(struct usb_device *usbdev) +{ + struct hub_desc *hub_desc; + struct usb_device *down; + uint16_t *port_status; + int i; + + //Mehr als 8 Ports sollte es nicht geben, die Gr??e des Deskriptors wird + //daher wohl nicht gr??er als sizeof()+16 werden + hub_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*hub_desc) + 16, + CLS_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_DEVICE << 8, 0); + for (i = 0; i < hub_desc->nbr_ports; i++) + { + port_status = do_control(usbdev, DEV_TO_HOST, NULL, 4, CLS_REQUEST, + REC_OTHER, GET_STATUS, 0, i+1); + if (!(port_status[0] & PORT_DEVICE)) { + continue; + } + //Strom an + do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, + SET_FEATURE, PORTF_POWER, i+1); + cdi_sleep_ms(hub_desc->pwron2pwrgood * 2); + //Resetten + do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, + SET_FEATURE, PORTF_RESET, i+1); + //Reset sollte jetzt eigtl. schon beendet sein + //(do_control wartet ja 50 ms) + down = malloc(sizeof(struct usb_device)); + down->hci = usbdev->hci; + down->id = 0; + down->port = i; + down->low_speed = (port_status[0] & PORT_LOWSPEED) && 1; + enum_device(down); + } +} + +void enumerate_hci(struct hci *hci) +{ + struct usb_device *usbdev; + cdi_list_t usb_devices; + + if (hci->find_devices != NULL) + { + usb_devices = hci->find_devices(hci); + while ((usbdev = cdi_list_pop(usb_devices)) != NULL) + { + hci->activate_device(hci, usbdev); + cdi_sleep_ms(50); + enum_device(usbdev); + } + } +} diff --git a/usb/msd.c b/usb/msd.c new file mode 100644 index 0000000..11fbd6a --- /dev/null +++ b/usb/msd.c @@ -0,0 +1,599 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include +#include +#include + +#include "cdi/misc.h" +#include "cdi/storage.h" + +#include "msd.h" +#include "usb.h" + +//DEBUG definieren, um einige Debugmeldungen anzuzeigen +#define DEBUG +//FULLDEBUG definieren, um ALLE Debugmeldungen anzuzeigen +#define FULLDEBUG + +#if defined FULLDEBUG && !defined DEBUG +#define DEBUG +#endif + +#ifdef DEBUG +#define dprintf(fmt, args...) printf("[usb-msc] " fmt, ##args) +#define _dprintf(fmt, args...) printf(fmt, ##args) +#else +static int dprintf(const char *fmt, ...) +{ + return 0; +} +#define _dprintf(fmt, args...) dprintf(fmt, ##args) +#endif + +#ifdef FULLDEBUG +#define fdprintf(fmt, args...) printf("[usb-msc] " fmt, ##args) +#else +static int fdprintf(const char *fmt, ...) +{ + return 0; +} +#endif + +static uint32_t cbw_tag = 1; + +static const char *driver_name = "usb-msc"; + +static struct cdi_storage_driver cdi_driver; + +static void deinit_msd(struct cdi_device *cdi_msd); +static void get_partitions(struct cdi_storage_device *cdistrg); +static void init_msd(struct cdi_device *cdi_msd); +static void kill_driver(struct cdi_driver *cdi_msc_driver); +static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer); +static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer); +static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count); + +//TODO: CDI-Funktion w?re hier sicher n?tzlich... +#define CPU_IS_LITTLE_ENDIAN + +#define change_endianess_32(_) ((((_)&0xFF000000)>>24)|(((_)&0xFF0000)>>8)|(((_)&0xFF00)<<8)|(((_)&0xFF)<<24)) +#ifdef CPU_IS_LITTLE_ENDIAN +#define CPU2BE(_) change_endianess_32(_) +#define CPU2LE(_) (_) +#define BE2CPU(_) change_endianess_32(_) +#define LE2CPU(_) (_) +#else +#define CPU2BE(_) (_) +#define CPU2LE(_) change_endianess_32(_) +#define BE2CPU(_) (_) +#define LE2CPU(_) change_endianess_32(_) +#endif + +//Das muss definiert werden, um vor einem Befehl zu warten, bis das MSD fertig +//ist. Theoretisch ist das ?u?erst sinnvoll, praktisch h?ngt sich der Treiber +//bisweilen auf - das sollte allerdings mit dem Timeout gegessen sein +/** +#define WAIT_FOR_MSD_READY +*/ + +//TODO: Storage bietet eine SCSI-Klasse. Die k?nnte man durchaus benutzen. xD + +void init_msc_driver() +{ + struct cdi_driver *stddrv = (struct cdi_driver *)&cdi_driver; + stddrv->type = CDI_STORAGE; + stddrv->name = driver_name; + stddrv->init_device = &init_msd; + stddrv->remove_device = &deinit_msd; + stddrv->destroy = &kill_driver; + cdi_driver.read_blocks = &msd_cdi_read; + cdi_driver.write_blocks = &msd_cdi_write; + cdi_storage_driver_init(&cdi_driver); + cdi_storage_driver_register(&cdi_driver); +} + +void register_msd(struct usb_device *usbdev) +{ + struct cdi_storage_device *strgdev; + struct cdi_msd *cdimsd; + struct msclass_data *msc; + struct endpoint_desc *ep_desc; + void *address; + int i; + uint32_t bs, bc; + uint64_t size; + static int msdnum = 0; + + if (usbdev->interface->interface_protocol != 0x50) + { + dprintf("Es werden nur bulk-only-Interfaces unterst?tzt.\n"); + return; + } + cdimsd = malloc(sizeof(struct cdi_msd)); + if (cdimsd == NULL) { + return; + } + cdimsd->usb_device = usbdev; + cdimsd->offset = 0; + strgdev = (struct cdi_storage_device *)cdimsd; + strgdev->dev.type = CDI_STORAGE; + strgdev->dev.name = malloc(10); + if (strgdev->dev.name == NULL) + { + free(strgdev); + return; + } + sprintf((char *)strgdev->dev.name, "msd%i", msdnum++); + msc = malloc(sizeof(struct msclass_data)); + if (msc == NULL) + { + free((void *)strgdev->dev.name); + free(strgdev); + return; + } + usbdev->classd = (struct class_data *)msc; + msc->bulk_ep_in = NULL; + msc->bulk_ep_out = NULL; + address = (void *)usbdev->interface + sizeof(struct interface_desc); + for (i = 0; i < usbdev->interface->num_endpoints; i++) + { + ep_desc = address; + if ((ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (msc->bulk_ep_in == NULL)) { //BULK-IN + msc->bulk_ep_in = ep_desc; + } else if (!(ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (msc->bulk_ep_out == NULL)) { //BULK-OUT + msc->bulk_ep_out = ep_desc; + } + address += sizeof(struct endpoint_desc); + } + if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL)) + { + dprintf("Nicht gen?gend Endpoints gefunden.\n"); + return; + } + if (!msd_get_capacity(usbdev, &bs, &bc)) + { + strgdev->block_size = 0; + strgdev->block_count = 0; + dprintf("Konnte Gr??e f?r %s nicht ermitteln.\n", strgdev->dev.name); + } + else + { + strgdev->block_size = bs; + strgdev->block_count = bc; + size = bs; + size *= bc; + dprintf("%s: %i * %i B (ca. %i MB).\n", strgdev->dev.name, bc, bs, size >> 20); + } + cdi_storage_device_init(strgdev); + cdi_list_push(cdi_driver.drv.devices, strgdev); + get_partitions(strgdev); +} + +static void init_msd(struct cdi_device *cdi_dev) +{ + //Hier wird nichts gemacht, da die Partitionen schon in register_msd() + //erkannt werden M?SSEN. Somit l?uft die ganze Initialisierung dort ab. +} + +static void kill_driver(struct cdi_driver *cdi_msc_driver) +{ + //TODO: Man k?nnte doch noch irgendwie die Ger?te entfernen +} + +static void deinit_msd(struct cdi_device *cdi_msd) +{ + //TODO: Und gerade hier... +} + +static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + struct msd_capacity *cap; + uintptr_t pcbw, pcsw, pcap; + uint32_t expected_tag; + + if ((usbdev == NULL) || (block_size == NULL) || (block_count == NULL)) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct msd_capacity), + (void **)&cap, (void **)&pcap) == -1) { + return 0; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = sizeof(struct msd_capacity); + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_CAPACITY; + cbw->cbw_cb[1] = 0; //LUN: 0 + if (do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, pcbw, + 0x1F, 0, USB_TOD_COMMAND) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(20); + if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcap, + sizeof(struct msd_capacity), 0, USB_TOD_DATA_IN) + != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(5); + if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcsw, + 0x0D, 0, USB_TOD_STATUS) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(5); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) { + return 0; + } + *block_size = BE2CPU(cap->block_length); + *block_count = BE2CPU(cap->last_lba) + 1; + return 1; +} + +#ifdef WAIT_FOR_MSD_READY +static int msd_ready(struct usb_device *usbdev) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + uintptr_t pcbw, pcsw; + uint32_t expected_tag; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return 0; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = 0; + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" + if (do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, pcbw, + 0x1F, 0, USB_TOD_COMMAND) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(7); + if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcsw, + 0x0D, 0, USB_TOD_STATUS) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(4); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) { + return 0; + } + return 1; +} +#endif + +static inline int tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} + +static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length); + +static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer) +{ + void *vb; + uintptr_t pb; + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int j; +#endif + uint32_t i; + struct usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + + fdprintf("read(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) + { + dprintf("Blockspeicher konnte nicht allociert werden.\n"); + return -1; + } + if (!count) + { + dprintf("Leere Leseanfrage.\n"); + return 0; + } + while (tsl(&usbdev->locked)) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + for (i = 0; i < count; i++) + { +#ifdef WAIT_FOR_MSD_READY + for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { + cdi_sleep_ms(20); + } +#endif + error = msd_read(usbdev, start + i, 1, pb, bs); + if (error != USB_NO_ERROR) + { + dprintf("Lesefehler 0x%X bei Block %i.\n", error, i); + usbdev->locked = 0; + return -1; + } + memcpy(buffer + i*bs, vb, bs); + } + usbdev->locked = 0; + return 0; +} + +static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + uintptr_t pcbw, pcsw; + uint32_t expected_tag; + int error; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (!phys_buffer || !length) { + return USB_TRIVIAL_ERROR; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = length; + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_READ10; + cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; + cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; + cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; + cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; + cbw->cbw_cb[8] = sectors & 0x000000FF; + error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, + pcbw, 0x1F, 0, USB_TOD_COMMAND); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(20); + error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, + phys_buffer, length, 0, USB_TOD_DATA_IN); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(5); + error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, + pcsw, 0x0D, 0, USB_TOD_STATUS); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(4); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) + { + dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature, + csw->csw_tag, + expected_tag, + csw->csw_status); + return USB_CRC; //Watt wei? denn ich... + } + return USB_NO_ERROR; +} + +static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length); + +static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer) +{ + void *vb; + uintptr_t pb; + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int j; +#endif + uint32_t i; + struct usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + + fdprintf("write(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) + { + dprintf("Blockspeicher zum Schreiben konnte nicht allociert werden.\n"); + return -1; + } + while (tsl(&usbdev->locked)) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + for (i = 0; i < count; i++) + { + memcpy(vb, buffer + i*bs, bs); +#ifdef WAIT_FOR_MSD_READY + for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { + cdi_sleep_ms(20); + } +#endif + error = msd_write(usbdev, start + i, 1, pb, bs); + if (error != USB_NO_ERROR) + { + dprintf("Schreibfehler 0x%X bei Block %i.\n", error, i); + usbdev->locked = 0; + return -1; + } + } + usbdev->locked = 0; + return 0; +} + +static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + uintptr_t pcbw, pcsw; + uint32_t expected_tag; + int error; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (!phys_buffer || !length) { + return USB_TRIVIAL_ERROR; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = length; + cbw->cbw_flags = 0x00; //OUT + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_WRITE10; + cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; + cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; + cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; + cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; + cbw->cbw_cb[8] = sectors & 0x000000FF; + error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, + pcbw, 0x1F, 0, USB_TOD_COMMAND); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(20); + error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, + phys_buffer, length, 0, USB_TOD_DATA_OUT); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(5); + error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, + pcsw, 0x0D, 0, USB_TOD_STATUS); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(4); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) + { + dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature, + csw->csw_tag, + expected_tag, + csw->csw_status); + return USB_CRC; + } + return USB_NO_ERROR; +} + +static void get_partitions(struct cdi_storage_device *cdistrg) +{ + void *mbr; + struct part_table_entry *partition; + struct cdi_msd *cdimsd, *base_cdimsd = (struct cdi_msd *)cdistrg; + int i, j = 0; + + mbr = malloc(512); + if (msd_cdi_read(cdistrg, 0, 1, mbr) != USB_NO_ERROR) { + dprintf("MBR konnte nicht eingelesen werden.\n"); + return; + } + if (((uint16_t *)mbr)[255] != 0xAA55) { + return; + } + partition = mbr + 0x1BE; + dprintf("Partitionen auf %s:", cdistrg->dev.name); + for (i = 0; i < 4; i++) + { + if (partition[i].type && partition[i].size && (partition[i].start + partition[i].size < cdistrg->block_count)) + { + cdimsd = malloc(sizeof(struct cdi_msd)); + if (cdimsd == NULL) { + break; + } + cdimsd->offset = partition[i].start; + cdimsd->usb_device = base_cdimsd->usb_device; + cdimsd->cdi_device.dev.type = CDI_STORAGE; + cdimsd->cdi_device.dev.name = malloc(14); + if (cdimsd->cdi_device.dev.name == NULL) + { + free(cdimsd); + break; + } + sprintf((char *)cdimsd->cdi_device.dev.name, "%sp%i", cdistrg->dev.name, j++); + cdimsd->cdi_device.block_size = base_cdimsd->cdi_device.block_size; + cdimsd->cdi_device.block_count = partition[i].size; + _dprintf(" %s", cdimsd->cdi_device.dev.name); + cdi_storage_device_init((struct cdi_storage_device *)cdimsd); + cdi_list_push(cdi_driver.drv.devices, cdimsd); + } + } + _dprintf("\n"); +} diff --git a/usb/uhci.c b/usb/uhci.c new file mode 100644 index 0000000..c09075c --- /dev/null +++ b/usb/uhci.c @@ -0,0 +1,339 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include + +#include "cdi/io.h" +#include "cdi/lists.h" +#include "cdi/misc.h" +#include "cdi/pci.h" + +#include "uhci.h" +#include "usb.h" + +#define DEBUG + +#ifdef DEBUG +#include +#include +#define dprintf(fmt, args...) printf("[uhci] " fmt, ##args) +#define _dprintf(fmt, args...) printf(fmt, ##args) +#else +static int dprintf(const char *fmt, ...) +{ + return 0; +} +#define _dprintf(fmt, args...) dprintf(fmt, ##args) +#endif + +static const char *driver_name = "uhci"; + +static struct cdi_driver cdi_driver; +static cdi_list_t active_transfers; + +static void uhci_handler(struct cdi_device *dev); +static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle); +static cdi_list_t get_devices(struct hci *gen_hci); +static void activate_device(struct hci *gen_hci, struct usb_device *device); +static int get_current_frame(struct hci *gen_hci); +static void uhci_deinit(struct cdi_device *cdi_hci); +static void uhci_kill(struct cdi_driver *cdi_hcd); + + +struct cdi_driver *init_uhcd() +{ + active_transfers = cdi_list_create(); + cdi_driver.type = CDI_UNKNOWN; + cdi_driver.name = driver_name; + cdi_driver.init_device = &uhci_init; + cdi_driver.remove_device = &uhci_deinit; + cdi_driver.destroy = &uhci_kill; + cdi_driver_init(&cdi_driver); + cdi_driver_register(&cdi_driver); + return &cdi_driver; +} + +static void uhci_kill(struct cdi_driver *cdi_hcd) +{ + //TODO: Da geht doch noch was +} + +static void uhci_deinit(struct cdi_device *cdi_hci) +{ + cdi_outw(((struct uhci *)((struct cdi_hci *)cdi_hci)->hci)->pbase + UHCI_USBCMD, + MAXP); //HC anhalten + //TODO: Hier doch auch +} + +void uhci_init(struct cdi_device *cdi_hci) +{ + struct hci *gen_hci = ((struct cdi_hci *)cdi_hci)->hci; + struct uhci *uhci = (struct uhci *)gen_hci; + struct cdi_pci_resource *res; + int i, size = 0x14; + + //cdi_pci_alloc_ioports(gen_hci->pcidev); + uhci->pbase = 0; + for (i = 0; (res = cdi_list_get(gen_hci->pcidev->resources, i)) != NULL; + i++) + { + if (res->type == CDI_PCI_IOPORTS) + { + size = res->length ? res->length : 0x14; + if (cdi_ioports_alloc(res->start, size) == -1) + { + dprintf("I/O-Ports konnten nicht reserviert werden.\n"); + return; + } + uhci->pbase = res->start; + break; + } + } + if (!uhci->pbase) + { + dprintf("I/O-Basis nicht gefunden!\n"); + return; + } + if (cdi_alloc_phys_mem(4096, (void **)&uhci->frame_list, + (void **)&uhci->phys_frame_list) == -1) + { + dprintf("Frame List konnte nicht allociert werden!\n"); + return; + } + cdi_register_irq(gen_hci->pcidev->irq, &uhci_handler, cdi_hci); + uhci->root_ports = (size - 0x10) >> 1; + if (uhci->root_ports > 7) { //Laut Linuxkernel ist das so "weird", dass da was nicht stimmen kann... + uhci->root_ports = 2; + } + dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase, uhci->root_ports, gen_hci->pcidev->irq); + for (i = 0; i < 1024; i++) { + uhci->frame_list[i] = 1; //Invalid + } + dprintf("Resetten...\n"); + //HC zur?cksetzen + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); + for (i = 0; (cdi_inw(uhci->pbase + UHCI_USBCMD) & HCRESET) && (i < 50); i++) { + cdi_sleep_ms(10); + } + //Reset auf dem USB treiben + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | GRESET); + for (i = 0; (cdi_inw(uhci->pbase + UHCI_USBCMD) & GRESET) && (i < 50); i++) { + cdi_sleep_ms(10); + } + //Alle Interrupts senden + cdi_outw(uhci->pbase + UHCI_USBINTR, 0xF); + //Framelistadresse eintragen + cdi_outl(uhci->pbase + UHCI_FRBASEADD, uhci->phys_frame_list); + cdi_outw(uhci->pbase + UHCI_FRNUM, 0); //Frame zur?cksetzen + //Ports abschalten, damit selektiv eingeschaltet werden kann (Enumeration) + for (i = 0; i < uhci->root_ports; i++) { + cdi_outw(uhci->pbase + UHCI_RPORTS + i*2, RPORT_CSC); + } + dprintf(" Fertig\n"); + gen_hci->find_devices = &get_devices; + gen_hci->activate_device = &activate_device; + gen_hci->do_packet = &enqueue_request; + gen_hci->get_frame = &get_current_frame; + + enumerate_hci(gen_hci); +} + +static cdi_list_t get_devices(struct hci *gen_hci) +{ + struct uhci *uhci = (struct uhci *)gen_hci; + struct usb_device *dev; + cdi_list_t dlist = cdi_list_create(); + int i; + + dprintf("Ger?te finden: "); + cdi_outw(uhci->pbase + UHCI_USBCMD, USB_RUN); + for (i = 0; i < uhci->root_ports; i++) + { + if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_DEVICE)) + { + _dprintf("-"); + continue; + } + _dprintf("!"); + dev = malloc(sizeof(struct usb_device)); + dev->hci = gen_hci; + dev->id = 0; + dev->port = i; + dev->low_speed = + (cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_LOSPD) && 1; + cdi_list_push(dlist, dev); + } + _dprintf("\n"); + return dlist; +} + +static void activate_device(struct hci *gen_hci, struct usb_device *device) +{ + struct uhci *uhci = (struct uhci *)gen_hci; + + dprintf("Ger?t an Port %i wird aktiviert.\n", device->port); + cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, + RPORT_RESET | RPORT_ENABLE | RPORT_CSC); + cdi_sleep_ms(20); +} + +static int get_current_frame(struct hci *gen_hci) +{ + return cdi_inw(((struct uhci *)gen_hci)->pbase + UHCI_FRNUM); +} + +static inline int tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} +static volatile int locked = 0; + +static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle) +{ + struct uhci *uhci = (struct uhci *)gen_hci; + struct uhci_td *td; + struct uhci_qh *qh; + uintptr_t ptd, pqh; + struct transfer *addr; + + if (cdi_alloc_phys_mem(sizeof(struct uhci_td), + (void **)&td, (void **)&ptd) == -1) { + return USB_TRIVIAL_ERROR; + } + if (cdi_alloc_phys_mem(sizeof(struct uhci_qh), + (void **)&qh, (void **)&pqh) == -1) { + return USB_TRIVIAL_ERROR; + } + while (tsl(&locked)) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + qh->next = 1; //Invalid + qh->transfer = ptd; + memset(td, 0, sizeof(struct uhci_td)); + td->next = 1; //Invalid + td->active = 1; + td->ioc = 1; + td->data_toggle = datatoggle; + td->low_speed = low_speed; + td->errors = 1; + td->pid = type; + td->device = device; + td->endpoint = endpoint; + td->maxlen = length ? length-1 : 0x7FF; + td->buffer = phys_data; + addr = malloc(sizeof(struct transfer)); + addr->virt = td; + addr->phys = ptd; + addr->error = 0xFFFF; + cdi_list_push(active_transfers, addr); + uhci->frame_list[frame] = pqh | 2; +#ifdef CDI_STANDALONE + /*int cframe = get_current_frame(gen_hci), nframe; + nframe = (cframe > frame) ? frame + 1024 : frame; //?berl?ufe korrigieren + //Ann?hern, wann er wohl fertig ist + cdi_sleep_ms(nframe + 30 - get_current_frame(gen_hci));*/ +#else + while (!(qh->transfer & 1)) { + __asm__ __volatile__ ("hlt"); + } +#endif + while (addr->error == 0xFFFF) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + uhci->frame_list[frame] = 1; + locked = 0; + return addr->error; +} + +static void uhci_handler(struct cdi_device *cdi_hci) +{ + struct uhci *uhci = (struct uhci *)((struct cdi_hci *)cdi_hci)->hci; + int status = cdi_inw(uhci->pbase + UHCI_USBSTS), i; + struct transfer *addr; + struct uhci_td *td; + + if (!status) { //Also, von hier kommt der IRQ nicht. + return; + } + if (status & ~0x0001) { + dprintf("Unerwarteter IRQ 0x%04X von %s\n", status, cdi_hci->name); + } + if (status & 0x10) + { + printf("[uhci] SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n"); + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! + } + else + { + for (i = 0; (addr = cdi_list_get(active_transfers, i)) != NULL; i++) + { + td = addr->virt; + if (td->active) { + continue; + } + addr->error = USB_NO_ERROR; + cdi_list_remove(active_transfers, i--); + if (td->stalled_err) + { + dprintf("ENDPOINT STALLED\n"); + addr->error |= USB_STALLED; + } + if (td->buf_err) + { + dprintf("Puffer?berlauf oder Underrun\n"); + addr->error |= USB_BUFFER_ERROR; + } + if (td->babble) + { + dprintf("Puh, da war ein Ger?t wohl sehr gespr?chig: Babble.\n"); + addr->error |= USB_BABBLE; + } + if (td->nak) + { + dprintf("NAK empfangen\n"); + addr->error |= USB_NAK; + } + if (td->crc_time_err) + { + dprintf("CRC-Fehler oder Timeout\n"); + addr->error |= USB_CRC | USB_TIMEOUT; + } + if (td->bitstuff_err) + { + dprintf("Bitstufffehler\n"); + addr->error |= USB_BITSTUFF; + } + //TODO: free(td) - dazu w?re eine CDI-Funktion n?tzlich + } + } + cdi_outw(uhci->pbase + UHCI_USBSTS, status); +} -- 1.6.0.2 From max at tyndur.org Mon Aug 17 20:56:17 2009 From: max at tyndur.org (Max Reitz) Date: Mon, 17 Aug 2009 20:56:17 +0200 Subject: [cdi-devel] [PATCH] UHCI, USB, MSC Message-ID: <1250535377-10593-1-git-send-email-max@tyndur.org> From: Max Reitz + UHCI/USB/MSC (MSDs erscheinen als CDI.storage), der Schreibsupport ist gro?e Gl?ckssache, Treiber nur unter QEMU erfolgreich getestet... Signed-off-by: Max Reitz --- usb/include/msd.h | 104 +++++++++ usb/include/uhci.h | 110 ++++++++++ usb/include/usb.h | 287 +++++++++++++++++++++++++ usb/main.c | 451 +++++++++++++++++++++++++++++++++++++++ usb/msd.c | 599 ++++++++++++++++++++++++++++++++++++++++++++++++++++ usb/uhci.c | 339 +++++++++++++++++++++++++++++ 6 files changed, 1890 insertions(+), 0 deletions(-) create mode 100644 usb/include/msd.h create mode 100644 usb/include/uhci.h create mode 100644 usb/include/usb.h create mode 100644 usb/main.c create mode 100644 usb/msd.c create mode 100644 usb/uhci.c diff --git a/usb/include/msd.h b/usb/include/msd.h new file mode 100644 index 0000000..99c796d --- /dev/null +++ b/usb/include/msd.h @@ -0,0 +1,104 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef _CDI__USB__MSD_H +#define _CDI__USB__MSD_H + +#include + +#include "cdi/storage.h" + +#include "usb.h" + + +//"USBC" +#define CBW_SIGNATURE 0x43425355 +struct command_block_wrapper +{ + uint32_t cbw_signature; + uint32_t cbw_tag; + uint32_t cbw_data_transfer_length; + uint8_t cbw_flags; + uint8_t cbw_lun; + uint8_t cbw_cb_length; + uint8_t cbw_cb[16]; +} __attribute__((packed)); + +//"USBS" +#define CSW_SIGNATURE 0x53425355 +struct command_status_wrapper +{ + uint32_t csw_signature; + uint32_t csw_tag; + uint32_t csw_data_residue; + uint8_t csw_status; +} __attribute__((packed)); + +struct msc_sense +{ + unsigned error : 7; + unsigned valid : 1; + uint8_t rsvd0; + unsigned sense_key : 4; + unsigned rsvd1 : 4; + uint32_t information; + uint8_t additional_length; + uint32_t rsvd2; + uint8_t additional_code; + uint8_t additional_code_qualifier; + uint32_t rsvd; +} __attribute__((packed)); + +struct msd_capacity +{ + uint32_t last_lba; + uint32_t block_length; +} __attribute__((packed)); + +struct cdi_msd +{ + struct cdi_storage_device cdi_device; + struct usb_device *usb_device; + uint32_t offset; //F?r Partitionen +}; + +struct part_table_entry +{ + uint8_t active; + uint8_t begin_chs[3]; + uint8_t type; + uint8_t end_chs[3]; + uint32_t start; + uint32_t size; +} __attribute__((packed)); + + +#define MSC_CMD_REZERO 0x01 +#define MSC_CMD_SENSE 0x03 +#define MSC_CMD_CAPACITY 0x25 +#define MSC_CMD_READ10 0x28 +#define MSC_CMD_WRITE10 0x2A +#define MSC_CMD_SEEK 0x2B +#define MSC_CMD_READ12 0xA8 +#define MSC_CMD_WRITE12 0xAA + +#endif diff --git a/usb/include/uhci.h b/usb/include/uhci.h new file mode 100644 index 0000000..0158bd2 --- /dev/null +++ b/usb/include/uhci.h @@ -0,0 +1,110 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef _CDI__USB__UHCI_H +#define _CDI__USB__UHCI_H + +#include + +#include "cdi/misc.h" +#include "cdi/pci.h" + +#include "usb.h" + + +#define UHCI_USBCMD 0x00 //w +#define UHCI_USBSTS 0x02 //w +#define UHCI_USBINTR 0x04 //w +#define UHCI_FRNUM 0x06 //w +#define UHCI_FRBASEADD 0x08 //l +#define UHCI_SOFMOD 0x0C //b +#define UHCI_RPORTS 0x10 +#define UHCI_PORTSC1 0x10 //w +#define UHCI_PORTSC2 0x12 //w + +#define MAXP 0x0080 +#define GRESET 0x0004 +#define HCRESET 0x0002 +#define USB_RUN 0x0001 + +#define RPORT_RESET 0x0200 +#define RPORT_LOSPD 0x0100 //Low speed device attached +#define RPORT_ENABLE 0x0004 +#define RPORT_CSC 0x0002 //Connect status change +#define RPORT_DEVICE 0x0001 + + +struct uhci +{ + struct hci gen_hci; + uint16_t pbase; + uintptr_t phys_frame_list; + uint32_t *frame_list; + int root_ports; +}; + +struct uhci_qh +{ + volatile uint32_t next; + volatile uint32_t transfer; +} __attribute__((packed)); + +struct uhci_td +{ + volatile uint32_t next; + + unsigned trans_len : 11; + unsigned rsvd0 : 6; + unsigned bitstuff_err : 1; + unsigned crc_time_err : 1; + unsigned nak : 1; + unsigned babble : 1; + unsigned buf_err : 1; + unsigned stalled_err : 1; + unsigned active : 1; + unsigned ioc : 1; + unsigned isochronous : 1; + unsigned low_speed : 1; + unsigned errors : 2; + unsigned spd : 1; + unsigned rsvd1 : 2; + + unsigned pid : 8; + unsigned device : 7; + unsigned endpoint : 4; + unsigned data_toggle : 1; + unsigned rsvd2 : 1; + unsigned maxlen : 11; + + uint32_t buffer; + + uint32_t user[4]; +} __attribute__((packed)); + +struct transfer +{ + void *virt; + uintptr_t phys; + volatile int error; +}; + +#endif diff --git a/usb/include/usb.h b/usb/include/usb.h new file mode 100644 index 0000000..2c435d8 --- /dev/null +++ b/usb/include/usb.h @@ -0,0 +1,287 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef _CDI__USB__USB_H +#define _CDI__USB__USB_H + +#include + +#include "cdi/misc.h" +#include "cdi/pci.h" + + +#define PACKET_SETUP 0x2D +#define PACKET_IN 0x69 +#define PACKET_OUT 0xE1 + +#define NO_DATA 0x01 +#define DEV_TO_HOST 0x80 +#define HOST_TO_DEV 0x00 +#define STD_REQUEST 0x00 +#define CLS_REQUEST 0x20 +#define VEN_REQUEST 0x40 +#define REC_DEVICE 0x00 +#define REC_INTERFACE 0x01 +#define REC_ENDPOINT 0x02 +#define REC_OTHER 0x03 + +#define GET_STATUS 0 +#define CLEAR_FEATURE 1 +#define SET_FEATURE 3 +#define SET_ADDRESS 5 +#define GET_DESCRIPTOR 6 +#define SET_DESCRIPTOR 7 +#define GET_CONFIGURATION 8 +#define SET_CONFIGURATION 9 +#define GET_INTERFACE 10 +#define SET_INTERFACE 11 +#define SYNC_FRAME 12 + +#define PORT_DEVICE 0x0001 +#define PORT_ENABLED 0x0002 +#define PORT_SUSPENDED 0x0004 +#define PORT_OVERCURRENT 0x0008 +#define PORT_RESET 0x0010 +#define PORT_POWER 0x0100 +#define PORT_LOWSPEED 0x0200 +#define PORT_HIGHSPEED 0x0400 +#define PORT_TEST 0x0800 +#define PORT_INDICATOR 0x1000 + +#define PORTF_CONNECTION 0 +#define PORTF_ENABLE 1 +#define PORTF_SUSPEND 2 +#define PORTF_OVERCURRENT 3 +#define PORTF_RESET 4 +#define PORTF_POWER 8 +#define PORTF_LOWSPEED 9 +#define PORTF_HIGHSPEED 10 +#define PORTF_C_CONNECTION 16 +#define PORTF_C_ENABLE 17 +#define PORTF_C_SUSPEND 18 +#define PORTF_C_OVERCURRENT 19 +#define PORTF_C_RESET 20 +#define PORTF_TEST 21 +#define PORTF_INDICATOR 22 + +#define DESC_DEVICE 1 +#define DESC_CONFIGURATION 2 +#define DESC_STRING 3 +#define DESC_INTERFACE 4 +#define DESC_ENDPOINT 5 +#define DESC_DEVICE_QUALIFIER 6 +#define DESC_OTHER_SPEED_CONFIGURATION 7 +#define DESC_INTERFACE_POWER 8 + +#define USB_NO_ERROR 0x00 +#define USB_STALLED 0x01 +#define USB_BUFFER_ERROR 0x02 +#define USB_BABBLE 0x04 +#define USB_NAK 0x08 +#define USB_CRC 0x10 +#define USB_TIMEOUT 0x20 +#define USB_BITSTUFF 0x40 +#define USB_TRIVIAL_ERROR 0x80 + +#define USB_TOD_SETUP 0x0001 +#define USB_TOD_SETUP_DATA_IN 0x0002 +#define USB_TOD_SETUP_DATA_OUT 0x0004 +#define USB_TOD_SETUP_ACK_IN 0x0008 +#define USB_TOD_SETUP_ACK_OUT 0x0010 +#define USB_TOD_COMMAND 0x0020 +#define USB_TOD_DATA_IN 0x0040 +#define USB_TOD_DATA_OUT 0x0080 +#define USB_TOD_STATUS 0x0100 + +#define USB_TODS_SETUP 0 +#define USB_TODS_SETUP_DATA_IN 1 +#define USB_TODS_SETUP_DATA_OUT 2 +#define USB_TODS_SETUP_ACK_IN 3 +#define USB_TODS_SETUP_ACK_OUT 4 +#define USB_TODS_COMMAND 5 +#define USB_TODS_DATA_IN 6 +#define USB_TODS_DATA_OUT 7 +#define USB_TODS_STATUS 8 + + +typedef enum +{ + HCI_UHCI, + HCI_OHCI, + HCI_EHCI +} hci_type_t; + +struct cdi_hci +{ + struct cdi_device cdi_device; + struct hci *hci; +}; + +struct usb_device; + +struct hci +{ + struct cdi_pci_device *pcidev; + hci_type_t type; + cdi_list_t (*find_devices)(struct hci *); + void (*activate_device)(struct hci *, struct usb_device *); + int (*do_packet)(struct hci *, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle); + int (*get_frame)(struct hci *); +}; + +struct setup_packet +{ + uint8_t request_type; + uint8_t request; + uint16_t value; + uint16_t index; + uint16_t length; +} __attribute__((packed)); + +struct device_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint16_t bcdUSB; + uint8_t class_id; + uint8_t subclass_id; + uint8_t protocol_id; + uint8_t max_packet_size0; + uint16_t vendor_id; + uint16_t device_id; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t num_configurations; +} __attribute__((packed)); + +struct config_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint16_t total_length; + uint8_t num_interfaces; + uint8_t config_value; + uint8_t iConfiguration; + uint8_t attributes; + uint8_t max_power; +} __attribute__((packed)); + +struct interface_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint8_t interface_number; + uint8_t alternate_setting; + uint8_t num_endpoints; + uint8_t interface_class; + uint8_t interface_subclass; + uint8_t interface_protocol; + uint8_t iInterface; +} __attribute__((packed)); + +struct endpoint_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint8_t endpoint_address; + uint8_t attributes; + uint16_t max_packet_size; + uint8_t interval; +} __attribute__((packed)); + +struct hub_desc +{ + uint8_t length; + uint8_t descriptor_type; + uint8_t nbr_ports; + uint16_t hub_characteristics; + uint8_t pwron2pwrgood; + uint8_t hub_contr_current; +} __attribute__((packed)); + +typedef enum +{ + USBDC_COMPOSITE = 0x00, + USBDC_AUDIO = 0x01, + USBDC_COMM_CDC = 0x02, + USBDC_HID = 0x03, + USBDC_PID = 0x05, + USBDC_IMAGE = 0x06, + USBDC_PRINTER = 0x07, + USBDC_MSC = 0x08, + USBDC_HUB = 0x09, + USBDC_CDC_DATA = 0x0A, + USBDC_CHIP = 0x0B, + USBDC_SECURITY = 0x0D, + USBDC_VIDEO = 0x0E, + USBDC_HEALTH = 0x0F, + USBDC_DIAGNOSIS = 0xDC, + USBDC_WIRELESS = 0xE0, + USBDC_MISC = 0xEF, + USBDC_SOFT_SPEC = 0xFE, + USBDC_VENDOR_SPEC = 0xFF +} usb_class_t; + +struct class_data +{ + usb_class_t type; +}; + +struct usb_device +{ + struct hci *hci; + int id; + int port; + int low_speed; + struct device_desc *device; + struct config_desc *config; + struct interface_desc *interface; + struct class_data *classd; + int stalled; + int locked; + int expects; +}; + +struct msclass_data +{ + struct class_data gen_class; + struct endpoint_desc *bulk_ep_in; + struct endpoint_desc *bulk_ep_out; +}; + + +#include "uhci.h" + + +#define HCI_STRUCT_SIZE sizeof(struct uhci) + + +int do_packet(struct usb_device *device, int type, int endpoint, uintptr_t phys_data, int length, int datatoggle, int type_of_data); +void enumerate_hci(struct hci *); +struct cdi_driver *init_uhcd(void); +void init_msc_driver(void); +void register_msd(struct usb_device *usbdev); +void uhci_init(struct cdi_device *cdi_hci); + +#endif diff --git a/usb/main.c b/usb/main.c new file mode 100644 index 0000000..c06f15d --- /dev/null +++ b/usb/main.c @@ -0,0 +1,451 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include +#include + +#include "cdi/io.h" +#include "cdi/misc.h" +#include "cdi/pci.h" + +#include "usb.h" + +#define DEBUG + +#ifdef DEBUG +#include +#define dprintf(fmt, args...) printf("[usb] " fmt, ##args) +#define _dprintf(fmt, args...) printf(fmt, ##args) +#else +static int dprintf(const char *fmt, ...) +{ + return 0; +} +#define _dprintf(fmt, args...) dprintf(fmt, ##args) +#endif + +static struct usb_device *devices[128]; +static cdi_list_t ehci, ohci, uhci; +static void (*ccdriver_take_device[0x100])(struct usb_device *); +static int usb_dev_ids = 1; + +static void usb_init(void); +static void enumerate_hub(struct usb_device *usbdev); + +#ifdef CDI_STANDALONE +int main(void) +#else +int init_usb(void) +#endif +{ + cdi_init(); + + memset(devices, 0, 128 * sizeof(struct usb_device *)); + + usb_init(); + + cdi_run_drivers(); + + return 0; +} + +static const int next_data_type[9] = { + USB_TOD_SETUP_DATA_IN | USB_TOD_SETUP_DATA_OUT | USB_TOD_SETUP_ACK_IN, + USB_TOD_SETUP_ACK_OUT, + USB_TOD_SETUP_ACK_IN, + USB_TOD_SETUP | USB_TOD_COMMAND, + USB_TOD_SETUP | USB_TOD_COMMAND, + USB_TOD_DATA_IN | USB_TOD_DATA_OUT | USB_TOD_STATUS, + USB_TOD_STATUS, + USB_TOD_STATUS, + USB_TOD_SETUP | USB_TOD_COMMAND +}; + +/*static const char *tod_name[9] = { + "SETUP", + "SETUP DS IN", + "SETUP DS OUT", + "SETUP ACK IN", + "SETUP ACK OUT", + "COMMAND", + "DATA IN", + "DATA OUT", + "STATUS" +};*/ + +/** +* Verarbeitet ein USB-Paket. +* +* @param device Das USB-Ger?t +* @param type Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) +* @param endpoint Der gew?nschte Endpoint (0 bis 15) +* @param phys_data Die physische Adresse des zu verwendenden Datenpuffers +* @param length Die L?nge des Puffers +* @param datatoggle Gibt an, ob DATA0 (0) oder DATA1 (1) verwendet werden soll +* @param type_of_data Typ der Daten (zur Sicherheit, damit es kein doofes STALL gibt) +*/ + +int do_packet(struct usb_device *device, int type, int endpoint, uintptr_t phys_data, int length, int datatoggle, int type_of_data) +{ + int error; + int tod_short; + + if (!(device->expects & type_of_data)) { + dprintf("Expected 0x%04X, got 0x%04X...?\n", device->expects, type_of_data); + for (;;); + } + type_of_data >>= 1; + for (tod_short = 0; type_of_data; tod_short++) { + type_of_data >>= 1; + } + //dprintf("TOD: %s\n", tod_name[tod_short]); + device->expects = next_data_type[tod_short]; + endpoint &= 0x07; + type &= 0xFF; + if (device->stalled) //TODO: Irgendwie entstallen, evtl.? oO + { + dprintf("Zugriff auf stalled-Ger?t verweigert.\n"); + return USB_STALLED; + } + error = device->hci->do_packet(device->hci, + (device->hci->get_frame(device->hci) + 3) & 0x3FF, + type, device->id, endpoint, + device->low_speed, phys_data, length, + datatoggle); + if (error == USB_STALLED) + { + printf("[usb] ENDPOINT %i DES GER?TS %i STALLED!\n", endpoint, device->id); + device->stalled = 1; + } + return error; +} + +static void *do_control(struct usb_device *device, int direction, void *buffer, int length, int rtype, int recipient, int request, int value, int index) +{ + void *rbuf; + int rval, no_data; + struct setup_packet *setup; + uintptr_t pbuf, psetup; + + no_data = direction & NO_DATA; + if (!no_data && (cdi_alloc_phys_mem(length, &rbuf, (void **)&pbuf) == -1)) { + return NULL; + } + if (cdi_alloc_phys_mem(sizeof(struct setup_packet), + (void **)&setup, (void **)&psetup) == -1) { + return NULL; + } + direction &= 0x80; + if ((direction == HOST_TO_DEV) && length && (buffer != NULL)) { + memcpy(rbuf, buffer, length); + } + setup->request_type = direction | (rtype & 0x60) | (recipient & 0x1F); + setup->request = request; + setup->value = value; + setup->index = index; + setup->length = length; + if (do_packet(device, PACKET_SETUP, 0, psetup, sizeof(struct setup_packet), + 0, USB_TOD_SETUP) != USB_NO_ERROR) { + return NULL; + } + cdi_sleep_ms(20); + if (no_data) + { + rval = USB_NO_ERROR; + rbuf = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten + } + else + { + if (direction == DEV_TO_HOST) { + rval = do_packet(device, PACKET_IN, 0, pbuf, length, 1, + USB_TOD_SETUP_DATA_IN); + } else { + rval = do_packet(device, PACKET_OUT, 0, pbuf, length, 1, + USB_TOD_SETUP_DATA_OUT); + } + } + if (rval == USB_NO_ERROR) + { + cdi_sleep_ms(20); + if (no_data || (direction == HOST_TO_DEV)) { + rval = do_packet(device, PACKET_IN, 0, 0, 0, 1, USB_TOD_SETUP_ACK_IN); + } else { + rval = do_packet(device, PACKET_OUT, 0, 0, 0, 1, USB_TOD_SETUP_ACK_OUT); + } + } + return (rval == USB_NO_ERROR) ? rbuf : NULL; +} + +static void usb_init(void) +{ + struct cdi_pci_device *dev; + struct hci *hci; + struct cdi_hci *cdi_hci; + struct cdi_driver *uhcd; + char *dev_name; + int i; + + init_msc_driver(); + + memset(ccdriver_take_device, 0, sizeof(void (*)(struct usb_device *)) * 0x100); + ccdriver_take_device[8] = ®ister_msd; + ccdriver_take_device[9] = &enumerate_hub; + + ehci = cdi_list_create(); + ohci = cdi_list_create(); + uhci = cdi_list_create(); + + cdi_list_t pci_devices = cdi_list_create(); + cdi_pci_get_all_devices(pci_devices); + for (i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) + { + if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03)) { + cdi_pci_device_destroy(dev); + } else { + switch (dev->interface_id) + { + case 0x00: + hci = malloc(HCI_STRUCT_SIZE); + hci->pcidev = dev; + hci->type = HCI_UHCI; + cdi_list_push(uhci, hci); + break; + case 0x10: + hci = malloc(HCI_STRUCT_SIZE); + hci->pcidev = dev; + hci->type = HCI_OHCI; + cdi_list_push(ohci, hci); + break; + case 0x20: + hci = malloc(HCI_STRUCT_SIZE); + hci->pcidev = dev; + hci->type = HCI_EHCI; + cdi_list_push(ehci, hci); + } + } + } + + cdi_list_destroy(pci_devices); + + dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n", cdi_list_size(ehci), + cdi_list_size(ohci), + cdi_list_size(uhci)); + + uhcd = init_uhcd(); + + for (i = 0; (hci = cdi_list_pop(uhci)) != NULL; i++) + { + cdi_hci = malloc(sizeof(struct cdi_hci)); + cdi_hci->cdi_device.type = CDI_UNKNOWN; + dev_name = malloc(10); + sprintf(dev_name, "uhci%i", i); + cdi_hci->cdi_device.name = dev_name; + hci->find_devices = NULL; + cdi_hci->hci = hci; + //cdi_list_push(uhcd->devices, cdi_hci); + //^ Das ist zwar sehr sch?n, geht so aber leider nicht + dprintf("%s registriert.\n", dev_name); + cdi_hci->cdi_device.driver = uhcd; + uhci_init(&cdi_hci->cdi_device); + } + + cdi_list_destroy(ehci); + cdi_list_destroy(ohci); + cdi_list_destroy(uhci); +} + +static void enum_device(struct usb_device *usbdev) +{ + struct device_desc *dev_desc; + struct config_desc *conf_desc; + struct interface_desc *if_desc, *best_if; + char *name; + void *position; + int i, id; + + usbdev->stalled = 0; + usbdev->locked = 0; + usbdev->expects = USB_TOD_SETUP | USB_TOD_COMMAND; + dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_DEVICE << 8, 0); + if (dev_desc == NULL) { + return; + } + usbdev->device = dev_desc; + dprintf("0x%04X:0x%04X (%i) -> ", dev_desc->vendor_id, dev_desc->device_id, + dev_desc->class_id); + do_control(usbdev, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_ADDRESS, (id = usb_dev_ids++), 0); + usbdev->id = id; + _dprintf("%i\n", id); + if (dev_desc->iManufacturer) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iManufacturer, 0); + if (name == NULL) { + return; + } + dprintf(" -> Hersteller: "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf("\n"); + } + if (dev_desc->iProduct) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iProduct, 0); + if (name == NULL) { + return; + } + dprintf(" -> Produkt: "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf("\n"); + } + devices[id] = usbdev; + //TODO: Man kann doch nicht immer den ersten nehmen... + conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, + sizeof(struct config_desc), STD_REQUEST, REC_DEVICE, + GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return; + } + conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, conf_desc->total_length, + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return; + } + usbdev->config = conf_desc; + do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_CONFIGURATION, conf_desc->config_value, 0); + if (conf_desc->iConfiguration) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | conf_desc->iConfiguration, 0); + if (name == NULL) { + return; + } + dprintf("Verwende Konfiguration "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf(".\n"); + } + position = conf_desc; + position += sizeof(struct config_desc); + best_if = position; //Standard-IF + for (i = 0; i < conf_desc->num_interfaces; i++) + { + if_desc = position; + if (if_desc->interface_class == 8) //TODO: Mehr Klassencodes nat?rlich! + { + best_if = if_desc; + break; + } + position += sizeof(struct interface_desc) + if_desc->num_endpoints * sizeof(struct endpoint_desc); + } + do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_INTERFACE, best_if->interface_number, 0); + usbdev->interface = best_if; + if (best_if->iInterface) + { + name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | best_if->iInterface, 0); + if (name == NULL) { + return; + } + dprintf("Verwende Interface "); + for (i = 2; i < name[0]; i += 2) { + _dprintf("%c", name[i]); + } + _dprintf(".\n"); + } + dprintf("Konfiguration: %i:%i (%i)\n", conf_desc->config_value, + best_if->interface_number, + best_if->interface_class); + if (ccdriver_take_device[best_if->interface_class] != NULL) { + ccdriver_take_device[best_if->interface_class](usbdev); + } +} + +static void enumerate_hub(struct usb_device *usbdev) +{ + struct hub_desc *hub_desc; + struct usb_device *down; + uint16_t *port_status; + int i; + + //Mehr als 8 Ports sollte es nicht geben, die Gr??e des Deskriptors wird + //daher wohl nicht gr??er als sizeof()+16 werden + hub_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*hub_desc) + 16, + CLS_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_DEVICE << 8, 0); + for (i = 0; i < hub_desc->nbr_ports; i++) + { + port_status = do_control(usbdev, DEV_TO_HOST, NULL, 4, CLS_REQUEST, + REC_OTHER, GET_STATUS, 0, i+1); + if (!(port_status[0] & PORT_DEVICE)) { + continue; + } + //Strom an + do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, + SET_FEATURE, PORTF_POWER, i+1); + cdi_sleep_ms(hub_desc->pwron2pwrgood * 2); + //Resetten + do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, + SET_FEATURE, PORTF_RESET, i+1); + //Reset sollte jetzt eigtl. schon beendet sein + //(do_control wartet ja 50 ms) + down = malloc(sizeof(struct usb_device)); + down->hci = usbdev->hci; + down->id = 0; + down->port = i; + down->low_speed = (port_status[0] & PORT_LOWSPEED) && 1; + enum_device(down); + } +} + +void enumerate_hci(struct hci *hci) +{ + struct usb_device *usbdev; + cdi_list_t usb_devices; + + if (hci->find_devices != NULL) + { + usb_devices = hci->find_devices(hci); + while ((usbdev = cdi_list_pop(usb_devices)) != NULL) + { + hci->activate_device(hci, usbdev); + cdi_sleep_ms(50); + enum_device(usbdev); + } + } +} diff --git a/usb/msd.c b/usb/msd.c new file mode 100644 index 0000000..11fbd6a --- /dev/null +++ b/usb/msd.c @@ -0,0 +1,599 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include +#include +#include + +#include "cdi/misc.h" +#include "cdi/storage.h" + +#include "msd.h" +#include "usb.h" + +//DEBUG definieren, um einige Debugmeldungen anzuzeigen +#define DEBUG +//FULLDEBUG definieren, um ALLE Debugmeldungen anzuzeigen +#define FULLDEBUG + +#if defined FULLDEBUG && !defined DEBUG +#define DEBUG +#endif + +#ifdef DEBUG +#define dprintf(fmt, args...) printf("[usb-msc] " fmt, ##args) +#define _dprintf(fmt, args...) printf(fmt, ##args) +#else +static int dprintf(const char *fmt, ...) +{ + return 0; +} +#define _dprintf(fmt, args...) dprintf(fmt, ##args) +#endif + +#ifdef FULLDEBUG +#define fdprintf(fmt, args...) printf("[usb-msc] " fmt, ##args) +#else +static int fdprintf(const char *fmt, ...) +{ + return 0; +} +#endif + +static uint32_t cbw_tag = 1; + +static const char *driver_name = "usb-msc"; + +static struct cdi_storage_driver cdi_driver; + +static void deinit_msd(struct cdi_device *cdi_msd); +static void get_partitions(struct cdi_storage_device *cdistrg); +static void init_msd(struct cdi_device *cdi_msd); +static void kill_driver(struct cdi_driver *cdi_msc_driver); +static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer); +static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer); +static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count); + +//TODO: CDI-Funktion w?re hier sicher n?tzlich... +#define CPU_IS_LITTLE_ENDIAN + +#define change_endianess_32(_) ((((_)&0xFF000000)>>24)|(((_)&0xFF0000)>>8)|(((_)&0xFF00)<<8)|(((_)&0xFF)<<24)) +#ifdef CPU_IS_LITTLE_ENDIAN +#define CPU2BE(_) change_endianess_32(_) +#define CPU2LE(_) (_) +#define BE2CPU(_) change_endianess_32(_) +#define LE2CPU(_) (_) +#else +#define CPU2BE(_) (_) +#define CPU2LE(_) change_endianess_32(_) +#define BE2CPU(_) (_) +#define LE2CPU(_) change_endianess_32(_) +#endif + +//Das muss definiert werden, um vor einem Befehl zu warten, bis das MSD fertig +//ist. Theoretisch ist das ?u?erst sinnvoll, praktisch h?ngt sich der Treiber +//bisweilen auf - das sollte allerdings mit dem Timeout gegessen sein +/** +#define WAIT_FOR_MSD_READY +*/ + +//TODO: Storage bietet eine SCSI-Klasse. Die k?nnte man durchaus benutzen. xD + +void init_msc_driver() +{ + struct cdi_driver *stddrv = (struct cdi_driver *)&cdi_driver; + stddrv->type = CDI_STORAGE; + stddrv->name = driver_name; + stddrv->init_device = &init_msd; + stddrv->remove_device = &deinit_msd; + stddrv->destroy = &kill_driver; + cdi_driver.read_blocks = &msd_cdi_read; + cdi_driver.write_blocks = &msd_cdi_write; + cdi_storage_driver_init(&cdi_driver); + cdi_storage_driver_register(&cdi_driver); +} + +void register_msd(struct usb_device *usbdev) +{ + struct cdi_storage_device *strgdev; + struct cdi_msd *cdimsd; + struct msclass_data *msc; + struct endpoint_desc *ep_desc; + void *address; + int i; + uint32_t bs, bc; + uint64_t size; + static int msdnum = 0; + + if (usbdev->interface->interface_protocol != 0x50) + { + dprintf("Es werden nur bulk-only-Interfaces unterst?tzt.\n"); + return; + } + cdimsd = malloc(sizeof(struct cdi_msd)); + if (cdimsd == NULL) { + return; + } + cdimsd->usb_device = usbdev; + cdimsd->offset = 0; + strgdev = (struct cdi_storage_device *)cdimsd; + strgdev->dev.type = CDI_STORAGE; + strgdev->dev.name = malloc(10); + if (strgdev->dev.name == NULL) + { + free(strgdev); + return; + } + sprintf((char *)strgdev->dev.name, "msd%i", msdnum++); + msc = malloc(sizeof(struct msclass_data)); + if (msc == NULL) + { + free((void *)strgdev->dev.name); + free(strgdev); + return; + } + usbdev->classd = (struct class_data *)msc; + msc->bulk_ep_in = NULL; + msc->bulk_ep_out = NULL; + address = (void *)usbdev->interface + sizeof(struct interface_desc); + for (i = 0; i < usbdev->interface->num_endpoints; i++) + { + ep_desc = address; + if ((ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (msc->bulk_ep_in == NULL)) { //BULK-IN + msc->bulk_ep_in = ep_desc; + } else if (!(ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (msc->bulk_ep_out == NULL)) { //BULK-OUT + msc->bulk_ep_out = ep_desc; + } + address += sizeof(struct endpoint_desc); + } + if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL)) + { + dprintf("Nicht gen?gend Endpoints gefunden.\n"); + return; + } + if (!msd_get_capacity(usbdev, &bs, &bc)) + { + strgdev->block_size = 0; + strgdev->block_count = 0; + dprintf("Konnte Gr??e f?r %s nicht ermitteln.\n", strgdev->dev.name); + } + else + { + strgdev->block_size = bs; + strgdev->block_count = bc; + size = bs; + size *= bc; + dprintf("%s: %i * %i B (ca. %i MB).\n", strgdev->dev.name, bc, bs, size >> 20); + } + cdi_storage_device_init(strgdev); + cdi_list_push(cdi_driver.drv.devices, strgdev); + get_partitions(strgdev); +} + +static void init_msd(struct cdi_device *cdi_dev) +{ + //Hier wird nichts gemacht, da die Partitionen schon in register_msd() + //erkannt werden M?SSEN. Somit l?uft die ganze Initialisierung dort ab. +} + +static void kill_driver(struct cdi_driver *cdi_msc_driver) +{ + //TODO: Man k?nnte doch noch irgendwie die Ger?te entfernen +} + +static void deinit_msd(struct cdi_device *cdi_msd) +{ + //TODO: Und gerade hier... +} + +static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + struct msd_capacity *cap; + uintptr_t pcbw, pcsw, pcap; + uint32_t expected_tag; + + if ((usbdev == NULL) || (block_size == NULL) || (block_count == NULL)) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct msd_capacity), + (void **)&cap, (void **)&pcap) == -1) { + return 0; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = sizeof(struct msd_capacity); + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_CAPACITY; + cbw->cbw_cb[1] = 0; //LUN: 0 + if (do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, pcbw, + 0x1F, 0, USB_TOD_COMMAND) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(20); + if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcap, + sizeof(struct msd_capacity), 0, USB_TOD_DATA_IN) + != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(5); + if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcsw, + 0x0D, 0, USB_TOD_STATUS) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(5); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) { + return 0; + } + *block_size = BE2CPU(cap->block_length); + *block_count = BE2CPU(cap->last_lba) + 1; + return 1; +} + +#ifdef WAIT_FOR_MSD_READY +static int msd_ready(struct usb_device *usbdev) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + uintptr_t pcbw, pcsw; + uint32_t expected_tag; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return 0; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return 0; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = 0; + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" + if (do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, pcbw, + 0x1F, 0, USB_TOD_COMMAND) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(7); + if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcsw, + 0x0D, 0, USB_TOD_STATUS) != USB_NO_ERROR) { + return 0; + } + cdi_sleep_ms(4); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) { + return 0; + } + return 1; +} +#endif + +static inline int tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} + +static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length); + +static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer) +{ + void *vb; + uintptr_t pb; + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int j; +#endif + uint32_t i; + struct usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + + fdprintf("read(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) + { + dprintf("Blockspeicher konnte nicht allociert werden.\n"); + return -1; + } + if (!count) + { + dprintf("Leere Leseanfrage.\n"); + return 0; + } + while (tsl(&usbdev->locked)) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + for (i = 0; i < count; i++) + { +#ifdef WAIT_FOR_MSD_READY + for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { + cdi_sleep_ms(20); + } +#endif + error = msd_read(usbdev, start + i, 1, pb, bs); + if (error != USB_NO_ERROR) + { + dprintf("Lesefehler 0x%X bei Block %i.\n", error, i); + usbdev->locked = 0; + return -1; + } + memcpy(buffer + i*bs, vb, bs); + } + usbdev->locked = 0; + return 0; +} + +static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + uintptr_t pcbw, pcsw; + uint32_t expected_tag; + int error; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (!phys_buffer || !length) { + return USB_TRIVIAL_ERROR; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = length; + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_READ10; + cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; + cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; + cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; + cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; + cbw->cbw_cb[8] = sectors & 0x000000FF; + error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, + pcbw, 0x1F, 0, USB_TOD_COMMAND); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(20); + error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, + phys_buffer, length, 0, USB_TOD_DATA_IN); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(5); + error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, + pcsw, 0x0D, 0, USB_TOD_STATUS); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(4); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) + { + dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature, + csw->csw_tag, + expected_tag, + csw->csw_status); + return USB_CRC; //Watt wei? denn ich... + } + return USB_NO_ERROR; +} + +static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length); + +static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer) +{ + void *vb; + uintptr_t pb; + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int j; +#endif + uint32_t i; + struct usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + + fdprintf("write(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) + { + dprintf("Blockspeicher zum Schreiben konnte nicht allociert werden.\n"); + return -1; + } + while (tsl(&usbdev->locked)) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + for (i = 0; i < count; i++) + { + memcpy(vb, buffer + i*bs, bs); +#ifdef WAIT_FOR_MSD_READY + for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { + cdi_sleep_ms(20); + } +#endif + error = msd_write(usbdev, start + i, 1, pb, bs); + if (error != USB_NO_ERROR) + { + dprintf("Schreibfehler 0x%X bei Block %i.\n", error, i); + usbdev->locked = 0; + return -1; + } + } + usbdev->locked = 0; + return 0; +} + +static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) +{ + struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + uintptr_t pcbw, pcsw; + uint32_t expected_tag; + int error; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return USB_TRIVIAL_ERROR; + } + if (!phys_buffer || !length) { + return USB_TRIVIAL_ERROR; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = length; + cbw->cbw_flags = 0x00; //OUT + cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_WRITE10; + cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; + cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; + cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; + cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; + cbw->cbw_cb[8] = sectors & 0x000000FF; + error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, + pcbw, 0x1F, 0, USB_TOD_COMMAND); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(20); + error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, + phys_buffer, length, 0, USB_TOD_DATA_OUT); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(5); + error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, + pcsw, 0x0D, 0, USB_TOD_STATUS); + if (error != USB_NO_ERROR) { + return error; + } + cdi_sleep_ms(4); + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || + csw->csw_status) + { + dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature, + csw->csw_tag, + expected_tag, + csw->csw_status); + return USB_CRC; + } + return USB_NO_ERROR; +} + +static void get_partitions(struct cdi_storage_device *cdistrg) +{ + void *mbr; + struct part_table_entry *partition; + struct cdi_msd *cdimsd, *base_cdimsd = (struct cdi_msd *)cdistrg; + int i, j = 0; + + mbr = malloc(512); + if (msd_cdi_read(cdistrg, 0, 1, mbr) != USB_NO_ERROR) { + dprintf("MBR konnte nicht eingelesen werden.\n"); + return; + } + if (((uint16_t *)mbr)[255] != 0xAA55) { + return; + } + partition = mbr + 0x1BE; + dprintf("Partitionen auf %s:", cdistrg->dev.name); + for (i = 0; i < 4; i++) + { + if (partition[i].type && partition[i].size && (partition[i].start + partition[i].size < cdistrg->block_count)) + { + cdimsd = malloc(sizeof(struct cdi_msd)); + if (cdimsd == NULL) { + break; + } + cdimsd->offset = partition[i].start; + cdimsd->usb_device = base_cdimsd->usb_device; + cdimsd->cdi_device.dev.type = CDI_STORAGE; + cdimsd->cdi_device.dev.name = malloc(14); + if (cdimsd->cdi_device.dev.name == NULL) + { + free(cdimsd); + break; + } + sprintf((char *)cdimsd->cdi_device.dev.name, "%sp%i", cdistrg->dev.name, j++); + cdimsd->cdi_device.block_size = base_cdimsd->cdi_device.block_size; + cdimsd->cdi_device.block_count = partition[i].size; + _dprintf(" %s", cdimsd->cdi_device.dev.name); + cdi_storage_device_init((struct cdi_storage_device *)cdimsd); + cdi_list_push(cdi_driver.drv.devices, cdimsd); + } + } + _dprintf("\n"); +} diff --git a/usb/uhci.c b/usb/uhci.c new file mode 100644 index 0000000..c09075c --- /dev/null +++ b/usb/uhci.c @@ -0,0 +1,339 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include +#include + +#include "cdi/io.h" +#include "cdi/lists.h" +#include "cdi/misc.h" +#include "cdi/pci.h" + +#include "uhci.h" +#include "usb.h" + +#define DEBUG + +#ifdef DEBUG +#include +#include +#define dprintf(fmt, args...) printf("[uhci] " fmt, ##args) +#define _dprintf(fmt, args...) printf(fmt, ##args) +#else +static int dprintf(const char *fmt, ...) +{ + return 0; +} +#define _dprintf(fmt, args...) dprintf(fmt, ##args) +#endif + +static const char *driver_name = "uhci"; + +static struct cdi_driver cdi_driver; +static cdi_list_t active_transfers; + +static void uhci_handler(struct cdi_device *dev); +static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle); +static cdi_list_t get_devices(struct hci *gen_hci); +static void activate_device(struct hci *gen_hci, struct usb_device *device); +static int get_current_frame(struct hci *gen_hci); +static void uhci_deinit(struct cdi_device *cdi_hci); +static void uhci_kill(struct cdi_driver *cdi_hcd); + + +struct cdi_driver *init_uhcd() +{ + active_transfers = cdi_list_create(); + cdi_driver.type = CDI_UNKNOWN; + cdi_driver.name = driver_name; + cdi_driver.init_device = &uhci_init; + cdi_driver.remove_device = &uhci_deinit; + cdi_driver.destroy = &uhci_kill; + cdi_driver_init(&cdi_driver); + cdi_driver_register(&cdi_driver); + return &cdi_driver; +} + +static void uhci_kill(struct cdi_driver *cdi_hcd) +{ + //TODO: Da geht doch noch was +} + +static void uhci_deinit(struct cdi_device *cdi_hci) +{ + cdi_outw(((struct uhci *)((struct cdi_hci *)cdi_hci)->hci)->pbase + UHCI_USBCMD, + MAXP); //HC anhalten + //TODO: Hier doch auch +} + +void uhci_init(struct cdi_device *cdi_hci) +{ + struct hci *gen_hci = ((struct cdi_hci *)cdi_hci)->hci; + struct uhci *uhci = (struct uhci *)gen_hci; + struct cdi_pci_resource *res; + int i, size = 0x14; + + //cdi_pci_alloc_ioports(gen_hci->pcidev); + uhci->pbase = 0; + for (i = 0; (res = cdi_list_get(gen_hci->pcidev->resources, i)) != NULL; + i++) + { + if (res->type == CDI_PCI_IOPORTS) + { + size = res->length ? res->length : 0x14; + if (cdi_ioports_alloc(res->start, size) == -1) + { + dprintf("I/O-Ports konnten nicht reserviert werden.\n"); + return; + } + uhci->pbase = res->start; + break; + } + } + if (!uhci->pbase) + { + dprintf("I/O-Basis nicht gefunden!\n"); + return; + } + if (cdi_alloc_phys_mem(4096, (void **)&uhci->frame_list, + (void **)&uhci->phys_frame_list) == -1) + { + dprintf("Frame List konnte nicht allociert werden!\n"); + return; + } + cdi_register_irq(gen_hci->pcidev->irq, &uhci_handler, cdi_hci); + uhci->root_ports = (size - 0x10) >> 1; + if (uhci->root_ports > 7) { //Laut Linuxkernel ist das so "weird", dass da was nicht stimmen kann... + uhci->root_ports = 2; + } + dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase, uhci->root_ports, gen_hci->pcidev->irq); + for (i = 0; i < 1024; i++) { + uhci->frame_list[i] = 1; //Invalid + } + dprintf("Resetten...\n"); + //HC zur?cksetzen + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); + for (i = 0; (cdi_inw(uhci->pbase + UHCI_USBCMD) & HCRESET) && (i < 50); i++) { + cdi_sleep_ms(10); + } + //Reset auf dem USB treiben + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | GRESET); + for (i = 0; (cdi_inw(uhci->pbase + UHCI_USBCMD) & GRESET) && (i < 50); i++) { + cdi_sleep_ms(10); + } + //Alle Interrupts senden + cdi_outw(uhci->pbase + UHCI_USBINTR, 0xF); + //Framelistadresse eintragen + cdi_outl(uhci->pbase + UHCI_FRBASEADD, uhci->phys_frame_list); + cdi_outw(uhci->pbase + UHCI_FRNUM, 0); //Frame zur?cksetzen + //Ports abschalten, damit selektiv eingeschaltet werden kann (Enumeration) + for (i = 0; i < uhci->root_ports; i++) { + cdi_outw(uhci->pbase + UHCI_RPORTS + i*2, RPORT_CSC); + } + dprintf(" Fertig\n"); + gen_hci->find_devices = &get_devices; + gen_hci->activate_device = &activate_device; + gen_hci->do_packet = &enqueue_request; + gen_hci->get_frame = &get_current_frame; + + enumerate_hci(gen_hci); +} + +static cdi_list_t get_devices(struct hci *gen_hci) +{ + struct uhci *uhci = (struct uhci *)gen_hci; + struct usb_device *dev; + cdi_list_t dlist = cdi_list_create(); + int i; + + dprintf("Ger?te finden: "); + cdi_outw(uhci->pbase + UHCI_USBCMD, USB_RUN); + for (i = 0; i < uhci->root_ports; i++) + { + if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_DEVICE)) + { + _dprintf("-"); + continue; + } + _dprintf("!"); + dev = malloc(sizeof(struct usb_device)); + dev->hci = gen_hci; + dev->id = 0; + dev->port = i; + dev->low_speed = + (cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_LOSPD) && 1; + cdi_list_push(dlist, dev); + } + _dprintf("\n"); + return dlist; +} + +static void activate_device(struct hci *gen_hci, struct usb_device *device) +{ + struct uhci *uhci = (struct uhci *)gen_hci; + + dprintf("Ger?t an Port %i wird aktiviert.\n", device->port); + cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, + RPORT_RESET | RPORT_ENABLE | RPORT_CSC); + cdi_sleep_ms(20); +} + +static int get_current_frame(struct hci *gen_hci) +{ + return cdi_inw(((struct uhci *)gen_hci)->pbase + UHCI_FRNUM); +} + +static inline int tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} +static volatile int locked = 0; + +static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle) +{ + struct uhci *uhci = (struct uhci *)gen_hci; + struct uhci_td *td; + struct uhci_qh *qh; + uintptr_t ptd, pqh; + struct transfer *addr; + + if (cdi_alloc_phys_mem(sizeof(struct uhci_td), + (void **)&td, (void **)&ptd) == -1) { + return USB_TRIVIAL_ERROR; + } + if (cdi_alloc_phys_mem(sizeof(struct uhci_qh), + (void **)&qh, (void **)&pqh) == -1) { + return USB_TRIVIAL_ERROR; + } + while (tsl(&locked)) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + qh->next = 1; //Invalid + qh->transfer = ptd; + memset(td, 0, sizeof(struct uhci_td)); + td->next = 1; //Invalid + td->active = 1; + td->ioc = 1; + td->data_toggle = datatoggle; + td->low_speed = low_speed; + td->errors = 1; + td->pid = type; + td->device = device; + td->endpoint = endpoint; + td->maxlen = length ? length-1 : 0x7FF; + td->buffer = phys_data; + addr = malloc(sizeof(struct transfer)); + addr->virt = td; + addr->phys = ptd; + addr->error = 0xFFFF; + cdi_list_push(active_transfers, addr); + uhci->frame_list[frame] = pqh | 2; +#ifdef CDI_STANDALONE + /*int cframe = get_current_frame(gen_hci), nframe; + nframe = (cframe > frame) ? frame + 1024 : frame; //?berl?ufe korrigieren + //Ann?hern, wann er wohl fertig ist + cdi_sleep_ms(nframe + 30 - get_current_frame(gen_hci));*/ +#else + while (!(qh->transfer & 1)) { + __asm__ __volatile__ ("hlt"); + } +#endif + while (addr->error == 0xFFFF) + { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + uhci->frame_list[frame] = 1; + locked = 0; + return addr->error; +} + +static void uhci_handler(struct cdi_device *cdi_hci) +{ + struct uhci *uhci = (struct uhci *)((struct cdi_hci *)cdi_hci)->hci; + int status = cdi_inw(uhci->pbase + UHCI_USBSTS), i; + struct transfer *addr; + struct uhci_td *td; + + if (!status) { //Also, von hier kommt der IRQ nicht. + return; + } + if (status & ~0x0001) { + dprintf("Unerwarteter IRQ 0x%04X von %s\n", status, cdi_hci->name); + } + if (status & 0x10) + { + printf("[uhci] SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n"); + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! + } + else + { + for (i = 0; (addr = cdi_list_get(active_transfers, i)) != NULL; i++) + { + td = addr->virt; + if (td->active) { + continue; + } + addr->error = USB_NO_ERROR; + cdi_list_remove(active_transfers, i--); + if (td->stalled_err) + { + dprintf("ENDPOINT STALLED\n"); + addr->error |= USB_STALLED; + } + if (td->buf_err) + { + dprintf("Puffer?berlauf oder Underrun\n"); + addr->error |= USB_BUFFER_ERROR; + } + if (td->babble) + { + dprintf("Puh, da war ein Ger?t wohl sehr gespr?chig: Babble.\n"); + addr->error |= USB_BABBLE; + } + if (td->nak) + { + dprintf("NAK empfangen\n"); + addr->error |= USB_NAK; + } + if (td->crc_time_err) + { + dprintf("CRC-Fehler oder Timeout\n"); + addr->error |= USB_CRC | USB_TIMEOUT; + } + if (td->bitstuff_err) + { + dprintf("Bitstufffehler\n"); + addr->error |= USB_BITSTUFF; + } + //TODO: free(td) - dazu w?re eine CDI-Funktion n?tzlich + } + } + cdi_outw(uhci->pbase + UHCI_USBSTS, status); +} -- 1.6.0.2 From kevin at tyndur.org Fri Aug 21 21:30:53 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Fri, 21 Aug 2009 21:30:53 +0200 Subject: [cdi-devel] [PATCH 0/2] Compiling CDI on Linux Message-ID: <1250883055-24218-1-git-send-email-kevin@tyndur.org> This series allows to compile the CDI drivers on Linux for basic patch validation. It removes unnecessary dependencies on the tyndur libc. Kevin Wolf (2): cdi: Add Makefile cdi: Fix compilation on Linux Makefile | 17 +++++++++++++++++ ata/main.c | 1 + ata/request.c | 1 + e1000/device.c | 4 ++-- include/cdi.h | 1 - iso9660/dir.c | 1 + iso9660/init.c | 4 ++-- iso9660/sector.c | 1 + ne2k/main.c | 1 + ne2k/ne2k.c | 1 + pcnet/pcnet.c | 4 ++-- ramdisk/file.c | 2 ++ ramdisk/init.c | 4 +++- ramdisk/link.c | 2 ++ ramdisk/res.c | 2 ++ rtl8139/main.c | 1 + rtl8139/rtl8139.c | 1 + serial/init.c | 2 ++ serial/res.c | 2 ++ 19 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 Makefile From kevin at tyndur.org Fri Aug 21 21:30:54 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Fri, 21 Aug 2009 21:30:54 +0200 Subject: [cdi-devel] [PATCH 1/2] cdi: Add Makefile In-Reply-To: <1250883055-24218-1-git-send-email-kevin@tyndur.org> References: <1250883055-24218-1-git-send-email-kevin@tyndur.org> Message-ID: <1250883055-24218-2-git-send-email-kevin@tyndur.org> Add a Makefile to the CDI repository. Drivers are compiled using the system compiler, but they are not linked. This allows at least some basic compile testing without moving patches into the source tree of an OS. Signed-off-by: Kevin Wolf --- Makefile | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b0ae0e0 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +SRCS = $(shell find -name '*.c') +OBJS = $(addsuffix .o,$(basename $(SRCS))) + +CC = gcc +CFLAGS = -m32 -Wall -g -I include -Werror -D_GNU_SOURCE + +run_command = $(if $(V),$(2), at echo $(1);$(2)) + +all: $(OBJS) + +%.o: %.c + $(call run_command,"CC $^",$(CC) $(CFLAGS) $(addprefix -I,$(addprefix $(dir $^),. include)) -c -o $@ $^) + +clean: + rm $(OBJS) + +.PHONY: clean -- 1.6.0.2 From kevin at tyndur.org Fri Aug 21 21:30:55 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Fri, 21 Aug 2009 21:30:55 +0200 Subject: [cdi-devel] [PATCH 2/2] cdi: Fix compilation on Linux In-Reply-To: <1250883055-24218-1-git-send-email-kevin@tyndur.org> References: <1250883055-24218-1-git-send-email-kevin@tyndur.org> Message-ID: <1250883055-24218-3-git-send-email-kevin@tyndur.org> Most CDI drivers assume a strange libc like the tyndur one. This patch cleans them up and allows to compile the drivers on Linux. Signed-off-by: Kevin Wolf --- ata/main.c | 1 + ata/request.c | 1 + e1000/device.c | 4 ++-- include/cdi.h | 1 - iso9660/dir.c | 1 + iso9660/init.c | 4 ++-- iso9660/sector.c | 1 + ne2k/main.c | 1 + ne2k/ne2k.c | 1 + pcnet/pcnet.c | 4 ++-- ramdisk/file.c | 2 ++ ramdisk/init.c | 4 +++- ramdisk/link.c | 2 ++ ramdisk/res.c | 2 ++ rtl8139/main.c | 1 + rtl8139/rtl8139.c | 1 + serial/init.c | 2 ++ serial/res.c | 2 ++ 18 files changed, 27 insertions(+), 8 deletions(-) diff --git a/ata/main.c b/ata/main.c index 144a6f8..d5200c2 100644 --- a/ata/main.c +++ b/ata/main.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "cdi/storage.h" #include "cdi/lists.h" diff --git a/ata/request.c b/ata/request.c index 6724416..e98ee60 100644 --- a/ata/request.c +++ b/ata/request.c @@ -28,6 +28,7 @@ #include #include +#include #include "cdi.h" #include "cdi/storage.h" diff --git a/e1000/device.c b/e1000/device.c index 48f3a58..dd37510 100644 --- a/e1000/device.c +++ b/e1000/device.c @@ -165,14 +165,14 @@ void e1000_init_device(struct cdi_device* device) } // Karte initialisieren - printf("e1000: IRQ %d, MMIO an %x Revision:%d\n", + printf("e1000: IRQ %d, MMIO an %p Revision:%d\n", netcard->pci->irq, netcard->mem_base, netcard->revision); printf("e1000: Fuehre Reset der Karte durch\n"); reset_nic(netcard); netcard->net.mac = get_mac_address(netcard); - printf("e1000: MAC-Adresse: %012llx\n", netcard->net.mac); + printf("e1000: MAC-Adresse: %012llx\n", (uint64_t) netcard->net.mac); cdi_net_device_init((struct cdi_net_device*) device); } diff --git a/include/cdi.h b/include/cdi.h index 7bcf9e4..d803751 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -19,7 +19,6 @@ #include "cdi/lists.h" #define CDI_STANDALONE -#define TYNDUR typedef enum { CDI_UNKNOWN = 0, diff --git a/iso9660/dir.c b/iso9660/dir.c index 0aec9cd..b1cae4f 100644 --- a/iso9660/dir.c +++ b/iso9660/dir.c @@ -18,6 +18,7 @@ #include #include +#include #include "cdi/fs.h" #include "cdi/lists.h" diff --git a/iso9660/init.c b/iso9660/init.c index 868ed7c..641d4f3 100644 --- a/iso9660/init.c +++ b/iso9660/init.c @@ -31,7 +31,7 @@ * @return If initialization was successful */ int iso9660_fs_init(struct cdi_fs_filesystem *fs) { - debug("iso9660_fs_init(0x%x)\n",fs); + debug("iso9660_fs_init(0x%p)\n",fs); struct iso9660_voldesc_prim *voldesc = malloc(sizeof(struct iso9660_voldesc_prim)); if (iso9660_voldesc_load(fs,ISO9660_VOLDESC_PRIM,voldesc)!=-1) { struct iso9660_fs_res *root_res = iso9660_dirrec_load(&voldesc->root_dir,NULL,voldesc); @@ -48,7 +48,7 @@ int iso9660_fs_init(struct cdi_fs_filesystem *fs) { * @return If destroy was successful */ int iso9660_fs_destroy(struct cdi_fs_filesystem *fs) { - fprintf(stderr,"iso9660_fs_destroy(0x%x)\n",fs); + fprintf(stderr,"iso9660_fs_destroy(0x%p)\n",fs); struct iso9660_fs_res *root_res = (struct iso9660_fs_res*)(fs->root_res); free(root_res->voldesc); cdi_cache_destroy(root_res->cache); diff --git a/iso9660/sector.c b/iso9660/sector.c index 48ffe82..645fb10 100644 --- a/iso9660/sector.c +++ b/iso9660/sector.c @@ -17,6 +17,7 @@ */ #include +#include #include "cdi/fs.h" #include "cdi/cache.h" diff --git a/ne2k/main.c b/ne2k/main.c index 7440ead..3a8a87e 100644 --- a/ne2k/main.c +++ b/ne2k/main.c @@ -27,6 +27,7 @@ */ #include +#include #include "cdi/net.h" #include "cdi/pci.h" diff --git a/ne2k/ne2k.c b/ne2k/ne2k.c index 043592b..a77465b 100644 --- a/ne2k/ne2k.c +++ b/ne2k/ne2k.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "cdi/net.h" #include "cdi/pci.h" diff --git a/pcnet/pcnet.c b/pcnet/pcnet.c index 130b29a..d9e54f9 100644 --- a/pcnet/pcnet.c +++ b/pcnet/pcnet.c @@ -242,7 +242,7 @@ void pcnet_dev_init(struct pcnet_device *netcard, int promiscuous) } #ifdef DEBUG - printf("pcnet: descriptor region at 0x%x virtual and 0x%x physical\n", virt_desc_region, phys_desc_region); + printf("pcnet: descriptor region at 0x%p virtual and 0x%p physical\n", virt_desc_region, phys_desc_region); #endif netcard->receive_descriptor = virt_desc_region; netcard->transmit_descriptor = virt_desc_region + 2 * 1024; @@ -263,7 +263,7 @@ void pcnet_dev_init(struct pcnet_device *netcard, int promiscuous) netcard->initialization_block->physical_address = netcard->net.mac; #ifdef DEBUG - printf("pcnet: initialization block at 0x%x virtual and 0x%x physical\n", + printf("pcnet: initialization block at 0x%p virtual and 0x%p physical\n", netcard->initialization_block, netcard->phys_initialization_block); #endif diff --git a/ramdisk/file.c b/ramdisk/file.c index 4358c1e..2f8676c 100644 --- a/ramdisk/file.c +++ b/ramdisk/file.c @@ -26,6 +26,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include + #include "ramdisk_cdi.h" size_t ramdisk_fs_file_read(struct cdi_fs_stream* stream, uint64_t start, diff --git a/ramdisk/init.c b/ramdisk/init.c index dfe833e..04028c0 100644 --- a/ramdisk/init.c +++ b/ramdisk/init.c @@ -26,9 +26,11 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include +#include + #include "ramdisk_cdi.h" #include "cdi/lists.h" -#include int ramdisk_fs_init(struct cdi_fs_filesystem* cdi_fs) { diff --git a/ramdisk/link.c b/ramdisk/link.c index f3681af..519c0fd 100644 --- a/ramdisk/link.c +++ b/ramdisk/link.c @@ -26,6 +26,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include + #include "ramdisk_cdi.h" const char *ramdisk_fs_link_read(struct cdi_fs_stream *stream) diff --git a/ramdisk/res.c b/ramdisk/res.c index 80874e3..0fab6d4 100644 --- a/ramdisk/res.c +++ b/ramdisk/res.c @@ -26,6 +26,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include + #include "ramdisk_cdi.h" int ramdisk_fs_res_load(struct cdi_fs_stream* stream) diff --git a/rtl8139/main.c b/rtl8139/main.c index b864724..07c1adf 100644 --- a/rtl8139/main.c +++ b/rtl8139/main.c @@ -27,6 +27,7 @@ */ #include +#include #include "cdi/net.h" #include "cdi/pci.h" diff --git a/rtl8139/rtl8139.c b/rtl8139/rtl8139.c index ab38d93..7b43154 100644 --- a/rtl8139/rtl8139.c +++ b/rtl8139/rtl8139.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "cdi/net.h" #include "cdi/pci.h" diff --git a/serial/init.c b/serial/init.c index db7959e..924a4c5 100644 --- a/serial/init.c +++ b/serial/init.c @@ -30,7 +30,9 @@ #include "serial.h" #include "cdi/lists.h" #include "cdi/misc.h" + #include +#include int serial_fs_init(struct cdi_fs_filesystem* cdi_fs) { diff --git a/serial/res.c b/serial/res.c index 82a015a..ae32487 100644 --- a/serial/res.c +++ b/serial/res.c @@ -26,6 +26,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include + #include "serial_cdi.h" #include "serial.h" -- 1.6.0.2 From kevin at tyndur.org Sat Aug 22 00:39:02 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 22 Aug 2009 00:39:02 +0200 Subject: [cdi-devel] [PATCH] usb: struct usb_packet Message-ID: <1250894342-8484-1-git-send-email-kevin@tyndur.org> Als ersten Schritt zu einem netteren Interface nimmt usb_do_packet jetzt statt einer endlosen Kette von Parametern jetzt eine struct, die ein Paket beschreibt. Signed-off-by: Kevin Wolf --- usb/include/usb.h | 27 ++++++++++-- usb/main.c | 85 +++++++++++++++++++++++++-------------- usb/msd.c | 114 +++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 167 insertions(+), 59 deletions(-) diff --git a/usb/include/usb.h b/usb/include/usb.h index 2c435d8..cca698a 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -28,10 +28,11 @@ #include "cdi/misc.h" #include "cdi/pci.h" - -#define PACKET_SETUP 0x2D -#define PACKET_IN 0x69 -#define PACKET_OUT 0xE1 +enum usb_packet_type { + PACKET_SETUP = 0x2D, + PACKET_IN = 0x69, + PACKET_OUT = 0xE1, +}; #define NO_DATA 0x01 #define DEV_TO_HOST 0x80 @@ -270,6 +271,22 @@ struct msclass_data struct endpoint_desc *bulk_ep_out; }; +/** Beschreibt ein USB-Paket */ +struct usb_packet { + /// Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) + enum usb_packet_type type; + /// Der gewuenschte Endpoint (0 bis 15) + int endpoint; + /// Die physische Adresse des zu verwendenden Datenpuffers + uintptr_t phys_data; + /// Die Laenge des Puffers + int length; + /// Gibt an, ob DATA0 (0) oder DATA1 (1) verwendet werden soll + int datatoggle; + /// Typ der Daten (zur Sicherheit, damit es kein doofes STALL gibt) + int type_of_data; +}; + #include "uhci.h" @@ -277,7 +294,7 @@ struct msclass_data #define HCI_STRUCT_SIZE sizeof(struct uhci) -int do_packet(struct usb_device *device, int type, int endpoint, uintptr_t phys_data, int length, int datatoggle, int type_of_data); +int usb_do_packet(struct usb_device *device, struct usb_packet* packet); void enumerate_hci(struct hci *); struct cdi_driver *init_uhcd(void); void init_msc_driver(void); diff --git a/usb/main.c b/usb/main.c index c06f15d..92a7096 100644 --- a/usb/main.c +++ b/usb/main.c @@ -97,44 +97,41 @@ static const int next_data_type[9] = { * Verarbeitet ein USB-Paket. * * @param device Das USB-Ger??t -* @param type Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) -* @param endpoint Der gew??nschte Endpoint (0 bis 15) -* @param phys_data Die physische Adresse des zu verwendenden Datenpuffers -* @param length Die L??nge des Puffers -* @param datatoggle Gibt an, ob DATA0 (0) oder DATA1 (1) verwendet werden soll -* @param type_of_data Typ der Daten (zur Sicherheit, damit es kein doofes STALL gibt) */ -int do_packet(struct usb_device *device, int type, int endpoint, uintptr_t phys_data, int length, int datatoggle, int type_of_data) +int usb_do_packet(struct usb_device *device, struct usb_packet* packet) { int error; int tod_short; - if (!(device->expects & type_of_data)) { - dprintf("Expected 0x%04X, got 0x%04X...?\n", device->expects, type_of_data); + if (!(device->expects & packet->type_of_data)) { + dprintf("Expected 0x%04X, got 0x%04X...?\n", device->expects, + packet->type_of_data); + // FIXME Das ist keine elegante Loesung des Problems for (;;); } - type_of_data >>= 1; - for (tod_short = 0; type_of_data; tod_short++) { - type_of_data >>= 1; + packet->type_of_data >>= 1; + for (tod_short = 0; packet->type_of_data; tod_short++) { + packet->type_of_data >>= 1; } //dprintf("TOD: %s\n", tod_name[tod_short]); device->expects = next_data_type[tod_short]; - endpoint &= 0x07; - type &= 0xFF; + packet->endpoint &= 0x07; + packet->type &= 0xFF; if (device->stalled) //TODO: Irgendwie entstallen, evtl.? oO { dprintf("Zugriff auf stalled-Ger??t verweigert.\n"); return USB_STALLED; } error = device->hci->do_packet(device->hci, - (device->hci->get_frame(device->hci) + 3) & 0x3FF, - type, device->id, endpoint, - device->low_speed, phys_data, length, - datatoggle); + (device->hci->get_frame(device->hci) + 3) & 0x3FF, + packet->type, device->id, packet->endpoint, + device->low_speed, packet->phys_data, packet->length, + packet->datatoggle); if (error == USB_STALLED) { - printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", endpoint, device->id); + printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", + packet->endpoint, device->id); device->stalled = 1; } return error; @@ -164,10 +161,21 @@ static void *do_control(struct usb_device *device, int direction, void *buffer, setup->value = value; setup->index = index; setup->length = length; - if (do_packet(device, PACKET_SETUP, 0, psetup, sizeof(struct setup_packet), - 0, USB_TOD_SETUP) != USB_NO_ERROR) { + + struct usb_packet setup_packet = { + .type = PACKET_SETUP, + .endpoint = 0, + .phys_data = psetup, + .length = sizeof(*setup), + .datatoggle = 0, + .type_of_data = USB_TOD_SETUP, + }; + + + if (usb_do_packet(device, &setup_packet)) { return NULL; } + cdi_sleep_ms(20); if (no_data) { @@ -176,22 +184,39 @@ static void *do_control(struct usb_device *device, int direction, void *buffer, } else { - if (direction == DEV_TO_HOST) { - rval = do_packet(device, PACKET_IN, 0, pbuf, length, 1, - USB_TOD_SETUP_DATA_IN); - } else { - rval = do_packet(device, PACKET_OUT, 0, pbuf, length, 1, - USB_TOD_SETUP_DATA_OUT); - } + struct usb_packet data_packet = { + .type = PACKET_IN, + .endpoint = 0, + .phys_data = pbuf, + .length = length, + .datatoggle = 1, + .type_of_data = (direction == DEV_TO_HOST ? + USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), + }; + + rval = usb_do_packet(device, &data_packet); } + if (rval == USB_NO_ERROR) { + struct usb_packet ack_packet = { + .endpoint = 0, + .phys_data = 0, + .length = 0, + .datatoggle = 1, + }; + cdi_sleep_ms(20); + if (no_data || (direction == HOST_TO_DEV)) { - rval = do_packet(device, PACKET_IN, 0, 0, 0, 1, USB_TOD_SETUP_ACK_IN); + ack_packet.type = PACKET_IN; + ack_packet.type_of_data = USB_TOD_SETUP_ACK_IN; } else { - rval = do_packet(device, PACKET_OUT, 0, 0, 0, 1, USB_TOD_SETUP_ACK_OUT); + ack_packet.type = PACKET_OUT; + ack_packet.type_of_data = USB_TOD_SETUP_ACK_OUT; } + + rval = usb_do_packet(device, &ack_packet); } return (rval == USB_NO_ERROR) ? rbuf : NULL; } diff --git a/usb/msd.c b/usb/msd.c index 11fbd6a..0f9b79e 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -187,7 +187,7 @@ void register_msd(struct usb_device *usbdev) strgdev->block_count = bc; size = bs; size *= bc; - dprintf("%s: %i * %i B (ca. %i MB).\n", strgdev->dev.name, bc, bs, size >> 20); + dprintf("%s: %i * %i B (ca. %lld MB).\n", strgdev->dev.name, bc, bs, size >> 20); } cdi_storage_device_init(strgdev); cdi_list_push(cdi_driver.drv.devices, strgdev); @@ -210,6 +210,36 @@ static void deinit_msd(struct cdi_device *cdi_msd) //TODO: Und gerade hier... } +static int write_cmd(struct usb_device* usbdev, uintptr_t src) +{ + struct msclass_data *msc = (struct msclass_data*) usbdev->classd; + struct usb_packet cmd_packet = { + .type = PACKET_OUT, + .endpoint = msc->bulk_ep_out->endpoint_address, + .phys_data = src, + .length = 0x1F, + .datatoggle = 0, + .type_of_data = USB_TOD_COMMAND, + }; + + return usb_do_packet(usbdev, &cmd_packet); +} + +static int read_status(struct usb_device* usbdev, uintptr_t dest) +{ + struct msclass_data *msc = (struct msclass_data*) usbdev->classd; + struct usb_packet status_packet = { + .type = PACKET_IN, + .endpoint = msc->bulk_ep_in->endpoint_address, + .phys_data = dest, + .length = 0x0D, + .datatoggle = 0, + .type_of_data = USB_TOD_STATUS, + }; + + return usb_do_packet(usbdev, &status_packet); +} + static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count) { struct msclass_data *msc; @@ -244,29 +274,43 @@ static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uin cbw->cbw_cb_length = 12; cbw->cbw_cb[0] = MSC_CMD_CAPACITY; cbw->cbw_cb[1] = 0; //LUN: 0 - if (do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, pcbw, - 0x1F, 0, USB_TOD_COMMAND) != USB_NO_ERROR) { + + if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { return 0; } + cdi_sleep_ms(20); - if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcap, - sizeof(struct msd_capacity), 0, USB_TOD_DATA_IN) - != USB_NO_ERROR) { + + struct usb_packet in_packet = { + .type = PACKET_IN, + .endpoint = msc->bulk_ep_in->endpoint_address, + .phys_data = pcap, + .length = sizeof(*cap), + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_IN, + }; + + if (usb_do_packet(usbdev, &in_packet) != USB_NO_ERROR) { return 0; } + cdi_sleep_ms(5); - if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcsw, - 0x0D, 0, USB_TOD_STATUS) != USB_NO_ERROR) { + + if (read_status(usbdev, pcsw) != USB_NO_ERROR) { return 0; } + cdi_sleep_ms(5); + if ((csw->csw_signature != CSW_SIGNATURE) || (csw->csw_tag != expected_tag) || csw->csw_status) { return 0; } + *block_size = BE2CPU(cap->block_length); *block_count = BE2CPU(cap->last_lba) + 1; + return 1; } @@ -295,21 +339,23 @@ static int msd_ready(struct usb_device *usbdev) cbw->cbw_flags = 0x80; //IN cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" - if (do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, pcbw, - 0x1F, 0, USB_TOD_COMMAND) != USB_NO_ERROR) { + + if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { return 0; } + cdi_sleep_ms(7); - if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcsw, - 0x0D, 0, USB_TOD_STATUS) != USB_NO_ERROR) { + if (read_status(usbdev, pcsw) != USB_NO_ERROR) { return 0; } + cdi_sleep_ms(4); if ((csw->csw_signature != CSW_SIGNATURE) || (csw->csw_tag != expected_tag) || csw->csw_status) { return 0; } + return 1; } #endif @@ -409,23 +455,33 @@ static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t secto cbw->cbw_cb[5] = lba & 0x000000FF; cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; cbw->cbw_cb[8] = sectors & 0x000000FF; - error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, - pcbw, 0x1F, 0, USB_TOD_COMMAND); + + error = write_cmd(usbdev, pcbw); if (error != USB_NO_ERROR) { return error; } cdi_sleep_ms(20); - error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, - phys_buffer, length, 0, USB_TOD_DATA_IN); + + struct usb_packet in_packet = { + .type = PACKET_IN, + .endpoint = msc->bulk_ep_in->endpoint_address, + .phys_data = phys_buffer, + .length = length, + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_IN, + }; + + error = usb_do_packet(usbdev, &in_packet); if (error != USB_NO_ERROR) { return error; } cdi_sleep_ms(5); - error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, - pcsw, 0x0D, 0, USB_TOD_STATUS); + + error = read_status(usbdev, pcsw); if (error != USB_NO_ERROR) { return error; } + cdi_sleep_ms(4); if ((csw->csw_signature != CSW_SIGNATURE) || (csw->csw_tag != expected_tag) || @@ -522,23 +578,33 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect cbw->cbw_cb[5] = lba & 0x000000FF; cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; cbw->cbw_cb[8] = sectors & 0x000000FF; - error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, - pcbw, 0x1F, 0, USB_TOD_COMMAND); + + error = write_cmd(usbdev, pcbw); if (error != USB_NO_ERROR) { return error; } cdi_sleep_ms(20); - error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, - phys_buffer, length, 0, USB_TOD_DATA_OUT); + + struct usb_packet out_packet = { + .type = PACKET_OUT, + .endpoint = msc->bulk_ep_out->endpoint_address, + .phys_data = phys_buffer, + .length = length, + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_OUT, + }; + + error = usb_do_packet(usbdev, &out_packet); if (error != USB_NO_ERROR) { return error; } + cdi_sleep_ms(5); - error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, - pcsw, 0x0D, 0, USB_TOD_STATUS); + error = read_status(usbdev, pcsw); if (error != USB_NO_ERROR) { return error; } + cdi_sleep_ms(4); if ((csw->csw_signature != CSW_SIGNATURE) || (csw->csw_tag != expected_tag) || -- 1.6.0.2 From max at tyndur.org Sat Aug 22 10:32:33 2009 From: max at tyndur.org (Max Reitz) Date: Sat, 22 Aug 2009 10:32:33 +0200 Subject: [cdi-devel] [PATCH] usb: struct usb_packet In-Reply-To: <1250894342-8484-1-git-send-email-kevin@tyndur.org> References: <1250894342-8484-1-git-send-email-kevin@tyndur.org> Message-ID: <4A8FAD21.6050801@tyndur.org> Kevin Wolf schrieb: > Als ersten Schritt zu einem netteren Interface nimmt usb_do_packet jetzt > statt einer endlosen Kette von Parametern jetzt eine struct, die ein > Paket beschreibt. > > Signed-off-by: Kevin Wolf > --- > usb/include/usb.h | 27 ++++++++++-- > usb/main.c | 85 +++++++++++++++++++++++++-------------- > usb/msd.c | 114 +++++++++++++++++++++++++++++++++++++++++----------- > 3 files changed, 167 insertions(+), 59 deletions(-) > > diff --git a/usb/include/usb.h b/usb/include/usb.h > index 2c435d8..cca698a 100644 > --- a/usb/include/usb.h > +++ b/usb/include/usb.h > @@ -28,10 +28,11 @@ > #include "cdi/misc.h" > #include "cdi/pci.h" > > - > -#define PACKET_SETUP 0x2D > -#define PACKET_IN 0x69 > -#define PACKET_OUT 0xE1 > +enum usb_packet_type { > + PACKET_SETUP = 0x2D, > + PACKET_IN = 0x69, > + PACKET_OUT = 0xE1, > +}; > > #define NO_DATA 0x01 > #define DEV_TO_HOST 0x80 > @@ -270,6 +271,22 @@ struct msclass_data > struct endpoint_desc *bulk_ep_out; > }; > > +/** Beschreibt ein USB-Paket */ > +struct usb_packet { > + /// Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) > + enum usb_packet_type type; > + /// Der gewuenschte Endpoint (0 bis 15) > + int endpoint; > + /// Die physische Adresse des zu verwendenden Datenpuffers > + uintptr_t phys_data; > + /// Die Laenge des Puffers > + int length; > + /// Gibt an, ob DATA0 (0) oder DATA1 (1) verwendet werden soll > + int datatoggle; > + /// Typ der Daten (zur Sicherheit, damit es kein doofes STALL gibt) > + int type_of_data; > +}; > + > > #include "uhci.h" > > @@ -277,7 +294,7 @@ struct msclass_data > #define HCI_STRUCT_SIZE sizeof(struct uhci) > > > -int do_packet(struct usb_device *device, int type, int endpoint, uintptr_t phys_data, int length, int datatoggle, int type_of_data); > +int usb_do_packet(struct usb_device *device, struct usb_packet* packet); > void enumerate_hci(struct hci *); > struct cdi_driver *init_uhcd(void); > void init_msc_driver(void); > diff --git a/usb/main.c b/usb/main.c > index c06f15d..92a7096 100644 > --- a/usb/main.c > +++ b/usb/main.c > @@ -97,44 +97,41 @@ static const int next_data_type[9] = { > * Verarbeitet ein USB-Paket. > * > * @param device Das USB-Ger?t > -* @param type Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) > -* @param endpoint Der gew?nschte Endpoint (0 bis 15) > -* @param phys_data Die physische Adresse des zu verwendenden Datenpuffers > -* @param length Die L?nge des Puffers > -* @param datatoggle Gibt an, ob DATA0 (0) oder DATA1 (1) verwendet werden soll > -* @param type_of_data Typ der Daten (zur Sicherheit, damit es kein doofes STALL gibt) > */ > > -int do_packet(struct usb_device *device, int type, int endpoint, uintptr_t phys_data, int length, int datatoggle, int type_of_data) > +int usb_do_packet(struct usb_device *device, struct usb_packet* packet) > { > int error; > int tod_short; > > - if (!(device->expects & type_of_data)) { > - dprintf("Expected 0x%04X, got 0x%04X...?\n", device->expects, type_of_data); > + if (!(device->expects & packet->type_of_data)) { > + dprintf("Expected 0x%04X, got 0x%04X...?\n", device->expects, > + packet->type_of_data); > + // FIXME Das ist keine elegante Loesung des Problems > for (;;); > } > - type_of_data >>= 1; > - for (tod_short = 0; type_of_data; tod_short++) { > - type_of_data >>= 1; > + packet->type_of_data >>= 1; > + for (tod_short = 0; packet->type_of_data; tod_short++) { > + packet->type_of_data >>= 1; > } > //dprintf("TOD: %s\n", tod_name[tod_short]); > device->expects = next_data_type[tod_short]; > - endpoint &= 0x07; > - type &= 0xFF; > + packet->endpoint &= 0x07; > + packet->type &= 0xFF; > if (device->stalled) //TODO: Irgendwie entstallen, evtl.? oO > { > dprintf("Zugriff auf stalled-Ger?t verweigert.\n"); > return USB_STALLED; > } > error = device->hci->do_packet(device->hci, > - (device->hci->get_frame(device->hci) + 3) & 0x3FF, > - type, device->id, endpoint, > - device->low_speed, phys_data, length, > - datatoggle); > + (device->hci->get_frame(device->hci) + 3) & 0x3FF, > + packet->type, device->id, packet->endpoint, > + device->low_speed, packet->phys_data, packet->length, > + packet->datatoggle); > if (error == USB_STALLED) > { > - printf("[usb] ENDPOINT %i DES GER?TS %i STALLED!\n", endpoint, device->id); > + printf("[usb] ENDPOINT %i DES GER?TS %i STALLED!\n", > + packet->endpoint, device->id); > device->stalled = 1; > } > return error; > @@ -164,10 +161,21 @@ static void *do_control(struct usb_device *device, int direction, void *buffer, > setup->value = value; > setup->index = index; > setup->length = length; > - if (do_packet(device, PACKET_SETUP, 0, psetup, sizeof(struct setup_packet), > - 0, USB_TOD_SETUP) != USB_NO_ERROR) { > + > + struct usb_packet setup_packet = { > + .type = PACKET_SETUP, > + .endpoint = 0, > + .phys_data = psetup, > + .length = sizeof(*setup), > + .datatoggle = 0, > + .type_of_data = USB_TOD_SETUP, > + }; > + > + > + if (usb_do_packet(device, &setup_packet)) { > return NULL; > } > + > cdi_sleep_ms(20); > if (no_data) > { > @@ -176,22 +184,39 @@ static void *do_control(struct usb_device *device, int direction, void *buffer, > } > else > { > - if (direction == DEV_TO_HOST) { > - rval = do_packet(device, PACKET_IN, 0, pbuf, length, 1, > - USB_TOD_SETUP_DATA_IN); > - } else { > - rval = do_packet(device, PACKET_OUT, 0, pbuf, length, 1, > - USB_TOD_SETUP_DATA_OUT); > - } > + struct usb_packet data_packet = { > + .type = PACKET_IN, > Das Feld "type" ist ebenso wie das Feld "type_of_data" von der Variable "direction" abh?ngig. Hier geh?rt also Folgendes hin: .type = (direction == DEV_TO_HOST ? PACKET_IN : PACKET_OUT), > + .endpoint = 0, > + .phys_data = pbuf, > + .length = length, > + .datatoggle = 1, > + .type_of_data = (direction == DEV_TO_HOST ? > + USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), > + }; > + > + rval = usb_do_packet(device, &data_packet); > } > + > if (rval == USB_NO_ERROR) > { > + struct usb_packet ack_packet = { > + .endpoint = 0, > + .phys_data = 0, > + .length = 0, > + .datatoggle = 1, > + }; > + > cdi_sleep_ms(20); > + > if (no_data || (direction == HOST_TO_DEV)) { > - rval = do_packet(device, PACKET_IN, 0, 0, 0, 1, USB_TOD_SETUP_ACK_IN); > + ack_packet.type = PACKET_IN; > + ack_packet.type_of_data = USB_TOD_SETUP_ACK_IN; > } else { > - rval = do_packet(device, PACKET_OUT, 0, 0, 0, 1, USB_TOD_SETUP_ACK_OUT); > + ack_packet.type = PACKET_OUT; > + ack_packet.type_of_data = USB_TOD_SETUP_ACK_OUT; > } > + > + rval = usb_do_packet(device, &ack_packet); > } > return (rval == USB_NO_ERROR) ? rbuf : NULL; > } > diff --git a/usb/msd.c b/usb/msd.c > index 11fbd6a..0f9b79e 100644 > --- a/usb/msd.c > +++ b/usb/msd.c > @@ -187,7 +187,7 @@ void register_msd(struct usb_device *usbdev) > strgdev->block_count = bc; > size = bs; > size *= bc; > - dprintf("%s: %i * %i B (ca. %i MB).\n", strgdev->dev.name, bc, bs, size >> 20); > + dprintf("%s: %i * %i B (ca. %lld MB).\n", strgdev->dev.name, bc, bs, size >> 20); > } > cdi_storage_device_init(strgdev); > cdi_list_push(cdi_driver.drv.devices, strgdev); > @@ -210,6 +210,36 @@ static void deinit_msd(struct cdi_device *cdi_msd) > //TODO: Und gerade hier... > } > > +static int write_cmd(struct usb_device* usbdev, uintptr_t src) > +{ > + struct msclass_data *msc = (struct msclass_data*) usbdev->classd; > + struct usb_packet cmd_packet = { > + .type = PACKET_OUT, > + .endpoint = msc->bulk_ep_out->endpoint_address, > + .phys_data = src, > + .length = 0x1F, > + .datatoggle = 0, > + .type_of_data = USB_TOD_COMMAND, > + }; > + > + return usb_do_packet(usbdev, &cmd_packet); > +} > + > +static int read_status(struct usb_device* usbdev, uintptr_t dest) > +{ > + struct msclass_data *msc = (struct msclass_data*) usbdev->classd; > + struct usb_packet status_packet = { > + .type = PACKET_IN, > + .endpoint = msc->bulk_ep_in->endpoint_address, > + .phys_data = dest, > + .length = 0x0D, > + .datatoggle = 0, > + .type_of_data = USB_TOD_STATUS, > + }; > + > + return usb_do_packet(usbdev, &status_packet); > +} > + > static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count) > { > struct msclass_data *msc; > @@ -244,29 +274,43 @@ static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uin > cbw->cbw_cb_length = 12; > cbw->cbw_cb[0] = MSC_CMD_CAPACITY; > cbw->cbw_cb[1] = 0; //LUN: 0 > - if (do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, pcbw, > - 0x1F, 0, USB_TOD_COMMAND) != USB_NO_ERROR) { > + > + if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { > return 0; > } > + > cdi_sleep_ms(20); > - if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcap, > - sizeof(struct msd_capacity), 0, USB_TOD_DATA_IN) > - != USB_NO_ERROR) { > + > + struct usb_packet in_packet = { > + .type = PACKET_IN, > + .endpoint = msc->bulk_ep_in->endpoint_address, > + .phys_data = pcap, > + .length = sizeof(*cap), > + .datatoggle = 0, > + .type_of_data = USB_TOD_DATA_IN, > + }; > + > + if (usb_do_packet(usbdev, &in_packet) != USB_NO_ERROR) { > return 0; > } > + > cdi_sleep_ms(5); > - if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcsw, > - 0x0D, 0, USB_TOD_STATUS) != USB_NO_ERROR) { > + > + if (read_status(usbdev, pcsw) != USB_NO_ERROR) { > return 0; > } > + > cdi_sleep_ms(5); > + > if ((csw->csw_signature != CSW_SIGNATURE) || > (csw->csw_tag != expected_tag) || > csw->csw_status) { > return 0; > } > + > *block_size = BE2CPU(cap->block_length); > *block_count = BE2CPU(cap->last_lba) + 1; > + > return 1; > } > > @@ -295,21 +339,23 @@ static int msd_ready(struct usb_device *usbdev) > cbw->cbw_flags = 0x80; //IN > cbw->cbw_lun = 0; //Was wei? ich, wie viele LUNs datt Dingens hat? > cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" > - if (do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, pcbw, > - 0x1F, 0, USB_TOD_COMMAND) != USB_NO_ERROR) { > + > + if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { > return 0; > } > + > cdi_sleep_ms(7); > - if (do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, pcsw, > - 0x0D, 0, USB_TOD_STATUS) != USB_NO_ERROR) { > + if (read_status(usbdev, pcsw) != USB_NO_ERROR) { > return 0; > } > + > cdi_sleep_ms(4); > if ((csw->csw_signature != CSW_SIGNATURE) || > (csw->csw_tag != expected_tag) || > csw->csw_status) { > return 0; > } > + > return 1; > } > #endif > @@ -409,23 +455,33 @@ static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t secto > cbw->cbw_cb[5] = lba & 0x000000FF; > cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; > cbw->cbw_cb[8] = sectors & 0x000000FF; > - error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, > - pcbw, 0x1F, 0, USB_TOD_COMMAND); > + > + error = write_cmd(usbdev, pcbw); > if (error != USB_NO_ERROR) { > return error; > } > cdi_sleep_ms(20); > - error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, > - phys_buffer, length, 0, USB_TOD_DATA_IN); > + > + struct usb_packet in_packet = { > + .type = PACKET_IN, > + .endpoint = msc->bulk_ep_in->endpoint_address, > + .phys_data = phys_buffer, > + .length = length, > + .datatoggle = 0, > + .type_of_data = USB_TOD_DATA_IN, > + }; > + > + error = usb_do_packet(usbdev, &in_packet); > if (error != USB_NO_ERROR) { > return error; > } > cdi_sleep_ms(5); > - error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, > - pcsw, 0x0D, 0, USB_TOD_STATUS); > + > + error = read_status(usbdev, pcsw); > if (error != USB_NO_ERROR) { > return error; > } > + > cdi_sleep_ms(4); > if ((csw->csw_signature != CSW_SIGNATURE) || > (csw->csw_tag != expected_tag) || > @@ -522,23 +578,33 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect > cbw->cbw_cb[5] = lba & 0x000000FF; > cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; > cbw->cbw_cb[8] = sectors & 0x000000FF; > - error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, > - pcbw, 0x1F, 0, USB_TOD_COMMAND); > + > + error = write_cmd(usbdev, pcbw); > if (error != USB_NO_ERROR) { > return error; > } > cdi_sleep_ms(20); > - error = do_packet(usbdev, PACKET_OUT, msc->bulk_ep_out->endpoint_address, > - phys_buffer, length, 0, USB_TOD_DATA_OUT); > + > + struct usb_packet out_packet = { > + .type = PACKET_OUT, > + .endpoint = msc->bulk_ep_out->endpoint_address, > + .phys_data = phys_buffer, > + .length = length, > + .datatoggle = 0, > + .type_of_data = USB_TOD_DATA_OUT, > + }; > + > + error = usb_do_packet(usbdev, &out_packet); > if (error != USB_NO_ERROR) { > return error; > } > + > cdi_sleep_ms(5); > - error = do_packet(usbdev, PACKET_IN, msc->bulk_ep_in->endpoint_address, > - pcsw, 0x0D, 0, USB_TOD_STATUS); > + error = read_status(usbdev, pcsw); > if (error != USB_NO_ERROR) { > return error; > } > + > cdi_sleep_ms(4); > if ((csw->csw_signature != CSW_SIGNATURE) || > (csw->csw_tag != expected_tag) || > > ------------------------------------------------------------------------ > > _______________________________________________ > cdi-devel mailing list > cdi-devel at tyndur.org > http://list.tyndur.org/cgi-bin/mailman/listinfo/cdi-devel > From toni at tyndur.org Sat Aug 22 10:50:26 2009 From: toni at tyndur.org (Antoine Kaufmann) Date: Sat, 22 Aug 2009 10:50:26 +0200 Subject: [cdi-devel] [PATCH 1/2] cdi: Add Makefile In-Reply-To: <1250883055-24218-2-git-send-email-kevin@tyndur.org> References: <1250883055-24218-1-git-send-email-kevin@tyndur.org> <1250883055-24218-2-git-send-email-kevin@tyndur.org> Message-ID: <20090822085026.GA5363@einstein.kaufmann.lan> On Fri, Aug 21 21:30, Kevin Wolf wrote: > Add a Makefile to the CDI repository. Drivers are compiled using the > system compiler, but they are not linked. This allows at least some > basic compile testing without moving patches into the source tree of an > OS. > > Signed-off-by: Kevin Wolf Acked-by: Antoine Kaufmann -- Antoine Kaufmann -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: not available URL: From toni at tyndur.org Sat Aug 22 11:23:32 2009 From: toni at tyndur.org (Antoine Kaufmann) Date: Sat, 22 Aug 2009 11:23:32 +0200 Subject: [cdi-devel] [PATCH 2/2] cdi: Fix compilation on Linux In-Reply-To: <1250883055-24218-3-git-send-email-kevin@tyndur.org> References: <1250883055-24218-1-git-send-email-kevin@tyndur.org> <1250883055-24218-3-git-send-email-kevin@tyndur.org> Message-ID: <20090822092332.GB5363@einstein.kaufmann.lan> On Fri, Aug 21 21:30, Kevin Wolf wrote: > Most CDI drivers assume a strange libc like the tyndur one. This patch > cleans them up and allows to compile the drivers on Linux. > > Signed-off-by: Kevin Wolf Doesn't look like it would do any harm, but I haven't tested if it compiles on linux... ;-) Acked-by: Antoine Kaufmann -- Antoine Kaufmann -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: not available URL: From max at tyndur.org Sat Aug 22 14:34:21 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 22 Aug 2009 14:34:21 +0200 Subject: [cdi-devel] [PATCH] "Better" indent, wait for ACK instead of sleep Message-ID: <1250944461-4553-1-git-send-email-max@tyndur.org> From: Max Reitz * indent done with the program "indent" + While a USB device is not ready, it sends NAK in response to IN/OUT - it is a lot better to wait for an ACK instead of sleeping randomly. Signed-off-by: Max Reitz --- usb/include/msd.h | 50 +++---- usb/include/uhci.h | 80 ++++++------ usb/include/usb.h | 67 ++++------ usb/main.c | 192 ++++++++++++-------------- usb/msd.c | 377 +++++++++++++++++++++++----------------------------- usb/uhci.c | 117 +++++++---------- 6 files changed, 392 insertions(+), 491 deletions(-) diff --git a/usb/include/msd.h b/usb/include/msd.h index 99c796d..cc3bb4d 100644 --- a/usb/include/msd.h +++ b/usb/include/msd.h @@ -32,8 +32,7 @@ //"USBC" #define CBW_SIGNATURE 0x43425355 -struct command_block_wrapper -{ +struct command_block_wrapper { uint32_t cbw_signature; uint32_t cbw_tag; uint32_t cbw_data_transfer_length; @@ -41,55 +40,50 @@ struct command_block_wrapper uint8_t cbw_lun; uint8_t cbw_cb_length; uint8_t cbw_cb[16]; -} __attribute__((packed)); +} __attribute__ ((packed)); //"USBS" #define CSW_SIGNATURE 0x53425355 -struct command_status_wrapper -{ +struct command_status_wrapper { uint32_t csw_signature; uint32_t csw_tag; uint32_t csw_data_residue; uint8_t csw_status; -} __attribute__((packed)); +} __attribute__ ((packed)); -struct msc_sense -{ - unsigned error : 7; - unsigned valid : 1; +struct msc_sense { + unsigned error:7; + unsigned valid:1; uint8_t rsvd0; - unsigned sense_key : 4; - unsigned rsvd1 : 4; + unsigned sense_key:4; + unsigned rsvd1:4; uint32_t information; uint8_t additional_length; uint32_t rsvd2; uint8_t additional_code; uint8_t additional_code_qualifier; uint32_t rsvd; -} __attribute__((packed)); +} __attribute__ ((packed)); -struct msd_capacity -{ +struct msd_capacity { uint32_t last_lba; uint32_t block_length; -} __attribute__((packed)); +} __attribute__ ((packed)); -struct cdi_msd -{ +struct cdi_msd { struct cdi_storage_device cdi_device; struct usb_device *usb_device; - uint32_t offset; //F??r Partitionen + uint32_t offset; //F??r Partitionen }; -struct part_table_entry -{ - uint8_t active; - uint8_t begin_chs[3]; - uint8_t type; - uint8_t end_chs[3]; - uint32_t start; - uint32_t size; -} __attribute__((packed)); +struct part_table_entry { + uint8_t active; + uint8_t begin_chs[3]; + uint8_t type; + uint8_t end_chs[3]; + uint32_t start; + uint32_t size; +} __attribute__ ((packed)); #define MSC_CMD_REZERO 0x01 diff --git a/usb/include/uhci.h b/usb/include/uhci.h index 0158bd2..bf22c4a 100644 --- a/usb/include/uhci.h +++ b/usb/include/uhci.h @@ -31,15 +31,15 @@ #include "usb.h" -#define UHCI_USBCMD 0x00 //w -#define UHCI_USBSTS 0x02 //w -#define UHCI_USBINTR 0x04 //w -#define UHCI_FRNUM 0x06 //w -#define UHCI_FRBASEADD 0x08 //l -#define UHCI_SOFMOD 0x0C //b +#define UHCI_USBCMD 0x00 //w +#define UHCI_USBSTS 0x02 //w +#define UHCI_USBINTR 0x04 //w +#define UHCI_FRNUM 0x06 //w +#define UHCI_FRBASEADD 0x08 //l +#define UHCI_SOFMOD 0x0C //b #define UHCI_RPORTS 0x10 -#define UHCI_PORTSC1 0x10 //w -#define UHCI_PORTSC2 0x12 //w +#define UHCI_PORTSC1 0x10 //w +#define UHCI_PORTSC2 0x12 //w #define MAXP 0x0080 #define GRESET 0x0004 @@ -47,14 +47,13 @@ #define USB_RUN 0x0001 #define RPORT_RESET 0x0200 -#define RPORT_LOSPD 0x0100 //Low speed device attached +#define RPORT_LOSPD 0x0100 //Low speed device attached #define RPORT_ENABLE 0x0004 -#define RPORT_CSC 0x0002 //Connect status change +#define RPORT_CSC 0x0002 //Connect status change #define RPORT_DEVICE 0x0001 -struct uhci -{ +struct uhci { struct hci gen_hci; uint16_t pbase; uintptr_t phys_frame_list; @@ -62,46 +61,43 @@ struct uhci int root_ports; }; -struct uhci_qh -{ +struct uhci_qh { volatile uint32_t next; volatile uint32_t transfer; -} __attribute__((packed)); +} __attribute__ ((packed)); -struct uhci_td -{ +struct uhci_td { volatile uint32_t next; - unsigned trans_len : 11; - unsigned rsvd0 : 6; - unsigned bitstuff_err : 1; - unsigned crc_time_err : 1; - unsigned nak : 1; - unsigned babble : 1; - unsigned buf_err : 1; - unsigned stalled_err : 1; - unsigned active : 1; - unsigned ioc : 1; - unsigned isochronous : 1; - unsigned low_speed : 1; - unsigned errors : 2; - unsigned spd : 1; - unsigned rsvd1 : 2; - - unsigned pid : 8; - unsigned device : 7; - unsigned endpoint : 4; - unsigned data_toggle : 1; - unsigned rsvd2 : 1; - unsigned maxlen : 11; + unsigned trans_len:11; + unsigned rsvd0:6; + unsigned bitstuff_err:1; + unsigned crc_time_err:1; + unsigned nak:1; + unsigned babble:1; + unsigned buf_err:1; + unsigned stalled_err:1; + unsigned active:1; + unsigned ioc:1; + unsigned isochronous:1; + unsigned low_speed:1; + unsigned errors:2; + unsigned spd:1; + unsigned rsvd1:2; + + unsigned pid:8; + unsigned device:7; + unsigned endpoint:4; + unsigned data_toggle:1; + unsigned rsvd2:1; + unsigned maxlen:11; uint32_t buffer; uint32_t user[4]; -} __attribute__((packed)); +} __attribute__ ((packed)); -struct transfer -{ +struct transfer { void *virt; uintptr_t phys; volatile int error; diff --git a/usb/include/usb.h b/usb/include/usb.h index cca698a..9ec8b37 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -30,8 +30,8 @@ enum usb_packet_type { PACKET_SETUP = 0x2D, - PACKET_IN = 0x69, - PACKET_OUT = 0xE1, + PACKET_IN = 0x69, + PACKET_OUT = 0xE1 }; #define NO_DATA 0x01 @@ -124,42 +124,39 @@ enum usb_packet_type { #define USB_TODS_STATUS 8 -typedef enum -{ +typedef enum { HCI_UHCI, HCI_OHCI, HCI_EHCI } hci_type_t; -struct cdi_hci -{ +struct cdi_hci { struct cdi_device cdi_device; struct hci *hci; }; struct usb_device; -struct hci -{ +struct hci { struct cdi_pci_device *pcidev; hci_type_t type; - cdi_list_t (*find_devices)(struct hci *); - void (*activate_device)(struct hci *, struct usb_device *); - int (*do_packet)(struct hci *, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle); - int (*get_frame)(struct hci *); + cdi_list_t (*find_devices) (struct hci *); + void (*activate_device) (struct hci *, struct usb_device *); + int (*do_packet) (struct hci *, int frame, int type, int device, + int endpoint, int low_speed, uintptr_t phys_data, + int length, int datatoggle); + int (*get_frame) (struct hci *); }; -struct setup_packet -{ +struct setup_packet { uint8_t request_type; uint8_t request; uint16_t value; uint16_t index; uint16_t length; -} __attribute__((packed)); +} __attribute__ ((packed)); -struct device_desc -{ +struct device_desc { uint8_t length; uint8_t descriptor_type; uint16_t bcdUSB; @@ -174,10 +171,9 @@ struct device_desc uint8_t iProduct; uint8_t iSerialNumber; uint8_t num_configurations; -} __attribute__((packed)); +} __attribute__ ((packed)); -struct config_desc -{ +struct config_desc { uint8_t length; uint8_t descriptor_type; uint16_t total_length; @@ -186,10 +182,9 @@ struct config_desc uint8_t iConfiguration; uint8_t attributes; uint8_t max_power; -} __attribute__((packed)); +} __attribute__ ((packed)); -struct interface_desc -{ +struct interface_desc { uint8_t length; uint8_t descriptor_type; uint8_t interface_number; @@ -199,30 +194,27 @@ struct interface_desc uint8_t interface_subclass; uint8_t interface_protocol; uint8_t iInterface; -} __attribute__((packed)); +} __attribute__ ((packed)); -struct endpoint_desc -{ +struct endpoint_desc { uint8_t length; uint8_t descriptor_type; uint8_t endpoint_address; uint8_t attributes; uint16_t max_packet_size; uint8_t interval; -} __attribute__((packed)); +} __attribute__ ((packed)); -struct hub_desc -{ +struct hub_desc { uint8_t length; uint8_t descriptor_type; uint8_t nbr_ports; uint16_t hub_characteristics; uint8_t pwron2pwrgood; uint8_t hub_contr_current; -} __attribute__((packed)); +} __attribute__ ((packed)); -typedef enum -{ +typedef enum { USBDC_COMPOSITE = 0x00, USBDC_AUDIO = 0x01, USBDC_COMM_CDC = 0x02, @@ -244,13 +236,11 @@ typedef enum USBDC_VENDOR_SPEC = 0xFF } usb_class_t; -struct class_data -{ +struct class_data { usb_class_t type; }; -struct usb_device -{ +struct usb_device { struct hci *hci; int id; int port; @@ -264,8 +254,7 @@ struct usb_device int expects; }; -struct msclass_data -{ +struct msclass_data { struct class_data gen_class; struct endpoint_desc *bulk_ep_in; struct endpoint_desc *bulk_ep_out; @@ -294,7 +283,7 @@ struct usb_packet { #define HCI_STRUCT_SIZE sizeof(struct uhci) -int usb_do_packet(struct usb_device *device, struct usb_packet* packet); +int usb_do_packet(struct usb_device *device, struct usb_packet *packet); void enumerate_hci(struct hci *); struct cdi_driver *init_uhcd(void); void init_msc_driver(void); diff --git a/usb/main.c b/usb/main.c index 9200613..51ef95b 100644 --- a/usb/main.c +++ b/usb/main.c @@ -41,12 +41,13 @@ static int dprintf(const char *fmt, ...) { return 0; } + #define _dprintf(fmt, args...) dprintf(fmt, ##args) #endif static struct usb_device *devices[128]; static cdi_list_t ehci, ohci, uhci; -static void (*ccdriver_take_device[0x100])(struct usb_device *); +static void (*ccdriver_take_device[0x100]) (struct usb_device *); static int usb_dev_ids = 1; static void usb_init(void); @@ -94,18 +95,18 @@ static const int next_data_type[9] = { };*/ /** -* Verarbeitet ein USB-Paket. -* -* @param device Das USB-Ger??t -*/ + * Verarbeitet ein USB-Paket. + * + * @param device Das USB-Ger??t + */ -int usb_do_packet(struct usb_device *device, struct usb_packet* packet) +int usb_do_packet(struct usb_device *device, struct usb_packet *packet) { - int error; + int error = USB_NAK; int tod_short; if (!(device->expects & packet->type_of_data)) { - dprintf("Expected 0x%04X, got 0x%04X...?\n", device->expects, + dprintf("0x%04X erwartet, 0x%04X bekommen...?\n", device->expects, packet->type_of_data); // FIXME Das ist keine elegante Loesung des Problems for (;;); @@ -118,18 +119,18 @@ int usb_do_packet(struct usb_device *device, struct usb_packet* packet) device->expects = next_data_type[tod_short]; packet->endpoint &= 0x07; packet->type &= 0xFF; - if (device->stalled) //TODO: Irgendwie entstallen, evtl.? oO - { + if (device->stalled) { + //TODO: Irgendwie entstallen, evtl.? oO dprintf("Zugriff auf stalled-Ger??t verweigert.\n"); return USB_STALLED; } - error = device->hci->do_packet(device->hci, - (device->hci->get_frame(device->hci) + 3) & 0x3FF, - packet->type, device->id, packet->endpoint, - device->low_speed, packet->phys_data, packet->length, - packet->datatoggle); - if (error == USB_STALLED) - { + while (error == USB_NAK) { + error = device->hci->do_packet(device->hci, + (device->hci->get_frame(device->hci) + 3) & 0x3FF, + packet->type, device->id, packet->endpoint, device->low_speed, + packet->phys_data, packet->length, packet->datatoggle); + } + if (error == USB_STALLED) { printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", packet->endpoint, device->id); device->stalled = 1; @@ -137,7 +138,8 @@ int usb_do_packet(struct usb_device *device, struct usb_packet* packet) return error; } -static void *do_control(struct usb_device *device, int direction, void *buffer, int length, int rtype, int recipient, int request, int value, int index) +static void *do_control(struct usb_device *device, int direction, void *buffer, + int length, int rtype, int recipient, int request, int value, int index) { void *rbuf; int rval, no_data; @@ -149,7 +151,7 @@ static void *do_control(struct usb_device *device, int direction, void *buffer, return NULL; } if (cdi_alloc_phys_mem(sizeof(struct setup_packet), - (void **)&setup, (void **)&psetup) == -1) { + (void **)&setup, (void **)&psetup) == -1) { return NULL; } direction &= 0x80; @@ -163,12 +165,12 @@ static void *do_control(struct usb_device *device, int direction, void *buffer, setup->length = length; struct usb_packet setup_packet = { - .type = PACKET_SETUP, - .endpoint = 0, - .phys_data = psetup, - .length = sizeof(*setup), - .datatoggle = 0, - .type_of_data = USB_TOD_SETUP, + .type = PACKET_SETUP, + .endpoint = 0, + .phys_data = psetup, + .length = sizeof(*setup), + .datatoggle = 0, + .type_of_data = USB_TOD_SETUP, }; @@ -177,43 +179,38 @@ static void *do_control(struct usb_device *device, int direction, void *buffer, } cdi_sleep_ms(20); - if (no_data) - { + if (no_data) { rval = USB_NO_ERROR; - rbuf = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten - } - else - { + rbuf = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten + } else { struct usb_packet data_packet = { - .type = (direction == DEV_TO_HOST ? - PACKET_IN : PACKET_OUT), - .endpoint = 0, - .phys_data = pbuf, - .length = length, - .datatoggle = 1, - .type_of_data = (direction == DEV_TO_HOST ? - USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), + .type = (direction == DEV_TO_HOST ? PACKET_IN : PACKET_OUT), + .endpoint = 0, + .phys_data = pbuf, + .length = length, + .datatoggle = 1, + .type_of_data = (direction == DEV_TO_HOST ? + USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), }; rval = usb_do_packet(device, &data_packet); } - if (rval == USB_NO_ERROR) - { + if (rval == USB_NO_ERROR) { struct usb_packet ack_packet = { - .endpoint = 0, - .phys_data = 0, - .length = 0, - .datatoggle = 1, + .endpoint = 0, + .phys_data = 0, + .length = 0, + .datatoggle = 1, }; cdi_sleep_ms(20); if (no_data || (direction == HOST_TO_DEV)) { - ack_packet.type = PACKET_IN; + ack_packet.type = PACKET_IN; ack_packet.type_of_data = USB_TOD_SETUP_ACK_IN; } else { - ack_packet.type = PACKET_OUT; + ack_packet.type = PACKET_OUT; ack_packet.type_of_data = USB_TOD_SETUP_ACK_OUT; } @@ -233,7 +230,8 @@ static void usb_init(void) init_msc_driver(); - memset(ccdriver_take_device, 0, sizeof(void (*)(struct usb_device *)) * 0x100); + memset(ccdriver_take_device, 0, + sizeof(void (*)(struct usb_device *)) * 0x100); ccdriver_take_device[8] = ®ister_msd; ccdriver_take_device[9] = &enumerate_hub; @@ -243,13 +241,11 @@ static void usb_init(void) cdi_list_t pci_devices = cdi_list_create(); cdi_pci_get_all_devices(pci_devices); - for (i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) - { + for (i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) { if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03)) { cdi_pci_device_destroy(dev); } else { - switch (dev->interface_id) - { + switch (dev->interface_id) { case 0x00: hci = malloc(HCI_STRUCT_SIZE); hci->pcidev = dev; @@ -273,14 +269,12 @@ static void usb_init(void) cdi_list_destroy(pci_devices); - dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n", cdi_list_size(ehci), - cdi_list_size(ohci), - cdi_list_size(uhci)); + dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n", + cdi_list_size(ehci), cdi_list_size(ohci), cdi_list_size(uhci)); uhcd = init_uhcd(); - for (i = 0; (hci = cdi_list_pop(uhci)) != NULL; i++) - { + for (i = 0; (hci = cdi_list_pop(uhci)) != NULL; i++) { cdi_hci = malloc(sizeof(struct cdi_hci)); cdi_hci->cdi_device.type = CDI_UNKNOWN; dev_name = malloc(10); @@ -313,23 +307,21 @@ static void enum_device(struct usb_device *usbdev) usbdev->locked = 0; usbdev->expects = USB_TOD_SETUP | USB_TOD_COMMAND; dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), - STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, - DESC_DEVICE << 8, 0); + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); if (dev_desc == NULL) { return; } usbdev->device = dev_desc; dprintf("0x%04X:0x%04X (%i) -> ", dev_desc->vendor_id, dev_desc->device_id, - dev_desc->class_id); + dev_desc->class_id); do_control(usbdev, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, REC_DEVICE, - SET_ADDRESS, (id = usb_dev_ids++), 0); + SET_ADDRESS, (id = usb_dev_ids++), 0); usbdev->id = id; _dprintf("%i\n", id); - if (dev_desc->iManufacturer) - { + if (dev_desc->iManufacturer) { name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, - REC_DEVICE, GET_DESCRIPTOR, - (DESC_STRING << 8) | dev_desc->iManufacturer, 0); + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iManufacturer, 0); if (name == NULL) { return; } @@ -339,11 +331,10 @@ static void enum_device(struct usb_device *usbdev) } _dprintf("\n"); } - if (dev_desc->iProduct) - { + if (dev_desc->iProduct) { name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, - REC_DEVICE, GET_DESCRIPTOR, - (DESC_STRING << 8) | dev_desc->iProduct, 0); + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iProduct, 0); if (name == NULL) { return; } @@ -354,27 +345,25 @@ static void enum_device(struct usb_device *usbdev) _dprintf("\n"); } devices[id] = usbdev; - //TODO: Man kann doch nicht immer den ersten nehmen... + //TODO: Man kann doch nicht immer den ersten nehmen... conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, - sizeof(struct config_desc), STD_REQUEST, REC_DEVICE, - GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); + sizeof(struct config_desc), STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); if (conf_desc == NULL) { return; } conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, conf_desc->total_length, - STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, - DESC_CONFIGURATION << 8, 0); + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); if (conf_desc == NULL) { return; } usbdev->config = conf_desc; do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, - SET_CONFIGURATION, conf_desc->config_value, 0); - if (conf_desc->iConfiguration) - { + SET_CONFIGURATION, conf_desc->config_value, 0); + if (conf_desc->iConfiguration) { name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, - REC_DEVICE, GET_DESCRIPTOR, - (DESC_STRING << 8) | conf_desc->iConfiguration, 0); + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | conf_desc->iConfiguration, 0); if (name == NULL) { return; } @@ -386,25 +375,25 @@ static void enum_device(struct usb_device *usbdev) } position = conf_desc; position += sizeof(struct config_desc); - best_if = position; //Standard-IF - for (i = 0; i < conf_desc->num_interfaces; i++) - { + best_if = position; //Standard-IF + for (i = 0; i < conf_desc->num_interfaces; i++) { if_desc = position; - if (if_desc->interface_class == 8) //TODO: Mehr Klassencodes nat??rlich! - { + //TODO: Mehr Klassencodes nat??rlich! + if (if_desc->interface_class == 8) { best_if = if_desc; break; } - position += sizeof(struct interface_desc) + if_desc->num_endpoints * sizeof(struct endpoint_desc); + position += + sizeof(struct interface_desc) + + if_desc->num_endpoints * sizeof(struct endpoint_desc); } do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, - SET_INTERFACE, best_if->interface_number, 0); + SET_INTERFACE, best_if->interface_number, 0); usbdev->interface = best_if; - if (best_if->iInterface) - { + if (best_if->iInterface) { name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, - REC_DEVICE, GET_DESCRIPTOR, - (DESC_STRING << 8) | best_if->iInterface, 0); + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | best_if->iInterface, 0); if (name == NULL) { return; } @@ -415,10 +404,9 @@ static void enum_device(struct usb_device *usbdev) _dprintf(".\n"); } dprintf("Konfiguration: %i:%i (%i)\n", conf_desc->config_value, - best_if->interface_number, - best_if->interface_class); + best_if->interface_number, best_if->interface_class); if (ccdriver_take_device[best_if->interface_class] != NULL) { - ccdriver_take_device[best_if->interface_class](usbdev); + ccdriver_take_device[best_if->interface_class] (usbdev); } } @@ -432,22 +420,20 @@ static void enumerate_hub(struct usb_device *usbdev) //Mehr als 8 Ports sollte es nicht geben, die Gr????e des Deskriptors wird //daher wohl nicht gr????er als sizeof()+16 werden hub_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*hub_desc) + 16, - CLS_REQUEST, REC_DEVICE, GET_DESCRIPTOR, - DESC_DEVICE << 8, 0); - for (i = 0; i < hub_desc->nbr_ports; i++) - { + CLS_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); + for (i = 0; i < hub_desc->nbr_ports; i++) { port_status = do_control(usbdev, DEV_TO_HOST, NULL, 4, CLS_REQUEST, - REC_OTHER, GET_STATUS, 0, i+1); + REC_OTHER, GET_STATUS, 0, i + 1); if (!(port_status[0] & PORT_DEVICE)) { continue; } //Strom an do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, - SET_FEATURE, PORTF_POWER, i+1); + SET_FEATURE, PORTF_POWER, i + 1); cdi_sleep_ms(hub_desc->pwron2pwrgood * 2); //Resetten do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, - SET_FEATURE, PORTF_RESET, i+1); + SET_FEATURE, PORTF_RESET, i + 1); //Reset sollte jetzt eigtl. schon beendet sein //(do_control wartet ja 50 ms) down = malloc(sizeof(struct usb_device)); @@ -464,11 +450,9 @@ void enumerate_hci(struct hci *hci) struct usb_device *usbdev; cdi_list_t usb_devices; - if (hci->find_devices != NULL) - { + if (hci->find_devices != NULL) { usb_devices = hci->find_devices(hci); - while ((usbdev = cdi_list_pop(usb_devices)) != NULL) - { + while ((usbdev = cdi_list_pop(usb_devices)) != NULL) { hci->activate_device(hci, usbdev); cdi_sleep_ms(50); enum_device(usbdev); diff --git a/usb/msd.c b/usb/msd.c index 0f9b79e..9614473 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -48,6 +48,7 @@ static int dprintf(const char *fmt, ...) { return 0; } + #define _dprintf(fmt, args...) dprintf(fmt, ##args) #endif @@ -70,9 +71,12 @@ static void deinit_msd(struct cdi_device *cdi_msd); static void get_partitions(struct cdi_storage_device *cdistrg); static void init_msd(struct cdi_device *cdi_msd); static void kill_driver(struct cdi_driver *cdi_msc_driver); -static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer); -static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer); -static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count); +static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, + uint64_t count, void *buffer); +static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, + uint64_t count, void *buffer); +static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, + uint32_t *block_count); //TODO: CDI-Funktion w??re hier sicher n??tzlich... #define CPU_IS_LITTLE_ENDIAN @@ -125,10 +129,9 @@ void register_msd(struct usb_device *usbdev) uint64_t size; static int msdnum = 0; - if (usbdev->interface->interface_protocol != 0x50) - { - dprintf("Es werden nur bulk-only-Interfaces unterst??tzt.\n"); - return; + if (usbdev->interface->interface_protocol != 0x50) { + dprintf("Es werden nur bulk-only-Interfaces unterst??tzt.\n"); + return; } cdimsd = malloc(sizeof(struct cdi_msd)); if (cdimsd == NULL) { @@ -139,15 +142,13 @@ void register_msd(struct usb_device *usbdev) strgdev = (struct cdi_storage_device *)cdimsd; strgdev->dev.type = CDI_STORAGE; strgdev->dev.name = malloc(10); - if (strgdev->dev.name == NULL) - { + if (strgdev->dev.name == NULL) { free(strgdev); return; } sprintf((char *)strgdev->dev.name, "msd%i", msdnum++); msc = malloc(sizeof(struct msclass_data)); - if (msc == NULL) - { + if (msc == NULL) { free((void *)strgdev->dev.name); free(strgdev); return; @@ -156,38 +157,30 @@ void register_msd(struct usb_device *usbdev) msc->bulk_ep_in = NULL; msc->bulk_ep_out = NULL; address = (void *)usbdev->interface + sizeof(struct interface_desc); - for (i = 0; i < usbdev->interface->num_endpoints; i++) - { + for (i = 0; i < usbdev->interface->num_endpoints; i++) { ep_desc = address; - if ((ep_desc->endpoint_address & 0x80) && - (ep_desc->attributes == 0x02) && - (msc->bulk_ep_in == NULL)) { //BULK-IN + if ((ep_desc->endpoint_address & 0x80) && (ep_desc->attributes == 0x02) && (msc->bulk_ep_in == NULL)) { //BULK-IN msc->bulk_ep_in = ep_desc; - } else if (!(ep_desc->endpoint_address & 0x80) && - (ep_desc->attributes == 0x02) && - (msc->bulk_ep_out == NULL)) { //BULK-OUT + } else if (!(ep_desc->endpoint_address & 0x80) && (ep_desc->attributes == 0x02) && (msc->bulk_ep_out == NULL)) { //BULK-OUT msc->bulk_ep_out = ep_desc; } address += sizeof(struct endpoint_desc); } - if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL)) - { + if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL)) { dprintf("Nicht gen??gend Endpoints gefunden.\n"); return; } - if (!msd_get_capacity(usbdev, &bs, &bc)) - { + if (!msd_get_capacity(usbdev, &bs, &bc)) { strgdev->block_size = 0; strgdev->block_count = 0; dprintf("Konnte Gr????e f??r %s nicht ermitteln.\n", strgdev->dev.name); - } - else - { + } else { strgdev->block_size = bs; strgdev->block_count = bc; size = bs; size *= bc; - dprintf("%s: %i * %i B (ca. %lld MB).\n", strgdev->dev.name, bc, bs, size >> 20); + dprintf("%s: %i * %i B (ca. %lld MB).\n", strgdev->dev.name, bc, bs, + size >> 20); } cdi_storage_device_init(strgdev); cdi_list_push(cdi_driver.drv.devices, strgdev); @@ -210,58 +203,81 @@ static void deinit_msd(struct cdi_device *cdi_msd) //TODO: Und gerade hier... } -static int write_cmd(struct usb_device* usbdev, uintptr_t src) +static int write_cmd(struct usb_device *usbdev, uintptr_t src) { - struct msclass_data *msc = (struct msclass_data*) usbdev->classd; + struct msclass_data *msc = (struct msclass_data *)usbdev->classd; struct usb_packet cmd_packet = { - .type = PACKET_OUT, - .endpoint = msc->bulk_ep_out->endpoint_address, - .phys_data = src, - .length = 0x1F, - .datatoggle = 0, - .type_of_data = USB_TOD_COMMAND, + .type = PACKET_OUT, + .endpoint = msc->bulk_ep_out->endpoint_address, + .phys_data = src, + .length = 0x1F, + .datatoggle = 0, + .type_of_data = USB_TOD_COMMAND, }; return usb_do_packet(usbdev, &cmd_packet); } -static int read_status(struct usb_device* usbdev, uintptr_t dest) +/** + * Liest den Status von einem MSD + * + * @param usbdev Das bewusste Ger??t + * @param expected_tag Das erwartete Tag + * + * @return Wenn der Status OK ist USB_NO_ERROR, sonst entsprechender Fehler + */ + +static int read_status(struct usb_device *usbdev, uint32_t expected_tag) { - struct msclass_data *msc = (struct msclass_data*) usbdev->classd; + uintptr_t pcsw; + struct msclass_data *msc = (struct msclass_data *)usbdev->classd; + struct command_status_wrapper *csw; + int error; + + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), + (void **)&csw, (void **)&pcsw) == -1) { + return USB_TRIVIAL_ERROR; + } + struct usb_packet status_packet = { - .type = PACKET_IN, - .endpoint = msc->bulk_ep_in->endpoint_address, - .phys_data = dest, - .length = 0x0D, - .datatoggle = 0, - .type_of_data = USB_TOD_STATUS, + .type = PACKET_IN, + .endpoint = msc->bulk_ep_in->endpoint_address, + .phys_data = pcsw, + .length = 0x0D, + .datatoggle = 0, + .type_of_data = USB_TOD_STATUS, }; - return usb_do_packet(usbdev, &status_packet); + error = usb_do_packet(usbdev, &status_packet); + if (error != USB_NO_ERROR) { + return error; + } + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || csw->csw_status) { + return USB_CRC; //Watt wei?? denn ich + } + + return USB_NO_ERROR; } -static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count) +static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, + uint32_t *block_count) { struct msclass_data *msc; struct command_block_wrapper *cbw; - struct command_status_wrapper *csw; struct msd_capacity *cap; - uintptr_t pcbw, pcsw, pcap; + uintptr_t pcbw, pcap; uint32_t expected_tag; if ((usbdev == NULL) || (block_size == NULL) || (block_count == NULL)) { return 0; } if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), - (void **)&cbw, (void **)&pcbw) == -1) { - return 0; - } - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), - (void **)&csw, (void **)&pcsw) == -1) { + (void **)&cbw, (void **)&pcbw) == -1) { return 0; } if (cdi_alloc_phys_mem(sizeof(struct msd_capacity), - (void **)&cap, (void **)&pcap) == -1) { + (void **)&cap, (void **)&pcap) == -1) { return 0; } msc = (struct msclass_data *)usbdev->classd; @@ -269,42 +285,30 @@ static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uin cbw->cbw_signature = CBW_SIGNATURE; cbw->cbw_tag = (expected_tag = cbw_tag++); cbw->cbw_data_transfer_length = sizeof(struct msd_capacity); - cbw->cbw_flags = 0x80; //IN - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? cbw->cbw_cb_length = 12; cbw->cbw_cb[0] = MSC_CMD_CAPACITY; - cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[1] = 0; //LUN: 0 if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { return 0; } - cdi_sleep_ms(20); - struct usb_packet in_packet = { - .type = PACKET_IN, - .endpoint = msc->bulk_ep_in->endpoint_address, - .phys_data = pcap, - .length = sizeof(*cap), - .datatoggle = 0, - .type_of_data = USB_TOD_DATA_IN, + .type = PACKET_IN, + .endpoint = msc->bulk_ep_in->endpoint_address, + .phys_data = pcap, + .length = sizeof(*cap), + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_IN, }; if (usb_do_packet(usbdev, &in_packet) != USB_NO_ERROR) { return 0; } - cdi_sleep_ms(5); - - if (read_status(usbdev, pcsw) != USB_NO_ERROR) { - return 0; - } - - cdi_sleep_ms(5); - - if ((csw->csw_signature != CSW_SIGNATURE) || - (csw->csw_tag != expected_tag) || - csw->csw_status) { + if (read_status(usbdev, expected_tag) != USB_NO_ERROR) { return 0; } @@ -317,46 +321,33 @@ static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uin #ifdef WAIT_FOR_MSD_READY static int msd_ready(struct usb_device *usbdev) { - struct msclass_data *msc; - struct command_block_wrapper *cbw; - struct command_status_wrapper *csw; - uintptr_t pcbw, pcsw; - uint32_t expected_tag; - - if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), - (void **)&cbw, (void **)&pcbw) == -1) { - return 0; - } - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), - (void **)&csw, (void **)&pcsw) == -1) { - return 0; - } - msc = (struct msclass_data *)usbdev->classd; - memset(cbw, 0, 0x1F); - cbw->cbw_signature = CBW_SIGNATURE; - cbw->cbw_tag = (expected_tag = cbw_tag++); - cbw->cbw_data_transfer_length = 0; - cbw->cbw_flags = 0x80; //IN - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? - cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" - - if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { - return 0; - } - - cdi_sleep_ms(7); - if (read_status(usbdev, pcsw) != USB_NO_ERROR) { - return 0; - } - - cdi_sleep_ms(4); - if ((csw->csw_signature != CSW_SIGNATURE) || - (csw->csw_tag != expected_tag) || - csw->csw_status) { - return 0; - } - - return 1; + struct msclass_data *msc; + struct command_block_wrapper *cbw; + uintptr_t pcbw; + uint32_t expected_tag; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), + (void **)&cbw, (void **)&pcbw) == -1) { + return 0; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = 0; + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" + + if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { + return 0; + } + + if (read_status(usbdev, expected_tag) != USB_NO_ERROR) { + return 0; + } + + return 1; } #endif @@ -368,9 +359,11 @@ static inline int tsl(volatile int *variable) return rval; } -static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length); +static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, + uint16_t sectors, uintptr_t phys_buffer, size_t length); -static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer) +static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, + uint64_t count, void *buffer) { void *vb; uintptr_t pb; @@ -383,57 +376,48 @@ static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint fdprintf("read(%i, %i)\n", (int)start, (int)count); start += ((struct cdi_msd *)strgdev)->offset; - if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) - { + if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) { dprintf("Blockspeicher konnte nicht allociert werden.\n"); return -1; } - if (!count) - { + if (!count) { dprintf("Leere Leseanfrage.\n"); return 0; } - while (tsl(&usbdev->locked)) - { + while (tsl(&usbdev->locked)) { #ifndef CDI_STANDALONE - __asm__ __volatile__ ("hlt"); + __asm__ __volatile__("hlt"); #endif } - for (i = 0; i < count; i++) - { + for (i = 0; i < count; i++) { #ifdef WAIT_FOR_MSD_READY for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { cdi_sleep_ms(20); } #endif error = msd_read(usbdev, start + i, 1, pb, bs); - if (error != USB_NO_ERROR) - { + if (error != USB_NO_ERROR) { dprintf("Lesefehler 0x%X bei Block %i.\n", error, i); usbdev->locked = 0; return -1; } - memcpy(buffer + i*bs, vb, bs); + memcpy(buffer + i * bs, vb, bs); } usbdev->locked = 0; return 0; } -static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) +static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, + uint16_t sectors, uintptr_t phys_buffer, size_t length) { struct msclass_data *msc; struct command_block_wrapper *cbw; - struct command_status_wrapper *csw; - uintptr_t pcbw, pcsw; + uintptr_t pcbw; uint32_t expected_tag; int error; if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), - (void **)&cbw, (void **)&pcbw) == -1) { - return USB_TRIVIAL_ERROR; - } - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), - (void **)&csw, (void **)&pcsw) == -1) { + (void **)&cbw, (void **)&pcbw) == -1) { return USB_TRIVIAL_ERROR; } if (!phys_buffer || !length) { @@ -444,61 +428,50 @@ static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t secto cbw->cbw_signature = CBW_SIGNATURE; cbw->cbw_tag = (expected_tag = cbw_tag++); cbw->cbw_data_transfer_length = length; - cbw->cbw_flags = 0x80; //IN - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? cbw->cbw_cb_length = 12; cbw->cbw_cb[0] = MSC_CMD_READ10; - cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[1] = 0; //LUN: 0 cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; - cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[5] = lba & 0x000000FF; cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; - cbw->cbw_cb[8] = sectors & 0x000000FF; + cbw->cbw_cb[8] = sectors & 0x000000FF; error = write_cmd(usbdev, pcbw); if (error != USB_NO_ERROR) { return error; } - cdi_sleep_ms(20); struct usb_packet in_packet = { - .type = PACKET_IN, - .endpoint = msc->bulk_ep_in->endpoint_address, - .phys_data = phys_buffer, - .length = length, - .datatoggle = 0, - .type_of_data = USB_TOD_DATA_IN, + .type = PACKET_IN, + .endpoint = msc->bulk_ep_in->endpoint_address, + .phys_data = phys_buffer, + .length = length, + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_IN, }; error = usb_do_packet(usbdev, &in_packet); if (error != USB_NO_ERROR) { return error; } - cdi_sleep_ms(5); - error = read_status(usbdev, pcsw); + error = read_status(usbdev, expected_tag); if (error != USB_NO_ERROR) { return error; } - cdi_sleep_ms(4); - if ((csw->csw_signature != CSW_SIGNATURE) || - (csw->csw_tag != expected_tag) || - csw->csw_status) - { - dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature, - csw->csw_tag, - expected_tag, - csw->csw_status); - return USB_CRC; //Watt wei?? denn ich... - } return USB_NO_ERROR; } -static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length); +static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, + uint16_t sectors, uintptr_t phys_buffer, size_t length); -static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer) +static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, + uint64_t count, void *buffer) { void *vb; uintptr_t pb; @@ -511,28 +484,24 @@ static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uin fdprintf("write(%i, %i)\n", (int)start, (int)count); start += ((struct cdi_msd *)strgdev)->offset; - if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) - { + if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) { dprintf("Blockspeicher zum Schreiben konnte nicht allociert werden.\n"); return -1; } - while (tsl(&usbdev->locked)) - { + while (tsl(&usbdev->locked)) { #ifndef CDI_STANDALONE - __asm__ __volatile__ ("hlt"); + __asm__ __volatile__("hlt"); #endif } - for (i = 0; i < count; i++) - { - memcpy(vb, buffer + i*bs, bs); + for (i = 0; i < count; i++) { + memcpy(vb, buffer + i * bs, bs); #ifdef WAIT_FOR_MSD_READY for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { cdi_sleep_ms(20); } #endif error = msd_write(usbdev, start + i, 1, pb, bs); - if (error != USB_NO_ERROR) - { + if (error != USB_NO_ERROR) { dprintf("Schreibfehler 0x%X bei Block %i.\n", error, i); usbdev->locked = 0; return -1; @@ -542,7 +511,8 @@ static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uin return 0; } -static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) +static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, + uint16_t sectors, uintptr_t phys_buffer, size_t length) { struct msclass_data *msc; struct command_block_wrapper *cbw; @@ -552,11 +522,11 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect int error; if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), - (void **)&cbw, (void **)&pcbw) == -1) { + (void **)&cbw, (void **)&pcbw) == -1) { return USB_TRIVIAL_ERROR; } if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), - (void **)&csw, (void **)&pcsw) == -1) { + (void **)&csw, (void **)&pcsw) == -1) { return USB_TRIVIAL_ERROR; } if (!phys_buffer || !length) { @@ -567,31 +537,30 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect cbw->cbw_signature = CBW_SIGNATURE; cbw->cbw_tag = (expected_tag = cbw_tag++); cbw->cbw_data_transfer_length = length; - cbw->cbw_flags = 0x00; //OUT - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_flags = 0x00; //OUT + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? cbw->cbw_cb_length = 12; cbw->cbw_cb[0] = MSC_CMD_WRITE10; - cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[1] = 0; //LUN: 0 cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; - cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[5] = lba & 0x000000FF; cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; - cbw->cbw_cb[8] = sectors & 0x000000FF; + cbw->cbw_cb[8] = sectors & 0x000000FF; error = write_cmd(usbdev, pcbw); if (error != USB_NO_ERROR) { return error; } - cdi_sleep_ms(20); struct usb_packet out_packet = { - .type = PACKET_OUT, - .endpoint = msc->bulk_ep_out->endpoint_address, - .phys_data = phys_buffer, - .length = length, - .datatoggle = 0, - .type_of_data = USB_TOD_DATA_OUT, + .type = PACKET_OUT, + .endpoint = msc->bulk_ep_out->endpoint_address, + .phys_data = phys_buffer, + .length = length, + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_OUT, }; error = usb_do_packet(usbdev, &out_packet); @@ -599,23 +568,11 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect return error; } - cdi_sleep_ms(5); - error = read_status(usbdev, pcsw); + error = read_status(usbdev, expected_tag); if (error != USB_NO_ERROR) { return error; } - cdi_sleep_ms(4); - if ((csw->csw_signature != CSW_SIGNATURE) || - (csw->csw_tag != expected_tag) || - csw->csw_status) - { - dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature, - csw->csw_tag, - expected_tag, - csw->csw_status); - return USB_CRC; - } return USB_NO_ERROR; } @@ -631,15 +588,15 @@ static void get_partitions(struct cdi_storage_device *cdistrg) dprintf("MBR konnte nicht eingelesen werden.\n"); return; } - if (((uint16_t *)mbr)[255] != 0xAA55) { + if (((uint16_t *) mbr)[255] != 0xAA55) { return; } partition = mbr + 0x1BE; dprintf("Partitionen auf %s:", cdistrg->dev.name); - for (i = 0; i < 4; i++) - { - if (partition[i].type && partition[i].size && (partition[i].start + partition[i].size < cdistrg->block_count)) - { + for (i = 0; i < 4; i++) { + if (partition[i].type && partition[i].size + && (partition[i].start + partition[i].size < + cdistrg->block_count)) { cdimsd = malloc(sizeof(struct cdi_msd)); if (cdimsd == NULL) { break; @@ -648,12 +605,12 @@ static void get_partitions(struct cdi_storage_device *cdistrg) cdimsd->usb_device = base_cdimsd->usb_device; cdimsd->cdi_device.dev.type = CDI_STORAGE; cdimsd->cdi_device.dev.name = malloc(14); - if (cdimsd->cdi_device.dev.name == NULL) - { + if (cdimsd->cdi_device.dev.name == NULL) { free(cdimsd); break; } - sprintf((char *)cdimsd->cdi_device.dev.name, "%sp%i", cdistrg->dev.name, j++); + sprintf((char *)cdimsd->cdi_device.dev.name, "%sp%i", + cdistrg->dev.name, j++); cdimsd->cdi_device.block_size = base_cdimsd->cdi_device.block_size; cdimsd->cdi_device.block_count = partition[i].size; _dprintf(" %s", cdimsd->cdi_device.dev.name); diff --git a/usb/uhci.c b/usb/uhci.c index 799d526..6cdc44c 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -43,6 +43,7 @@ static int dprintf(const char *fmt, ...) { return 0; } + #define _dprintf(fmt, args...) dprintf(fmt, ##args) #endif @@ -52,7 +53,9 @@ static struct cdi_driver cdi_driver; static cdi_list_t active_transfers; static void uhci_handler(struct cdi_device *dev); -static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle); +static int enqueue_request(struct hci *gen_hci, int frame, int type, + int device, int endpoint, int low_speed, + uintptr_t phys_data, int length, int datatoggle); static cdi_list_t get_devices(struct hci *gen_hci); static void activate_device(struct hci *gen_hci, struct usb_device *device); static int get_current_frame(struct hci *gen_hci); @@ -75,14 +78,13 @@ struct cdi_driver *init_uhcd() static void uhci_kill(struct cdi_driver *cdi_hcd) { - //TODO: Da geht doch noch was + //TODO: Da geht doch noch was } static void uhci_deinit(struct cdi_device *cdi_hci) { - cdi_outw(((struct uhci *)((struct cdi_hci *)cdi_hci)->hci)->pbase + UHCI_USBCMD, - MAXP); //HC anhalten - //TODO: Hier doch auch + cdi_outw(((struct uhci *)((struct cdi_hci *)cdi_hci)->hci)->pbase + UHCI_USBCMD, MAXP); //HC anhalten + //TODO: Hier doch auch } void uhci_init(struct cdi_device *cdi_hci) @@ -95,13 +97,10 @@ void uhci_init(struct cdi_device *cdi_hci) //cdi_pci_alloc_ioports(gen_hci->pcidev); uhci->pbase = 0; for (i = 0; (res = cdi_list_get(gen_hci->pcidev->resources, i)) != NULL; - i++) - { - if (res->type == CDI_PCI_IOPORTS) - { + i++) { + if (res->type == CDI_PCI_IOPORTS) { size = res->length ? res->length : 0x14; - if (cdi_ioports_alloc(res->start, size) == -1) - { + if (cdi_ioports_alloc(res->start, size) == -1) { dprintf("I/O-Ports konnten nicht reserviert werden.\n"); return; } @@ -109,14 +108,12 @@ void uhci_init(struct cdi_device *cdi_hci) break; } } - if (!uhci->pbase) - { + if (!uhci->pbase) { dprintf("I/O-Basis nicht gefunden!\n"); return; } if (cdi_alloc_phys_mem(4096, (void **)&uhci->frame_list, - (void **)&uhci->phys_frame_list) == -1) - { + (void **)&uhci->phys_frame_list) == -1) { dprintf("Frame List konnte nicht allociert werden!\n"); return; } @@ -125,9 +122,10 @@ void uhci_init(struct cdi_device *cdi_hci) if (uhci->root_ports > 7) { //Laut Linuxkernel ist das so "weird", dass da was nicht stimmen kann... uhci->root_ports = 2; } - dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase, uhci->root_ports, gen_hci->pcidev->irq); + dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase, + uhci->root_ports, gen_hci->pcidev->irq); for (i = 0; i < 1024; i++) { - uhci->frame_list[i] = 1; //Invalid + uhci->frame_list[i] = 1; //Invalid } dprintf("Resetten...\n"); //HC zur??cksetzen @@ -144,10 +142,10 @@ void uhci_init(struct cdi_device *cdi_hci) cdi_outw(uhci->pbase + UHCI_USBINTR, 0xF); //Framelistadresse eintragen cdi_outl(uhci->pbase + UHCI_FRBASEADD, uhci->phys_frame_list); - cdi_outw(uhci->pbase + UHCI_FRNUM, 0); //Frame zur??cksetzen + cdi_outw(uhci->pbase + UHCI_FRNUM, 0); //Frame zur??cksetzen //Ports abschalten, damit selektiv eingeschaltet werden kann (Enumeration) for (i = 0; i < uhci->root_ports; i++) { - cdi_outw(uhci->pbase + UHCI_RPORTS + i*2, RPORT_CSC); + cdi_outw(uhci->pbase + UHCI_RPORTS + i * 2, RPORT_CSC); } dprintf(" Fertig\n"); gen_hci->find_devices = &get_devices; @@ -167,10 +165,8 @@ static cdi_list_t get_devices(struct hci *gen_hci) dprintf("Ger??te finden: "); cdi_outw(uhci->pbase + UHCI_USBCMD, USB_RUN); - for (i = 0; i < uhci->root_ports; i++) - { - if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_DEVICE)) - { + for (i = 0; i < uhci->root_ports; i++) { + if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & RPORT_DEVICE)) { _dprintf("-"); continue; } @@ -180,7 +176,7 @@ static cdi_list_t get_devices(struct hci *gen_hci) dev->id = 0; dev->port = i; dev->low_speed = - (cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_LOSPD) && 1; + (cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & RPORT_LOSPD) && 1; cdi_list_push(dlist, dev); } _dprintf("\n"); @@ -193,7 +189,7 @@ static void activate_device(struct hci *gen_hci, struct usb_device *device) dprintf("Ger??t an Port %i wird aktiviert.\n", device->port); cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, - RPORT_RESET | RPORT_ENABLE | RPORT_CSC); + RPORT_RESET | RPORT_ENABLE | RPORT_CSC); cdi_sleep_ms(20); } @@ -209,34 +205,37 @@ static inline int tsl(volatile int *variable) *variable = 1; return rval; } + static volatile int locked = 0; -static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle) +static int enqueue_request(struct hci *gen_hci, int frame, int type, + int device, int endpoint, int low_speed, + uintptr_t phys_data, int length, int datatoggle) { struct uhci *uhci = (struct uhci *)gen_hci; struct uhci_td *td; struct uhci_qh *qh; uintptr_t ptd, pqh; struct transfer *addr; + int timeout; if (cdi_alloc_phys_mem(sizeof(struct uhci_td), - (void **)&td, (void **)&ptd) == -1) { + (void **)&td, (void **)&ptd) == -1) { return USB_TRIVIAL_ERROR; } if (cdi_alloc_phys_mem(sizeof(struct uhci_qh), - (void **)&qh, (void **)&pqh) == -1) { + (void **)&qh, (void **)&pqh) == -1) { return USB_TRIVIAL_ERROR; } - while (tsl(&locked)) - { + while (tsl(&locked)) { #ifndef CDI_STANDALONE - __asm__ __volatile__ ("hlt"); + __asm__ __volatile__("hlt"); #endif } - qh->next = 1; //Invalid + qh->next = 1; //Invalid qh->transfer = ptd; memset(td, 0, sizeof(struct uhci_td)); - td->next = 1; //Invalid + td->next = 1; //Invalid td->active = 1; td->ioc = 1; td->data_toggle = datatoggle; @@ -245,7 +244,7 @@ static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, td->pid = type; td->device = device; td->endpoint = endpoint; - td->maxlen = length ? length-1 : 0x7FF; + td->maxlen = length ? length - 1 : 0x7FF; td->buffer = phys_data; addr = malloc(sizeof(struct transfer)); addr->virt = td; @@ -253,20 +252,12 @@ static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, addr->error = 0xFFFF; cdi_list_push(active_transfers, addr); uhci->frame_list[frame] = pqh | 2; -#ifdef CDI_STANDALONE - /*int cframe = get_current_frame(gen_hci), nframe; - nframe = (cframe > frame) ? frame + 1024 : frame; //??berl??ufe korrigieren - //Ann??hern, wann er wohl fertig ist - cdi_sleep_ms(nframe + 30 - get_current_frame(gen_hci));*/ -#else - while (!(qh->transfer & 1)) { - __asm__ __volatile__ ("hlt"); + for (timeout = 0; !(qh->transfer & 1) && (timeout < 1000); timeout++) { + cdi_sleep_ms(1); } -#endif - while (addr->error == 0xFFFF) - { + while ((timeout < 1000) && (addr->error == 0xFFFF)) { #ifndef CDI_STANDALONE - __asm__ __volatile__ ("hlt"); + __asm__ __volatile__("hlt"); #endif } uhci->frame_list[frame] = 1; @@ -281,54 +272,44 @@ static void uhci_handler(struct cdi_device *cdi_hci) struct transfer *addr; struct uhci_td *td; - if (!status) { //Also, von hier kommt der IRQ nicht. + if (!status) { //Also, von hier kommt der IRQ nicht. return; } if (status & ~0x0001) { dprintf("Unerwarteter IRQ 0x%04X von %s\n", status, cdi_hci->name); } - if (status & 0x10) - { + if (status & 0x10) { printf("[uhci] SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n"); - cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! - } - else - { - for (i = 0; (addr = cdi_list_get(active_transfers, i)) != NULL; i++) - { + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! + } else { + for (i = 0; (addr = cdi_list_get(active_transfers, i)) != NULL; i++) { td = addr->virt; if (td->active) { continue; } addr->error = USB_NO_ERROR; cdi_list_remove(active_transfers, i--); - if (td->stalled_err) - { + if (td->stalled_err) { dprintf("ENDPOINT STALLED\n"); addr->error |= USB_STALLED; } - if (td->buf_err) - { + if (td->buf_err) { dprintf("Puffer??berlauf oder Underrun\n"); addr->error |= USB_BUFFER_ERROR; } - if (td->babble) - { - dprintf("Puh, da war ein Ger??t wohl sehr gespr??chig: Babble.\n"); + if (td->babble) { + dprintf("Da war ein Ger??t wohl sehr gespr??chig: Babble.\n"); addr->error |= USB_BABBLE; } - if (td->nak) - { + if (td->nak) { dprintf("NAK empfangen\n"); addr->error |= USB_NAK; } - if (td->crc_time_err) - { + if (td->crc_time_err) { dprintf("CRC-Fehler oder Timeout\n"); addr->error |= USB_CRC | USB_TIMEOUT; } - if (td->bitstuff_err) - { + if (td->bitstuff_err) { dprintf("Bitstufffehler\n"); addr->error |= USB_BITSTUFF; } -- 1.6.3.3 From kevin at tyndur.org Sat Aug 22 15:17:06 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 22 Aug 2009 15:17:06 +0200 Subject: [cdi-devel] [PATCH] "Better" indent, wait for ACK instead of sleep In-Reply-To: <1250944461-4553-1-git-send-email-max@tyndur.org> References: <1250944461-4553-1-git-send-email-max@tyndur.org> Message-ID: <20090822131701.GA4351@silvanus> On Sat, Aug 22, 2009 at 02:34:21PM +0200, max at tyndur.org wrote: > From: Max Reitz > > * indent done with the program "indent" > + While a USB device is not ready, it sends NAK in > response to IN/OUT - it is a lot better to wait > for an ACK instead of sleeping randomly. > > Signed-off-by: Max Reitz Please, please, don't do this. Coding style cleanup should never be mixed with functional changes. This makes it extremely difficult to review the patch. And it makes it impossible to drop to changes made by indent if they aren't nice (they are not, at least not all of them). > --- > usb/include/msd.h | 50 +++---- > usb/include/uhci.h | 80 ++++++------ > usb/include/usb.h | 67 ++++------ > usb/main.c | 192 ++++++++++++-------------- > usb/msd.c | 377 +++++++++++++++++++++++----------------------------- > usb/uhci.c | 117 +++++++---------- > 6 files changed, 392 insertions(+), 491 deletions(-) > > diff --git a/usb/include/msd.h b/usb/include/msd.h > index 99c796d..cc3bb4d 100644 > --- a/usb/include/msd.h > +++ b/usb/include/msd.h > @@ -32,8 +32,7 @@ > > //"USBC" > #define CBW_SIGNATURE 0x43425355 > -struct command_block_wrapper > -{ > +struct command_block_wrapper { > uint32_t cbw_signature; > uint32_t cbw_tag; > uint32_t cbw_data_transfer_length; > @@ -41,55 +40,50 @@ struct command_block_wrapper > uint8_t cbw_lun; > uint8_t cbw_cb_length; > uint8_t cbw_cb[16]; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > //"USBS" > #define CSW_SIGNATURE 0x53425355 > -struct command_status_wrapper > -{ > +struct command_status_wrapper { > uint32_t csw_signature; > uint32_t csw_tag; > uint32_t csw_data_residue; > uint8_t csw_status; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -struct msc_sense > -{ > - unsigned error : 7; > - unsigned valid : 1; > +struct msc_sense { > + unsigned error:7; > + unsigned valid:1; > uint8_t rsvd0; > - unsigned sense_key : 4; > - unsigned rsvd1 : 4; > + unsigned sense_key:4; > + unsigned rsvd1:4; Much nicer to read with a space. > uint32_t information; > uint8_t additional_length; > uint32_t rsvd2; > uint8_t additional_code; > uint8_t additional_code_qualifier; > uint32_t rsvd; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -struct msd_capacity > -{ > +struct msd_capacity { > uint32_t last_lba; > uint32_t block_length; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -struct cdi_msd > -{ > +struct cdi_msd { > struct cdi_storage_device cdi_device; > struct usb_device *usb_device; > - uint32_t offset; //F??r Partitionen > + uint32_t offset; //F??r Partitionen > }; > > -struct part_table_entry > -{ > - uint8_t active; > - uint8_t begin_chs[3]; > - uint8_t type; > - uint8_t end_chs[3]; > - uint32_t start; > - uint32_t size; > -} __attribute__((packed)); > +struct part_table_entry { > + uint8_t active; > + uint8_t begin_chs[3]; > + uint8_t type; > + uint8_t end_chs[3]; > + uint32_t start; > + uint32_t size; > +} __attribute__ ((packed)); > > > #define MSC_CMD_REZERO 0x01 > diff --git a/usb/include/uhci.h b/usb/include/uhci.h > index 0158bd2..bf22c4a 100644 > --- a/usb/include/uhci.h > +++ b/usb/include/uhci.h > @@ -31,15 +31,15 @@ > #include "usb.h" > > > -#define UHCI_USBCMD 0x00 //w > -#define UHCI_USBSTS 0x02 //w > -#define UHCI_USBINTR 0x04 //w > -#define UHCI_FRNUM 0x06 //w > -#define UHCI_FRBASEADD 0x08 //l > -#define UHCI_SOFMOD 0x0C //b > +#define UHCI_USBCMD 0x00 //w > +#define UHCI_USBSTS 0x02 //w > +#define UHCI_USBINTR 0x04 //w > +#define UHCI_FRNUM 0x06 //w > +#define UHCI_FRBASEADD 0x08 //l > +#define UHCI_SOFMOD 0x0C //b > #define UHCI_RPORTS 0x10 > -#define UHCI_PORTSC1 0x10 //w > -#define UHCI_PORTSC2 0x12 //w > +#define UHCI_PORTSC1 0x10 //w > +#define UHCI_PORTSC2 0x12 //w > > #define MAXP 0x0080 > #define GRESET 0x0004 > @@ -47,14 +47,13 @@ > #define USB_RUN 0x0001 > > #define RPORT_RESET 0x0200 > -#define RPORT_LOSPD 0x0100 //Low speed device attached > +#define RPORT_LOSPD 0x0100 //Low speed device attached > #define RPORT_ENABLE 0x0004 > -#define RPORT_CSC 0x0002 //Connect status change > +#define RPORT_CSC 0x0002 //Connect status change > #define RPORT_DEVICE 0x0001 > > > -struct uhci > -{ > +struct uhci { > struct hci gen_hci; > uint16_t pbase; > uintptr_t phys_frame_list; > @@ -62,46 +61,43 @@ struct uhci > int root_ports; > }; > > -struct uhci_qh > -{ > +struct uhci_qh { > volatile uint32_t next; > volatile uint32_t transfer; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -struct uhci_td > -{ > +struct uhci_td { > volatile uint32_t next; > > - unsigned trans_len : 11; > - unsigned rsvd0 : 6; > - unsigned bitstuff_err : 1; > - unsigned crc_time_err : 1; > - unsigned nak : 1; > - unsigned babble : 1; > - unsigned buf_err : 1; > - unsigned stalled_err : 1; > - unsigned active : 1; > - unsigned ioc : 1; > - unsigned isochronous : 1; > - unsigned low_speed : 1; > - unsigned errors : 2; > - unsigned spd : 1; > - unsigned rsvd1 : 2; > - > - unsigned pid : 8; > - unsigned device : 7; > - unsigned endpoint : 4; > - unsigned data_toggle : 1; > - unsigned rsvd2 : 1; > - unsigned maxlen : 11; > + unsigned trans_len:11; > + unsigned rsvd0:6; > + unsigned bitstuff_err:1; > + unsigned crc_time_err:1; > + unsigned nak:1; > + unsigned babble:1; > + unsigned buf_err:1; > + unsigned stalled_err:1; > + unsigned active:1; > + unsigned ioc:1; > + unsigned isochronous:1; > + unsigned low_speed:1; > + unsigned errors:2; > + unsigned spd:1; > + unsigned rsvd1:2; > + > + unsigned pid:8; > + unsigned device:7; > + unsigned endpoint:4; > + unsigned data_toggle:1; > + unsigned rsvd2:1; > + unsigned maxlen:11; > > uint32_t buffer; > > uint32_t user[4]; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -struct transfer > -{ > +struct transfer { > void *virt; > uintptr_t phys; > volatile int error; > diff --git a/usb/include/usb.h b/usb/include/usb.h > index cca698a..9ec8b37 100644 > --- a/usb/include/usb.h > +++ b/usb/include/usb.h > @@ -30,8 +30,8 @@ > > enum usb_packet_type { > PACKET_SETUP = 0x2D, > - PACKET_IN = 0x69, > - PACKET_OUT = 0xE1, > + PACKET_IN = 0x69, > + PACKET_OUT = 0xE1 Meh. Destroys nicely aligned initializers. > }; > > #define NO_DATA 0x01 > @@ -124,42 +124,39 @@ enum usb_packet_type { > #define USB_TODS_STATUS 8 > > > -typedef enum > -{ > +typedef enum { > HCI_UHCI, > HCI_OHCI, > HCI_EHCI > } hci_type_t; > > -struct cdi_hci > -{ > +struct cdi_hci { > struct cdi_device cdi_device; > struct hci *hci; > }; > > struct usb_device; > > -struct hci > -{ > +struct hci { > struct cdi_pci_device *pcidev; > hci_type_t type; > - cdi_list_t (*find_devices)(struct hci *); > - void (*activate_device)(struct hci *, struct usb_device *); > - int (*do_packet)(struct hci *, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle); > - int (*get_frame)(struct hci *); > + cdi_list_t (*find_devices) (struct hci *); > + void (*activate_device) (struct hci *, struct usb_device *); > + int (*do_packet) (struct hci *, int frame, int type, int device, > + int endpoint, int low_speed, uintptr_t phys_data, > + int length, int datatoggle); > + int (*get_frame) (struct hci *); > }; > > -struct setup_packet > -{ > +struct setup_packet { > uint8_t request_type; > uint8_t request; > uint16_t value; > uint16_t index; > uint16_t length; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -struct device_desc > -{ > +struct device_desc { > uint8_t length; > uint8_t descriptor_type; > uint16_t bcdUSB; > @@ -174,10 +171,9 @@ struct device_desc > uint8_t iProduct; > uint8_t iSerialNumber; > uint8_t num_configurations; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -struct config_desc > -{ > +struct config_desc { > uint8_t length; > uint8_t descriptor_type; > uint16_t total_length; > @@ -186,10 +182,9 @@ struct config_desc > uint8_t iConfiguration; > uint8_t attributes; > uint8_t max_power; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -struct interface_desc > -{ > +struct interface_desc { > uint8_t length; > uint8_t descriptor_type; > uint8_t interface_number; > @@ -199,30 +194,27 @@ struct interface_desc > uint8_t interface_subclass; > uint8_t interface_protocol; > uint8_t iInterface; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -struct endpoint_desc > -{ > +struct endpoint_desc { > uint8_t length; > uint8_t descriptor_type; > uint8_t endpoint_address; > uint8_t attributes; > uint16_t max_packet_size; > uint8_t interval; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -struct hub_desc > -{ > +struct hub_desc { > uint8_t length; > uint8_t descriptor_type; > uint8_t nbr_ports; > uint16_t hub_characteristics; > uint8_t pwron2pwrgood; > uint8_t hub_contr_current; > -} __attribute__((packed)); > +} __attribute__ ((packed)); > > -typedef enum > -{ > +typedef enum { > USBDC_COMPOSITE = 0x00, > USBDC_AUDIO = 0x01, > USBDC_COMM_CDC = 0x02, > @@ -244,13 +236,11 @@ typedef enum > USBDC_VENDOR_SPEC = 0xFF > } usb_class_t; > > -struct class_data > -{ > +struct class_data { > usb_class_t type; > }; > > -struct usb_device > -{ > +struct usb_device { > struct hci *hci; > int id; > int port; > @@ -264,8 +254,7 @@ struct usb_device > int expects; > }; > > -struct msclass_data > -{ > +struct msclass_data { > struct class_data gen_class; > struct endpoint_desc *bulk_ep_in; > struct endpoint_desc *bulk_ep_out; > @@ -294,7 +283,7 @@ struct usb_packet { > #define HCI_STRUCT_SIZE sizeof(struct uhci) > > > -int usb_do_packet(struct usb_device *device, struct usb_packet* packet); > +int usb_do_packet(struct usb_device *device, struct usb_packet *packet); > void enumerate_hci(struct hci *); > struct cdi_driver *init_uhcd(void); > void init_msc_driver(void); > diff --git a/usb/main.c b/usb/main.c > index 9200613..51ef95b 100644 > --- a/usb/main.c > +++ b/usb/main.c > @@ -41,12 +41,13 @@ static int dprintf(const char *fmt, ...) > { > return 0; > } > + > #define _dprintf(fmt, args...) dprintf(fmt, ##args) > #endif > > static struct usb_device *devices[128]; > static cdi_list_t ehci, ohci, uhci; > -static void (*ccdriver_take_device[0x100])(struct usb_device *); > +static void (*ccdriver_take_device[0x100]) (struct usb_device *); > static int usb_dev_ids = 1; > > static void usb_init(void); > @@ -94,18 +95,18 @@ static const int next_data_type[9] = { > };*/ > > /** > -* Verarbeitet ein USB-Paket. > -* > -* @param device Das USB-Ger??t > -*/ > + * Verarbeitet ein USB-Paket. > + * > + * @param device Das USB-Ger??t > + */ > > -int usb_do_packet(struct usb_device *device, struct usb_packet* packet) > +int usb_do_packet(struct usb_device *device, struct usb_packet *packet) Bah. ;-) > { > - int error; > + int error = USB_NAK; > int tod_short; > > if (!(device->expects & packet->type_of_data)) { > - dprintf("Expected 0x%04X, got 0x%04X...?\n", device->expects, > + dprintf("0x%04X erwartet, 0x%04X bekommen...?\n", device->expects, > packet->type_of_data); > // FIXME Das ist keine elegante Loesung des Problems > for (;;); > @@ -118,18 +119,18 @@ int usb_do_packet(struct usb_device *device, struct usb_packet* packet) > device->expects = next_data_type[tod_short]; > packet->endpoint &= 0x07; > packet->type &= 0xFF; > - if (device->stalled) //TODO: Irgendwie entstallen, evtl.? oO > - { > + if (device->stalled) { > + //TODO: Irgendwie entstallen, evtl.? oO > dprintf("Zugriff auf stalled-Ger??t verweigert.\n"); > return USB_STALLED; > } > - error = device->hci->do_packet(device->hci, > - (device->hci->get_frame(device->hci) + 3) & 0x3FF, > - packet->type, device->id, packet->endpoint, > - device->low_speed, packet->phys_data, packet->length, > - packet->datatoggle); > - if (error == USB_STALLED) > - { > + while (error == USB_NAK) { > + error = device->hci->do_packet(device->hci, > + (device->hci->get_frame(device->hci) + 3) & 0x3FF, > + packet->type, device->id, packet->endpoint, device->low_speed, > + packet->phys_data, packet->length, packet->datatoggle); Don't know, maybe here a short sleep would be nice in case of failure? > + } > + if (error == USB_STALLED) { > printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", > packet->endpoint, device->id); > device->stalled = 1; > @@ -137,7 +138,8 @@ int usb_do_packet(struct usb_device *device, struct usb_packet* packet) > return error; > } > > -static void *do_control(struct usb_device *device, int direction, void *buffer, int length, int rtype, int recipient, int request, int value, int index) > +static void *do_control(struct usb_device *device, int direction, void *buffer, > + int length, int rtype, int recipient, int request, int value, int index) > { > void *rbuf; > int rval, no_data; > @@ -149,7 +151,7 @@ static void *do_control(struct usb_device *device, int direction, void *buffer, > return NULL; > } > if (cdi_alloc_phys_mem(sizeof(struct setup_packet), > - (void **)&setup, (void **)&psetup) == -1) { > + (void **)&setup, (void **)&psetup) == -1) { > return NULL; > } > direction &= 0x80; > @@ -163,12 +165,12 @@ static void *do_control(struct usb_device *device, int direction, void *buffer, > setup->length = length; > > struct usb_packet setup_packet = { > - .type = PACKET_SETUP, > - .endpoint = 0, > - .phys_data = psetup, > - .length = sizeof(*setup), > - .datatoggle = 0, > - .type_of_data = USB_TOD_SETUP, > + .type = PACKET_SETUP, > + .endpoint = 0, > + .phys_data = psetup, > + .length = sizeof(*setup), > + .datatoggle = 0, > + .type_of_data = USB_TOD_SETUP, *sigh* > }; > > > @@ -177,43 +179,38 @@ static void *do_control(struct usb_device *device, int direction, void *buffer, > } > > cdi_sleep_ms(20); > - if (no_data) > - { > + if (no_data) { > rval = USB_NO_ERROR; > - rbuf = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten > - } > - else > - { > + rbuf = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten > + } else { > struct usb_packet data_packet = { > - .type = (direction == DEV_TO_HOST ? > - PACKET_IN : PACKET_OUT), > - .endpoint = 0, > - .phys_data = pbuf, > - .length = length, > - .datatoggle = 1, > - .type_of_data = (direction == DEV_TO_HOST ? > - USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), > + .type = (direction == DEV_TO_HOST ? PACKET_IN : PACKET_OUT), > + .endpoint = 0, > + .phys_data = pbuf, > + .length = length, > + .datatoggle = 1, > + .type_of_data = (direction == DEV_TO_HOST ? > + USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), > }; > > rval = usb_do_packet(device, &data_packet); > } > > - if (rval == USB_NO_ERROR) > - { > + if (rval == USB_NO_ERROR) { > struct usb_packet ack_packet = { > - .endpoint = 0, > - .phys_data = 0, > - .length = 0, > - .datatoggle = 1, > + .endpoint = 0, > + .phys_data = 0, > + .length = 0, > + .datatoggle = 1, > }; > > cdi_sleep_ms(20); > > if (no_data || (direction == HOST_TO_DEV)) { > - ack_packet.type = PACKET_IN; > + ack_packet.type = PACKET_IN; > ack_packet.type_of_data = USB_TOD_SETUP_ACK_IN; > } else { > - ack_packet.type = PACKET_OUT; > + ack_packet.type = PACKET_OUT; > ack_packet.type_of_data = USB_TOD_SETUP_ACK_OUT; > } > > @@ -233,7 +230,8 @@ static void usb_init(void) > > init_msc_driver(); > > - memset(ccdriver_take_device, 0, sizeof(void (*)(struct usb_device *)) * 0x100); > + memset(ccdriver_take_device, 0, > + sizeof(void (*)(struct usb_device *)) * 0x100); > ccdriver_take_device[8] = ®ister_msd; > ccdriver_take_device[9] = &enumerate_hub; > > @@ -243,13 +241,11 @@ static void usb_init(void) > > cdi_list_t pci_devices = cdi_list_create(); > cdi_pci_get_all_devices(pci_devices); > - for (i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) > - { > + for (i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) { > if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03)) { > cdi_pci_device_destroy(dev); > } else { > - switch (dev->interface_id) > - { > + switch (dev->interface_id) { > case 0x00: > hci = malloc(HCI_STRUCT_SIZE); > hci->pcidev = dev; > @@ -273,14 +269,12 @@ static void usb_init(void) > > cdi_list_destroy(pci_devices); > > - dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n", cdi_list_size(ehci), > - cdi_list_size(ohci), > - cdi_list_size(uhci)); > + dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n", > + cdi_list_size(ehci), cdi_list_size(ohci), cdi_list_size(uhci)); > > uhcd = init_uhcd(); > > - for (i = 0; (hci = cdi_list_pop(uhci)) != NULL; i++) > - { > + for (i = 0; (hci = cdi_list_pop(uhci)) != NULL; i++) { > cdi_hci = malloc(sizeof(struct cdi_hci)); > cdi_hci->cdi_device.type = CDI_UNKNOWN; > dev_name = malloc(10); > @@ -313,23 +307,21 @@ static void enum_device(struct usb_device *usbdev) > usbdev->locked = 0; > usbdev->expects = USB_TOD_SETUP | USB_TOD_COMMAND; > dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), > - STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, > - DESC_DEVICE << 8, 0); > + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); > if (dev_desc == NULL) { > return; > } > usbdev->device = dev_desc; > dprintf("0x%04X:0x%04X (%i) -> ", dev_desc->vendor_id, dev_desc->device_id, > - dev_desc->class_id); > + dev_desc->class_id); > do_control(usbdev, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, REC_DEVICE, > - SET_ADDRESS, (id = usb_dev_ids++), 0); > + SET_ADDRESS, (id = usb_dev_ids++), 0); > usbdev->id = id; > _dprintf("%i\n", id); > - if (dev_desc->iManufacturer) > - { > + if (dev_desc->iManufacturer) { > name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, > - REC_DEVICE, GET_DESCRIPTOR, > - (DESC_STRING << 8) | dev_desc->iManufacturer, 0); > + REC_DEVICE, GET_DESCRIPTOR, > + (DESC_STRING << 8) | dev_desc->iManufacturer, 0); > if (name == NULL) { > return; > } > @@ -339,11 +331,10 @@ static void enum_device(struct usb_device *usbdev) > } > _dprintf("\n"); > } > - if (dev_desc->iProduct) > - { > + if (dev_desc->iProduct) { > name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, > - REC_DEVICE, GET_DESCRIPTOR, > - (DESC_STRING << 8) | dev_desc->iProduct, 0); > + REC_DEVICE, GET_DESCRIPTOR, > + (DESC_STRING << 8) | dev_desc->iProduct, 0); > if (name == NULL) { > return; > } > @@ -354,27 +345,25 @@ static void enum_device(struct usb_device *usbdev) > _dprintf("\n"); > } > devices[id] = usbdev; > - //TODO: Man kann doch nicht immer den ersten nehmen... > + //TODO: Man kann doch nicht immer den ersten nehmen... > conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, > - sizeof(struct config_desc), STD_REQUEST, REC_DEVICE, > - GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); > + sizeof(struct config_desc), STD_REQUEST, > + REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); > if (conf_desc == NULL) { > return; > } > conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, conf_desc->total_length, > - STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, > - DESC_CONFIGURATION << 8, 0); > + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); > if (conf_desc == NULL) { > return; > } > usbdev->config = conf_desc; > do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, > - SET_CONFIGURATION, conf_desc->config_value, 0); > - if (conf_desc->iConfiguration) > - { > + SET_CONFIGURATION, conf_desc->config_value, 0); > + if (conf_desc->iConfiguration) { > name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, > - REC_DEVICE, GET_DESCRIPTOR, > - (DESC_STRING << 8) | conf_desc->iConfiguration, 0); > + REC_DEVICE, GET_DESCRIPTOR, > + (DESC_STRING << 8) | conf_desc->iConfiguration, 0); > if (name == NULL) { > return; > } > @@ -386,25 +375,25 @@ static void enum_device(struct usb_device *usbdev) > } > position = conf_desc; > position += sizeof(struct config_desc); > - best_if = position; //Standard-IF > - for (i = 0; i < conf_desc->num_interfaces; i++) > - { > + best_if = position; //Standard-IF > + for (i = 0; i < conf_desc->num_interfaces; i++) { > if_desc = position; > - if (if_desc->interface_class == 8) //TODO: Mehr Klassencodes nat??rlich! > - { > + //TODO: Mehr Klassencodes nat??rlich! > + if (if_desc->interface_class == 8) { > best_if = if_desc; > break; > } > - position += sizeof(struct interface_desc) + if_desc->num_endpoints * sizeof(struct endpoint_desc); > + position += > + sizeof(struct interface_desc) + > + if_desc->num_endpoints * sizeof(struct endpoint_desc); > } > do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, > - SET_INTERFACE, best_if->interface_number, 0); > + SET_INTERFACE, best_if->interface_number, 0); > usbdev->interface = best_if; > - if (best_if->iInterface) > - { > + if (best_if->iInterface) { > name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, > - REC_DEVICE, GET_DESCRIPTOR, > - (DESC_STRING << 8) | best_if->iInterface, 0); > + REC_DEVICE, GET_DESCRIPTOR, > + (DESC_STRING << 8) | best_if->iInterface, 0); > if (name == NULL) { > return; > } > @@ -415,10 +404,9 @@ static void enum_device(struct usb_device *usbdev) > _dprintf(".\n"); > } > dprintf("Konfiguration: %i:%i (%i)\n", conf_desc->config_value, > - best_if->interface_number, > - best_if->interface_class); > + best_if->interface_number, best_if->interface_class); > if (ccdriver_take_device[best_if->interface_class] != NULL) { > - ccdriver_take_device[best_if->interface_class](usbdev); > + ccdriver_take_device[best_if->interface_class] (usbdev); > } > } > > @@ -432,22 +420,20 @@ static void enumerate_hub(struct usb_device *usbdev) > //Mehr als 8 Ports sollte es nicht geben, die Gr????e des Deskriptors wird > //daher wohl nicht gr????er als sizeof()+16 werden > hub_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*hub_desc) + 16, > - CLS_REQUEST, REC_DEVICE, GET_DESCRIPTOR, > - DESC_DEVICE << 8, 0); > - for (i = 0; i < hub_desc->nbr_ports; i++) > - { > + CLS_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); > + for (i = 0; i < hub_desc->nbr_ports; i++) { > port_status = do_control(usbdev, DEV_TO_HOST, NULL, 4, CLS_REQUEST, > - REC_OTHER, GET_STATUS, 0, i+1); > + REC_OTHER, GET_STATUS, 0, i + 1); > if (!(port_status[0] & PORT_DEVICE)) { > continue; > } > //Strom an > do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, > - SET_FEATURE, PORTF_POWER, i+1); > + SET_FEATURE, PORTF_POWER, i + 1); > cdi_sleep_ms(hub_desc->pwron2pwrgood * 2); > //Resetten > do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, > - SET_FEATURE, PORTF_RESET, i+1); > + SET_FEATURE, PORTF_RESET, i + 1); > //Reset sollte jetzt eigtl. schon beendet sein > //(do_control wartet ja 50 ms) > down = malloc(sizeof(struct usb_device)); > @@ -464,11 +450,9 @@ void enumerate_hci(struct hci *hci) > struct usb_device *usbdev; > cdi_list_t usb_devices; > > - if (hci->find_devices != NULL) > - { > + if (hci->find_devices != NULL) { > usb_devices = hci->find_devices(hci); > - while ((usbdev = cdi_list_pop(usb_devices)) != NULL) > - { > + while ((usbdev = cdi_list_pop(usb_devices)) != NULL) { > hci->activate_device(hci, usbdev); > cdi_sleep_ms(50); > enum_device(usbdev); > diff --git a/usb/msd.c b/usb/msd.c > index 0f9b79e..9614473 100644 > --- a/usb/msd.c > +++ b/usb/msd.c > @@ -48,6 +48,7 @@ static int dprintf(const char *fmt, ...) > { > return 0; > } > + > #define _dprintf(fmt, args...) dprintf(fmt, ##args) > #endif > > @@ -70,9 +71,12 @@ static void deinit_msd(struct cdi_device *cdi_msd); > static void get_partitions(struct cdi_storage_device *cdistrg); > static void init_msd(struct cdi_device *cdi_msd); > static void kill_driver(struct cdi_driver *cdi_msc_driver); > -static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer); > -static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer); > -static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count); > +static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, > + uint64_t count, void *buffer); > +static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, > + uint64_t count, void *buffer); > +static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, > + uint32_t *block_count); > > //TODO: CDI-Funktion w??re hier sicher n??tzlich... > #define CPU_IS_LITTLE_ENDIAN > @@ -125,10 +129,9 @@ void register_msd(struct usb_device *usbdev) > uint64_t size; > static int msdnum = 0; > > - if (usbdev->interface->interface_protocol != 0x50) > - { > - dprintf("Es werden nur bulk-only-Interfaces unterst??tzt.\n"); > - return; > + if (usbdev->interface->interface_protocol != 0x50) { > + dprintf("Es werden nur bulk-only-Interfaces unterst??tzt.\n"); > + return; > } > cdimsd = malloc(sizeof(struct cdi_msd)); > if (cdimsd == NULL) { > @@ -139,15 +142,13 @@ void register_msd(struct usb_device *usbdev) > strgdev = (struct cdi_storage_device *)cdimsd; > strgdev->dev.type = CDI_STORAGE; > strgdev->dev.name = malloc(10); > - if (strgdev->dev.name == NULL) > - { > + if (strgdev->dev.name == NULL) { > free(strgdev); > return; > } > sprintf((char *)strgdev->dev.name, "msd%i", msdnum++); > msc = malloc(sizeof(struct msclass_data)); > - if (msc == NULL) > - { > + if (msc == NULL) { > free((void *)strgdev->dev.name); > free(strgdev); > return; > @@ -156,38 +157,30 @@ void register_msd(struct usb_device *usbdev) > msc->bulk_ep_in = NULL; > msc->bulk_ep_out = NULL; > address = (void *)usbdev->interface + sizeof(struct interface_desc); > - for (i = 0; i < usbdev->interface->num_endpoints; i++) > - { > + for (i = 0; i < usbdev->interface->num_endpoints; i++) { > ep_desc = address; > - if ((ep_desc->endpoint_address & 0x80) && > - (ep_desc->attributes == 0x02) && > - (msc->bulk_ep_in == NULL)) { //BULK-IN > + if ((ep_desc->endpoint_address & 0x80) && (ep_desc->attributes == 0x02) && (msc->bulk_ep_in == NULL)) { //BULK-IN > msc->bulk_ep_in = ep_desc; > - } else if (!(ep_desc->endpoint_address & 0x80) && > - (ep_desc->attributes == 0x02) && > - (msc->bulk_ep_out == NULL)) { //BULK-OUT > + } else if (!(ep_desc->endpoint_address & 0x80) && (ep_desc->attributes == 0x02) && (msc->bulk_ep_out == NULL)) { //BULK-OUT > msc->bulk_ep_out = ep_desc; > } > address += sizeof(struct endpoint_desc); > } > - if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL)) > - { > + if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL)) { > dprintf("Nicht gen??gend Endpoints gefunden.\n"); > return; > } > - if (!msd_get_capacity(usbdev, &bs, &bc)) > - { > + if (!msd_get_capacity(usbdev, &bs, &bc)) { > strgdev->block_size = 0; > strgdev->block_count = 0; > dprintf("Konnte Gr????e f??r %s nicht ermitteln.\n", strgdev->dev.name); > - } > - else > - { > + } else { > strgdev->block_size = bs; > strgdev->block_count = bc; > size = bs; > size *= bc; > - dprintf("%s: %i * %i B (ca. %lld MB).\n", strgdev->dev.name, bc, bs, size >> 20); > + dprintf("%s: %i * %i B (ca. %lld MB).\n", strgdev->dev.name, bc, bs, > + size >> 20); > } > cdi_storage_device_init(strgdev); > cdi_list_push(cdi_driver.drv.devices, strgdev); > @@ -210,58 +203,81 @@ static void deinit_msd(struct cdi_device *cdi_msd) > //TODO: Und gerade hier... > } > > -static int write_cmd(struct usb_device* usbdev, uintptr_t src) > +static int write_cmd(struct usb_device *usbdev, uintptr_t src) > { > - struct msclass_data *msc = (struct msclass_data*) usbdev->classd; > + struct msclass_data *msc = (struct msclass_data *)usbdev->classd; > struct usb_packet cmd_packet = { > - .type = PACKET_OUT, > - .endpoint = msc->bulk_ep_out->endpoint_address, > - .phys_data = src, > - .length = 0x1F, > - .datatoggle = 0, > - .type_of_data = USB_TOD_COMMAND, > + .type = PACKET_OUT, > + .endpoint = msc->bulk_ep_out->endpoint_address, > + .phys_data = src, > + .length = 0x1F, > + .datatoggle = 0, > + .type_of_data = USB_TOD_COMMAND, > }; > > return usb_do_packet(usbdev, &cmd_packet); > } > > -static int read_status(struct usb_device* usbdev, uintptr_t dest) > +/** > + * Liest den Status von einem MSD > + * > + * @param usbdev Das bewusste Ger??t > + * @param expected_tag Das erwartete Tag > + * > + * @return Wenn der Status OK ist USB_NO_ERROR, sonst entsprechender Fehler > + */ > + > +static int read_status(struct usb_device *usbdev, uint32_t expected_tag) > { > - struct msclass_data *msc = (struct msclass_data*) usbdev->classd; > + uintptr_t pcsw; > + struct msclass_data *msc = (struct msclass_data *)usbdev->classd; > + struct command_status_wrapper *csw; > + int error; > + > + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), sizeof(*csw) would be shorter > + (void **)&csw, (void **)&pcsw) == -1) { > + return USB_TRIVIAL_ERROR; > + } > + > struct usb_packet status_packet = { > - .type = PACKET_IN, > - .endpoint = msc->bulk_ep_in->endpoint_address, > - .phys_data = dest, > - .length = 0x0D, > - .datatoggle = 0, > - .type_of_data = USB_TOD_STATUS, > + .type = PACKET_IN, > + .endpoint = msc->bulk_ep_in->endpoint_address, > + .phys_data = pcsw, > + .length = 0x0D, > + .datatoggle = 0, > + .type_of_data = USB_TOD_STATUS, > }; > > - return usb_do_packet(usbdev, &status_packet); > + error = usb_do_packet(usbdev, &status_packet); > + if (error != USB_NO_ERROR) { > + return error; > + } > + if ((csw->csw_signature != CSW_SIGNATURE) || > + (csw->csw_tag != expected_tag) || csw->csw_status) { > + return USB_CRC; //Watt wei?? denn ich Hm. Is this really the right value? > + } > + > + return USB_NO_ERROR; > } > > -static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uint32_t *block_count) > +static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, > + uint32_t *block_count) > { > struct msclass_data *msc; > struct command_block_wrapper *cbw; > - struct command_status_wrapper *csw; > struct msd_capacity *cap; > - uintptr_t pcbw, pcsw, pcap; > + uintptr_t pcbw, pcap; > uint32_t expected_tag; > > if ((usbdev == NULL) || (block_size == NULL) || (block_count == NULL)) { > return 0; > } > if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), > - (void **)&cbw, (void **)&pcbw) == -1) { > - return 0; > - } > - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), > - (void **)&csw, (void **)&pcsw) == -1) { > + (void **)&cbw, (void **)&pcbw) == -1) { > return 0; > } > if (cdi_alloc_phys_mem(sizeof(struct msd_capacity), > - (void **)&cap, (void **)&pcap) == -1) { > + (void **)&cap, (void **)&pcap) == -1) { > return 0; > } > msc = (struct msclass_data *)usbdev->classd; > @@ -269,42 +285,30 @@ static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uin > cbw->cbw_signature = CBW_SIGNATURE; > cbw->cbw_tag = (expected_tag = cbw_tag++); > cbw->cbw_data_transfer_length = sizeof(struct msd_capacity); > - cbw->cbw_flags = 0x80; //IN > - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? > + cbw->cbw_flags = 0x80; //IN > + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? > cbw->cbw_cb_length = 12; > cbw->cbw_cb[0] = MSC_CMD_CAPACITY; > - cbw->cbw_cb[1] = 0; //LUN: 0 > + cbw->cbw_cb[1] = 0; //LUN: 0 > > if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { > return 0; > } > > - cdi_sleep_ms(20); > - > struct usb_packet in_packet = { > - .type = PACKET_IN, > - .endpoint = msc->bulk_ep_in->endpoint_address, > - .phys_data = pcap, > - .length = sizeof(*cap), > - .datatoggle = 0, > - .type_of_data = USB_TOD_DATA_IN, > + .type = PACKET_IN, > + .endpoint = msc->bulk_ep_in->endpoint_address, > + .phys_data = pcap, > + .length = sizeof(*cap), > + .datatoggle = 0, > + .type_of_data = USB_TOD_DATA_IN, > }; > > if (usb_do_packet(usbdev, &in_packet) != USB_NO_ERROR) { > return 0; > } > > - cdi_sleep_ms(5); > - > - if (read_status(usbdev, pcsw) != USB_NO_ERROR) { > - return 0; > - } > - > - cdi_sleep_ms(5); > - > - if ((csw->csw_signature != CSW_SIGNATURE) || > - (csw->csw_tag != expected_tag) || > - csw->csw_status) { > + if (read_status(usbdev, expected_tag) != USB_NO_ERROR) { > return 0; > } > > @@ -317,46 +321,33 @@ static int msd_get_capacity(struct usb_device *usbdev, uint32_t *block_size, uin > #ifdef WAIT_FOR_MSD_READY > static int msd_ready(struct usb_device *usbdev) > { > - struct msclass_data *msc; > - struct command_block_wrapper *cbw; > - struct command_status_wrapper *csw; > - uintptr_t pcbw, pcsw; > - uint32_t expected_tag; > - > - if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), > - (void **)&cbw, (void **)&pcbw) == -1) { > - return 0; > - } > - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), > - (void **)&csw, (void **)&pcsw) == -1) { > - return 0; > - } > - msc = (struct msclass_data *)usbdev->classd; > - memset(cbw, 0, 0x1F); > - cbw->cbw_signature = CBW_SIGNATURE; > - cbw->cbw_tag = (expected_tag = cbw_tag++); > - cbw->cbw_data_transfer_length = 0; > - cbw->cbw_flags = 0x80; //IN > - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? > - cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" > - > - if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { > - return 0; > - } > - > - cdi_sleep_ms(7); > - if (read_status(usbdev, pcsw) != USB_NO_ERROR) { > - return 0; > - } > - > - cdi_sleep_ms(4); > - if ((csw->csw_signature != CSW_SIGNATURE) || > - (csw->csw_tag != expected_tag) || > - csw->csw_status) { > - return 0; > - } > - > - return 1; > + struct msclass_data *msc; > + struct command_block_wrapper *cbw; > + uintptr_t pcbw; > + uint32_t expected_tag; > + > + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), > + (void **)&cbw, (void **)&pcbw) == -1) { > + return 0; > + } > + msc = (struct msclass_data *)usbdev->classd; > + memset(cbw, 0, 0x1F); > + cbw->cbw_signature = CBW_SIGNATURE; > + cbw->cbw_tag = (expected_tag = cbw_tag++); > + cbw->cbw_data_transfer_length = 0; > + cbw->cbw_flags = 0x80; //IN > + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? > + cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" > + > + if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { > + return 0; > + } > + > + if (read_status(usbdev, expected_tag) != USB_NO_ERROR) { > + return 0; > + } > + > + return 1; > } > #endif > > @@ -368,9 +359,11 @@ static inline int tsl(volatile int *variable) > return rval; > } > > -static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length); > +static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, > + uint16_t sectors, uintptr_t phys_buffer, size_t length); > > -static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer) > +static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, > + uint64_t count, void *buffer) > { > void *vb; > uintptr_t pb; > @@ -383,57 +376,48 @@ static int msd_cdi_read(struct cdi_storage_device *strgdev, uint64_t start, uint > > fdprintf("read(%i, %i)\n", (int)start, (int)count); > start += ((struct cdi_msd *)strgdev)->offset; > - if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) > - { > + if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) { > dprintf("Blockspeicher konnte nicht allociert werden.\n"); > return -1; > } > - if (!count) > - { > + if (!count) { > dprintf("Leere Leseanfrage.\n"); > return 0; > } > - while (tsl(&usbdev->locked)) > - { > + while (tsl(&usbdev->locked)) { > #ifndef CDI_STANDALONE > - __asm__ __volatile__ ("hlt"); > + __asm__ __volatile__("hlt"); > #endif > } > - for (i = 0; i < count; i++) > - { > + for (i = 0; i < count; i++) { > #ifdef WAIT_FOR_MSD_READY > for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { > cdi_sleep_ms(20); > } > #endif > error = msd_read(usbdev, start + i, 1, pb, bs); > - if (error != USB_NO_ERROR) > - { > + if (error != USB_NO_ERROR) { > dprintf("Lesefehler 0x%X bei Block %i.\n", error, i); > usbdev->locked = 0; > return -1; > } > - memcpy(buffer + i*bs, vb, bs); > + memcpy(buffer + i * bs, vb, bs); > } > usbdev->locked = 0; > return 0; > } > > -static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) > +static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, > + uint16_t sectors, uintptr_t phys_buffer, size_t length) > { > struct msclass_data *msc; > struct command_block_wrapper *cbw; > - struct command_status_wrapper *csw; > - uintptr_t pcbw, pcsw; > + uintptr_t pcbw; > uint32_t expected_tag; > int error; > > if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), > - (void **)&cbw, (void **)&pcbw) == -1) { > - return USB_TRIVIAL_ERROR; > - } > - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), > - (void **)&csw, (void **)&pcsw) == -1) { > + (void **)&cbw, (void **)&pcbw) == -1) { > return USB_TRIVIAL_ERROR; > } > if (!phys_buffer || !length) { > @@ -444,61 +428,50 @@ static uint32_t msd_read(struct usb_device *usbdev, uint32_t lba, uint16_t secto > cbw->cbw_signature = CBW_SIGNATURE; > cbw->cbw_tag = (expected_tag = cbw_tag++); > cbw->cbw_data_transfer_length = length; > - cbw->cbw_flags = 0x80; //IN > - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? > + cbw->cbw_flags = 0x80; //IN > + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? > cbw->cbw_cb_length = 12; > cbw->cbw_cb[0] = MSC_CMD_READ10; > - cbw->cbw_cb[1] = 0; //LUN: 0 > + cbw->cbw_cb[1] = 0; //LUN: 0 > cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; > cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; > cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; > - cbw->cbw_cb[5] = lba & 0x000000FF; > + cbw->cbw_cb[5] = lba & 0x000000FF; > cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; > - cbw->cbw_cb[8] = sectors & 0x000000FF; > + cbw->cbw_cb[8] = sectors & 0x000000FF; > > error = write_cmd(usbdev, pcbw); > if (error != USB_NO_ERROR) { > return error; > } > - cdi_sleep_ms(20); > > struct usb_packet in_packet = { > - .type = PACKET_IN, > - .endpoint = msc->bulk_ep_in->endpoint_address, > - .phys_data = phys_buffer, > - .length = length, > - .datatoggle = 0, > - .type_of_data = USB_TOD_DATA_IN, > + .type = PACKET_IN, > + .endpoint = msc->bulk_ep_in->endpoint_address, > + .phys_data = phys_buffer, > + .length = length, > + .datatoggle = 0, > + .type_of_data = USB_TOD_DATA_IN, > }; > > error = usb_do_packet(usbdev, &in_packet); > if (error != USB_NO_ERROR) { > return error; > } > - cdi_sleep_ms(5); > > - error = read_status(usbdev, pcsw); > + error = read_status(usbdev, expected_tag); > if (error != USB_NO_ERROR) { > return error; > } > > - cdi_sleep_ms(4); > - if ((csw->csw_signature != CSW_SIGNATURE) || > - (csw->csw_tag != expected_tag) || > - csw->csw_status) > - { > - dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature, > - csw->csw_tag, > - expected_tag, > - csw->csw_status); > - return USB_CRC; //Watt wei?? denn ich... > - } > return USB_NO_ERROR; > } > > -static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length); > +static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, > + uint16_t sectors, uintptr_t phys_buffer, size_t length); > > -static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uint64_t count, void *buffer) > +static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, > + uint64_t count, void *buffer) > { > void *vb; > uintptr_t pb; > @@ -511,28 +484,24 @@ static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uin > > fdprintf("write(%i, %i)\n", (int)start, (int)count); > start += ((struct cdi_msd *)strgdev)->offset; > - if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) > - { > + if (cdi_alloc_phys_mem(bs, &vb, (void **)&pb) == -1) { > dprintf("Blockspeicher zum Schreiben konnte nicht allociert werden.\n"); > return -1; > } > - while (tsl(&usbdev->locked)) > - { > + while (tsl(&usbdev->locked)) { > #ifndef CDI_STANDALONE > - __asm__ __volatile__ ("hlt"); > + __asm__ __volatile__("hlt"); > #endif > } > - for (i = 0; i < count; i++) > - { > - memcpy(vb, buffer + i*bs, bs); > + for (i = 0; i < count; i++) { > + memcpy(vb, buffer + i * bs, bs); > #ifdef WAIT_FOR_MSD_READY > for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { > cdi_sleep_ms(20); > } > #endif > error = msd_write(usbdev, start + i, 1, pb, bs); > - if (error != USB_NO_ERROR) > - { > + if (error != USB_NO_ERROR) { > dprintf("Schreibfehler 0x%X bei Block %i.\n", error, i); > usbdev->locked = 0; > return -1; > @@ -542,7 +511,8 @@ static int msd_cdi_write(struct cdi_storage_device *strgdev, uint64_t start, uin > return 0; > } > > -static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) > +static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, > + uint16_t sectors, uintptr_t phys_buffer, size_t length) > { > struct msclass_data *msc; > struct command_block_wrapper *cbw; > @@ -552,11 +522,11 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect > int error; > > if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), > - (void **)&cbw, (void **)&pcbw) == -1) { > + (void **)&cbw, (void **)&pcbw) == -1) { > return USB_TRIVIAL_ERROR; > } > if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), > - (void **)&csw, (void **)&pcsw) == -1) { > + (void **)&csw, (void **)&pcsw) == -1) { > return USB_TRIVIAL_ERROR; > } > if (!phys_buffer || !length) { > @@ -567,31 +537,30 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect > cbw->cbw_signature = CBW_SIGNATURE; > cbw->cbw_tag = (expected_tag = cbw_tag++); > cbw->cbw_data_transfer_length = length; > - cbw->cbw_flags = 0x00; //OUT > - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? > + cbw->cbw_flags = 0x00; //OUT > + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? > cbw->cbw_cb_length = 12; > cbw->cbw_cb[0] = MSC_CMD_WRITE10; > - cbw->cbw_cb[1] = 0; //LUN: 0 > + cbw->cbw_cb[1] = 0; //LUN: 0 > cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; > cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; > cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; > - cbw->cbw_cb[5] = lba & 0x000000FF; > + cbw->cbw_cb[5] = lba & 0x000000FF; > cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; > - cbw->cbw_cb[8] = sectors & 0x000000FF; > + cbw->cbw_cb[8] = sectors & 0x000000FF; > > error = write_cmd(usbdev, pcbw); > if (error != USB_NO_ERROR) { > return error; > } > - cdi_sleep_ms(20); > > struct usb_packet out_packet = { > - .type = PACKET_OUT, > - .endpoint = msc->bulk_ep_out->endpoint_address, > - .phys_data = phys_buffer, > - .length = length, > - .datatoggle = 0, > - .type_of_data = USB_TOD_DATA_OUT, > + .type = PACKET_OUT, > + .endpoint = msc->bulk_ep_out->endpoint_address, > + .phys_data = phys_buffer, > + .length = length, > + .datatoggle = 0, > + .type_of_data = USB_TOD_DATA_OUT, > }; > > error = usb_do_packet(usbdev, &out_packet); > @@ -599,23 +568,11 @@ static uint32_t msd_write(struct usb_device *usbdev, uint32_t lba, uint16_t sect > return error; > } > > - cdi_sleep_ms(5); > - error = read_status(usbdev, pcsw); > + error = read_status(usbdev, expected_tag); > if (error != USB_NO_ERROR) { > return error; > } > > - cdi_sleep_ms(4); > - if ((csw->csw_signature != CSW_SIGNATURE) || > - (csw->csw_tag != expected_tag) || > - csw->csw_status) > - { > - dprintf("0x%08X 0x%08X==0x%08X 0x%02X\n", csw->csw_signature, > - csw->csw_tag, > - expected_tag, > - csw->csw_status); > - return USB_CRC; > - } > return USB_NO_ERROR; > } > > @@ -631,15 +588,15 @@ static void get_partitions(struct cdi_storage_device *cdistrg) > dprintf("MBR konnte nicht eingelesen werden.\n"); > return; > } > - if (((uint16_t *)mbr)[255] != 0xAA55) { > + if (((uint16_t *) mbr)[255] != 0xAA55) { > return; > } > partition = mbr + 0x1BE; > dprintf("Partitionen auf %s:", cdistrg->dev.name); > - for (i = 0; i < 4; i++) > - { > - if (partition[i].type && partition[i].size && (partition[i].start + partition[i].size < cdistrg->block_count)) > - { > + for (i = 0; i < 4; i++) { > + if (partition[i].type && partition[i].size > + && (partition[i].start + partition[i].size < > + cdistrg->block_count)) { > cdimsd = malloc(sizeof(struct cdi_msd)); > if (cdimsd == NULL) { > break; > @@ -648,12 +605,12 @@ static void get_partitions(struct cdi_storage_device *cdistrg) > cdimsd->usb_device = base_cdimsd->usb_device; > cdimsd->cdi_device.dev.type = CDI_STORAGE; > cdimsd->cdi_device.dev.name = malloc(14); > - if (cdimsd->cdi_device.dev.name == NULL) > - { > + if (cdimsd->cdi_device.dev.name == NULL) { > free(cdimsd); > break; > } > - sprintf((char *)cdimsd->cdi_device.dev.name, "%sp%i", cdistrg->dev.name, j++); > + sprintf((char *)cdimsd->cdi_device.dev.name, "%sp%i", > + cdistrg->dev.name, j++); > cdimsd->cdi_device.block_size = base_cdimsd->cdi_device.block_size; > cdimsd->cdi_device.block_count = partition[i].size; > _dprintf(" %s", cdimsd->cdi_device.dev.name); > diff --git a/usb/uhci.c b/usb/uhci.c > index 799d526..6cdc44c 100644 > --- a/usb/uhci.c > +++ b/usb/uhci.c > @@ -43,6 +43,7 @@ static int dprintf(const char *fmt, ...) > { > return 0; > } > + > #define _dprintf(fmt, args...) dprintf(fmt, ##args) > #endif > > @@ -52,7 +53,9 @@ static struct cdi_driver cdi_driver; > static cdi_list_t active_transfers; > > static void uhci_handler(struct cdi_device *dev); > -static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle); > +static int enqueue_request(struct hci *gen_hci, int frame, int type, > + int device, int endpoint, int low_speed, > + uintptr_t phys_data, int length, int datatoggle); > static cdi_list_t get_devices(struct hci *gen_hci); > static void activate_device(struct hci *gen_hci, struct usb_device *device); > static int get_current_frame(struct hci *gen_hci); > @@ -75,14 +78,13 @@ struct cdi_driver *init_uhcd() > > static void uhci_kill(struct cdi_driver *cdi_hcd) > { > - //TODO: Da geht doch noch was > + //TODO: Da geht doch noch was > } > > static void uhci_deinit(struct cdi_device *cdi_hci) > { > - cdi_outw(((struct uhci *)((struct cdi_hci *)cdi_hci)->hci)->pbase + UHCI_USBCMD, > - MAXP); //HC anhalten > - //TODO: Hier doch auch > + cdi_outw(((struct uhci *)((struct cdi_hci *)cdi_hci)->hci)->pbase + UHCI_USBCMD, MAXP); //HC anhalten > + //TODO: Hier doch auch > } > > void uhci_init(struct cdi_device *cdi_hci) > @@ -95,13 +97,10 @@ void uhci_init(struct cdi_device *cdi_hci) > //cdi_pci_alloc_ioports(gen_hci->pcidev); > uhci->pbase = 0; > for (i = 0; (res = cdi_list_get(gen_hci->pcidev->resources, i)) != NULL; > - i++) > - { > - if (res->type == CDI_PCI_IOPORTS) > - { > + i++) { > + if (res->type == CDI_PCI_IOPORTS) { > size = res->length ? res->length : 0x14; > - if (cdi_ioports_alloc(res->start, size) == -1) > - { > + if (cdi_ioports_alloc(res->start, size) == -1) { > dprintf("I/O-Ports konnten nicht reserviert werden.\n"); > return; > } > @@ -109,14 +108,12 @@ void uhci_init(struct cdi_device *cdi_hci) > break; > } > } > - if (!uhci->pbase) > - { > + if (!uhci->pbase) { > dprintf("I/O-Basis nicht gefunden!\n"); > return; > } > if (cdi_alloc_phys_mem(4096, (void **)&uhci->frame_list, > - (void **)&uhci->phys_frame_list) == -1) > - { > + (void **)&uhci->phys_frame_list) == -1) { > dprintf("Frame List konnte nicht allociert werden!\n"); > return; > } > @@ -125,9 +122,10 @@ void uhci_init(struct cdi_device *cdi_hci) > if (uhci->root_ports > 7) { //Laut Linuxkernel ist das so "weird", dass da was nicht stimmen kann... > uhci->root_ports = 2; > } > - dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase, uhci->root_ports, gen_hci->pcidev->irq); > + dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase, > + uhci->root_ports, gen_hci->pcidev->irq); > for (i = 0; i < 1024; i++) { > - uhci->frame_list[i] = 1; //Invalid > + uhci->frame_list[i] = 1; //Invalid > } > dprintf("Resetten...\n"); > //HC zur??cksetzen > @@ -144,10 +142,10 @@ void uhci_init(struct cdi_device *cdi_hci) > cdi_outw(uhci->pbase + UHCI_USBINTR, 0xF); > //Framelistadresse eintragen > cdi_outl(uhci->pbase + UHCI_FRBASEADD, uhci->phys_frame_list); > - cdi_outw(uhci->pbase + UHCI_FRNUM, 0); //Frame zur??cksetzen > + cdi_outw(uhci->pbase + UHCI_FRNUM, 0); //Frame zur??cksetzen > //Ports abschalten, damit selektiv eingeschaltet werden kann (Enumeration) > for (i = 0; i < uhci->root_ports; i++) { > - cdi_outw(uhci->pbase + UHCI_RPORTS + i*2, RPORT_CSC); > + cdi_outw(uhci->pbase + UHCI_RPORTS + i * 2, RPORT_CSC); > } > dprintf(" Fertig\n"); > gen_hci->find_devices = &get_devices; > @@ -167,10 +165,8 @@ static cdi_list_t get_devices(struct hci *gen_hci) > > dprintf("Ger??te finden: "); > cdi_outw(uhci->pbase + UHCI_USBCMD, USB_RUN); > - for (i = 0; i < uhci->root_ports; i++) > - { > - if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_DEVICE)) > - { > + for (i = 0; i < uhci->root_ports; i++) { > + if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & RPORT_DEVICE)) { > _dprintf("-"); > continue; > } > @@ -180,7 +176,7 @@ static cdi_list_t get_devices(struct hci *gen_hci) > dev->id = 0; > dev->port = i; > dev->low_speed = > - (cdi_inw(uhci->pbase + UHCI_RPORTS + 2*i) & RPORT_LOSPD) && 1; > + (cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & RPORT_LOSPD) && 1; > cdi_list_push(dlist, dev); > } > _dprintf("\n"); > @@ -193,7 +189,7 @@ static void activate_device(struct hci *gen_hci, struct usb_device *device) > > dprintf("Ger??t an Port %i wird aktiviert.\n", device->port); > cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, > - RPORT_RESET | RPORT_ENABLE | RPORT_CSC); > + RPORT_RESET | RPORT_ENABLE | RPORT_CSC); > cdi_sleep_ms(20); > } > > @@ -209,34 +205,37 @@ static inline int tsl(volatile int *variable) > *variable = 1; > return rval; > } > + > static volatile int locked = 0; > > -static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, int endpoint, int low_speed, uintptr_t phys_data, int length, int datatoggle) > +static int enqueue_request(struct hci *gen_hci, int frame, int type, > + int device, int endpoint, int low_speed, > + uintptr_t phys_data, int length, int datatoggle) > { > struct uhci *uhci = (struct uhci *)gen_hci; > struct uhci_td *td; > struct uhci_qh *qh; > uintptr_t ptd, pqh; > struct transfer *addr; > + int timeout; > > if (cdi_alloc_phys_mem(sizeof(struct uhci_td), > - (void **)&td, (void **)&ptd) == -1) { > + (void **)&td, (void **)&ptd) == -1) { > return USB_TRIVIAL_ERROR; > } > if (cdi_alloc_phys_mem(sizeof(struct uhci_qh), > - (void **)&qh, (void **)&pqh) == -1) { > + (void **)&qh, (void **)&pqh) == -1) { > return USB_TRIVIAL_ERROR; > } > - while (tsl(&locked)) > - { > + while (tsl(&locked)) { > #ifndef CDI_STANDALONE > - __asm__ __volatile__ ("hlt"); > + __asm__ __volatile__("hlt"); > #endif > } > - qh->next = 1; //Invalid > + qh->next = 1; //Invalid > qh->transfer = ptd; > memset(td, 0, sizeof(struct uhci_td)); > - td->next = 1; //Invalid > + td->next = 1; //Invalid > td->active = 1; > td->ioc = 1; > td->data_toggle = datatoggle; > @@ -245,7 +244,7 @@ static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, > td->pid = type; > td->device = device; > td->endpoint = endpoint; > - td->maxlen = length ? length-1 : 0x7FF; > + td->maxlen = length ? length - 1 : 0x7FF; > td->buffer = phys_data; > addr = malloc(sizeof(struct transfer)); > addr->virt = td; > @@ -253,20 +252,12 @@ static int enqueue_request(struct hci *gen_hci, int frame, int type, int device, > addr->error = 0xFFFF; > cdi_list_push(active_transfers, addr); > uhci->frame_list[frame] = pqh | 2; > -#ifdef CDI_STANDALONE > - /*int cframe = get_current_frame(gen_hci), nframe; > - nframe = (cframe > frame) ? frame + 1024 : frame; //??berl??ufe korrigieren > - //Ann??hern, wann er wohl fertig ist > - cdi_sleep_ms(nframe + 30 - get_current_frame(gen_hci));*/ > -#else > - while (!(qh->transfer & 1)) { > - __asm__ __volatile__ ("hlt"); > + for (timeout = 0; !(qh->transfer & 1) && (timeout < 1000); timeout++) { > + cdi_sleep_ms(1); > } > -#endif > - while (addr->error == 0xFFFF) > - { > + while ((timeout < 1000) && (addr->error == 0xFFFF)) { > #ifndef CDI_STANDALONE > - __asm__ __volatile__ ("hlt"); > + __asm__ __volatile__("hlt"); > #endif > } > uhci->frame_list[frame] = 1; > @@ -281,54 +272,44 @@ static void uhci_handler(struct cdi_device *cdi_hci) > struct transfer *addr; > struct uhci_td *td; > > - if (!status) { //Also, von hier kommt der IRQ nicht. > + if (!status) { //Also, von hier kommt der IRQ nicht. > return; > } > if (status & ~0x0001) { > dprintf("Unerwarteter IRQ 0x%04X von %s\n", status, cdi_hci->name); > } > - if (status & 0x10) > - { > + if (status & 0x10) { > printf("[uhci] SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n"); > - cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! > - } > - else > - { > - for (i = 0; (addr = cdi_list_get(active_transfers, i)) != NULL; i++) > - { > + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! > + } else { > + for (i = 0; (addr = cdi_list_get(active_transfers, i)) != NULL; i++) { > td = addr->virt; > if (td->active) { > continue; > } > addr->error = USB_NO_ERROR; > cdi_list_remove(active_transfers, i--); > - if (td->stalled_err) > - { > + if (td->stalled_err) { > dprintf("ENDPOINT STALLED\n"); > addr->error |= USB_STALLED; > } > - if (td->buf_err) > - { > + if (td->buf_err) { > dprintf("Puffer??berlauf oder Underrun\n"); > addr->error |= USB_BUFFER_ERROR; > } > - if (td->babble) > - { > - dprintf("Puh, da war ein Ger??t wohl sehr gespr??chig: Babble.\n"); > + if (td->babble) { > + dprintf("Da war ein Ger??t wohl sehr gespr??chig: Babble.\n"); > addr->error |= USB_BABBLE; > } > - if (td->nak) > - { > + if (td->nak) { > dprintf("NAK empfangen\n"); > addr->error |= USB_NAK; > } > - if (td->crc_time_err) > - { > + if (td->crc_time_err) { > dprintf("CRC-Fehler oder Timeout\n"); > addr->error |= USB_CRC | USB_TIMEOUT; > } > - if (td->bitstuff_err) > - { > + if (td->bitstuff_err) { > dprintf("Bitstufffehler\n"); > addr->error |= USB_BITSTUFF; > } Most likely there are some more points. This patch is just unreadable and I can't be bothered reviewing each patch hunk if anything other than style is changed. From max at tyndur.org Sat Aug 22 20:08:12 2009 From: max at tyndur.org (Max Reitz) Date: Sat, 22 Aug 2009 20:08:12 +0200 Subject: [cdi-devel] [PATCH] Reformatting of the reformatting Message-ID: <1250964492-24196-1-git-send-email-max@tyndur.org> Signed-off-by: Max Reitz --- usb/include/msd.h | 2 +- usb/include/uhci.h | 4 +- usb/include/usb.h | 2 +- usb/main.c | 202 +++++++++++++++---------------- usb/msd.c | 336 +++++++++++++++++++++++++--------------------------- usb/uhci.c | 50 ++------ 6 files changed, 274 insertions(+), 322 deletions(-) diff --git a/usb/include/msd.h b/usb/include/msd.h index 9f40469..1af55f3 100644 --- a/usb/include/msd.h +++ b/usb/include/msd.h @@ -95,4 +95,4 @@ struct part_table_entry { #define MSC_CMD_READ12 0xA8 #define MSC_CMD_WRITE12 0xAA -#endif +#endif \ No newline at end of file diff --git a/usb/include/uhci.h b/usb/include/uhci.h index 390298d..edb98db 100644 --- a/usb/include/uhci.h +++ b/usb/include/uhci.h @@ -35,7 +35,7 @@ #define UHCI_USBSTS 0x02 //w #define UHCI_USBINTR 0x04 //w #define UHCI_FRNUM 0x06 //w -#define UHCI_FRBASEADD 0x08 //l +#define UHCI_FRBASEADD 0x08 //l #define UHCI_SOFMOD 0x0C //b #define UHCI_RPORTS 0x10 #define UHCI_PORTSC1 0x10 //w @@ -103,4 +103,4 @@ struct transfer { volatile int error; }; -#endif +#endif \ No newline at end of file diff --git a/usb/include/usb.h b/usb/include/usb.h index 223ecf7..3536f5a 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -290,4 +290,4 @@ void init_msc_driver(void); void register_msd(struct usb_device* usbdev); void uhci_init(struct cdi_device* cdi_hci); -#endif +#endif \ No newline at end of file diff --git a/usb/main.c b/usb/main.c index 02079e2..0cca9b7 100644 --- a/usb/main.c +++ b/usb/main.c @@ -34,7 +34,7 @@ #ifdef DEBUG #include -#define dprintf(fmt, args...) printf("[usb] " fmt, ## args) +#define dprintf(fmt, args...) printf("[usb] " fmt, ## args) #define _dprintf(fmt, args...) printf(fmt, ## args) #else static int dprintf(const char* fmt, ...) @@ -77,8 +77,8 @@ static const int next_data_type[9] = { USB_TOD_SETUP | USB_TOD_COMMAND, USB_TOD_SETUP | USB_TOD_COMMAND, USB_TOD_DATA_IN | USB_TOD_DATA_OUT | USB_TOD_STATUS, - USB_TOD_STATUS, - USB_TOD_STATUS, + USB_TOD_STATUS | USB_TOD_DATA_IN, + USB_TOD_STATUS | USB_TOD_DATA_OUT, USB_TOD_SETUP | USB_TOD_COMMAND }; @@ -95,30 +95,26 @@ static const int next_data_type[9] = { };*/ /** - * Verarbeitet ein USB-Paket. - * - * @param device Das USB-Ger??t - */ + * Verarbeitet ein USB-Paket. + * + * @param device Das USB-Ger??t + */ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) { int error = USB_NAK; - int tod_short; + int tod_short, tod = packet->type_of_data; - if (!(device->expects & packet->type_of_data)) { - dprintf("0x%04X erwartet, 0x%04X bekommen...?\n", device->expects, - packet->type_of_data); + if (!(device->expects & tod)) { + dprintf("0x%04X erwartet, 0x%04X bekommen...?\n", device->expects, tod); // FIXME Das ist keine elegante Loesung des Problems for (;;) { - ; } } - - packet->type_of_data >>= 1; - for (tod_short = 0; packet->type_of_data; tod_short++) { - packet->type_of_data >>= 1; + tod >>= 1; + for (tod_short = 0; tod; tod_short++) { + tod >>= 1; } - //dprintf("TOD: %s\n", tod_name[tod_short]); device->expects = next_data_type[tod_short]; packet->endpoint &= 0x07; @@ -128,20 +124,20 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) dprintf("Zugriff auf stalled-Ger??t verweigert.\n"); return USB_STALLED; } - while (error == USB_NAK) { - error = device->hci->do_packet(device->hci, - (device->hci->get_frame(device->hci) + 3) & 0x3FF, - packet->type, device->id, packet->endpoint, device->low_speed, - packet->phys_data, packet->length, packet->datatoggle); + error = + device->hci->do_packet(device->hci, + (device->hci->get_frame( + device->hci) + 3) & 0x3FF, packet->type, device->id, + packet->endpoint, + device->low_speed, packet->phys_data, packet->length, + packet->datatoggle); } - if (error == USB_STALLED) { - printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", - packet->endpoint, device->id); + printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", packet->endpoint, + device->id); device->stalled = 1; } - return error; } @@ -160,18 +156,15 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, { return NULL; } - - if (cdi_alloc_phys_mem(sizeof(struct setup_packet), - (void**) &setup, (void**) &psetup) == -1) + if (cdi_alloc_phys_mem(sizeof(struct setup_packet), (void**) &setup, + (void**) &psetup) == -1) { return NULL; } - direction &= 0x80; if ((direction == HOST_TO_DEV) && length && (buffer != NULL)) { memcpy(rbuf, buffer, length); } - setup->request_type = direction | (rtype & 0x60) | (recipient & 0x1F); setup->request = request; setup->value = value; @@ -179,12 +172,12 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, setup->length = length; struct usb_packet setup_packet = { - .type = PACKET_SETUP, - .endpoint = 0, - .phys_data = psetup, - .length = sizeof(*setup), - .datatoggle = 0, - .type_of_data = USB_TOD_SETUP, + .type = PACKET_SETUP, + .endpoint = 0, + .phys_data = psetup, + .length = sizeof(*setup), + .datatoggle = 0, + .type_of_data = USB_TOD_SETUP, }; @@ -192,20 +185,19 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, return NULL; } - cdi_sleep_ms(20); if (no_data) { rval = USB_NO_ERROR; rbuf = (void*) 0xFFFFFFFF; //Kein Fehler, aber auch keine Daten } else { struct usb_packet data_packet = { - .type = - (direction == DEV_TO_HOST ? PACKET_IN : PACKET_OUT), - .endpoint = 0, - .phys_data = pbuf, - .length = length, - .datatoggle = 1, - .type_of_data = (direction == DEV_TO_HOST ? - USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), + .type = (direction == DEV_TO_HOST ? PACKET_IN : PACKET_OUT), + .endpoint = 0, + .phys_data = pbuf, + .length = length, + .datatoggle = 1, + .type_of_data = + (direction == + DEV_TO_HOST ? USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), }; rval = usb_do_packet(device, &data_packet); @@ -219,8 +211,6 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, .datatoggle = 1, }; - cdi_sleep_ms(20); - if (no_data || (direction == HOST_TO_DEV)) { ack_packet.type = PACKET_IN; ack_packet.type_of_data = USB_TOD_SETUP_ACK_IN; @@ -231,7 +221,6 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, rval = usb_do_packet(device, &ack_packet); } - return (rval == USB_NO_ERROR) ? rbuf : NULL; } @@ -246,8 +235,8 @@ static void usb_init(void) init_msc_driver(); - memset(ccdriver_take_device, 0, - sizeof(void(*) (struct usb_device*)) * 0x100); + memset(ccdriver_take_device, 0, sizeof(void(*) ( + struct usb_device*)) * 0x100); ccdriver_take_device[8] = ®ister_msd; ccdriver_take_device[9] = &enumerate_hub; @@ -268,14 +257,12 @@ static void usb_init(void) hci->type = HCI_UHCI; cdi_list_push(uhci, hci); break; - case 0x10: hci = malloc(HCI_STRUCT_SIZE); hci->pcidev = dev; hci->type = HCI_OHCI; cdi_list_push(ohci, hci); break; - case 0x20: hci = malloc(HCI_STRUCT_SIZE); hci->pcidev = dev; @@ -287,8 +274,8 @@ static void usb_init(void) cdi_list_destroy(pci_devices); - dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n", - cdi_list_size(ehci), cdi_list_size(ohci), cdi_list_size(uhci)); + dprintf("%i EHCIs, %i OHCIs und %i UHCIs gefunden.\n", cdi_list_size( + ehci), cdi_list_size(ohci), cdi_list_size(uhci)); uhcd = init_uhcd(); @@ -324,85 +311,85 @@ static void enum_device(struct usb_device* usbdev) usbdev->stalled = 0; usbdev->locked = 0; usbdev->expects = USB_TOD_SETUP | USB_TOD_COMMAND; - dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), - STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); + dev_desc = + do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, + 0); if (dev_desc == NULL) { return; } - usbdev->device = dev_desc; dprintf("0x%04X:0x%04X (%i) -> ", dev_desc->vendor_id, dev_desc->device_id, dev_desc->class_id); do_control(usbdev, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, REC_DEVICE, - SET_ADDRESS, (id = usb_dev_ids++), 0); + SET_ADDRESS, (id = usb_dev_ids++), + 0); usbdev->id = id; _dprintf("%i\n", id); if (dev_desc->iManufacturer) { - name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, - REC_DEVICE, GET_DESCRIPTOR, - (DESC_STRING << 8) | dev_desc->iManufacturer, 0); + name = + do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, REC_DEVICE, + GET_DESCRIPTOR, (DESC_STRING << 8) | dev_desc->iManufacturer, + 0); if (name == NULL) { return; } - dprintf(" -> Hersteller: "); for (i = 2; i < name[0]; i += 2) { _dprintf("%c", name[i]); } - _dprintf("\n"); } - if (dev_desc->iProduct) { - name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, - REC_DEVICE, GET_DESCRIPTOR, - (DESC_STRING << 8) | dev_desc->iProduct, 0); + name = + do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, REC_DEVICE, + GET_DESCRIPTOR, (DESC_STRING << 8) | dev_desc->iProduct, + 0); if (name == NULL) { return; } - dprintf(" -> Produkt: "); for (i = 2; i < name[0]; i += 2) { _dprintf("%c", name[i]); } - _dprintf("\n"); } - devices[id] = usbdev; //TODO: Man kann doch nicht immer den ersten nehmen... - conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, - sizeof(struct config_desc), STD_REQUEST, - REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); + conf_desc = + do_control(usbdev, DEV_TO_HOST, NULL, sizeof(struct config_desc), + STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, + 0); if (conf_desc == NULL) { return; } - - conf_desc = do_control(usbdev, DEV_TO_HOST, NULL, conf_desc->total_length, - STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); + conf_desc = + do_control(usbdev, DEV_TO_HOST, NULL, conf_desc->total_length, + STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, + 0); if (conf_desc == NULL) { return; } - usbdev->config = conf_desc; do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, - SET_CONFIGURATION, conf_desc->config_value, 0); + SET_CONFIGURATION, conf_desc->config_value, + 0); if (conf_desc->iConfiguration) { - name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, - REC_DEVICE, GET_DESCRIPTOR, - (DESC_STRING << 8) | conf_desc->iConfiguration, 0); + name = + do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, REC_DEVICE, + GET_DESCRIPTOR, (DESC_STRING << 8) | conf_desc->iConfiguration, + 0); if (name == NULL) { return; } - dprintf("Verwende Konfiguration "); for (i = 2; i < name[0]; i += 2) { _dprintf("%c", name[i]); } - _dprintf(".\n"); } - position = conf_desc; position += sizeof(struct config_desc); best_if = position; //Standard-IF @@ -413,33 +400,30 @@ static void enum_device(struct usb_device* usbdev) best_if = if_desc; break; } - - position += - sizeof(struct interface_desc) + - if_desc->num_endpoints * sizeof(struct endpoint_desc); + position += sizeof(struct interface_desc) + if_desc->num_endpoints * + sizeof(struct endpoint_desc); } - do_control(usbdev, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, - SET_INTERFACE, best_if->interface_number, 0); + SET_INTERFACE, best_if->interface_number, + 0); usbdev->interface = best_if; if (best_if->iInterface) { - name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, - REC_DEVICE, GET_DESCRIPTOR, - (DESC_STRING << 8) | best_if->iInterface, 0); + name = + do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, REC_DEVICE, + GET_DESCRIPTOR, (DESC_STRING << 8) | best_if->iInterface, + 0); if (name == NULL) { return; } - dprintf("Verwende Interface "); for (i = 2; i < name[0]; i += 2) { _dprintf("%c", name[i]); } - _dprintf(".\n"); } - dprintf("Konfiguration: %i:%i (%i)\n", conf_desc->config_value, - best_if->interface_number, best_if->interface_class); + best_if->interface_number, + best_if->interface_class); if (ccdriver_take_device[best_if->interface_class] != NULL) { ccdriver_take_device[best_if->interface_class](usbdev); } @@ -454,22 +438,28 @@ static void enumerate_hub(struct usb_device* usbdev) //Mehr als 8 Ports sollte es nicht geben, die Gr????e des Deskriptors wird //daher wohl nicht gr????er als sizeof()+16 werden - hub_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*hub_desc) + 16, - CLS_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); + hub_desc = + do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*hub_desc) + 16, + CLS_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, + 0); for (i = 0; i < hub_desc->nbr_ports; i++) { - port_status = do_control(usbdev, DEV_TO_HOST, NULL, 4, CLS_REQUEST, - REC_OTHER, GET_STATUS, 0, i + 1); + port_status = + do_control(usbdev, DEV_TO_HOST, NULL, 4, CLS_REQUEST, REC_OTHER, + GET_STATUS, 0, + i + 1); if (!(port_status[0] & PORT_DEVICE)) { continue; } - //Strom an do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, - SET_FEATURE, PORTF_POWER, i + 1); + SET_FEATURE, PORTF_POWER, + i + 1); cdi_sleep_ms(hub_desc->pwron2pwrgood * 2); //Resetten do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, - SET_FEATURE, PORTF_RESET, i + 1); + SET_FEATURE, PORTF_RESET, + i + 1); //Reset sollte jetzt eigtl. schon beendet sein //(do_control wartet ja 50 ms) down = malloc(sizeof(struct usb_device)); @@ -494,4 +484,4 @@ void enumerate_hci(struct hci* hci) enum_device(usbdev); } } -} +} \ No newline at end of file diff --git a/usb/msd.c b/usb/msd.c index 8fe029b..16d119c 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -41,7 +41,7 @@ #endif #ifdef DEBUG -#define dprintf(fmt, args...) printf("[usb-msc] " fmt, ## args) +#define dprintf(fmt, args...) printf("[usb-msc] " fmt, ## args) #define _dprintf(fmt, args...) printf(fmt, ## args) #else static int dprintf(const char* fmt, ...) @@ -59,6 +59,7 @@ static int fdprintf(const char* fmt, ...) { return 0; } + #endif static uint32_t cbw_tag = 1; @@ -91,30 +92,29 @@ static int msd_get_capacity(struct usb_device* usbdev, uint32_t* block_size, 8) | \ (((_) & 0xFF00) << 8) | (((_) & 0xFF) << 24)) #ifdef CPU_IS_LITTLE_ENDIAN -#define CPU2BE(_) change_endianess_32(_) -#define CPU2LE(_) (_) -#define BE2CPU(_) change_endianess_32(_) -#define LE2CPU(_) (_) +#define CPU2BE(_) change_endianess_32(_) +#define CPU2LE(_) (_) +#define BE2CPU(_) change_endianess_32(_) +#define LE2CPU(_) (_) #else -#define CPU2BE(_) (_) -#define CPU2LE(_) change_endianess_32(_) -#define BE2CPU(_) (_) -#define LE2CPU(_) change_endianess_32(_) +#define CPU2BE(_) (_) +#define CPU2LE(_) change_endianess_32(_) +#define BE2CPU(_) (_) +#define LE2CPU(_) change_endianess_32(_) #endif //Das muss definiert werden, um vor einem Befehl zu warten, bis das MSD fertig //ist. Theoretisch ist das ??u??erst sinnvoll, praktisch h??ngt sich der Treiber //bisweilen auf - das sollte allerdings mit dem Timeout gegessen sein /** - #define WAIT_FOR_MSD_READY - */ + #define WAIT_FOR_MSD_READY + */ //TODO: Storage bietet eine SCSI-Klasse. Die k??nnte man durchaus benutzen. xD void init_msc_driver() { struct cdi_driver* stddrv = (struct cdi_driver*) &cdi_driver; - stddrv->type = CDI_STORAGE; stddrv->name = driver_name; stddrv->init_device = &init_msd; @@ -142,12 +142,10 @@ void register_msd(struct usb_device* usbdev) dprintf("Es werden nur bulk-only-Interfaces unterst??tzt.\n"); return; } - cdimsd = malloc(sizeof(struct cdi_msd)); if (cdimsd == NULL) { return; } - cdimsd->usb_device = usbdev; cdimsd->offset = 0; strgdev = (struct cdi_storage_device*) cdimsd; @@ -157,7 +155,6 @@ void register_msd(struct usb_device* usbdev) free(strgdev); return; } - sprintf((char*) strgdev->dev.name, "msd%i", msdnum++); msc = malloc(sizeof(struct msclass_data)); if (msc == NULL) { @@ -165,7 +162,6 @@ void register_msd(struct usb_device* usbdev) free(strgdev); return; } - usbdev->classd = (struct class_data*) msc; msc->bulk_ep_in = NULL; msc->bulk_ep_out = NULL; @@ -173,24 +169,21 @@ void register_msd(struct usb_device* usbdev) for (i = 0; i < usbdev->interface->num_endpoints; i++) { ep_desc = address; if ((ep_desc->endpoint_address & 0x80) && - (ep_desc->attributes == 0x02) && (msc->bulk_ep_in == NULL)) //BULK-IN + (ep_desc->attributes == 0x02) && (msc->bulk_ep_in == NULL)) //BULK-IN { msc->bulk_ep_in = ep_desc; } else if (!(ep_desc->endpoint_address & 0x80) && (ep_desc->attributes == 0x02) && - (msc->bulk_ep_out == NULL)) //BULK-OUT + (msc->bulk_ep_out == NULL)) //BULK-OUT { msc->bulk_ep_out = ep_desc; } - address += sizeof(struct endpoint_desc); } - if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL)) { dprintf("Nicht gen??gend Endpoints gefunden.\n"); return; } - if (!msd_get_capacity(usbdev, &bs, &bc)) { strgdev->block_size = 0; strgdev->block_count = 0; @@ -200,10 +193,13 @@ void register_msd(struct usb_device* usbdev) strgdev->block_count = bc; size = bs; size *= bc; - dprintf("%s: %i * %i B (ca. %lld MB).\n", strgdev->dev.name, bc, bs, + dprintf("%s: %i * %i B (ca. %i MB).\n", strgdev->dev.name, bc, bs, size >> 20); + dprintf("EP %i: %i; EP %i: %i\n", msc->bulk_ep_in->endpoint_address, + msc->bulk_ep_in->max_packet_size, + msc->bulk_ep_out->endpoint_address, + msc->bulk_ep_out->max_packet_size); } - cdi_storage_device_init(strgdev); cdi_list_push(cdi_driver.drv.devices, strgdev); get_partitions(strgdev); @@ -229,25 +225,25 @@ static int write_cmd(struct usb_device* usbdev, uintptr_t src) { struct msclass_data* msc = (struct msclass_data*) usbdev->classd; struct usb_packet cmd_packet = { - .type = PACKET_OUT, - .endpoint = msc->bulk_ep_out->endpoint_address, - .phys_data = src, - .length = 0x1F, - .datatoggle = 0, - .type_of_data = USB_TOD_COMMAND, + .type = PACKET_OUT, + .endpoint = msc->bulk_ep_out->endpoint_address, + .phys_data = src, + .length = 0x1F, + .datatoggle = 0, + .type_of_data = USB_TOD_COMMAND, }; return usb_do_packet(usbdev, &cmd_packet); } /** - * Liest den Status von einem MSD - * - * @param usbdev Das bewusste Ger??t - * @param expected_tag Das erwartete Tag - * - * @return Wenn der Status OK ist USB_NO_ERROR, sonst entsprechender Fehler - */ + * Liest den Status von einem MSD + * + * @param usbdev Das bewusste Ger??t + * @param expected_tag Das erwartete Tag + * + * @return Wenn der Status OK ist USB_NO_ERROR, sonst entsprechender Fehler + */ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) { @@ -256,32 +252,31 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) struct command_status_wrapper* csw; int error; - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), - (void**) &csw, (void**) &pcsw) == -1) + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), (void**) &csw, + (void**) &pcsw) == -1) { return USB_TRIVIAL_ERROR; } struct usb_packet status_packet = { - .type = PACKET_IN, - .endpoint = msc->bulk_ep_in->endpoint_address, - .phys_data = pcsw, - .length = 0x0D, - .datatoggle = 0, - .type_of_data = USB_TOD_STATUS, + .type = PACKET_IN, + .endpoint = msc->bulk_ep_in->endpoint_address, + .phys_data = pcsw, + .length = 0x0D, + .datatoggle = 0, + .type_of_data = USB_TOD_STATUS, }; error = usb_do_packet(usbdev, &status_packet); if (error != USB_NO_ERROR) { return error; } - if ((csw->csw_signature != CSW_SIGNATURE) || (csw->csw_tag != expected_tag) || csw->csw_status) { - return USB_CRC; //Watt wei?? denn ich - } + return USB_CRC; //Watt wei?? denn ich + } return USB_NO_ERROR; } @@ -297,19 +292,16 @@ static int msd_get_capacity(struct usb_device* usbdev, uint32_t* block_size, if ((usbdev == NULL) || (block_size == NULL) || (block_count == NULL)) { return 0; } - - if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), - (void**) &cbw, (void**) &pcbw) == -1) + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), (void**) &cbw, + (void**) &pcbw) == -1) { return 0; } - - if (cdi_alloc_phys_mem(sizeof(struct msd_capacity), - (void**) &cap, (void**) &pcap) == -1) + if (cdi_alloc_phys_mem(sizeof(struct msd_capacity), (void**) &cap, + (void**) &pcap) == -1) { return 0; } - msc = (struct msclass_data*) usbdev->classd; memset(cbw, 0, 0x1F); cbw->cbw_signature = CBW_SIGNATURE; @@ -326,12 +318,12 @@ static int msd_get_capacity(struct usb_device* usbdev, uint32_t* block_size, } struct usb_packet in_packet = { - .type = PACKET_IN, - .endpoint = msc->bulk_ep_in->endpoint_address, - .phys_data = pcap, - .length = sizeof(*cap), - .datatoggle = 0, - .type_of_data = USB_TOD_DATA_IN, + .type = PACKET_IN, + .endpoint = msc->bulk_ep_in->endpoint_address, + .phys_data = pcap, + .length = sizeof(*cap), + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_IN, }; if (usb_do_packet(usbdev, &in_packet) != USB_NO_ERROR) { @@ -356,12 +348,11 @@ static int msd_ready(struct usb_device* usbdev) uintptr_t pcbw; uint32_t expected_tag; - if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), - (void**) &cbw, (void**) &pcbw) == -1) + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), (void**) &cbw, + (void**) &pcbw) == -1) { return 0; } - msc = (struct msclass_data*) usbdev->classd; memset(cbw, 0, 0x1F); cbw->cbw_signature = CBW_SIGNATURE; @@ -381,12 +372,12 @@ static int msd_ready(struct usb_device* usbdev) return 1; } + #endif static inline int tsl(volatile int* variable) { int rval; - rval = *variable; *variable = 1; return rval; @@ -403,7 +394,6 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, void* vb; uintptr_t pb; int bs = strgdev->block_size, error; - #ifdef WAIT_FOR_MSD_READY int j; #endif @@ -416,35 +406,31 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, dprintf("Blockspeicher konnte nicht allociert werden.\n"); return -1; } - if (!count) { dprintf("Leere Leseanfrage.\n"); return 0; } - while (tsl(&usbdev->locked)) { #ifndef CDI_STANDALONE __asm__ __volatile__ ("hlt"); #endif } - - for (i = 0; i < count; i++) { + /*for (i = 0; i < count; i++) + {*/ #ifdef WAIT_FOR_MSD_READY - for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { - cdi_sleep_ms(20); - } + for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { + cdi_sleep_ms(20); + } #endif - error = msd_read(usbdev, start + i, 1, pb, bs); - if (error != USB_NO_ERROR) { - dprintf("Lesefehler 0x%X bei Block %i.\n", error, i); - usbdev->locked = 0; - return -1; - } - - memcpy(buffer + i * bs, vb, bs); + error = msd_read(usbdev, start /* + i*/, count, pb, bs); + if (error != USB_NO_ERROR) { + dprintf("Lesefehler 0x%X bei Block %i.\n", error, 0); + usbdev->locked = 0; + return -1; } - + memcpy(buffer /* + i * bs*/, vb, count * bs); + //} usbdev->locked = 0; return 0; } @@ -457,18 +443,16 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, struct command_block_wrapper* cbw; uintptr_t pcbw; uint32_t expected_tag; - int error; + int error, i, ep_size; - if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), - (void**) &cbw, (void**) &pcbw) == -1) + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), (void**) &cbw, + (void**) &pcbw) == -1) { return USB_TRIVIAL_ERROR; } - if (!phys_buffer || !length) { return USB_TRIVIAL_ERROR; } - msc = (struct msclass_data*) usbdev->classd; memset(cbw, 0, 0x1F); cbw->cbw_signature = CBW_SIGNATURE; @@ -492,19 +476,32 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, } struct usb_packet in_packet = { - .type = PACKET_IN, - .endpoint = msc->bulk_ep_in->endpoint_address, - .phys_data = phys_buffer, - .length = length, - .datatoggle = 0, - .type_of_data = USB_TOD_DATA_IN, + .type = PACKET_IN, + .endpoint = msc->bulk_ep_in->endpoint_address, + .phys_data = phys_buffer, + .length = 0, + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_IN, }; - error = usb_do_packet(usbdev, &in_packet); - if (error != USB_NO_ERROR) { - return error; + //Transaktionen eintragen + ep_size = msc->bulk_ep_in->max_packet_size; + for (i = 0; i < length; i += ep_size) { + in_packet.length = (length - i > ep_size) ? ep_size : length - i; + error = usb_do_packet(usbdev, &in_packet); + if (error != USB_NO_ERROR) { + return error; + } + in_packet.phys_data += ep_size; + in_packet.datatoggle ^= 1; } + /*//Auf deren Ende warten + for (i = 0; i < length; i += ep_size) + { + + }*/ + error = read_status(usbdev, expected_tag); if (error != USB_NO_ERROR) { return error; @@ -524,7 +521,6 @@ static int msd_cdi_write(struct cdi_storage_device* strgdev, uint64_t start, void* vb; uintptr_t pb; int bs = strgdev->block_size, error; - #ifdef WAIT_FOR_MSD_READY int j; #endif @@ -537,13 +533,11 @@ static int msd_cdi_write(struct cdi_storage_device* strgdev, uint64_t start, dprintf("Blockspeicher zum Schreiben konnte nicht allociert werden.\n"); return -1; } - while (tsl(&usbdev->locked)) { #ifndef CDI_STANDALONE __asm__ __volatile__ ("hlt"); #endif } - for (i = 0; i < count; i++) { memcpy(vb, buffer + i * bs, bs); #ifdef WAIT_FOR_MSD_READY @@ -559,7 +553,6 @@ static int msd_cdi_write(struct cdi_storage_device* strgdev, uint64_t start, return -1; } } - usbdev->locked = 0; return 0; } @@ -568,71 +561,71 @@ static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) { - struct msclass_data* msc; - struct command_block_wrapper* cbw; - struct command_status_wrapper* csw; - uintptr_t pcbw, pcsw; - uint32_t expected_tag; - int error; - - if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), - (void**) &cbw, (void**) &pcbw) == -1) - { - return USB_TRIVIAL_ERROR; - } - - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), - (void**) &csw, (void**) &pcsw) == -1) - { - return USB_TRIVIAL_ERROR; - } - - if (!phys_buffer || !length) { - return USB_TRIVIAL_ERROR; - } - - msc = (struct msclass_data*) usbdev->classd; - memset(cbw, 0, 0x1F); - cbw->cbw_signature = CBW_SIGNATURE; - cbw->cbw_tag = (expected_tag = cbw_tag++); - cbw->cbw_data_transfer_length = length; - cbw->cbw_flags = 0x00; //OUT - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? - cbw->cbw_cb_length = 12; - cbw->cbw_cb[0] = MSC_CMD_WRITE10; - cbw->cbw_cb[1] = 0; //LUN: 0 - cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; - cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; - cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; - cbw->cbw_cb[5] = lba & 0x000000FF; - cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; - cbw->cbw_cb[8] = sectors & 0x000000FF; - - error = write_cmd(usbdev, pcbw); - if (error != USB_NO_ERROR) { - return error; - } - - struct usb_packet out_packet = { - .type = PACKET_OUT, - .endpoint = msc->bulk_ep_out->endpoint_address, - .phys_data = phys_buffer, - .length = length, - .datatoggle = 0, - .type_of_data = USB_TOD_DATA_OUT, - }; - - error = usb_do_packet(usbdev, &out_packet); - if (error != USB_NO_ERROR) { - return error; - } - - error = read_status(usbdev, expected_tag); - if (error != USB_NO_ERROR) { - return error; - } - - return USB_NO_ERROR; + /*struct msclass_data *msc; + struct command_block_wrapper *cbw; + struct command_status_wrapper *csw; + uintptr_t pcbw, pcsw; + uint32_t expected_tag; + int error; + + if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), (void **)&cbw, (void **)&pcbw) == -1) + { + return USB_TRIVIAL_ERROR; + } + if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), (void **)&csw, (void **)&pcsw) == -1) + { + return USB_TRIVIAL_ERROR; + } + if (!phys_buffer || !length) + { + return USB_TRIVIAL_ERROR; + } + msc = (struct msclass_data *)usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = length; + cbw->cbw_flags = 0x00; //OUT + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_WRITE10; + cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; + cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; + cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; + cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; + cbw->cbw_cb[8] = sectors & 0x000000FF; + + error = write_cmd(usbdev, pcbw); + if (error != USB_NO_ERROR) + { + return error; + } + + struct usb_packet out_packet = { + .type = PACKET_OUT, + .endpoint = msc->bulk_ep_out->endpoint_address, + .phys_data = phys_buffer, + .length = length, + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_OUT, + }; + + error = usb_do_packet(usbdev, &out_packet); + if (error != USB_NO_ERROR) + { + return error; + } + + error = read_status(usbdev, expected_tag); + if (error != USB_NO_ERROR) + { + return error; + } + + return USB_NO_ERROR;*/ + return USB_STALLED; } static void get_partitions(struct cdi_storage_device* cdistrg) @@ -647,23 +640,19 @@ static void get_partitions(struct cdi_storage_device* cdistrg) dprintf("MBR konnte nicht eingelesen werden.\n"); return; } - if (((uint16_t*) mbr)[255] != 0xAA55) { return; } - partition = mbr + 0x1BE; dprintf("Partitionen auf %s:", cdistrg->dev.name); for (i = 0; i < 4; i++) { - if (partition[i].type && partition[i].size - && (partition[i].start + partition[i].size < - cdistrg->block_count)) + if (partition[i].type && partition[i].size && + (partition[i].start + partition[i].size < cdistrg->block_count)) { cdimsd = malloc(sizeof(struct cdi_msd)); if (cdimsd == NULL) { break; } - cdimsd->offset = partition[i].start; cdimsd->usb_device = base_cdimsd->usb_device; cdimsd->cdi_device.dev.type = CDI_STORAGE; @@ -672,9 +661,9 @@ static void get_partitions(struct cdi_storage_device* cdistrg) free(cdimsd); break; } - sprintf((char*) cdimsd->cdi_device.dev.name, "%sp%i", - cdistrg->dev.name, j++); + cdistrg->dev.name, + j++); cdimsd->cdi_device.block_size = base_cdimsd->cdi_device.block_size; cdimsd->cdi_device.block_count = partition[i].size; _dprintf(" %s", cdimsd->cdi_device.dev.name); @@ -682,6 +671,5 @@ static void get_partitions(struct cdi_storage_device* cdistrg) cdi_list_push(cdi_driver.drv.devices, cdimsd); } } - _dprintf("\n"); -} +} \ No newline at end of file diff --git a/usb/uhci.c b/usb/uhci.c index 208000e..e73d584 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -36,7 +36,7 @@ #ifdef DEBUG #include #include -#define dprintf(fmt, args...) printf("[uhci] " fmt, ## args) +#define dprintf(fmt, args...) printf("[uhci] " fmt, ## args) #define _dprintf(fmt, args...) printf(fmt, ## args) #else static int dprintf(const char* fmt, ...) @@ -85,7 +85,7 @@ static void uhci_deinit(struct cdi_device* cdi_hci) { cdi_outw( ((struct uhci*) ((struct cdi_hci*) cdi_hci)->hci)->pbase + UHCI_USBCMD, - MAXP); //HC anhalten + MAXP); //HC anhalten //TODO: Hier doch auch } @@ -107,36 +107,31 @@ void uhci_init(struct cdi_device* cdi_hci) dprintf("I/O-Ports konnten nicht reserviert werden.\n"); return; } - uhci->pbase = res->start; break; } } - if (!uhci->pbase) { dprintf("I/O-Basis nicht gefunden!\n"); return; } - if (cdi_alloc_phys_mem(4096, (void**) &uhci->frame_list, (void**) &uhci->phys_frame_list) == -1) { dprintf("Frame List konnte nicht allociert werden!\n"); return; } - cdi_register_irq(gen_hci->pcidev->irq, &uhci_handler, cdi_hci); uhci->root_ports = (size - 0x10) >> 1; - if (uhci->root_ports > 7) { //Laut Linuxkernel ist das so "weird", dass da was nicht stimmen kann... + if (uhci->root_ports > 7) { //Laut Linuxkernel ist das so "weird", dass da was nicht stimmen kann... uhci->root_ports = 2; } - dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase, - uhci->root_ports, gen_hci->pcidev->irq); + uhci->root_ports, + gen_hci->pcidev->irq); for (i = 0; i < 1024; i++) { - uhci->frame_list[i] = 1; //Invalid + uhci->frame_list[i] = 1; //Invalid } - dprintf("Resetten...\n"); //HC zur??cksetzen cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); @@ -145,7 +140,6 @@ void uhci_init(struct cdi_device* cdi_hci) { cdi_sleep_ms(10); } - //Reset auf dem USB treiben cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | GRESET); for (i = 0; (cdi_inw(uhci->pbase + UHCI_USBCMD) & GRESET) && (i < 50); @@ -153,7 +147,6 @@ void uhci_init(struct cdi_device* cdi_hci) { cdi_sleep_ms(10); } - //Alle Interrupts senden cdi_outw(uhci->pbase + UHCI_USBINTR, 0xF); //Framelistadresse eintragen @@ -163,7 +156,6 @@ void uhci_init(struct cdi_device* cdi_hci) for (i = 0; i < uhci->root_ports; i++) { cdi_outw(uhci->pbase + UHCI_RPORTS + i * 2, RPORT_CSC); } - dprintf(" Fertig\n"); gen_hci->find_devices = &get_devices; gen_hci->activate_device = &activate_device; @@ -187,7 +179,6 @@ static cdi_list_t get_devices(struct hci* gen_hci) _dprintf("-"); continue; } - _dprintf("!"); dev = malloc(sizeof(struct usb_device)); dev->hci = gen_hci; @@ -197,7 +188,6 @@ static cdi_list_t get_devices(struct hci* gen_hci) (cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & RPORT_LOSPD) && 1; cdi_list_push(dlist, dev); } - _dprintf("\n"); return dlist; } @@ -220,7 +210,6 @@ static int get_current_frame(struct hci* gen_hci) static inline int tsl(volatile int* variable) { int rval; - rval = *variable; *variable = 1; return rval; @@ -239,24 +228,21 @@ static int enqueue_request(struct hci* gen_hci, int frame, int type, int device, struct transfer* addr; int timeout; - if (cdi_alloc_phys_mem(sizeof(struct uhci_td), - (void**) &td, (void**) &ptd) == -1) + if (cdi_alloc_phys_mem(sizeof(struct uhci_td), (void**) &td, + (void**) &ptd) == -1) { return USB_TRIVIAL_ERROR; } - - if (cdi_alloc_phys_mem(sizeof(struct uhci_qh), - (void**) &qh, (void**) &pqh) == -1) + if (cdi_alloc_phys_mem(sizeof(struct uhci_qh), (void**) &qh, + (void**) &pqh) == -1) { return USB_TRIVIAL_ERROR; } - while (tsl(&locked)) { #ifndef CDI_STANDALONE __asm__ __volatile__ ("hlt"); #endif } - qh->next = 1; //Invalid qh->transfer = ptd; memset(td, 0, sizeof(struct uhci_td)); @@ -280,13 +266,11 @@ static int enqueue_request(struct hci* gen_hci, int frame, int type, int device, for (timeout = 0; !(qh->transfer & 1) && (timeout < 1000); timeout++) { cdi_sleep_ms(1); } - while ((timeout < 1000) && (addr->error == 0xFFFF)) { #ifndef CDI_STANDALONE __asm__ __volatile__ ("hlt"); #endif } - uhci->frame_list[frame] = 1; locked = 0; return addr->error; @@ -299,14 +283,12 @@ static void uhci_handler(struct cdi_device* cdi_hci) struct transfer* addr; struct uhci_td* td; - if (!status) { //Also, von hier kommt der IRQ nicht. + if (!status) { //Also, von hier kommt der IRQ nicht. return; } - if (status & ~0x0001) { dprintf("Unerwarteter IRQ 0x%04X von %s\n", status, cdi_hci->name); } - if (status & 0x10) { printf("[uhci] SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n"); cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! @@ -316,42 +298,34 @@ static void uhci_handler(struct cdi_device* cdi_hci) if (td->active) { continue; } - addr->error = USB_NO_ERROR; cdi_list_remove(active_transfers, i--); if (td->stalled_err) { dprintf("ENDPOINT STALLED\n"); addr->error |= USB_STALLED; } - if (td->buf_err) { dprintf("Puffer??berlauf oder Underrun\n"); addr->error |= USB_BUFFER_ERROR; } - if (td->babble) { dprintf("Da war ein Ger??t wohl sehr gespr??chig: Babble.\n"); addr->error |= USB_BABBLE; } - if (td->nak) { dprintf("NAK empfangen\n"); addr->error |= USB_NAK; } - if (td->crc_time_err) { dprintf("CRC-Fehler oder Timeout\n"); addr->error |= USB_CRC | USB_TIMEOUT; } - if (td->bitstuff_err) { dprintf("Bitstufffehler\n"); addr->error |= USB_BITSTUFF; } - //TODO: free(td) - dazu w??re eine CDI-Funktion n??tzlich } } - cdi_outw(uhci->pbase + UHCI_USBSTS, status); -} +} \ No newline at end of file -- 1.6.3.3 From kevin at tyndur.org Sat Aug 22 20:41:59 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 22 Aug 2009 20:41:59 +0200 Subject: [cdi-devel] [PATCH] usb: uhci_do_packet interface rework Message-ID: <1250966519-22486-1-git-send-email-kevin@tyndur.org> Signed-off-by: Kevin Wolf --- usb/include/usb.h | 5 ++--- usb/main.c | 5 +---- usb/uhci.c | 30 +++++++++++++++--------------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/usb/include/usb.h b/usb/include/usb.h index 223ecf7..deb7dd5 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -136,15 +136,14 @@ struct cdi_hci { }; struct usb_device; +struct usb_packet; struct hci { struct cdi_pci_device* pcidev; hci_type_t type; cdi_list_t (* find_devices)(struct hci*); void (* activate_device)(struct hci*, struct usb_device*); - int (* do_packet)(struct hci*, int frame, int type, int device, - int endpoint, int low_speed, uintptr_t phys_data, int length, - int datatoggle); + int (* do_packet)(struct usb_device*, struct usb_packet* packet); int (* get_frame)(struct hci*); }; diff --git a/usb/main.c b/usb/main.c index 02079e2..2491343 100644 --- a/usb/main.c +++ b/usb/main.c @@ -130,10 +130,7 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) } while (error == USB_NAK) { - error = device->hci->do_packet(device->hci, - (device->hci->get_frame(device->hci) + 3) & 0x3FF, - packet->type, device->id, packet->endpoint, device->low_speed, - packet->phys_data, packet->length, packet->datatoggle); + error = device->hci->do_packet(device, packet); } if (error == USB_STALLED) { diff --git a/usb/uhci.c b/usb/uhci.c index 208000e..413c7f5 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -53,9 +53,8 @@ static struct cdi_driver cdi_driver; static cdi_list_t active_transfers; static void uhci_handler(struct cdi_device* dev); -static int enqueue_request(struct hci* gen_hci, int frame, int type, int device, - int endpoint, int low_speed, uintptr_t phys_data, int length, - int datatoggle); +static int uhci_do_packet(struct usb_device* usbdev, + struct usb_packet* packet); static cdi_list_t get_devices(struct hci* gen_hci); static void activate_device(struct hci* gen_hci, struct usb_device* device); static int get_current_frame(struct hci* gen_hci); @@ -167,7 +166,7 @@ void uhci_init(struct cdi_device* cdi_hci) dprintf(" Fertig\n"); gen_hci->find_devices = &get_devices; gen_hci->activate_device = &activate_device; - gen_hci->do_packet = &enqueue_request; + gen_hci->do_packet = &uhci_do_packet; gen_hci->get_frame = &get_current_frame; enumerate_hci(gen_hci); @@ -228,17 +227,18 @@ static inline int tsl(volatile int* variable) static volatile int locked = 0; -static int enqueue_request(struct hci* gen_hci, int frame, int type, int device, - int endpoint, int low_speed, uintptr_t phys_data, int length, - int datatoggle) +static int uhci_do_packet(struct usb_device* usbdev, + struct usb_packet* packet) { - struct uhci* uhci = (struct uhci*) gen_hci; + struct uhci* uhci = (struct uhci*) usbdev->hci; struct uhci_td* td; struct uhci_qh* qh; uintptr_t ptd, pqh; struct transfer* addr; int timeout; + int frame = (usbdev->hci->get_frame(usbdev->hci) + 3) & 0x3FF; + if (cdi_alloc_phys_mem(sizeof(struct uhci_td), (void**) &td, (void**) &ptd) == -1) { @@ -263,14 +263,14 @@ static int enqueue_request(struct hci* gen_hci, int frame, int type, int device, td->next = 1; //Invalid td->active = 1; td->ioc = 1; - td->data_toggle = datatoggle; - td->low_speed = low_speed; + td->data_toggle = packet->datatoggle; + td->low_speed = usbdev->low_speed; td->errors = 1; - td->pid = type; - td->device = device; - td->endpoint = endpoint; - td->maxlen = length ? length - 1 : 0x7FF; - td->buffer = phys_data; + td->pid = packet->type; + td->device = usbdev->id; + td->endpoint = packet->endpoint; + td->maxlen = packet->length ? packet->length - 1 : 0x7FF; + td->buffer = packet->phys_data; addr = malloc(sizeof(struct transfer)); addr->virt = td; addr->phys = ptd; -- 1.6.0.2 From max at tyndur.org Sat Aug 22 23:04:01 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 22 Aug 2009 23:04:01 +0200 Subject: [cdi-devel] [PATCH] No STALL Message-ID: <1250975041-20370-1-git-send-email-max@tyndur.org> From: Max Reitz * Stall problem removed, hopefully more correct msc driver code. Signed-off-by: Max Reitz --- usb/include/usb.h | 19 +++--- usb/main.c | 3 + usb/msd.c | 180 +++++++++++++++++++++++++---------------------------- 3 files changed, 98 insertions(+), 104 deletions(-) diff --git a/usb/include/usb.h b/usb/include/usb.h index b6ff391..584f31f 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -93,15 +93,16 @@ enum usb_packet_type { #define DESC_OTHER_SPEED_CONFIGURATION 7 #define DESC_INTERFACE_POWER 8 -#define USB_NO_ERROR 0x00 -#define USB_STALLED 0x01 -#define USB_BUFFER_ERROR 0x02 -#define USB_BABBLE 0x04 -#define USB_NAK 0x08 -#define USB_CRC 0x10 -#define USB_TIMEOUT 0x20 -#define USB_BITSTUFF 0x40 -#define USB_TRIVIAL_ERROR 0x80 +#define USB_NO_ERROR 0x0000 +#define USB_STALLED 0x0001 +#define USB_BUFFER_ERROR 0x0002 +#define USB_BABBLE 0x0004 +#define USB_NAK 0x0008 +#define USB_CRC 0x0010 +#define USB_TIMEOUT 0x0020 +#define USB_BITSTUFF 0x0040 +#define USB_TRIVIAL_ERROR 0x0080 +#define USB_STATUS_ERROR 0x0100 #define USB_TOD_SETUP 0x0001 #define USB_TOD_SETUP_DATA_IN 0x0002 diff --git a/usb/main.c b/usb/main.c index 878aefe..9eb20d8 100644 --- a/usb/main.c +++ b/usb/main.c @@ -126,6 +126,9 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) } while (error == USB_NAK) { error = device->hci->do_packet(device, packet); + if (error == USB_NAK) { + cdi_sleep_ms(5); + } } if (error == USB_STALLED) { printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", packet->endpoint, diff --git a/usb/msd.c b/usb/msd.c index ad6f362..471856a 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -169,13 +169,15 @@ void register_msd(struct usb_device* usbdev) for (i = 0; i < usbdev->interface->num_endpoints; i++) { ep_desc = address; if ((ep_desc->endpoint_address & 0x80) && - (ep_desc->attributes == 0x02) && (msc->bulk_ep_in == NULL)) //BULK-IN + (ep_desc->attributes == 0x02) && (msc->bulk_ep_in == NULL)) { + //BULK-IN msc->bulk_ep_in = ep_desc; } else if (!(ep_desc->endpoint_address & 0x80) && (ep_desc->attributes == 0x02) && - (msc->bulk_ep_out == NULL)) //BULK-OUT + (msc->bulk_ep_out == NULL)) { + //BULK-OUT msc->bulk_ep_out = ep_desc; } address += sizeof(struct endpoint_desc); @@ -252,7 +254,7 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) struct command_status_wrapper* csw; int error; - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), (void**) &csw, + if (cdi_alloc_phys_mem(sizeof(*csw), (void**) &csw, (void**) &pcsw) == -1) { return USB_TRIVIAL_ERROR; @@ -274,9 +276,11 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) if ((csw->csw_signature != CSW_SIGNATURE) || (csw->csw_tag != expected_tag) || csw->csw_status) { - return USB_CRC; //Watt wei?? denn ich - + dprintf("0x%08X %i==%i 0x%08X\n", csw->csw_signature, csw->csw_tag, + expected_tag, csw->csw_status); + return USB_STATUS_ERROR; } + return USB_NO_ERROR; } @@ -358,9 +362,9 @@ static int msd_ready(struct usb_device* usbdev) cbw->cbw_signature = CBW_SIGNATURE; cbw->cbw_tag = (expected_tag = cbw_tag++); cbw->cbw_data_transfer_length = 0; - cbw->cbw_flags = 0x80; //IN - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? - cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { return 0; @@ -395,14 +399,13 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, uintptr_t pb; int bs = strgdev->block_size, error; #ifdef WAIT_FOR_MSD_READY - int j; + int i; #endif -// uint32_t i; struct usb_device* usbdev = ((struct cdi_msd*) strgdev)->usb_device; fdprintf("read(%i, %i)\n", (int) start, (int) count); start += ((struct cdi_msd*) strgdev)->offset; - if (cdi_alloc_phys_mem(bs, &vb, (void**) &pb) == -1) { + if (cdi_alloc_phys_mem(count * bs, &vb, (void**) &pb) == -1) { dprintf("Blockspeicher konnte nicht allociert werden.\n"); return -1; } @@ -415,22 +418,19 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, __asm__ __volatile__ ("hlt"); #endif } - /*for (i = 0; i < count; i++) - {*/ #ifdef WAIT_FOR_MSD_READY - for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { cdi_sleep_ms(20); } #endif - error = msd_read(usbdev, start /* + i*/, count, pb, bs); + error = msd_read(usbdev, start, count, pb, count * bs); if (error != USB_NO_ERROR) { - dprintf("Lesefehler 0x%X bei Block %i.\n", error, 0); + dprintf("Lesefehler 0x%X bei Block %i.\n", error, start); usbdev->locked = 0; return -1; } - memcpy(buffer /* + i * bs*/, vb, count * bs); - //} + memcpy(buffer, vb, count * bs); usbdev->locked = 0; return 0; } @@ -458,11 +458,11 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, cbw->cbw_signature = CBW_SIGNATURE; cbw->cbw_tag = (expected_tag = cbw_tag++); cbw->cbw_data_transfer_length = length; - cbw->cbw_flags = 0x80; //IN - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? cbw->cbw_cb_length = 12; cbw->cbw_cb[0] = MSC_CMD_READ10; - cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[1] = 0; //LUN: 0 cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; @@ -484,7 +484,6 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, .type_of_data = USB_TOD_DATA_IN, }; - //Transaktionen eintragen ep_size = msc->bulk_ep_in->max_packet_size; for (i = 0; i < length; i += ep_size) { in_packet.length = (length - i > ep_size) ? ep_size : length - i; @@ -496,12 +495,6 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, in_packet.datatoggle ^= 1; } - /*//Auf deren Ende warten - for (i = 0; i < length; i += ep_size) - { - - }*/ - error = read_status(usbdev, expected_tag); if (error != USB_NO_ERROR) { return error; @@ -561,71 +554,69 @@ static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) { - /*struct msclass_data *msc; - struct command_block_wrapper *cbw; - struct command_status_wrapper *csw; - uintptr_t pcbw, pcsw; - uint32_t expected_tag; - int error; - - if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), (void **)&cbw, (void **)&pcbw) == -1) - { - return USB_TRIVIAL_ERROR; - } - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), (void **)&csw, (void **)&pcsw) == -1) - { - return USB_TRIVIAL_ERROR; - } - if (!phys_buffer || !length) - { - return USB_TRIVIAL_ERROR; - } - msc = (struct msclass_data *)usbdev->classd; - memset(cbw, 0, 0x1F); - cbw->cbw_signature = CBW_SIGNATURE; - cbw->cbw_tag = (expected_tag = cbw_tag++); - cbw->cbw_data_transfer_length = length; - cbw->cbw_flags = 0x00; //OUT - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? - cbw->cbw_cb_length = 12; - cbw->cbw_cb[0] = MSC_CMD_WRITE10; - cbw->cbw_cb[1] = 0; //LUN: 0 - cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; - cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; - cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; - cbw->cbw_cb[5] = lba & 0x000000FF; - cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; - cbw->cbw_cb[8] = sectors & 0x000000FF; - - error = write_cmd(usbdev, pcbw); - if (error != USB_NO_ERROR) - { - return error; - } - - struct usb_packet out_packet = { - .type = PACKET_OUT, - .endpoint = msc->bulk_ep_out->endpoint_address, - .phys_data = phys_buffer, - .length = length, - .datatoggle = 0, - .type_of_data = USB_TOD_DATA_OUT, - }; - - error = usb_do_packet(usbdev, &out_packet); - if (error != USB_NO_ERROR) - { - return error; - } - - error = read_status(usbdev, expected_tag); - if (error != USB_NO_ERROR) - { - return error; - } - - return USB_NO_ERROR;*/ - return USB_STALLED; + struct msclass_data* msc; + struct command_block_wrapper* cbw; + uintptr_t pcbw; + uint32_t expected_tag; + int error, i, ep_size; + + if (cdi_alloc_phys_mem(sizeof(*cbw), (void**) &cbw, + (void**) &pcbw) == -1) + { + return USB_TRIVIAL_ERROR; + } + if (!phys_buffer || !length) { + return USB_TRIVIAL_ERROR; + } + msc = (struct msclass_data*) usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = length; + cbw->cbw_flags = 0x00; //OUT + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_WRITE10; + cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; + cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; + cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; + cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; + cbw->cbw_cb[8] = sectors & 0x000000FF; + + error = write_cmd(usbdev, pcbw); + if (error != USB_NO_ERROR) { + return error; + } + + struct usb_packet out_packet = { + .type = PACKET_OUT, + .endpoint = msc->bulk_ep_out->endpoint_address, + .phys_data = phys_buffer, + .length = 0, + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_OUT, + }; + + //Transaktionen eintragen + ep_size = msc->bulk_ep_out->max_packet_size; + for (i = 0; i < length; i += ep_size) { + out_packet.length = (length - i > ep_size) ? ep_size : length - i; + error = usb_do_packet(usbdev, &out_packet); + if (error != USB_NO_ERROR) { + return error; + } + out_packet.phys_data += ep_size; + out_packet.datatoggle ^= 1; + } + + error = read_status(usbdev, expected_tag); + if (error != USB_NO_ERROR) { + return error; + } + + return USB_NO_ERROR; } static void get_partitions(struct cdi_storage_device* cdistrg) @@ -662,8 +653,7 @@ static void get_partitions(struct cdi_storage_device* cdistrg) break; } sprintf((char*) cdimsd->cdi_device.dev.name, "%sp%i", - cdistrg->dev.name, - j++); + cdistrg->dev.name, j++); cdimsd->cdi_device.block_size = base_cdimsd->cdi_device.block_size; cdimsd->cdi_device.block_count = partition[i].size; _dprintf(" %s", cdimsd->cdi_device.dev.name); -- 1.6.3.3 From max at tyndur.org Sun Aug 23 11:19:42 2009 From: max at tyndur.org (max at tyndur.org) Date: Sun, 23 Aug 2009 11:19:42 +0200 Subject: [cdi-devel] [PATCH] No STALL Message-ID: <1251019182-13729-1-git-send-email-max@tyndur.org> From: Max Reitz * Stall problem removed, hopefully more correct msc driver code. Signed-off-by: Max Reitz --- usb/include/usb.h | 19 +++--- usb/main.c | 3 + usb/msd.c | 180 +++++++++++++++++++++++++---------------------------- 3 files changed, 98 insertions(+), 104 deletions(-) diff --git a/usb/include/usb.h b/usb/include/usb.h index b6ff391..584f31f 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -93,15 +93,16 @@ enum usb_packet_type { #define DESC_OTHER_SPEED_CONFIGURATION 7 #define DESC_INTERFACE_POWER 8 -#define USB_NO_ERROR 0x00 -#define USB_STALLED 0x01 -#define USB_BUFFER_ERROR 0x02 -#define USB_BABBLE 0x04 -#define USB_NAK 0x08 -#define USB_CRC 0x10 -#define USB_TIMEOUT 0x20 -#define USB_BITSTUFF 0x40 -#define USB_TRIVIAL_ERROR 0x80 +#define USB_NO_ERROR 0x0000 +#define USB_STALLED 0x0001 +#define USB_BUFFER_ERROR 0x0002 +#define USB_BABBLE 0x0004 +#define USB_NAK 0x0008 +#define USB_CRC 0x0010 +#define USB_TIMEOUT 0x0020 +#define USB_BITSTUFF 0x0040 +#define USB_TRIVIAL_ERROR 0x0080 +#define USB_STATUS_ERROR 0x0100 #define USB_TOD_SETUP 0x0001 #define USB_TOD_SETUP_DATA_IN 0x0002 diff --git a/usb/main.c b/usb/main.c index 878aefe..9eb20d8 100644 --- a/usb/main.c +++ b/usb/main.c @@ -126,6 +126,9 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) } while (error == USB_NAK) { error = device->hci->do_packet(device, packet); + if (error == USB_NAK) { + cdi_sleep_ms(5); + } } if (error == USB_STALLED) { printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", packet->endpoint, diff --git a/usb/msd.c b/usb/msd.c index ad6f362..471856a 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -169,13 +169,15 @@ void register_msd(struct usb_device* usbdev) for (i = 0; i < usbdev->interface->num_endpoints; i++) { ep_desc = address; if ((ep_desc->endpoint_address & 0x80) && - (ep_desc->attributes == 0x02) && (msc->bulk_ep_in == NULL)) //BULK-IN + (ep_desc->attributes == 0x02) && (msc->bulk_ep_in == NULL)) { + //BULK-IN msc->bulk_ep_in = ep_desc; } else if (!(ep_desc->endpoint_address & 0x80) && (ep_desc->attributes == 0x02) && - (msc->bulk_ep_out == NULL)) //BULK-OUT + (msc->bulk_ep_out == NULL)) { + //BULK-OUT msc->bulk_ep_out = ep_desc; } address += sizeof(struct endpoint_desc); @@ -252,7 +254,7 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) struct command_status_wrapper* csw; int error; - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), (void**) &csw, + if (cdi_alloc_phys_mem(sizeof(*csw), (void**) &csw, (void**) &pcsw) == -1) { return USB_TRIVIAL_ERROR; @@ -274,9 +276,11 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) if ((csw->csw_signature != CSW_SIGNATURE) || (csw->csw_tag != expected_tag) || csw->csw_status) { - return USB_CRC; //Watt wei?? denn ich - + dprintf("0x%08X %i==%i 0x%08X\n", csw->csw_signature, csw->csw_tag, + expected_tag, csw->csw_status); + return USB_STATUS_ERROR; } + return USB_NO_ERROR; } @@ -358,9 +362,9 @@ static int msd_ready(struct usb_device* usbdev) cbw->cbw_signature = CBW_SIGNATURE; cbw->cbw_tag = (expected_tag = cbw_tag++); cbw->cbw_data_transfer_length = 0; - cbw->cbw_flags = 0x80; //IN - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? - cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; //Alles null, also "Test unit ready" if (write_cmd(usbdev, pcbw) != USB_NO_ERROR) { return 0; @@ -395,14 +399,13 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, uintptr_t pb; int bs = strgdev->block_size, error; #ifdef WAIT_FOR_MSD_READY - int j; + int i; #endif -// uint32_t i; struct usb_device* usbdev = ((struct cdi_msd*) strgdev)->usb_device; fdprintf("read(%i, %i)\n", (int) start, (int) count); start += ((struct cdi_msd*) strgdev)->offset; - if (cdi_alloc_phys_mem(bs, &vb, (void**) &pb) == -1) { + if (cdi_alloc_phys_mem(count * bs, &vb, (void**) &pb) == -1) { dprintf("Blockspeicher konnte nicht allociert werden.\n"); return -1; } @@ -415,22 +418,19 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, __asm__ __volatile__ ("hlt"); #endif } - /*for (i = 0; i < count; i++) - {*/ #ifdef WAIT_FOR_MSD_READY - for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { cdi_sleep_ms(20); } #endif - error = msd_read(usbdev, start /* + i*/, count, pb, bs); + error = msd_read(usbdev, start, count, pb, count * bs); if (error != USB_NO_ERROR) { - dprintf("Lesefehler 0x%X bei Block %i.\n", error, 0); + dprintf("Lesefehler 0x%X bei Block %i.\n", error, start); usbdev->locked = 0; return -1; } - memcpy(buffer /* + i * bs*/, vb, count * bs); - //} + memcpy(buffer, vb, count * bs); usbdev->locked = 0; return 0; } @@ -458,11 +458,11 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, cbw->cbw_signature = CBW_SIGNATURE; cbw->cbw_tag = (expected_tag = cbw_tag++); cbw->cbw_data_transfer_length = length; - cbw->cbw_flags = 0x80; //IN - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_flags = 0x80; //IN + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? cbw->cbw_cb_length = 12; cbw->cbw_cb[0] = MSC_CMD_READ10; - cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[1] = 0; //LUN: 0 cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; @@ -484,7 +484,6 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, .type_of_data = USB_TOD_DATA_IN, }; - //Transaktionen eintragen ep_size = msc->bulk_ep_in->max_packet_size; for (i = 0; i < length; i += ep_size) { in_packet.length = (length - i > ep_size) ? ep_size : length - i; @@ -496,12 +495,6 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, in_packet.datatoggle ^= 1; } - /*//Auf deren Ende warten - for (i = 0; i < length; i += ep_size) - { - - }*/ - error = read_status(usbdev, expected_tag); if (error != USB_NO_ERROR) { return error; @@ -561,71 +554,69 @@ static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, uint16_t sectors, uintptr_t phys_buffer, size_t length) { - /*struct msclass_data *msc; - struct command_block_wrapper *cbw; - struct command_status_wrapper *csw; - uintptr_t pcbw, pcsw; - uint32_t expected_tag; - int error; - - if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), (void **)&cbw, (void **)&pcbw) == -1) - { - return USB_TRIVIAL_ERROR; - } - if (cdi_alloc_phys_mem(sizeof(struct command_status_wrapper), (void **)&csw, (void **)&pcsw) == -1) - { - return USB_TRIVIAL_ERROR; - } - if (!phys_buffer || !length) - { - return USB_TRIVIAL_ERROR; - } - msc = (struct msclass_data *)usbdev->classd; - memset(cbw, 0, 0x1F); - cbw->cbw_signature = CBW_SIGNATURE; - cbw->cbw_tag = (expected_tag = cbw_tag++); - cbw->cbw_data_transfer_length = length; - cbw->cbw_flags = 0x00; //OUT - cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? - cbw->cbw_cb_length = 12; - cbw->cbw_cb[0] = MSC_CMD_WRITE10; - cbw->cbw_cb[1] = 0; //LUN: 0 - cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; - cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; - cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; - cbw->cbw_cb[5] = lba & 0x000000FF; - cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; - cbw->cbw_cb[8] = sectors & 0x000000FF; - - error = write_cmd(usbdev, pcbw); - if (error != USB_NO_ERROR) - { - return error; - } - - struct usb_packet out_packet = { - .type = PACKET_OUT, - .endpoint = msc->bulk_ep_out->endpoint_address, - .phys_data = phys_buffer, - .length = length, - .datatoggle = 0, - .type_of_data = USB_TOD_DATA_OUT, - }; - - error = usb_do_packet(usbdev, &out_packet); - if (error != USB_NO_ERROR) - { - return error; - } - - error = read_status(usbdev, expected_tag); - if (error != USB_NO_ERROR) - { - return error; - } - - return USB_NO_ERROR;*/ - return USB_STALLED; + struct msclass_data* msc; + struct command_block_wrapper* cbw; + uintptr_t pcbw; + uint32_t expected_tag; + int error, i, ep_size; + + if (cdi_alloc_phys_mem(sizeof(*cbw), (void**) &cbw, + (void**) &pcbw) == -1) + { + return USB_TRIVIAL_ERROR; + } + if (!phys_buffer || !length) { + return USB_TRIVIAL_ERROR; + } + msc = (struct msclass_data*) usbdev->classd; + memset(cbw, 0, 0x1F); + cbw->cbw_signature = CBW_SIGNATURE; + cbw->cbw_tag = (expected_tag = cbw_tag++); + cbw->cbw_data_transfer_length = length; + cbw->cbw_flags = 0x00; //OUT + cbw->cbw_lun = 0; //Was wei?? ich, wie viele LUNs datt Dingens hat? + cbw->cbw_cb_length = 12; + cbw->cbw_cb[0] = MSC_CMD_WRITE10; + cbw->cbw_cb[1] = 0; //LUN: 0 + cbw->cbw_cb[2] = (lba & 0xFF000000) >> 24; + cbw->cbw_cb[3] = (lba & 0x00FF0000) >> 16; + cbw->cbw_cb[4] = (lba & 0x0000FF00) >> 8; + cbw->cbw_cb[5] = lba & 0x000000FF; + cbw->cbw_cb[7] = (sectors & 0x0000FF00) >> 8; + cbw->cbw_cb[8] = sectors & 0x000000FF; + + error = write_cmd(usbdev, pcbw); + if (error != USB_NO_ERROR) { + return error; + } + + struct usb_packet out_packet = { + .type = PACKET_OUT, + .endpoint = msc->bulk_ep_out->endpoint_address, + .phys_data = phys_buffer, + .length = 0, + .datatoggle = 0, + .type_of_data = USB_TOD_DATA_OUT, + }; + + //Transaktionen eintragen + ep_size = msc->bulk_ep_out->max_packet_size; + for (i = 0; i < length; i += ep_size) { + out_packet.length = (length - i > ep_size) ? ep_size : length - i; + error = usb_do_packet(usbdev, &out_packet); + if (error != USB_NO_ERROR) { + return error; + } + out_packet.phys_data += ep_size; + out_packet.datatoggle ^= 1; + } + + error = read_status(usbdev, expected_tag); + if (error != USB_NO_ERROR) { + return error; + } + + return USB_NO_ERROR; } static void get_partitions(struct cdi_storage_device* cdistrg) @@ -662,8 +653,7 @@ static void get_partitions(struct cdi_storage_device* cdistrg) break; } sprintf((char*) cdimsd->cdi_device.dev.name, "%sp%i", - cdistrg->dev.name, - j++); + cdistrg->dev.name, j++); cdimsd->cdi_device.block_size = base_cdimsd->cdi_device.block_size; cdimsd->cdi_device.block_count = partition[i].size; _dprintf(" %s", cdimsd->cdi_device.dev.name); -- 1.6.3.3 From max at tyndur.org Sun Aug 23 11:22:59 2009 From: max at tyndur.org (max at tyndur.org) Date: Sun, 23 Aug 2009 11:22:59 +0200 Subject: [cdi-devel] [PATCH] Correct DATA0/DATA1 treatment and packet size Message-ID: <1251019379-13834-1-git-send-email-max@tyndur.org> From: Max Reitz + The decision about the usage of DATA0 or DATA1 is now made by usb_do_packet(), that is more standard compliant. * You cannot send 512 byte packets to 64 byte endpoints, you have to send them in small 64 byte packets. Because of that, you also have to fetch the first eight byte of the device descriptor before to know the size of EP0 before you can fetch the whole descriptor. Signed-off-by: Max Reitz --- usb/include/msd.h | 2 +- usb/include/uhci.h | 2 +- usb/include/usb.h | 10 +++--- usb/main.c | 85 +++++++++++++++++++++++++++++++++++++-------------- usb/msd.c | 41 +++++++++---------------- usb/uhci.c | 14 ++++----- 6 files changed, 89 insertions(+), 65 deletions(-) diff --git a/usb/include/msd.h b/usb/include/msd.h index 1af55f3..9f40469 100644 --- a/usb/include/msd.h +++ b/usb/include/msd.h @@ -95,4 +95,4 @@ struct part_table_entry { #define MSC_CMD_READ12 0xA8 #define MSC_CMD_WRITE12 0xAA -#endif \ No newline at end of file +#endif diff --git a/usb/include/uhci.h b/usb/include/uhci.h index edb98db..240619b 100644 --- a/usb/include/uhci.h +++ b/usb/include/uhci.h @@ -103,4 +103,4 @@ struct transfer { volatile int error; }; -#endif \ No newline at end of file +#endif diff --git a/usb/include/usb.h b/usb/include/usb.h index 584f31f..a7aecd4 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -245,6 +245,7 @@ struct usb_device { int id; int port; int low_speed; + struct endpoint_desc* ep0; struct device_desc* device; struct config_desc* config; struct interface_desc* interface; @@ -252,6 +253,7 @@ struct usb_device { int stalled; int locked; int expects; + int data_toggle; }; struct msclass_data { @@ -264,14 +266,12 @@ struct msclass_data { struct usb_packet { /// Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) enum usb_packet_type type; - /// Der gewuenschte Endpoint (0 bis 15) - int endpoint; + /// Der gewuenschte Endpoint + struct endpoint_desc* endpoint; /// Die physische Adresse des zu verwendenden Datenpuffers uintptr_t phys_data; /// Die Laenge des Puffers int length; - /// Gibt an, ob DATA0 (0) oder DATA1 (1) verwendet werden soll - int datatoggle; /// Typ der Daten (zur Sicherheit, damit es kein doofes STALL gibt) int type_of_data; }; @@ -290,4 +290,4 @@ void init_msc_driver(void); void register_msd(struct usb_device* usbdev); void uhci_init(struct cdi_device* cdi_hci); -#endif \ No newline at end of file +#endif diff --git a/usb/main.c b/usb/main.c index 9eb20d8..f2e957e 100644 --- a/usb/main.c +++ b/usb/main.c @@ -104,6 +104,7 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) { int error = USB_NAK; int tod_short, tod = packet->type_of_data; + int i = 0, mps; if (!(device->expects & tod)) { dprintf("0x%04X erwartet, 0x%04X bekommen...?\n", device->expects, tod); @@ -117,23 +118,41 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) } //dprintf("TOD: %s\n", tod_name[tod_short]); device->expects = next_data_type[tod_short]; - packet->endpoint &= 0x07; packet->type &= 0xFF; if (device->stalled) { //TODO: Irgendwie entstallen, evtl.? oO dprintf("Zugriff auf stalled-Ger??t verweigert.\n"); return USB_STALLED; } - while (error == USB_NAK) { - error = device->hci->do_packet(device, packet); - if (error == USB_NAK) { - cdi_sleep_ms(5); + + struct usb_packet send_packet = { + .type = packet->type, + .endpoint = packet->endpoint, + .phys_data = packet->phys_data + }; + mps = packet->endpoint->max_packet_size; + + for (i = 0; (i < packet->length) || (!i && !packet->length); i += mps) { + send_packet.length = + (packet->length - i > mps) ? mps : (packet->length - i); + error = USB_NAK; + while (error == USB_NAK) { + error = device->hci->do_packet(device, &send_packet); + if (error == USB_NAK) { + cdi_sleep_ms(5); + } + } + device->data_toggle ^= 1; + send_packet.phys_data += mps; + if (error == USB_STALLED) { + printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", + packet->endpoint->endpoint_address, + device->id); + for (;;) { + } + device->stalled = 1; + break; } - } - if (error == USB_STALLED) { - printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", packet->endpoint, - device->id); - device->stalled = 1; } return error; } @@ -170,14 +189,12 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, struct usb_packet setup_packet = { .type = PACKET_SETUP, - .endpoint = 0, + .endpoint = device->ep0, .phys_data = psetup, .length = sizeof(*setup), - .datatoggle = 0, .type_of_data = USB_TOD_SETUP, }; - if (usb_do_packet(device, &setup_packet)) { return NULL; } @@ -188,10 +205,9 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, } else { struct usb_packet data_packet = { .type = (direction == DEV_TO_HOST ? PACKET_IN : PACKET_OUT), - .endpoint = 0, + .endpoint = device->ep0, .phys_data = pbuf, .length = length, - .datatoggle = 1, .type_of_data = (direction == DEV_TO_HOST ? USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), @@ -202,11 +218,11 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, if (rval == USB_NO_ERROR) { struct usb_packet ack_packet = { - .endpoint = 0, + .endpoint = device->ep0, .phys_data = 0, .length = 0, - .datatoggle = 1, }; + device->data_toggle = 1; if (no_data || (direction == HOST_TO_DEV)) { ack_packet.type = PACKET_IN; @@ -305,24 +321,45 @@ static void enum_device(struct usb_device* usbdev) void* position; int i, id; + //Ger??t und EP0 initialisieren usbdev->stalled = 0; usbdev->locked = 0; usbdev->expects = USB_TOD_SETUP | USB_TOD_COMMAND; + usbdev->data_toggle = 0; + usbdev->ep0 = malloc(sizeof(*usbdev->ep0)); + usbdev->ep0->length = sizeof(*usbdev->ep0); + usbdev->ep0->descriptor_type = DESC_ENDPOINT; + usbdev->ep0->endpoint_address = 0; + usbdev->ep0->attributes = 0; + usbdev->ep0->max_packet_size = 8; + usbdev->ep0->interval = 0; + + //Erste acht Bytes des Device-Descriptors abrufen und die maximale Paketgr????e von EP0 feststellen dev_desc = - do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), STD_REQUEST, - REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, + do_control(usbdev, DEV_TO_HOST, NULL, 8, STD_REQUEST, REC_DEVICE, + GET_DESCRIPTOR, + DESC_DEVICE << 8, 0); if (dev_desc == NULL) { return; } - usbdev->device = dev_desc; - dprintf("0x%04X:0x%04X (%i) -> ", dev_desc->vendor_id, dev_desc->device_id, - dev_desc->class_id); + usbdev->ep0->max_packet_size = dev_desc->max_packet_size0; + + //USB-Adresse zuweisen do_control(usbdev, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, REC_DEVICE, SET_ADDRESS, (id = usb_dev_ids++), 0); usbdev->id = id; - _dprintf("%i\n", id); + + dev_desc = + do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, + 0); + usbdev->device = dev_desc; + dprintf("0x%04X:0x%04X (%i) -> %i\n", dev_desc->vendor_id, + dev_desc->device_id, dev_desc->class_id, + id); + if (dev_desc->iManufacturer) { name = do_control(usbdev, DEV_TO_HOST, NULL, 64, STD_REQUEST, REC_DEVICE, @@ -389,7 +426,7 @@ static void enum_device(struct usb_device* usbdev) } position = conf_desc; position += sizeof(struct config_desc); - best_if = position; //Standard-IF + best_if = position; //Standard-IF for (i = 0; i < conf_desc->num_interfaces; i++) { if_desc = position; //TODO: Mehr Klassencodes nat??rlich! diff --git a/usb/msd.c b/usb/msd.c index 471856a..fcfb0a3 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -228,10 +228,9 @@ static int write_cmd(struct usb_device* usbdev, uintptr_t src) struct msclass_data* msc = (struct msclass_data*) usbdev->classd; struct usb_packet cmd_packet = { .type = PACKET_OUT, - .endpoint = msc->bulk_ep_out->endpoint_address, + .endpoint = msc->bulk_ep_out, .phys_data = src, .length = 0x1F, - .datatoggle = 0, .type_of_data = USB_TOD_COMMAND, }; @@ -262,10 +261,9 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) struct usb_packet status_packet = { .type = PACKET_IN, - .endpoint = msc->bulk_ep_in->endpoint_address, + .endpoint = msc->bulk_ep_in, .phys_data = pcsw, .length = 0x0D, - .datatoggle = 0, .type_of_data = USB_TOD_STATUS, }; @@ -277,7 +275,8 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) (csw->csw_tag != expected_tag) || csw->csw_status) { dprintf("0x%08X %i==%i 0x%08X\n", csw->csw_signature, csw->csw_tag, - expected_tag, csw->csw_status); + expected_tag, + csw->csw_status); return USB_STATUS_ERROR; } @@ -323,10 +322,9 @@ static int msd_get_capacity(struct usb_device* usbdev, uint32_t* block_size, struct usb_packet in_packet = { .type = PACKET_IN, - .endpoint = msc->bulk_ep_in->endpoint_address, + .endpoint = msc->bulk_ep_in, .phys_data = pcap, .length = sizeof(*cap), - .datatoggle = 0, .type_of_data = USB_TOD_DATA_IN, }; @@ -443,7 +441,7 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, struct command_block_wrapper* cbw; uintptr_t pcbw; uint32_t expected_tag; - int error, i, ep_size; + int error; if (cdi_alloc_phys_mem(sizeof(struct command_block_wrapper), (void**) &cbw, (void**) &pcbw) == -1) @@ -477,22 +475,15 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, struct usb_packet in_packet = { .type = PACKET_IN, - .endpoint = msc->bulk_ep_in->endpoint_address, + .endpoint = msc->bulk_ep_in, .phys_data = phys_buffer, - .length = 0, - .datatoggle = 0, + .length = length, .type_of_data = USB_TOD_DATA_IN, }; - ep_size = msc->bulk_ep_in->max_packet_size; - for (i = 0; i < length; i += ep_size) { - in_packet.length = (length - i > ep_size) ? ep_size : length - i; - error = usb_do_packet(usbdev, &in_packet); - if (error != USB_NO_ERROR) { - return error; - } - in_packet.phys_data += ep_size; - in_packet.datatoggle ^= 1; + error = usb_do_packet(usbdev, &in_packet); + if (error != USB_NO_ERROR) { + return error; } error = read_status(usbdev, expected_tag); @@ -592,14 +583,12 @@ static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, struct usb_packet out_packet = { .type = PACKET_OUT, - .endpoint = msc->bulk_ep_out->endpoint_address, + .endpoint = msc->bulk_ep_out, .phys_data = phys_buffer, .length = 0, - .datatoggle = 0, .type_of_data = USB_TOD_DATA_OUT, }; - //Transaktionen eintragen ep_size = msc->bulk_ep_out->max_packet_size; for (i = 0; i < length; i += ep_size) { out_packet.length = (length - i > ep_size) ? ep_size : length - i; @@ -608,7 +597,6 @@ static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, return error; } out_packet.phys_data += ep_size; - out_packet.datatoggle ^= 1; } error = read_status(usbdev, expected_tag); @@ -638,7 +626,7 @@ static void get_partitions(struct cdi_storage_device* cdistrg) dprintf("Partitionen auf %s:", cdistrg->dev.name); for (i = 0; i < 4; i++) { if (partition[i].type && partition[i].size && - (partition[i].start + partition[i].size < cdistrg->block_count)) + (partition[i].start + partition[i].size <= cdistrg->block_count)) { cdimsd = malloc(sizeof(struct cdi_msd)); if (cdimsd == NULL) { @@ -653,7 +641,8 @@ static void get_partitions(struct cdi_storage_device* cdistrg) break; } sprintf((char*) cdimsd->cdi_device.dev.name, "%sp%i", - cdistrg->dev.name, j++); + cdistrg->dev.name, + j++); cdimsd->cdi_device.block_size = base_cdimsd->cdi_device.block_size; cdimsd->cdi_device.block_count = partition[i].size; _dprintf(" %s", cdimsd->cdi_device.dev.name); diff --git a/usb/uhci.c b/usb/uhci.c index f0087f4..e300eb3 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -53,8 +53,7 @@ static struct cdi_driver cdi_driver; static cdi_list_t active_transfers; static void uhci_handler(struct cdi_device* dev); -static int uhci_do_packet(struct usb_device* usbdev, - struct usb_packet* packet); +static int uhci_do_packet(struct usb_device* usbdev, struct usb_packet* packet); static cdi_list_t get_devices(struct hci* gen_hci); static void activate_device(struct hci* gen_hci, struct usb_device* device); static int get_current_frame(struct hci* gen_hci); @@ -216,8 +215,7 @@ static inline int tsl(volatile int* variable) static volatile int locked = 0; -static int uhci_do_packet(struct usb_device* usbdev, - struct usb_packet* packet) +static int uhci_do_packet(struct usb_device* usbdev, struct usb_packet* packet) { struct uhci* uhci = (struct uhci*) usbdev->hci; struct uhci_td* td; @@ -243,18 +241,18 @@ static int uhci_do_packet(struct usb_device* usbdev, __asm__ __volatile__ ("hlt"); #endif } - qh->next = 1; //Invalid + qh->next = 1; //Invalid qh->transfer = ptd; memset(td, 0, sizeof(struct uhci_td)); - td->next = 1; //Invalid + td->next = 1; //Invalid td->active = 1; td->ioc = 1; - td->data_toggle = packet->datatoggle; + td->data_toggle = usbdev->data_toggle; td->low_speed = usbdev->low_speed; td->errors = 1; td->pid = packet->type; td->device = usbdev->id; - td->endpoint = packet->endpoint; + td->endpoint = packet->endpoint->endpoint_address & 0x07; td->maxlen = packet->length ? packet->length - 1 : 0x7FF; td->buffer = packet->phys_data; addr = malloc(sizeof(struct transfer)); -- 1.6.3.3 From max at tyndur.org Sun Aug 23 12:29:18 2009 From: max at tyndur.org (max at tyndur.org) Date: Sun, 23 Aug 2009 12:29:18 +0200 Subject: [cdi-devel] [PATCH] Double reset on enum, no toggle change on error Message-ID: <1251023358-15569-1-git-send-email-max@tyndur.org> From: Max Reitz * Reset is needed two times on device enumeration (before reading the first eight bytes of the device descriptor and afterwards). * DATA0/DATA1 toggle bit may not be changed if an error occured. Signed-off-by: Max Reitz --- usb/include/usb.h | 2 + usb/main.c | 37 ++++++++++++++++++++--------- usb/msd.c | 66 +++++++++++++++++++++++++--------------------------- usb/uhci.c | 17 ++++++++++++- 4 files changed, 74 insertions(+), 48 deletions(-) diff --git a/usb/include/usb.h b/usb/include/usb.h index 0fc6567..9a94b2e 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -242,6 +242,7 @@ struct class_data { struct usb_device { struct hci* hci; + struct usb_device* hub; int id; int port; int low_speed; @@ -254,6 +255,7 @@ struct usb_device { int locked; int expects; int data_toggle; + void (* reset)(struct usb_device* device); }; struct msclass_data { diff --git a/usb/main.c b/usb/main.c index abfb936..7250092 100644 --- a/usb/main.c +++ b/usb/main.c @@ -142,8 +142,12 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) cdi_sleep_ms(5); } } - device->data_toggle ^= 1; - send_packet.data += mps; + if (error != USB_NO_ERROR) { + i -= mps; + } else { + device->data_toggle ^= 1; + send_packet.data += mps; + } if (error == USB_STALLED) { printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", packet->endpoint->endpoint_address, @@ -167,11 +171,9 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, no_data = direction & NO_DATA; direction &= 0x80; - if ((direction != HOST_TO_DEV) || !length || (buffer == NULL)) { buffer = malloc(length); } - setup->request_type = direction | (rtype & 0x60) | (recipient & 0x1F); setup->request = request; setup->value = value; @@ -192,7 +194,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, if (no_data) { rval = USB_NO_ERROR; - buffer = (void*) 0xFFFFFFFF; //Kein Fehler, aber auch keine Daten + buffer = (void*) 0xFFFFFFFF; //Kein Fehler, aber auch keine Daten } else { struct usb_packet data_packet = { .type = (direction == DEV_TO_HOST ? PACKET_IN : PACKET_OUT), @@ -225,7 +227,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, rval = usb_do_packet(device, &ack_packet); } - return (rval == USB_NO_ERROR) ? buffer: NULL; + return (rval == USB_NO_ERROR) ? buffer : NULL; } static void usb_init(void) @@ -325,6 +327,9 @@ static void enum_device(struct usb_device* usbdev) usbdev->ep0->max_packet_size = 8; usbdev->ep0->interval = 0; + //Resetten + usbdev->reset(usbdev); + //Erste acht Bytes des Device-Descriptors abrufen und die maximale Paketgr????e von EP0 feststellen dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, 8, STD_REQUEST, REC_DEVICE, @@ -336,12 +341,16 @@ static void enum_device(struct usb_device* usbdev) } usbdev->ep0->max_packet_size = dev_desc->max_packet_size0; + //Nochmals resetten + usbdev->reset(usbdev); + //USB-Adresse zuweisen do_control(usbdev, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, REC_DEVICE, SET_ADDRESS, (id = usb_dev_ids++), 0); usbdev->id = id; + //Den ganzen Device-Descriptor einlesen dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, @@ -454,6 +463,14 @@ static void enum_device(struct usb_device* usbdev) } } +static void reset_hub_device(struct usb_device* usbdev) +{ + do_control(usbdev->hub, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, + SET_FEATURE, PORTF_RESET, + usbdev->port + 1); + cdi_sleep_ms(20); +} + static void enumerate_hub(struct usb_device* usbdev) { struct hub_desc* hub_desc; @@ -481,17 +498,13 @@ static void enumerate_hub(struct usb_device* usbdev) SET_FEATURE, PORTF_POWER, i + 1); cdi_sleep_ms(hub_desc->pwron2pwrgood * 2); - //Resetten - do_control(usbdev, HOST_TO_DEV, NULL, 0, CLS_REQUEST, REC_OTHER, - SET_FEATURE, PORTF_RESET, - i + 1); - //Reset sollte jetzt eigtl. schon beendet sein - //(do_control wartet ja 50 ms) down = malloc(sizeof(struct usb_device)); down->hci = usbdev->hci; + down->hub = usbdev; down->id = 0; down->port = i; down->low_speed = (port_status[0] & PORT_LOWSPEED) && 1; + down->reset = &reset_hub_device; enum_device(down); } } diff --git a/usb/msd.c b/usb/msd.c index 9d843bf..6fb8a42 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -367,10 +367,12 @@ static inline int tsl(volatile int* variable) } static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, - uint16_t sectors, void* buffer, size_t length); + uint16_t sectors, void* buffer, + size_t length); static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, - uint64_t count, void* buffer) + uint64_t count, + void* buffer) { int bs = strgdev->block_size, error; #ifdef WAIT_FOR_MSD_READY @@ -394,7 +396,6 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { cdi_sleep_ms(20); } - #endif error = msd_read(usbdev, start, count, buffer, count * bs); if (error != USB_NO_ERROR) { @@ -407,7 +408,8 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, } static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, - uint16_t sectors, void* buffer, size_t length) + uint16_t sectors, void* buffer, + size_t length) { struct msclass_data* msc; struct command_block_wrapper _cbw; @@ -415,10 +417,9 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, uint32_t expected_tag; int error; - if (!buffer || !length) { + if ((buffer == NULL) || !length) { return USB_TRIVIAL_ERROR; } - msc = (struct msclass_data*) usbdev->classd; memset(cbw, 0, 0x1F); cbw->cbw_signature = CBW_SIGNATURE; @@ -463,57 +464,59 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, } static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, - uint16_t sectors, void* buffer, size_t length); + uint16_t sectors, void* buffer, + size_t length); static int msd_cdi_write(struct cdi_storage_device* strgdev, uint64_t start, - uint64_t count, void* buffer) + uint64_t count, + void* buffer) { int bs = strgdev->block_size, error; #ifdef WAIT_FOR_MSD_READY - int j; + int i; #endif - uint32_t i; struct usb_device* usbdev = ((struct cdi_msd*) strgdev)->usb_device; fdprintf("write(%i, %i)\n", (int) start, (int) count); start += ((struct cdi_msd*) strgdev)->offset; + if (!count) { + dprintf("Leere Schreibanfrage.\n"); + return 0; + } while (tsl(&usbdev->locked)) { #ifndef CDI_STANDALONE __asm__ __volatile__ ("hlt"); #endif } - for (i = 0; i < count; i++) { #ifdef WAIT_FOR_MSD_READY - for (j = 0; !msd_ready(usbdev) && (j < 10); j++) { - cdi_sleep_ms(20); - } - + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { + cdi_sleep_ms(20); + } #endif - error = msd_write(usbdev, start + i, 1, buffer + i * bs, bs); - if (error != USB_NO_ERROR) { - dprintf("Schreibfehler 0x%X bei Block %i.\n", error, i); - usbdev->locked = 0; - return -1; - } + error = msd_write(usbdev, start, count, buffer, count * bs); + if (error != USB_NO_ERROR) { + dprintf("Schreibfehler 0x%X bei Block %i.\n", error, start); + usbdev->locked = 0; + return -1; } usbdev->locked = 0; return 0; } static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, - uint16_t sectors, void* buffer, size_t length) + uint16_t sectors, void* buffer, + size_t length) { struct msclass_data* msc; struct command_block_wrapper _cbw; struct command_block_wrapper* cbw = &_cbw; uint32_t expected_tag; - int error, i, ep_size; + int error; - if (!buffer || !length) { + if ((buffer == NULL) || !length) { return USB_TRIVIAL_ERROR; } - msc = (struct msclass_data*) usbdev->classd; memset(cbw, 0, 0x1F); cbw->cbw_signature = CBW_SIGNATURE; @@ -540,18 +543,13 @@ static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, .type = PACKET_OUT, .endpoint = msc->bulk_ep_out, .data = buffer, - .length = 0, + .length = length, .type_of_data = USB_TOD_DATA_OUT, }; - ep_size = msc->bulk_ep_out->max_packet_size; - for (i = 0; i < length; i += ep_size) { - out_packet.length = (length - i > ep_size) ? ep_size : length - i; - error = usb_do_packet(usbdev, &out_packet); - if (error != USB_NO_ERROR) { - return error; - } - out_packet.data += ep_size; + error = usb_do_packet(usbdev, &out_packet); + if (error != USB_NO_ERROR) { + return error; } error = read_status(usbdev, expected_tag); diff --git a/usb/uhci.c b/usb/uhci.c index 03ad4c6..d302364 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -59,6 +59,7 @@ static void activate_device(struct hci* gen_hci, struct usb_device* device); static int get_current_frame(struct hci* gen_hci); static void uhci_deinit(struct cdi_device* cdi_hci); static void uhci_kill(struct cdi_driver* cdi_hcd); +static void uhci_reset_device(struct usb_device* device); struct cdi_driver* init_uhcd() @@ -184,19 +185,29 @@ static cdi_list_t get_devices(struct hci* gen_hci) dev->port = i; dev->low_speed = (cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & RPORT_LOSPD) && 1; + dev->reset = &uhci_reset_device; cdi_list_push(dlist, dev); } _dprintf("\n"); return dlist; } +static void uhci_reset_device(struct usb_device* device) +{ + struct uhci* uhci = (struct uhci*) device->hci; + + cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, + RPORT_ENABLE | RPORT_RESET | RPORT_CSC); + cdi_sleep_ms(20); +} + static void activate_device(struct hci* gen_hci, struct usb_device* device) { struct uhci* uhci = (struct uhci*) gen_hci; dprintf("Ger??t an Port %i wird aktiviert.\n", device->port); cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, - RPORT_RESET | RPORT_ENABLE | RPORT_CSC); + RPORT_ENABLE | RPORT_CSC); cdi_sleep_ms(20); } @@ -243,7 +254,9 @@ static int uhci_do_packet(struct usb_device* usbdev, struct usb_packet* packet) data = NULL; phys_data = 0; } else { - if (cdi_alloc_phys_mem(packet->length, &data, (void**) &phys_data) == -1) { + if (cdi_alloc_phys_mem(packet->length, &data, + (void**) &phys_data) == -1) + { return USB_TRIVIAL_ERROR; } if (packet->type != PACKET_IN) { -- 1.6.3.3 From max at tyndur.org Sun Aug 23 18:29:07 2009 From: max at tyndur.org (max at tyndur.org) Date: Sun, 23 Aug 2009 18:29:07 +0200 Subject: [cdi-devel] [PATCH 2/2] [usb] No memory error, remove STALL Message-ID: <1251044947-15300-1-git-send-email-max@tyndur.org> From: Max Reitz * This is a REALLY ugly patch, though it works. At startup, the driver allocates about 1 MB of memory per UHC and uses that for the queue heads, the transfer descriptors and the packet buffer maximum packet size: 1 kB). + STALL is now removed via the ClearEndpointFeature(ENDPOINT_STALL) request. Signed-off-by: Max Reitz --- usb/include/uhci.h | 24 ++++++--- usb/include/usb.h | 1 - usb/main.c | 18 +++---- usb/msd.c | 45 +++++++++++------ usb/uhci.c | 143 ++++++++++++++++++++++++++++----------------------- 5 files changed, 131 insertions(+), 100 deletions(-) diff --git a/usb/include/uhci.h b/usb/include/uhci.h index 240619b..d1a7c61 100644 --- a/usb/include/uhci.h +++ b/usb/include/uhci.h @@ -53,17 +53,10 @@ #define RPORT_DEVICE 0x0001 -struct uhci { - struct hci gen_hci; - uint16_t pbase; - uintptr_t phys_frame_list; - uint32_t* frame_list; - int root_ports; -}; - struct uhci_qh { volatile uint32_t next; volatile uint32_t transfer; + uint32_t align[2]; //F??rs korrekte Alignment (an 16 Bytes) } __attribute__((packed)); struct uhci_td { @@ -103,4 +96,19 @@ struct transfer { volatile int error; }; +struct uhci { + struct hci gen_hci; + uint16_t pbase; + uintptr_t phys_frame_list; + uint32_t* frame_list; + int root_ports; + struct uhci_qh* queue_heads; + struct uhci_qh* phys_queue_heads; + struct uhci_td* transfer_descs; + struct uhci_td* phys_transfer_descs; + //FIXME Das tut einfach nur weh. + void** data_buffers; + uintptr_t* phys_data_buffers; +}; + #endif diff --git a/usb/include/usb.h b/usb/include/usb.h index 9a94b2e..459a340 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -251,7 +251,6 @@ struct usb_device { struct config_desc* config; struct interface_desc* interface; struct class_data* classd; - int stalled; int locked; int expects; int data_toggle; diff --git a/usb/main.c b/usb/main.c index 7250092..b09bfba 100644 --- a/usb/main.c +++ b/usb/main.c @@ -52,6 +52,9 @@ static int usb_dev_ids = 1; static void usb_init(void); static void enumerate_hub(struct usb_device* usbdev); +static void* do_control(struct usb_device* device, int direction, void* buffer, + int length, int rtype, int recipient, int request, int value, + int index); #ifdef CDI_STANDALONE int main(void) @@ -119,11 +122,6 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) //dprintf("TOD: %s\n", tod_name[tod_short]); device->expects = next_data_type[tod_short]; packet->type &= 0xFF; - if (device->stalled) { - //TODO: Irgendwie entstallen, evtl.? oO - dprintf("Zugriff auf stalled-Ger??t verweigert.\n"); - return USB_STALLED; - } struct usb_packet send_packet = { .type = packet->type, @@ -152,10 +150,9 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", packet->endpoint->endpoint_address, device->id); - for (;;) { - } - device->stalled = 1; - break; + do_control(device, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, + REC_ENDPOINT, CLEAR_FEATURE, 0, + packet->endpoint->endpoint_address); } } return error; @@ -171,9 +168,11 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, no_data = direction & NO_DATA; direction &= 0x80; + if ((direction != HOST_TO_DEV) || !length || (buffer == NULL)) { buffer = malloc(length); } + setup->request_type = direction | (rtype & 0x60) | (recipient & 0x1F); setup->request = request; setup->value = value; @@ -315,7 +314,6 @@ static void enum_device(struct usb_device* usbdev) int i, id; //Ger??t und EP0 initialisieren - usbdev->stalled = 0; usbdev->locked = 0; usbdev->expects = USB_TOD_SETUP | USB_TOD_COMMAND; usbdev->data_toggle = 0; diff --git a/usb/msd.c b/usb/msd.c index 6fb8a42..415657e 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -366,6 +366,8 @@ static inline int tsl(volatile int* variable) return rval; } +#define MAX_ACCESS_BLOCKS 32 + static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, uint16_t sectors, void* buffer, size_t length); @@ -379,6 +381,7 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, int i; #endif struct usb_device* usbdev = ((struct cdi_msd*) strgdev)->usb_device; + int j, bbs; fdprintf("read(%i, %i)\n", (int) start, (int) count); start += ((struct cdi_msd*) strgdev)->offset; @@ -392,17 +395,23 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, __asm__ __volatile__ ("hlt"); #endif } + for (j = 0; j < count; j += MAX_ACCESS_BLOCKS) { #ifdef WAIT_FOR_MSD_READY - for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { - cdi_sleep_ms(20); - } + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { + cdi_sleep_ms(20); + } #endif - error = msd_read(usbdev, start, count, buffer, count * bs); - if (error != USB_NO_ERROR) { - dprintf("Lesefehler 0x%X bei Block %lld.\n", error, start); - usbdev->locked = 0; - return -1; + bbs = (count - j > MAX_ACCESS_BLOCKS) ? MAX_ACCESS_BLOCKS : count - j; + _dprintf("/"); + error = msd_read(usbdev, start + j, bbs, buffer + j * bs, bbs * bs); + _dprintf("\\"); + if (error != USB_NO_ERROR) { + dprintf("Lesefehler 0x%X bei Block %lld.\n", error, start + j); + usbdev->locked = 0; + return -1; + } } + _dprintf("\n"); usbdev->locked = 0; return 0; } @@ -476,6 +485,7 @@ static int msd_cdi_write(struct cdi_storage_device* strgdev, uint64_t start, int i; #endif struct usb_device* usbdev = ((struct cdi_msd*) strgdev)->usb_device; + int j, bbs; fdprintf("write(%i, %i)\n", (int) start, (int) count); start += ((struct cdi_msd*) strgdev)->offset; @@ -489,16 +499,19 @@ static int msd_cdi_write(struct cdi_storage_device* strgdev, uint64_t start, __asm__ __volatile__ ("hlt"); #endif } + for (j = 0; j < count; j += MAX_ACCESS_BLOCKS) { #ifdef WAIT_FOR_MSD_READY - for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { - cdi_sleep_ms(20); - } + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { + cdi_sleep_ms(20); + } #endif - error = msd_write(usbdev, start, count, buffer, count * bs); - if (error != USB_NO_ERROR) { - dprintf("Schreibfehler 0x%X bei Block %i.\n", error, start); - usbdev->locked = 0; - return -1; + bbs = (count - j > MAX_ACCESS_BLOCKS) ? MAX_ACCESS_BLOCKS : count - j; + error = msd_write(usbdev, start + j, bbs, buffer + j * bs, bbs * bs); + if (error != USB_NO_ERROR) { + dprintf("Schreibfehler 0x%X bei Block %i.\n", error, start + j); + usbdev->locked = 0; + return -1; + } } usbdev->locked = 0; return 0; diff --git a/usb/uhci.c b/usb/uhci.c index d302364..fb1fdb3 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -95,7 +95,6 @@ void uhci_init(struct cdi_device* cdi_hci) struct cdi_pci_resource* res; int i, size = 0x14; - //cdi_pci_alloc_ioports(gen_hci->pcidev); uhci->pbase = 0; for (i = 0; (res = cdi_list_get(gen_hci->pcidev->resources, i)) != NULL; i++) @@ -120,16 +119,45 @@ void uhci_init(struct cdi_device* cdi_hci) dprintf("Frame List konnte nicht allociert werden!\n"); return; } + if (cdi_alloc_phys_mem(sizeof(*uhci->queue_heads) * 1024, + (void**) &uhci->queue_heads, + (void**) &uhci->phys_queue_heads) == -1) + { + dprintf("QH-Speicher konnte nicht allociert werden!\n"); + return; + } + if (cdi_alloc_phys_mem(sizeof(*uhci->transfer_descs) * 1024, + (void**) &uhci->transfer_descs, + (void**) &uhci->phys_transfer_descs) == -1) + { + dprintf("TD-Speicher konnte nicht allociert werden!\n"); + return; + } + //FIXME Das tut derma??en weh. + uhci->data_buffers = malloc(sizeof(void*) * 1024); + uhci->phys_data_buffers = malloc(sizeof(uintptr_t) * 1024); + for (i = 0; i < 1024; i++) { + if (cdi_alloc_phys_mem(1024, &uhci->data_buffers[i], + (void**) &uhci->phys_data_buffers[i]) == -1) + { + dprintf("Paketspeicher konnte nicht allociert werden!\n"); + return; + } + } cdi_register_irq(gen_hci->pcidev->irq, &uhci_handler, cdi_hci); uhci->root_ports = (size - 0x10) >> 1; - if (uhci->root_ports > 7) { //Laut Linuxkernel ist das so "weird", dass da was nicht stimmen kann... + if (uhci->root_ports > 7) { //Laut Linuxkernel ist das so "weird", dass da was nicht stimmen kann... uhci->root_ports = 2; } dprintf("UHC mit I/O 0x%04X (%i Ports) und IRQ %i\n", uhci->pbase, uhci->root_ports, gen_hci->pcidev->irq); for (i = 0; i < 1024; i++) { - uhci->frame_list[i] = 1; //Invalid + uhci->queue_heads[i].next = 1; //Invalid + uhci->queue_heads[i].transfer = 1; //Invalid + } + for (i = 0; i < 1024; i++) { + uhci->frame_list[i] = (uintptr_t) &uhci->phys_queue_heads[i] | 2; } dprintf("Resetten...\n"); //HC zur??cksetzen @@ -229,80 +257,65 @@ static volatile int locked = 0; static int uhci_do_packet(struct usb_device* usbdev, struct usb_packet* packet) { struct uhci* uhci = (struct uhci*) usbdev->hci; - struct uhci_td* td; - struct uhci_qh* qh; - uintptr_t ptd, pqh; - struct transfer* addr; - int timeout; - void* data; - uintptr_t phys_data; - - int frame = (usbdev->hci->get_frame(usbdev->hci) + 3) & 0x3FF; - - if (cdi_alloc_phys_mem(sizeof(struct uhci_td), (void**) &td, - (void**) &ptd) == -1) - { - return USB_TRIVIAL_ERROR; + struct uhci_td td; + struct transfer addr; + int timeout, frame; + + memset(&td, 0, sizeof(td)); + td.next = 1; //Invalid + td.active = 1; + td.ioc = 1; + td.data_toggle = usbdev->data_toggle; + td.low_speed = usbdev->low_speed; + td.errors = 1; + td.pid = packet->type; + td.device = usbdev->id; + td.endpoint = packet->endpoint->endpoint_address & 0x07; + td.maxlen = packet->length ? packet->length - 1 : 0x7FF; + while (tsl(&locked)) { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif } - if (cdi_alloc_phys_mem(sizeof(struct uhci_qh), (void**) &qh, - (void**) &pqh) == -1) - { - return USB_TRIVIAL_ERROR; + frame = (cdi_inw(uhci->pbase + UHCI_FRNUM) + 5) & 0x3FF; + while (!(uhci->queue_heads[frame].transfer & 1)) { + frame++; + frame &= 0x3FF; } - - if (packet->length == 0) { - data = NULL; - phys_data = 0; + if (!packet->length) { + td.buffer = 0; } else { - if (cdi_alloc_phys_mem(packet->length, &data, - (void**) &phys_data) == -1) - { - return USB_TRIVIAL_ERROR; - } + td.buffer = uhci->phys_data_buffers[frame]; if (packet->type != PACKET_IN) { - memcpy(data, packet->data, packet->length); + memcpy(uhci->data_buffers[frame], packet->data, packet->length); } } - - while (tsl(&locked)) { -#ifndef CDI_STANDALONE - __asm__ __volatile__ ("hlt"); -#endif - } - qh->next = 1; //Invalid - qh->transfer = ptd; - memset(td, 0, sizeof(struct uhci_td)); - td->next = 1; //Invalid - td->active = 1; - td->ioc = 1; - td->data_toggle = usbdev->data_toggle; - td->low_speed = usbdev->low_speed; - td->errors = 1; - td->pid = packet->type; - td->device = usbdev->id; - td->endpoint = packet->endpoint->endpoint_address & 0x07; - td->maxlen = packet->length ? packet->length - 1 : 0x7FF; - td->buffer = phys_data; - addr = malloc(sizeof(struct transfer)); - addr->virt = td; - addr->phys = ptd; - addr->error = 0xFFFF; - cdi_list_push(active_transfers, addr); - uhci->frame_list[frame] = pqh | 2; - for (timeout = 0; !(qh->transfer & 1) && (timeout < 1000); timeout++) { + memcpy(&uhci->transfer_descs[frame], &td, sizeof(td)); + uhci->queue_heads[frame].transfer = + (uintptr_t) &uhci->phys_transfer_descs[frame]; + addr.virt = &uhci->transfer_descs[frame]; + addr.phys = (uintptr_t) &uhci->phys_transfer_descs[frame]; + addr.error = 0xFFFF; + cdi_list_push(active_transfers, &addr); + locked = 0; + for (timeout = 0; + !(uhci->queue_heads[frame].transfer & 1) && (timeout < 1000); + timeout++) + { cdi_sleep_ms(1); } - while ((timeout < 1000) && (addr->error == 0xFFFF)) { + while ((timeout < 1000) && (addr.error == 0xFFFF)) { #ifndef CDI_STANDALONE __asm__ __volatile__ ("hlt"); #endif } - uhci->frame_list[frame] = 1; - locked = 0; if (packet->type == PACKET_IN) { - memcpy(packet->data, data, packet->length); + memcpy(packet->data, uhci->data_buffers[frame], packet->length); + } + if (addr.error == 0xFFFF) { + addr.error = USB_TIMEOUT; } - return addr->error; + return addr.error; } static void uhci_handler(struct cdi_device* cdi_hci) @@ -312,7 +325,7 @@ static void uhci_handler(struct cdi_device* cdi_hci) struct transfer* addr; struct uhci_td* td; - if (!status) { //Also, von hier kommt der IRQ nicht. + if (!status) { //Also, von hier kommt der IRQ nicht. return; } if (status & ~0x0001) { @@ -320,7 +333,7 @@ static void uhci_handler(struct cdi_device* cdi_hci) } if (status & 0x10) { printf("[uhci] SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n"); - cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! } else { for (i = 0; (addr = cdi_list_get(active_transfers, i)) != NULL; i++) { td = addr->virt; -- 1.6.3.3 From max at tyndur.org Sun Aug 23 18:44:07 2009 From: max at tyndur.org (max at tyndur.org) Date: Sun, 23 Aug 2009 18:44:07 +0200 Subject: [cdi-devel] [PATCH] usb: Quarter of memory Message-ID: <1251045847-15909-1-git-send-email-max@tyndur.org> From: Max Reitz * Four HC buffers per page Signed-off-by: Max Reitz --- usb/uhci.c | 16 ++++++++++++---- 1 files changed, 12 insertions(+), 4 deletions(-) diff --git a/usb/uhci.c b/usb/uhci.c index fb1fdb3..3e1407e 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -94,6 +94,8 @@ void uhci_init(struct cdi_device* cdi_hci) struct uhci* uhci = (struct uhci*) gen_hci; struct cdi_pci_resource* res; int i, size = 0x14; + void* nbuf; + uintptr_t npbuf; uhci->pbase = 0; for (i = 0; (res = cdi_list_get(gen_hci->pcidev->resources, i)) != NULL; @@ -136,13 +138,19 @@ void uhci_init(struct cdi_device* cdi_hci) //FIXME Das tut derma??en weh. uhci->data_buffers = malloc(sizeof(void*) * 1024); uhci->phys_data_buffers = malloc(sizeof(uintptr_t) * 1024); - for (i = 0; i < 1024; i++) { - if (cdi_alloc_phys_mem(1024, &uhci->data_buffers[i], - (void**) &uhci->phys_data_buffers[i]) == -1) - { + for (i = 0; i < 1024; i += 4) { + if (cdi_alloc_phys_mem(4096, &nbuf, (void**) &npbuf) == -1) { dprintf("Paketspeicher konnte nicht allociert werden!\n"); return; } + uhci->data_buffers[i + 0] = nbuf + 0x000; + uhci->data_buffers[i + 1] = nbuf + 0x400; + uhci->data_buffers[i + 2] = nbuf + 0x800; + uhci->data_buffers[i + 3] = nbuf + 0xC00; + uhci->phys_data_buffers[i + 0] = npbuf + 0x000; + uhci->phys_data_buffers[i + 1] = npbuf + 0x400; + uhci->phys_data_buffers[i + 2] = npbuf + 0x800; + uhci->phys_data_buffers[i + 3] = npbuf + 0xC00; } cdi_register_irq(gen_hci->pcidev->irq, &uhci_handler, cdi_hci); uhci->root_ports = (size - 0x10) >> 1; -- 1.6.3.3 From max at tyndur.org Wed Aug 26 22:35:34 2009 From: max at tyndur.org (max at tyndur.org) Date: Wed, 26 Aug 2009 22:35:34 +0200 Subject: [cdi-devel] [PATCH] Introduced pipes, less parameters, OHCI beginning Message-ID: <1251318934-30143-1-git-send-email-max@tyndur.org> From: Max Reitz + Pipes are more standard compliant (for example DATA0/DATA1 must be saved per pipe) and you need them for an OHCI driver + Beginning of an OHCI driver * You do not need to give for example the HCI if that is also saved in the device structure Signed-off-by: Max Reitz --- usb/include/ohci.h | 223 +++++++++++++++++++++++++++++++++++++ usb/include/usb.h | 73 ++++++++++-- usb/main.c | 73 ++++++++----- usb/msd.c | 50 +++++---- usb/ohci.c | 311 ++++++++++++++++++++++++++++++++++++++++++++++++++++ usb/uhci.c | 50 ++++---- 6 files changed, 693 insertions(+), 87 deletions(-) create mode 100644 usb/include/ohci.h create mode 100644 usb/ohci.c diff --git a/usb/include/ohci.h b/usb/include/ohci.h new file mode 100644 index 0000000..5f6ad01 --- /dev/null +++ b/usb/include/ohci.h @@ -0,0 +1,223 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#ifndef _CDI__USB__OHCI_H +#define _CDI__USB__OHCI_H + +#include + +#include "mempool.h" +#include "usb.h" + +#define OHC_USB_RESET 0x00000000 +#define OHC_USB_RESUME 0x00000040 +#define OHC_USB_OPERATIONAL 0x00000080 +#define OHC_USB_SUSPEND 0x000000C0 + +#define OHC_CTRL_CBSR 0x00000003 //Verh??ltnis zwischen Control und Bulk + //(cbsr+1 zu 1) +#define OHC_CTRL_PLE 0x00000004 //Periodische Transfers aktivieren +#define OHC_CTRL_IE 0x00000008 //Isochronous-Transfers aktivieren +#define OHC_CTRL_CLE 0x00000010 //Control-Transfers aktivieren +#define OHC_CTRL_BLE 0x00000020 //Bulk-Transfers aktivieren +#define OHC_CTRL_HCFS 0x000000C0 //Status des HCs +#define OHC_CTRL_IR 0x00000100 //Wenn gesetzt, dann werden IRQs zum SMB + //geleitet +#define OHC_CTRL_RWC 0x00000200 //Remote wakeup unterset??tzt +#define OHC_CTRL_RW 0x00000400 //Remote wakeup aktivieren + +#define OHC_CMST_RESET 0x00000001 //Reset +#define OHC_CMST_CLF 0x00000002 //Control list filled (muss gesetzt werden, + //wenn ein Eintrag zur Controlliste + //hinzugef??gt wird) +#define OHC_CMST_BLF 0x00000004 //Bulk list filled (muss gesetzt werden, wenn + //ein Eintrag zur Bulkliste hinzugef??gt wird) +#define OHC_CMST_OCR 0x00000008 //Ownership change request +#define OHC_CMST_SOC 0x00030000 //Scheduling overrun count + +#define OHC_INT_SO 0x00000001 //Scheduling overrun +#define OHC_INT_WDH 0x00000002 //Write back done head +#define OHC_INT_SF 0x00000004 //Start of frame (wird bei SOF gesetzt) +#define OHC_INT_RD 0x00000008 //Resume detected (wird bei einem von einem + //Ger??t gesendeten Resume gesetzt) +#define OHC_INT_UE 0x00000010 //Unrecoverable error (wird bei einem HC- + //Fehler gesetzt, der nichts mit USB zu tun + //hat) +#define OHC_INT_FNO 0x00000020 //Frame number overflow (wird gesetzt, wenn + //die Framenummer gr????er als 32767 und + //umgebrochen wird) +#define OHC_INT_RHSC 0x00000040 //Root hub status change (wird gesetzt, wenn + //sich irgendwas am Roothub ge??ndert hat) +#define OHC_INT_OC 0x40000000 //Ownership change +#define OHC_INT_MIE 0x80000000 //(De-)Aktiviert Interrupts + +#define OHC_RHA_NDP 0x000000FF //Anzahl an Rootports (1 bis 15) +#define OHC_RHA_PSM 0x00000100 //Gesetzt, wenn man die Stromzufuhr zu + //einzelnen Ports unabh??ngig setzen kann +#define OHC_RHA_NPS 0x00000200 //Gel??scht, wenn man die Stromzufuhr zu + //einzelnen Ports unabh??ngig setzen kann +#define OHC_RHA_DT 0x00000400 //Immer 0, zeigt so an, dass der Roothub kein + //Compound device ist. +#define OHC_RHA_OCPM 0x00000800 //Gesetzt, wenn ??berspannungen f??r jeden Port + //einzeln gemeldet werden +#define OHC_RHA_NOCP 0x00001000 //Gesetzt, wenn keine ??berspannungen erkannt + //bzw. verhindert werden k??nnen +#define OHC_RHA_POTPGT 0xFF000000 //Power on to power good time (potpgt * 2) + +#define OHC_RHS_LPS 0x00000001 //R: 0 + //W: Strom abdrehen +#define OHC_RHS_OCI 0x00000002 //R: Unspezifische ??berspannung +#define OHC_RHS_DRWE 0x00008000 //R: Irgendwas mit Remote Wakeup Enable + //W: Das aktivieren +#define OHC_RHS_LPSC 0x00010000 //R: 0 + //W: Strom andrehen +#define OHC_RHS_OCIC 0x00020000 //R: Bei ??nderungen von OCI gesetzt + //W: Feld l??schen +#define OHC_RHS_CRWE 0x80000000 //W: Remote Wakeup Enable deaktivieren + +#define OHC_RP_CCS 0x00000001 //R: Ger??t angeschlossen + //W: Deaktiviert den Port +#define OHC_RP_PES 0x00000002 //R: Port aktiviert + //W: Aktiviert den Port +#define OHC_RP_PSS 0x00000004 //R: Port schl??ft + //W: Schl??fert den Port ein +#define OHC_RP_POCI 0x00000008 //R: ??berspannung + //W: Port aufwecken +#define OHC_RP_PRS 0x00000010 //R: Reset l??uft + //W: Reset treiben +#define OHC_RP_PPS 0x00000100 //R: Strom an + //W: Strom andrehen +#define OHC_RP_LSDA 0x00000200 //R: Low-Speed-Ger??t + //W: Strom abdrehen +#define OHC_RP_CSC 0x00010000 //R: Ger??t an- oder abgezogen + //W: Bit l??schen +#define OHC_RP_PESC 0x00020000 //R: Port (de-)aktiviert + //W: Bit l??schen +#define OHC_RP_PSSC 0x00040000 //R: Port wurde geweckt + //W: Bit l??schen +#define OHC_RP_OCIC 0x00080000 //R: ??berspannungsbit ver??ndert + //W: Bit l??schen +#define OHC_RP_PRSC 0x00100000 //R: Reset beendet + //W: Bit l??schen + +struct ohci_registers { + uint32_t hc_revision; //Revision + volatile uint32_t hc_control; + volatile uint32_t hc_command_status; + volatile uint32_t hc_interrupt_status; + volatile uint32_t hc_interrupt_enable; + volatile uint32_t hc_interrupt_disable; + uint32_t hc_hcca; //Adresse des HCCA + volatile uint32_t hc_period_current_ed; //Adresse des aktuellen + //Endpointdescriptors der + //periodischen Transfers + uint32_t hc_control_head_ed; //Adresse des ersten Endpointdescriptors der + //Control-Transfers + volatile uint32_t hc_control_current_ed; //Adresse des aktuellen + //Endpointdescriptors der Control- + //Transfers + uint32_t hc_bulk_head_ed; //Adresse des ersten Endpointdescriptors der + //Bulk-Transfers + volatile uint32_t hc_bulk_current_ed; //Adresse des aktuellen + //Endpointdescriptors der Bulk- + //Transfers + volatile uint32_t hc_done_head; //Adresse des zuletzt abgearbeiteten + //Transferdeskriptors, der der Done-Liste + //hinzugef??gt wurde + uint32_t hc_fm_interval; //Gibt die L??nge eines Frames und die maximale + //Paketgr????e an + volatile uint32_t hc_fm_remaining; //Verbleibende Takte des aktuellen Frames + volatile uint32_t hc_fm_number; //Nummer des aktuellen Frames + uint32_t hc_periodic_start; //10% von HcFmInterval, wenn HcFmInterval diesen + //Wert erreicht, dann haben periodische + //Transfers Vorrang + uint32_t hc_ls_threshold; //Besser nicht ??ndern + uint32_t hc_rh_descriptor_a; + uint32_t hc_rh_descriptor_b; + volatile uint32_t hc_rh_status; + volatile uint32_t hc_rh_port_status[]; +} __attribute__((packed)); + +struct ohci_hcca { + uint32_t interrupt_table[32]; //Zeiger zu Interrupt-EDs + uint16_t frame_number; //Die Nummer des aktuellen Frames + uint16_t pad; //Wird auf 0 gesetzt, wenn frame_number neu geschrieben wird + //(Ausrichten an DWord-Grenzen vermutlich) + uint32_t done_head; + uint8_t rsvd[116]; +} __attribute__((packed)); + +struct ohci { + struct hci gen_hci; + struct ohci_registers* memory; + int root_ports; + struct ohci_hcca* hcca; + + struct mempool* ed_pool; + struct mempool* transfer_pool; +}; + +#define OHC_ED_DIR_TD 0 //Richtung im TD definiert +#define OHC_ED_DIR_OUT 1 +#define OHC_ED_DIR_IN 2 + +struct ohci_ed { + unsigned function : 7; //USB-Adresse des Ger??ts + unsigned endpoint : 4; //Nummer des Endpoints + unsigned direction : 2; //Richtung des Transfers + unsigned low_speed : 1; + unsigned skip : 1; + unsigned format : 1; //Bei Isochronous gesetzt + unsigned mps : 11; //Maximale Paketgr????e + unsigned user : 5; //F??r uns frei verf??gbar + uint32_t td_queue_tail; //Letzter TD in der Warteschlange + uint32_t td_queue_head; //N??chster TD in der Warteschlange + uint32_t next_ed; //N??chster ED +} __attribute__((packed)); + +#define OHC_TD_DIR_SETUP 0 +#define OHC_TD_DIR_OUT 1 +#define OHC_TD_DIR_IN 2 + +struct ohci_td { + unsigned user : 18; //F??r uns frei verf??gbar + unsigned rounding : 1; //Wenn gesetzt, dann sind zu kleine Pakete nicht so + //schlimm + unsigned direction : 2; //Richtung/Typ des Transfers (nur g??ltig, wenn das + //"direction"-Feld im ED 00b oder 11b ist) + unsigned di : 3; //Gibt an, wie viele Frames der HC vor Ausl??sen eines IRQs + //warten soll + unsigned toggle : 2; //Datatoggle (00b oder 01b - Wert muss aus dem ED + //ermittelt werden, 10b = DATA0, 11b = DATA1) + unsigned error : 2; //Anzahl der aufgetretenen Fehler - bei 11b wird der + //Status im "condition"-Feld gespeichert + unsigned condition : 4; //Status + uint32_t current_buffer_pointer; //Pointer zu den Daten (bei 0 wurden alle + //Daten ??bertragen) + uint32_t next_td; //N??chster TD + uint32_t buffer_end; //Letztes Datenbyte des Puffers +} __attribute__((packed)); + +struct cdi_driver* init_ohcd(void); +void ohci_init(struct cdi_device* cdi_hci); + +#endif diff --git a/usb/include/usb.h b/usb/include/usb.h index 459a340..896afc7 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -138,14 +138,44 @@ struct cdi_hci { struct usb_device; struct usb_packet; +struct usb_pipe; struct hci { struct cdi_pci_device* pcidev; hci_type_t type; - cdi_list_t (* find_devices)(struct hci*); - void (* activate_device)(struct hci*, struct usb_device*); - int (* do_packet)(struct usb_device*, struct usb_packet* packet); - int (* get_frame)(struct hci*); + + /** + * Listet alle Ger??te auf, die an den Rootports h??ngen + * + * @param hci Das HCI + * + * @return Die List der Ger??te (nur NULL, wenn cdi_list_create() + * fehlgeschlagen ist) + */ + cdi_list_t (* find_devices)(struct hci* hci); + + /** + * Aktiviert den zum angegebenen Ger??t geh??rigen Rootport + * + * @param device Das USB-Ger??t (muss an einem Rootport h??ngen) + */ + void (* activate_device)(struct usb_device* device); + + /** + * Verarbeitet ein USB-Paket + * + * @param packet Das Paket + * + * @return Gibt den Status an (z. B. USB_NO_ERROR f??r keinen Fehler) + */ + int (* do_packet)(struct usb_packet* packet); + + /** + * Erstellt eine neue Pipe + * + * @param pipe Die Pipe + */ + void (* add_pipe)(struct usb_pipe* pipe); }; struct setup_packet { @@ -246,29 +276,34 @@ struct usb_device { int id; int port; int low_speed; - struct endpoint_desc* ep0; + struct usb_pipe* ep0; struct device_desc* device; struct config_desc* config; struct interface_desc* interface; struct class_data* classd; int locked; int expects; - int data_toggle; + + /** + * Setzt das Ger??t zur??ck (muss vom Hub- oder HC-Driver gesetzt werden) + * + * @param device Das Ger??t + */ void (* reset)(struct usb_device* device); }; struct msclass_data { struct class_data gen_class; - struct endpoint_desc* bulk_ep_in; - struct endpoint_desc* bulk_ep_out; + struct usb_pipe* bulk_in; + struct usb_pipe* bulk_out; }; /** Beschreibt ein USB-Paket */ struct usb_packet { + /// Die gewuenschte Pipe + struct usb_pipe* pipe; /// Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) enum usb_packet_type type; - /// Der gewuenschte Endpoint - struct endpoint_desc* endpoint; /// Zeiger auf den Datenpuffer void* data; /// Die Laenge des Puffers @@ -277,14 +312,28 @@ struct usb_packet { int type_of_data; }; +/// Beschreibt eine Pipe +struct usb_pipe { + /// Ziel + struct usb_device* device; + /// Der Endpoint + struct endpoint_desc* endpoint; + /// Data-Toggle + int data_toggle; +}; + +#include "ohci.h" #include "uhci.h" -#define HCI_STRUCT_SIZE sizeof(struct uhci) +#ifndef max +#define max(v1, v2) (((v1) > (v2)) ? (v1) : (v2)) +#endif +#define HCI_STRUCT_SIZE max(sizeof(struct ohci), sizeof(struct uhci)) -int usb_do_packet(struct usb_device* device, struct usb_packet* packet); +int usb_do_packet(struct usb_packet* packet); void enumerate_hci(struct hci*); struct cdi_driver* init_uhcd(void); void init_msc_driver(void); diff --git a/usb/main.c b/usb/main.c index b09bfba..8861fcf 100644 --- a/usb/main.c +++ b/usb/main.c @@ -28,6 +28,8 @@ #include "cdi/misc.h" #include "cdi/pci.h" +#include "ohci.h" +#include "uhci.h" #include "usb.h" #define DEBUG @@ -100,14 +102,16 @@ static const int next_data_type[9] = { /** * Verarbeitet ein USB-Paket. * - * @param device Das USB-Ger??t + * @param packet Das Paket */ -int usb_do_packet(struct usb_device* device, struct usb_packet* packet) +int usb_do_packet(struct usb_packet* packet) { int error = USB_NAK; int tod_short, tod = packet->type_of_data; int i = 0, mps; + struct usb_pipe* pipe = packet->pipe; + struct usb_device* device = pipe->device; if (!(device->expects & tod)) { dprintf("0x%04X erwartet, 0x%04X bekommen...?\n", device->expects, tod); @@ -124,18 +128,18 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) packet->type &= 0xFF; struct usb_packet send_packet = { + .pipe = packet->pipe, .type = packet->type, - .endpoint = packet->endpoint, .data = packet->data }; - mps = packet->endpoint->max_packet_size; + mps = pipe->endpoint->max_packet_size; for (i = 0; (i < packet->length) || (!i && !packet->length); i += mps) { send_packet.length = (packet->length - i > mps) ? mps : (packet->length - i); error = USB_NAK; while (error == USB_NAK) { - error = device->hci->do_packet(device, &send_packet); + error = device->hci->do_packet(&send_packet); if (error == USB_NAK) { cdi_sleep_ms(5); } @@ -143,16 +147,16 @@ int usb_do_packet(struct usb_device* device, struct usb_packet* packet) if (error != USB_NO_ERROR) { i -= mps; } else { - device->data_toggle ^= 1; + pipe->data_toggle ^= 1; send_packet.data += mps; } if (error == USB_STALLED) { - printf("[usb] ENDPOINT %i DES GER??TS %i STALLED!\n", - packet->endpoint->endpoint_address, + printf("[usb] ENDPOINT 0x%02X DES GER??TS %i STALLED!\n", + pipe->endpoint->endpoint_address, device->id); do_control(device, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, REC_ENDPOINT, CLEAR_FEATURE, 0, - packet->endpoint->endpoint_address); + pipe->endpoint->endpoint_address); } } return error; @@ -180,14 +184,14 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, setup->length = length; struct usb_packet setup_packet = { + .pipe = device->ep0, .type = PACKET_SETUP, - .endpoint = device->ep0, .data = setup, .length = sizeof(*setup), .type_of_data = USB_TOD_SETUP, }; - if (usb_do_packet(device, &setup_packet)) { + if (usb_do_packet(&setup_packet)) { return NULL; } @@ -196,8 +200,8 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, buffer = (void*) 0xFFFFFFFF; //Kein Fehler, aber auch keine Daten } else { struct usb_packet data_packet = { + .pipe = device->ep0, .type = (direction == DEV_TO_HOST ? PACKET_IN : PACKET_OUT), - .endpoint = device->ep0, .data = buffer, .length = length, .type_of_data = @@ -205,16 +209,16 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, DEV_TO_HOST ? USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), }; - rval = usb_do_packet(device, &data_packet); + rval = usb_do_packet(&data_packet); } if (rval == USB_NO_ERROR) { struct usb_packet ack_packet = { - .endpoint = device->ep0, + .pipe = device->ep0, .data = NULL, .length = 0, }; - device->data_toggle = 1; + device->ep0->data_toggle = 1; if (no_data || (direction == HOST_TO_DEV)) { ack_packet.type = PACKET_IN; @@ -224,7 +228,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, ack_packet.type_of_data = USB_TOD_SETUP_ACK_OUT; } - rval = usb_do_packet(device, &ack_packet); + rval = usb_do_packet(&ack_packet); } return (rval == USB_NO_ERROR) ? buffer : NULL; } @@ -234,7 +238,7 @@ static void usb_init(void) struct cdi_pci_device* dev; struct hci* hci; struct cdi_hci* cdi_hci; - struct cdi_driver* uhcd; + struct cdi_driver* uhcd, * ohcd; char* dev_name; int i; @@ -283,6 +287,7 @@ static void usb_init(void) ehci), cdi_list_size(ohci), cdi_list_size(uhci)); uhcd = init_uhcd(); + ohcd = init_ohcd(); for (i = 0; (hci = cdi_list_pop(uhci)) != NULL; i++) { cdi_hci = malloc(sizeof(struct cdi_hci)); @@ -299,6 +304,20 @@ static void usb_init(void) uhci_init(&cdi_hci->cdi_device); } + for (i = 0; (hci = cdi_list_pop(ohci)) != NULL; i++) { + cdi_hci = malloc(sizeof(struct cdi_hci)); + cdi_hci->cdi_device.type = CDI_UNKNOWN; + dev_name = malloc(10); + sprintf(dev_name, "ohci%i", i); + cdi_hci->cdi_device.name = dev_name; + hci->find_devices = NULL; + cdi_hci->hci = hci; + //cdi_list_push(ohcd->devices, cdi_hci); + dprintf("%s registriert.\n", dev_name); + cdi_hci->cdi_device.driver = uhcd; + ohci_init(&cdi_hci->cdi_device); + } + cdi_list_destroy(ehci); cdi_list_destroy(ohci); cdi_list_destroy(uhci); @@ -316,14 +335,16 @@ static void enum_device(struct usb_device* usbdev) //Ger??t und EP0 initialisieren usbdev->locked = 0; usbdev->expects = USB_TOD_SETUP | USB_TOD_COMMAND; - usbdev->data_toggle = 0; usbdev->ep0 = malloc(sizeof(*usbdev->ep0)); - usbdev->ep0->length = sizeof(*usbdev->ep0); - usbdev->ep0->descriptor_type = DESC_ENDPOINT; - usbdev->ep0->endpoint_address = 0; - usbdev->ep0->attributes = 0; - usbdev->ep0->max_packet_size = 8; - usbdev->ep0->interval = 0; + usbdev->ep0->endpoint = malloc(sizeof(*usbdev->ep0->endpoint)); + usbdev->ep0->endpoint->length = sizeof(*usbdev->ep0->endpoint); + usbdev->ep0->endpoint->descriptor_type = DESC_ENDPOINT; + usbdev->ep0->endpoint->endpoint_address = 0; + usbdev->ep0->endpoint->attributes = 0; + usbdev->ep0->endpoint->max_packet_size = 8; + usbdev->ep0->endpoint->interval = 0; + usbdev->ep0->device = usbdev; + usbdev->ep0->data_toggle = 0; //Resetten usbdev->reset(usbdev); @@ -337,7 +358,7 @@ static void enum_device(struct usb_device* usbdev) if (dev_desc == NULL) { return; } - usbdev->ep0->max_packet_size = dev_desc->max_packet_size0; + usbdev->ep0->endpoint->max_packet_size = dev_desc->max_packet_size0; //Nochmals resetten usbdev->reset(usbdev); @@ -515,7 +536,7 @@ void enumerate_hci(struct hci* hci) if (hci->find_devices != NULL) { usb_devices = hci->find_devices(hci); while ((usbdev = cdi_list_pop(usb_devices)) != NULL) { - hci->activate_device(hci, usbdev); + hci->activate_device(usbdev); cdi_sleep_ms(50); enum_device(usbdev); } diff --git a/usb/msd.c b/usb/msd.c index a9f82a4..4410f37 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -163,26 +163,35 @@ void register_msd(struct usb_device* usbdev) return; } usbdev->classd = (struct class_data*) msc; - msc->bulk_ep_in = NULL; - msc->bulk_ep_out = NULL; + msc->bulk_in = malloc(sizeof(*msc->bulk_in)); + msc->bulk_in->device = usbdev; + msc->bulk_in->endpoint = NULL; + msc->bulk_in->data_toggle = 0; + msc->bulk_out = malloc(sizeof(*msc->bulk_out)); + msc->bulk_out->device = usbdev; + msc->bulk_out->endpoint = NULL; + msc->bulk_out->data_toggle = 0; address = (void*) usbdev->interface + sizeof(struct interface_desc); for (i = 0; i < usbdev->interface->num_endpoints; i++) { ep_desc = address; if ((ep_desc->endpoint_address & 0x80) && - (ep_desc->attributes == 0x02) && (msc->bulk_ep_in == NULL)) + (ep_desc->attributes == 0x02) && + (msc->bulk_in->endpoint == NULL)) { //BULK-IN - msc->bulk_ep_in = ep_desc; + msc->bulk_in->endpoint = ep_desc; } else if (!(ep_desc->endpoint_address & 0x80) && (ep_desc->attributes == 0x02) && - (msc->bulk_ep_out == NULL)) + (msc->bulk_out->endpoint == NULL)) { //BULK-OUT - msc->bulk_ep_out = ep_desc; + msc->bulk_out->endpoint = ep_desc; } address += sizeof(struct endpoint_desc); } - if ((msc->bulk_ep_in == NULL) || (msc->bulk_ep_out == NULL)) { + if ((msc->bulk_in->endpoint == NULL) || + (msc->bulk_out->endpoint == NULL)) + { dprintf("Nicht gen??gend Endpoints gefunden.\n"); return; } @@ -197,10 +206,6 @@ void register_msd(struct usb_device* usbdev) size *= bc; dprintf("%s: %i * %i B (ca. %lld MB).\n", strgdev->dev.name, bc, bs, size >> 20); - dprintf("EP %i: %i; EP %i: %i\n", msc->bulk_ep_in->endpoint_address, - msc->bulk_ep_in->max_packet_size, - msc->bulk_ep_out->endpoint_address, - msc->bulk_ep_out->max_packet_size); } cdi_storage_device_init(strgdev); cdi_list_push(cdi_driver.drv.devices, strgdev); @@ -227,14 +232,14 @@ static int write_cmd(struct usb_device* usbdev, void* src) { struct msclass_data* msc = (struct msclass_data*) usbdev->classd; struct usb_packet cmd_packet = { + .pipe = msc->bulk_out, .type = PACKET_OUT, - .endpoint = msc->bulk_ep_out, .data = src, .length = 0x1F, .type_of_data = USB_TOD_COMMAND, }; - return usb_do_packet(usbdev, &cmd_packet); + return usb_do_packet(&cmd_packet); } /** @@ -254,14 +259,14 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) int error; struct usb_packet status_packet = { + .pipe = msc->bulk_in, .type = PACKET_IN, - .endpoint = msc->bulk_ep_in, .data = csw, .length = 0x0D, .type_of_data = USB_TOD_STATUS, }; - error = usb_do_packet(usbdev, &status_packet); + error = usb_do_packet(&status_packet); if (error != USB_NO_ERROR) { return error; } @@ -307,14 +312,14 @@ static int msd_get_capacity(struct usb_device* usbdev, uint32_t* block_size, } struct usb_packet in_packet = { + .pipe = msc->bulk_in, .type = PACKET_IN, - .endpoint = msc->bulk_ep_in, .data = cap, .length = sizeof(*cap), .type_of_data = USB_TOD_DATA_IN, }; - if (usb_do_packet(usbdev, &in_packet) != USB_NO_ERROR) { + if (usb_do_packet(&in_packet) != USB_NO_ERROR) { return 0; } @@ -402,16 +407,13 @@ static int msd_cdi_read(struct cdi_storage_device* strgdev, uint64_t start, } #endif bbs = (count - j > MAX_ACCESS_BLOCKS) ? MAX_ACCESS_BLOCKS : count - j; - _dprintf("/"); error = msd_read(usbdev, start + j, bbs, buffer + j * bs, bbs * bs); - _dprintf("\\"); if (error != USB_NO_ERROR) { dprintf("Lesefehler 0x%X bei Block %lld.\n", error, start + j); usbdev->locked = 0; return -1; } } - _dprintf("\n"); usbdev->locked = 0; return 0; } @@ -452,14 +454,14 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, } struct usb_packet in_packet = { + .pipe = msc->bulk_in, .type = PACKET_IN, - .endpoint = msc->bulk_ep_in, .data = buffer, .length = length, .type_of_data = USB_TOD_DATA_IN, }; - error = usb_do_packet(usbdev, &in_packet); + error = usb_do_packet(&in_packet); if (error != USB_NO_ERROR) { return error; } @@ -553,14 +555,14 @@ static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, } struct usb_packet out_packet = { + .pipe = msc->bulk_out, .type = PACKET_OUT, - .endpoint = msc->bulk_ep_out, .data = buffer, .length = length, .type_of_data = USB_TOD_DATA_OUT, }; - error = usb_do_packet(usbdev, &out_packet); + error = usb_do_packet(&out_packet); if (error != USB_NO_ERROR) { return error; } diff --git a/usb/ohci.c b/usb/ohci.c new file mode 100644 index 0000000..e4de271 --- /dev/null +++ b/usb/ohci.c @@ -0,0 +1,311 @@ +/****************************************************************************** + * Copyright (c) 2009 Max Reitz * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + ******************************************************************************/ + +#include + +#include "cdi/io.h" +#include "cdi/misc.h" +#include "cdi/pci.h" + +#include "mempool.h" +#include "ohci.h" + +#define DEBUG + +#ifdef DEBUG +#include +#include +#define dprintf(fmt, args...) printf("[ohci] " fmt, ## args) +#define _dprintf(fmt, args...) printf(fmt, ## args) +#else +static int dprintf(const char* fmt, ...) +{ + return 0; +} + +#define _dprintf(fmt, args...) dprintf(fmt, ## args) +#endif + +static struct cdi_driver cdi_driver; +static const char* driver_name = "ohcd"; + +static void ohci_kill(struct cdi_driver* cdi_hcd); +static void ohci_deinit(struct cdi_device* cdi_hci); +void ohci_init(struct cdi_device* cdi_hci); +static void ohci_handler(struct cdi_device* dev); +static int ohci_do_packet(struct usb_packet* packet); +static cdi_list_t get_devices(struct hci* gen_hci); +static void activate_device(struct usb_device* device); +static void ohci_reset_device(struct usb_device* device); +static void ohci_establish_pipe(struct usb_pipe* pipe); + +struct cdi_driver* init_ohcd() +{ + cdi_driver.type = CDI_UNKNOWN; + cdi_driver.name = driver_name; + cdi_driver.init_device = &ohci_init; + cdi_driver.remove_device = &ohci_deinit; + cdi_driver.destroy = &ohci_kill; + cdi_driver_init(&cdi_driver); + cdi_driver_register(&cdi_driver); + return &cdi_driver; +} + +static void ohci_kill(struct cdi_driver* cdi_hcd) +{ + //TODO: Da geht doch noch was +} + +static void ohci_deinit(struct cdi_device* cdi_hci) +{ + cdi_outw( + ((struct uhci*) ((struct cdi_hci*) cdi_hci)->hci)->pbase + UHCI_USBCMD, + MAXP); //HC anhalten + //TODO: Hier doch auch +} + +void ohci_init(struct cdi_device* cdi_hci) +{ + struct hci* gen_hci = ((struct cdi_hci*) cdi_hci)->hci; + struct ohci* ohci = (struct ohci*) gen_hci; + struct cdi_pci_resource* res; + int i; + uintptr_t phys_hcca; + + ohci->memory = NULL; + for (i = 0; (res = cdi_list_get(gen_hci->pcidev->resources, i)) != NULL; + i++) + { + if (res->type == CDI_PCI_MEMORY) { + ohci->memory = cdi_alloc_phys_addr(res->length, res->start); + if (ohci->memory == NULL) { + dprintf("Speicher konnten nicht allokiert werden.\n"); + return; + } + break; + } + } + if (ohci->memory == NULL) { + dprintf("Speicher nicht gefunden!\n"); + return; + } + dprintf("Speicher @ 0x%08X\n", ohci->memory); + if ((ohci->memory->hc_revision & 0xFF) != 0x10) { + dprintf( + "Dieses OHCI k??nnte inkompatibel mit dem Treiber sein (HCI %i.%i, HCD 1.0)\n", + (ohci->memory->hc_revision & 0xF0) >> 4, + ohci->memory->hc_revision & 0xF); + } + if (cdi_alloc_phys_mem(sizeof(*ohci->hcca), (void**) &ohci->hcca, + (void**) &phys_hcca) == -1) + { + dprintf("Konnte den HCCA nicht allokieren.\n"); + return; + } + memset(ohci->hcca, 0, sizeof(*ohci->hcca)); + + dprintf("IRQ: %i\n", gen_hci->pcidev->irq); + cdi_register_irq(gen_hci->pcidev->irq, &ohci_handler, cdi_hci); + + ohci->ed_pool = + mempool_create(64 * sizeof(struct ohci_ed), sizeof(struct ohci_ed)); + if (ohci->ed_pool == NULL) { + dprintf("ED-Speicherpool konnte nicht erzeugt werden!\n"); + return; + } + + ohci->transfer_pool = mempool_create(32 * (1023 + sizeof(struct ohci_td)), + 1023 + sizeof(struct ohci_td)); + if (ohci->transfer_pool == NULL) { + dprintf("Transfer-Speicherpool konnte nicht erzeugt werden!\n"); + return; + } + + ohci->memory->hc_interrupt_disable = OHC_INT_MIE; + + if (ohci->memory->hc_control & OHC_CTRL_IR) { + dprintf("??bernahme der Kontrolle vom SMM... "); + //??nderung des "Besitzers" beantragen + ohci->memory->hc_command_status |= OHC_CMST_OCR; + for (i = 0; (ohci->memory->hc_control & OHC_CTRL_IR) && (i < 1000); + i++) + { + cdi_sleep_ms(1); + } + if (i < 100) { + _dprintf("Er hat mitgespielt.\n"); + } else { + _dprintf("Idiot.\n"); + ohci->memory->hc_control &= ~OHC_CTRL_IR; + } + } else if ((ohci->memory->hc_control & OHC_CTRL_CBSR) != OHC_USB_RESET) { + dprintf("BIOS aktiv\n"); + if ((ohci->memory->hc_control & OHC_CTRL_CBSR) != + OHC_USB_OPERATIONAL) + { + dprintf("RESUME aktivieren...\n"); + ohci->memory->hc_control = + (ohci->memory->hc_control & ~OHC_CTRL_CBSR) | OHC_USB_RESUME; + cdi_sleep_ms(10); + } + } else { //Nix, weder BIOS noch SMM + dprintf("RESET abwarten\n"); + cdi_sleep_ms(10); //Reset abwarten + } + + dprintf("Resetten...\n"); + ohci->memory->hc_command_status |= OHC_CMST_RESET; + //Normalerweise sollte man den Wert von hc_fm_interval von vor dem Reset + //wiederherstellen, aber ich finde das Schwachsinn. Die Werte sind so in + //Ordnung, wie sie jetzt sind. + + cdi_sleep_ms(3); //10 ??s f??r den Reset und 2 ms f??r Resume + if ((ohci->memory->hc_control & OHC_CTRL_CBSR) == OHC_USB_SUSPEND) { //Boah... + dprintf("Das Ding wurde nicht fertig...\n"); + ohci->memory->hc_control = + (ohci->memory->hc_control & ~OHC_CTRL_CBSR) | OHC_USB_RESUME; + cdi_sleep_ms(10); + } + + ohci->memory->hc_interrupt_disable = OHC_INT_MIE; + + ohci->memory->hc_hcca = phys_hcca; + ohci->memory->hc_interrupt_status = 0xFFFFFFFF; + ohci->memory->hc_interrupt_enable = + OHC_INT_SO | //Scheduling overrun + OHC_INT_WDH | //Write back done head + OHC_INT_RD | //Resume detected + OHC_INT_UE | //Schwerer Fehler + OHC_INT_RHSC | //Root hub status change + OHC_INT_MIE; + //Eigentlich sollte man alle Transfers aktivieren. Aber wir wollen ja noch + //gar keine periodischen Transfers... + //Periodisches Zeug deaktivieren + ohci->memory->hc_control &= ~(OHC_CTRL_PLE | OHC_CTRL_IE); + //Control und Bulk aktivieren + ohci->memory->hc_control |= OHC_CTRL_CLE | OHC_CTRL_BLE; + //Merkw??rdigerweise soll das jetzt nicht auf 10 %, sondern auf 90 % gesetzt + //werden. Aber das klingt total bescheuert, weil nicht erkl??rt wird, wann + //man es denn auf 10 % setzen soll. Deshalb machen wir das gleich. + ohci->memory->hc_periodic_start = 0x4B0; + + dprintf("HC wird aktiviert.\n"); + + //DO IT, MAN! + ohci->memory->hc_control = + (ohci->memory->hc_control & ~OHC_CTRL_CBSR) | OHC_USB_OPERATIONAL; + + ohci->memory->hc_rh_status |= OHC_RHS_LPSC; //Strom andrehen + ohci->root_ports = ohci->memory->hc_rh_descriptor_a & OHC_RHA_NDP; + cdi_sleep_ms((ohci->memory->hc_rh_descriptor_a & OHC_RHA_POTPGT) >> 23); + + dprintf("%i Rootports gefunden.\n", ohci->root_ports); + + for (i = 0; i < ohci->root_ports; i++) { + ohci->memory->hc_rh_port_status[i] |= OHC_RP_CCS; //Deaktivieren + + } + gen_hci->find_devices = &get_devices; + gen_hci->activate_device = &activate_device; + gen_hci->do_packet = &ohci_do_packet; + gen_hci->add_pipe = &ohci_establish_pipe; + + enumerate_hci(gen_hci); +} + +static int ohci_do_packet(struct usb_packet* packet) +{ + return USB_STALLED; +} + +static cdi_list_t get_devices(struct hci* gen_hci) +{ + struct ohci* ohci = (struct ohci*) gen_hci; + struct usb_device* dev; + cdi_list_t dlist = cdi_list_create(); + int i; + + dprintf("Ger??te finden: "); + for (i = 0; i < ohci->root_ports; i++) { + if (!(ohci->memory->hc_rh_port_status[i] & OHC_RP_CCS)) { + _dprintf("-"); + continue; + } + _dprintf("!"); + dev = malloc(sizeof(struct usb_device)); + dev->hci = gen_hci; + dev->id = 0; + dev->port = i; + dev->low_speed = !!(ohci->memory->hc_rh_port_status[i] & OHC_RP_LSDA); + dev->reset = &ohci_reset_device; + cdi_list_push(dlist, dev); + } + _dprintf("\n"); + return dlist; +} + +static void activate_device(struct usb_device* device) +{ + struct ohci* ohci = (struct ohci*) device->hci; + + dprintf("Ger??t an Port %i wird aktiviert.\n", device->port); + ohci->memory->hc_rh_port_status[device->port] |= OHC_RP_PES; + cdi_sleep_ms(20); +} + +static void ohci_reset_device(struct usb_device* device) +{ + struct ohci* ohci = (struct ohci*) device->hci; + + ohci->memory->hc_rh_port_status[device->port] |= OHC_RP_PRS; + cdi_sleep_ms(20); +} + +static void ohci_establish_pipe(struct usb_pipe* pipe) +{ +} + +static void ohci_handler(struct cdi_device* dev) +{ + struct ohci* ohci = (struct ohci*) ((struct cdi_hci*) dev)->hci; + uint32_t status, done = 0; + + status = ohci->memory->hc_interrupt_status; + + if (status & OHC_INT_RHSC) { + done |= OHC_INT_RHSC; + } + if (status & OHC_INT_UE) { + dprintf("Schwerer HC-Fehler.\n"); + //TODO HC anhalten + done |= OHC_INT_UE; + } + if (status & OHC_INT_SF) { //??hm... Na ja. + done |= OHC_INT_SF; + } + + if (status & ~done) { + dprintf("Nicht behandelter Interrupt: 0x%08X\n", status & ~done); + } + + ohci->memory->hc_interrupt_status = status; +} diff --git a/usb/uhci.c b/usb/uhci.c index e80754c..cb7b857 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -28,9 +28,9 @@ #include "cdi/misc.h" #include "cdi/pci.h" +#include "mempool.h" #include "uhci.h" #include "usb.h" -#include "mempool.h" #define DEBUG @@ -48,19 +48,19 @@ static int dprintf(const char* fmt, ...) #define _dprintf(fmt, args...) dprintf(fmt, ## args) #endif -static const char* driver_name = "uhci"; +static const char* driver_name = "uhcd"; static struct cdi_driver cdi_driver; static cdi_list_t active_transfers; static void uhci_handler(struct cdi_device* dev); -static int uhci_do_packet(struct usb_device* usbdev, struct usb_packet* packet); +static int uhci_do_packet(struct usb_packet* packet); static cdi_list_t get_devices(struct hci* gen_hci); -static void activate_device(struct hci* gen_hci, struct usb_device* device); -static int get_current_frame(struct hci* gen_hci); +static void activate_device(struct usb_device* device); static void uhci_deinit(struct cdi_device* cdi_hci); static void uhci_kill(struct cdi_driver* cdi_hcd); static void uhci_reset_device(struct usb_device* device); +static void uhci_establish_pipe(struct usb_pipe* pipe); struct cdi_driver* init_uhcd() @@ -115,10 +115,9 @@ void uhci_init(struct cdi_device* cdi_hci) dprintf("I/O-Basis nicht gefunden!\n"); return; } - request_size = - // Maximale Paketlaenge (TODO Stimmt das?) - 1024 + + // Maximale Paketl??nge (Full Speed) + 1023 + // Transferdeskriptor sizeof(struct uhci_td); @@ -135,10 +134,11 @@ void uhci_init(struct cdi_device* cdi_hci) dprintf("QH-Speicher konnte nicht allociert werden!\n"); return; } + if (cdi_alloc_phys_mem(4096, (void**) &uhci->frame_list, (void**) &uhci->phys_frame_list) == -1) { - dprintf("Frame List konnte nicht allociert werden!\n"); + dprintf("Frame List konnte nicht allokiert werden!\n"); return; } @@ -185,7 +185,7 @@ void uhci_init(struct cdi_device* cdi_hci) gen_hci->find_devices = &get_devices; gen_hci->activate_device = &activate_device; gen_hci->do_packet = &uhci_do_packet; - gen_hci->get_frame = &get_current_frame; + gen_hci->add_pipe = &uhci_establish_pipe; enumerate_hci(gen_hci); } @@ -227,9 +227,9 @@ static void uhci_reset_device(struct usb_device* device) cdi_sleep_ms(20); } -static void activate_device(struct hci* gen_hci, struct usb_device* device) +static void activate_device(struct usb_device* device) { - struct uhci* uhci = (struct uhci*) gen_hci; + struct uhci* uhci = (struct uhci*) device->hci; dprintf("Ger??t an Port %i wird aktiviert.\n", device->port); cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, @@ -237,11 +237,6 @@ static void activate_device(struct hci* gen_hci, struct usb_device* device) cdi_sleep_ms(20); } -static int get_current_frame(struct hci* gen_hci) -{ - return cdi_inw(((struct uhci*) gen_hci)->pbase + UHCI_FRNUM); -} - static inline int tsl(volatile int* variable) { int rval; @@ -252,8 +247,9 @@ static inline int tsl(volatile int* variable) static volatile int locked = 0; -static int uhci_do_packet(struct usb_device* usbdev, struct usb_packet* packet) +static int uhci_do_packet(struct usb_packet* packet) { + struct usb_device* usbdev = packet->pipe->device; struct uhci* uhci = (struct uhci*) usbdev->hci; struct uhci_td td; struct transfer addr; @@ -266,12 +262,12 @@ static int uhci_do_packet(struct usb_device* usbdev, struct usb_packet* packet) td.next = 1; //Invalid td.active = 1; td.ioc = 1; - td.data_toggle = usbdev->data_toggle; + td.data_toggle = packet->pipe->data_toggle; td.low_speed = usbdev->low_speed; td.errors = 1; td.pid = packet->type; td.device = usbdev->id; - td.endpoint = packet->endpoint->endpoint_address & 0x07; + td.endpoint = packet->pipe->endpoint->endpoint_address & 0x07; td.maxlen = packet->length ? packet->length - 1 : 0x7FF; while (tsl(&locked)) { #ifndef CDI_STANDALONE @@ -284,16 +280,15 @@ static int uhci_do_packet(struct usb_device* usbdev, struct usb_packet* packet) frame &= 0x3FF; } - if (mempool_get(uhci->buffers, &buf, &pbuf) < 0) { - // FIXME Oder sowas - return USB_NAK; - } + if (mempool_get(uhci->buffers, &buf, &pbuf) < 0) { + return USB_TRIVIAL_ERROR; + } addr.virt = buf; addr.phys = pbuf; addr.error = 0xFFFF; - buf = (void*)((uintptr_t) buf + sizeof(td)); + buf = (void*) ((uintptr_t) buf + sizeof(td)); pbuf += sizeof(td); if (!packet->length) { @@ -388,3 +383,8 @@ static void uhci_handler(struct cdi_device* cdi_hci) } cdi_outw(uhci->pbase + UHCI_USBSTS, status); } + +static void uhci_establish_pipe(struct usb_pipe* pipe) +{ + //Macht nix, weil das bei UHCI nicht n??tig ist +} -- 1.6.3.3 From max at tyndur.org Fri Aug 28 18:52:18 2009 From: max at tyndur.org (max at tyndur.org) Date: Fri, 28 Aug 2009 18:52:18 +0200 Subject: [cdi-devel] [PATCH] usb: OHCI driver Message-ID: <1251478338-14535-1-git-send-email-max@tyndur.org> From: Max Reitz + OHCI driver works on QEMU Signed-off-by: Max Reitz --- usb/include/ohci.h | 35 ++++++++-- usb/include/usb.h | 1 + usb/main.c | 4 + usb/msd.c | 2 + usb/ohci.c | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++- usb/uhci.c | 4 +- 6 files changed, 233 insertions(+), 12 deletions(-) diff --git a/usb/include/ohci.h b/usb/include/ohci.h index 5f6ad01..4c896b9 100644 --- a/usb/include/ohci.h +++ b/usb/include/ohci.h @@ -25,6 +25,8 @@ #include +#include "cdi/lists.h" + #include "mempool.h" #include "usb.h" @@ -174,6 +176,8 @@ struct ohci { struct mempool* ed_pool; struct mempool* transfer_pool; + + cdi_list_t ed_list; }; #define OHC_ED_DIR_TD 0 //Richtung im TD definiert @@ -190,10 +194,22 @@ struct ohci_ed { unsigned mps : 11; //Maximale Paketgr????e unsigned user : 5; //F??r uns frei verf??gbar uint32_t td_queue_tail; //Letzter TD in der Warteschlange - uint32_t td_queue_head; //N??chster TD in der Warteschlange + volatile uint32_t td_queue_head; //N??chster TD in der Warteschlange uint32_t next_ed; //N??chster ED } __attribute__((packed)); +struct ohci_ed_desc { + struct ohci_ed* virt; + uintptr_t phys; + int function; + int endpoint; + cdi_list_t transfers; + enum { + USB_CONTROL, + USB_BULK + } type; +} __attribute__((packed)); + #define OHC_TD_DIR_SETUP 0 #define OHC_TD_DIR_OUT 1 #define OHC_TD_DIR_IN 2 @@ -208,15 +224,22 @@ struct ohci_td { //warten soll unsigned toggle : 2; //Datatoggle (00b oder 01b - Wert muss aus dem ED //ermittelt werden, 10b = DATA0, 11b = DATA1) - unsigned error : 2; //Anzahl der aufgetretenen Fehler - bei 11b wird der - //Status im "condition"-Feld gespeichert - unsigned condition : 4; //Status - uint32_t current_buffer_pointer; //Pointer zu den Daten (bei 0 wurden alle - //Daten ??bertragen) + volatile unsigned error : 2; //Anzahl der aufgetretenen Fehler - bei 11b + //wird der Status im "condition"-Feld + //gespeichert. + volatile unsigned condition : 4; //Status + volatile uint32_t current_buffer_pointer; //Pointer zu den Daten (bei 0 + //wurden alle Daten ??bertragen) uint32_t next_td; //N??chster TD uint32_t buffer_end; //Letztes Datenbyte des Puffers } __attribute__((packed)); +struct ohci_td_desc { + struct ohci_td* virt; + uintptr_t phys; + struct ohci_ed_desc* endpoint; +} __attribute__((packed)); + struct cdi_driver* init_ohcd(void); void ohci_init(struct cdi_device* cdi_hci); diff --git a/usb/include/usb.h b/usb/include/usb.h index 896afc7..3882601 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -25,6 +25,7 @@ #include +#include "cdi/lists.h" #include "cdi/misc.h" #include "cdi/pci.h" diff --git a/usb/main.c b/usb/main.c index 8861fcf..cfab9a5 100644 --- a/usb/main.c +++ b/usb/main.c @@ -349,6 +349,8 @@ static void enum_device(struct usb_device* usbdev) //Resetten usbdev->reset(usbdev); + usbdev->hci->add_pipe(usbdev->ep0); + //Erste acht Bytes des Device-Descriptors abrufen und die maximale Paketgr????e von EP0 feststellen dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, 8, STD_REQUEST, REC_DEVICE, @@ -369,6 +371,8 @@ static void enum_device(struct usb_device* usbdev) 0); usbdev->id = id; + usbdev->hci->add_pipe(usbdev->ep0); + //Den ganzen Device-Descriptor einlesen dev_desc = do_control(usbdev, DEV_TO_HOST, NULL, sizeof(*dev_desc), STD_REQUEST, diff --git a/usb/msd.c b/usb/msd.c index 4410f37..f91df4d 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -195,6 +195,8 @@ void register_msd(struct usb_device* usbdev) dprintf("Nicht gen??gend Endpoints gefunden.\n"); return; } + usbdev->hci->add_pipe(msc->bulk_in); + usbdev->hci->add_pipe(msc->bulk_out); if (!msd_get_capacity(usbdev, &bs, &bc)) { strgdev->block_size = 0; strgdev->block_count = 0; diff --git a/usb/ohci.c b/usb/ohci.c index e4de271..e697fae 100644 --- a/usb/ohci.c +++ b/usb/ohci.c @@ -23,6 +23,7 @@ #include #include "cdi/io.h" +#include "cdi/lists.h" #include "cdi/misc.h" #include "cdi/pci.h" @@ -133,8 +134,8 @@ void ohci_init(struct cdi_device* cdi_hci) return; } - ohci->transfer_pool = mempool_create(32 * (1023 + sizeof(struct ohci_td)), - 1023 + sizeof(struct ohci_td)); + ohci->transfer_pool = mempool_create(32 * (1024 + sizeof(struct ohci_td)), + 1024 + sizeof(struct ohci_td)); if (ohci->transfer_pool == NULL) { dprintf("Transfer-Speicherpool konnte nicht erzeugt werden!\n"); return; @@ -224,6 +225,8 @@ void ohci_init(struct cdi_device* cdi_hci) ohci->memory->hc_rh_port_status[i] |= OHC_RP_CCS; //Deaktivieren } + ohci->ed_list = cdi_list_create(); + gen_hci->find_devices = &get_devices; gen_hci->activate_device = &activate_device; gen_hci->do_packet = &ohci_do_packet; @@ -234,7 +237,117 @@ void ohci_init(struct cdi_device* cdi_hci) static int ohci_do_packet(struct usb_packet* packet) { - return USB_STALLED; + struct ohci_ed_desc* edsc; + struct ohci_td_desc* tdsc, * otdsc; + int i, cond; + struct ohci* ohci = (struct ohci*) packet->pipe->device->hci; + struct ohci_td* vtd; + uintptr_t ptd; + + for (i = 0; (edsc = cdi_list_get(ohci->ed_list, i)) != NULL; i++) { + if ((edsc->function == packet->pipe->device->id) && + (edsc->endpoint == packet->pipe->endpoint->endpoint_address)) + { + break; + } + } + if (edsc == NULL) { + return USB_TIMEOUT; //Genau das w??rde passieren + } + if (mempool_get(ohci->transfer_pool, (void**) &vtd, &ptd) == -1) { + return USB_TRIVIAL_ERROR; + } + if ((packet->data != NULL) && (packet->type != PACKET_IN)) { + memcpy((void*) vtd + sizeof(struct ohci_td), packet->data, + packet->length); + } + vtd->rounding = 1; //Warum nicht + switch (packet->type) { + case PACKET_SETUP: + vtd->direction = OHC_TD_DIR_SETUP; + break; + case PACKET_IN: + vtd->direction = OHC_TD_DIR_IN; + break; + case PACKET_OUT: + vtd->direction = OHC_TD_DIR_OUT; + break; + default: + mempool_put(ohci->transfer_pool, vtd); + return USB_TRIVIAL_ERROR; //Hm, passt nicht ganz + } + vtd->di = 0; + vtd->toggle = 0x2 | packet->pipe->data_toggle; + vtd->error = 0; + vtd->condition = 15; + if ((packet->data == NULL) || !packet->length) { + vtd->current_buffer_pointer = 0; + vtd->buffer_end = 0; + } else { + vtd->current_buffer_pointer = ptd + sizeof(struct ohci_td); + vtd->buffer_end = ptd + sizeof(struct ohci_td) + packet->length - 1; + } + vtd->next_td = 0; + tdsc = malloc(sizeof(*tdsc)); + if (tdsc == NULL) { + mempool_put(ohci->transfer_pool, vtd); + return USB_TRIVIAL_ERROR; + } + tdsc->virt = vtd; + tdsc->phys = ptd; + tdsc->endpoint = edsc; + if (cdi_list_size(edsc->transfers)) { + otdsc = cdi_list_get(edsc->transfers, cdi_list_size( + edsc->transfers) - 1); + otdsc->virt->next_td = ptd; + } + cdi_list_insert(edsc->transfers, cdi_list_size(edsc->transfers), tdsc); + if (!edsc->virt->td_queue_head) { + edsc->virt->td_queue_head = ptd; + } + if (edsc->type == USB_CONTROL) { + ohci->memory->hc_command_status |= OHC_CMST_CLF; + } else if (edsc->type == USB_BULK) { + ohci->memory->hc_command_status |= OHC_CMST_BLF; + } + while (vtd->condition == 15) { + cdi_sleep_ms(1); + } + cond = vtd->condition; + if ((packet->data != NULL) && (packet->type == PACKET_IN)) { + memcpy(packet->data, (void*) vtd + sizeof(struct ohci_td), + packet->length); + } + if (vtd->next_td == 0) { + edsc->virt->td_queue_head = 0; + } + mempool_put(ohci->transfer_pool, vtd); + switch (cond) { + case 0x00: + return USB_NO_ERROR; + case 0x01: + return USB_CRC; + case 0x02: + return USB_BITSTUFF; + case 0x03: //Datatoggle-Fehler + return USB_CRC; + case 0x04: + return USB_STALLED; + case 0x05: + return USB_TIMEOUT; + case 0x06: //PID-Fehler + case 0x07: //Ebenso + return USB_CRC; + case 0x08: + return USB_BABBLE; + case 0x09: //Data underrun + return USB_CRC; + case 0x0C: //Buffer overrun + case 0x0D: //Buffer underrun + return USB_BUFFER_ERROR; + default: //??hm... + return USB_CRC; + } } static cdi_list_t get_devices(struct hci* gen_hci) @@ -282,12 +395,73 @@ static void ohci_reset_device(struct usb_device* device) static void ohci_establish_pipe(struct usb_pipe* pipe) { + struct ohci* ohci = (struct ohci*) pipe->device->hci; + struct ohci_ed* ved; + uintptr_t ped; + struct ohci_ed_desc* dsc; + //TODO Mehr als Control und Bulk + int iscontrol = 0; + + if (mempool_get(ohci->ed_pool, (void**) &ved, &ped) == -1) { + return; + } + ved->function = pipe->device->id; + ved->endpoint = pipe->endpoint->endpoint_address; + if (ved->endpoint == 0) { //EP0 + ved->direction = OHC_ED_DIR_TD; + iscontrol = 1; + } else if (pipe->endpoint->endpoint_address & 0x80) { + ved->direction = OHC_ED_DIR_IN; + } else { + ved->direction = OHC_ED_DIR_OUT; + } + ved->low_speed = pipe->device->low_speed; + ved->skip = 0; + ved->format = 0; + ved->mps = pipe->endpoint->max_packet_size; + ved->td_queue_tail = 0; + ved->td_queue_head = 0; + dsc = cdi_list_get(ohci->ed_list, 0); + if (dsc == NULL) { + ved->next_ed = 0; + } else { + ved->next_ed = dsc->phys; + } + dsc = malloc(sizeof(*dsc)); + if (dsc == NULL) { + mempool_put(ohci->ed_pool, ved); + return; + } + dsc->virt = ved; + dsc->phys = ped; + dsc->function = pipe->device->id; + dsc->endpoint = pipe->endpoint->endpoint_address; + dsc->type = iscontrol ? USB_CONTROL : USB_BULK; + dsc->transfers = cdi_list_create(); + cdi_list_push(ohci->ed_list, dsc); + + dprintf( + "%s-Endpoint %i (Ger??t %i) hinzugef??gt (%s)\n", + iscontrol ? "Control" : "Bulk", ved->endpoint, ved->function, + (ved->direction == + OHC_ED_DIR_TD) ? "IN/OUT/SETUP" : ((ved->direction == + OHC_ED_DIR_IN) ? "IN" : "OUT")); + + if (iscontrol) { + ohci->memory->hc_control_head_ed = ped; + } else { + ohci->memory->hc_bulk_head_ed = ped; + } } static void ohci_handler(struct cdi_device* dev) { struct ohci* ohci = (struct ohci*) ((struct cdi_hci*) dev)->hci; + struct ohci_ed_desc* edsc; + struct ohci_td_desc* tdsc; uint32_t status, done = 0; + int i, j; + uintptr_t phys; status = ohci->memory->hc_interrupt_status; @@ -296,12 +470,29 @@ static void ohci_handler(struct cdi_device* dev) } if (status & OHC_INT_UE) { dprintf("Schwerer HC-Fehler.\n"); - //TODO HC anhalten + ohci->memory->hc_command_status |= OHC_CMST_RESET; done |= OHC_INT_UE; } if (status & OHC_INT_SF) { //??hm... Na ja. done |= OHC_INT_SF; } + if (status & OHC_INT_WDH) { //Paket gesendet + done |= OHC_INT_WDH; + phys = ohci->hcca->done_head; + for (i = 0; (edsc = cdi_list_get(ohci->ed_list, i)) != NULL; i++) { + for (j = 0; (tdsc = cdi_list_get(edsc->transfers, j)) != NULL; + j++) + { + if (tdsc->phys == phys) { //Das ist der fertige Transfer + cdi_list_remove(edsc->transfers, j); + break; + } + } + if (tdsc != NULL) { //Gefunden + break; + } + } + } if (status & ~done) { dprintf("Nicht behandelter Interrupt: 0x%08X\n", status & ~done); diff --git a/usb/uhci.c b/usb/uhci.c index cb7b857..405a879 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -116,8 +116,8 @@ void uhci_init(struct cdi_device* cdi_hci) return; } request_size = - // Maximale Paketl??nge (Full Speed) - 1023 + + // Maximale Paketl??nge (Full Speed) ist 1023, Alignment au??erdem + 1024 + // Transferdeskriptor sizeof(struct uhci_td); -- 1.6.3.3 From kevin at tyndur.org Sat Aug 29 14:22:06 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 29 Aug 2009 14:22:06 +0200 Subject: [cdi-devel] [PATCH] usb: Handle multiple packets in usb_do_packet Message-ID: <1251548526-17488-1-git-send-email-kevin@tyndur.org> Signed-off-by: Kevin Wolf --- usb/include/usb.h | 2 +- usb/main.c | 93 ++++++++++++++++++++++++++++++++++++++++++----------- usb/msd.c | 10 +++--- 3 files changed, 80 insertions(+), 25 deletions(-) diff --git a/usb/include/usb.h b/usb/include/usb.h index 3882601..5f0f2cc 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -334,7 +334,7 @@ struct usb_pipe { #define HCI_STRUCT_SIZE max(sizeof(struct ohci), sizeof(struct uhci)) -int usb_do_packet(struct usb_packet* packet); +int usb_do_packet(struct usb_packet* packets, int num_packets); void enumerate_hci(struct hci*); struct cdi_driver* init_uhcd(void); void init_msc_driver(void); diff --git a/usb/main.c b/usb/main.c index cfab9a5..da767cb 100644 --- a/usb/main.c +++ b/usb/main.c @@ -99,20 +99,38 @@ static const int next_data_type[9] = { "STATUS" };*/ +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + /** * Verarbeitet ein USB-Paket. * * @param packet Das Paket */ -int usb_do_packet(struct usb_packet* packet) +int usb_do_packet(struct usb_packet* packets, int num_packets) { int error = USB_NAK; - int tod_short, tod = packet->type_of_data; + int tod_short, tod; int i = 0, mps; - struct usb_pipe* pipe = packet->pipe; - struct usb_device* device = pipe->device; + int num_out_packets; + struct usb_pipe* pipe; + struct usb_device* device; + struct usb_packet* out_packets; + + if (num_packets < 1) { + return USB_TRIVIAL_ERROR; + } + + // Alle Pakete muessen vom selben Typ sein und dieselbe Pipe benutzen + tod = packets[0].type_of_data; + pipe = packets[0].pipe; + for (i = 1; i < num_packets; i++) { + if ((packets[i].type_of_data != tod) || (packets[i].pipe != pipe)) { + return USB_TRIVIAL_ERROR; + } + } + device = pipe->device; if (!(device->expects & tod)) { dprintf("0x%04X erwartet, 0x%04X bekommen...?\n", device->expects, tod); // FIXME Das ist keine elegante Loesung des Problems @@ -125,31 +143,63 @@ int usb_do_packet(struct usb_packet* packet) } //dprintf("TOD: %s\n", tod_name[tod_short]); device->expects = next_data_type[tod_short]; - packet->type &= 0xFF; - struct usb_packet send_packet = { - .pipe = packet->pipe, - .type = packet->type, - .data = packet->data - }; + // Pruefen, wie viele Pakete wir draus machen muessen mps = pipe->endpoint->max_packet_size; + num_out_packets = 0; + for (i = 0; i < num_packets; i++) { + int num = (packets[i].length + mps - 1) / mps; + if (num > 0) { + num_out_packets += num; + } else { + // Pakete ohne Daten wollen auch gesendet werden + num_out_packets++; + } + } + + // Ggf. Pakete kopieren und splitten + if (num_out_packets == num_packets) { + out_packets = packets; + } else { + int j; + int cur = 0; + + out_packets = calloc(num_out_packets, sizeof(*out_packets)); + for (i = 0; i < num_packets; i++) { + int num = (packets[i].length + mps - 1) / mps; + if (num == 0) { + num = 1; + } + + for (j = 0; j < num; j++) { + out_packets[cur].pipe = pipe; + out_packets[cur].type = packets[i].type; + out_packets[cur].data = packets[i].data; + out_packets[cur].length = MIN(packets[i].length, mps); + out_packets[cur].type_of_data = tod; - for (i = 0; (i < packet->length) || (!i && !packet->length); i += mps) { - send_packet.length = - (packet->length - i > mps) ? mps : (packet->length - i); + packets[i].data += out_packets[cur].length; + packets[i].length -= out_packets[cur].length; + cur++; + } + } + } + + // Pakete (FIXME: einzeln) senden + for (i = 0; i < num_out_packets; i++) { error = USB_NAK; while (error == USB_NAK) { - error = device->hci->do_packet(&send_packet); + error = device->hci->do_packet(&out_packets[i]); if (error == USB_NAK) { cdi_sleep_ms(5); } } if (error != USB_NO_ERROR) { - i -= mps; + i--; } else { pipe->data_toggle ^= 1; - send_packet.data += mps; } + if (error == USB_STALLED) { printf("[usb] ENDPOINT 0x%02X DES GER??TS %i STALLED!\n", pipe->endpoint->endpoint_address, @@ -159,6 +209,11 @@ int usb_do_packet(struct usb_packet* packet) pipe->endpoint->endpoint_address); } } + + if (out_packets != packets) { + free(out_packets); + } + return error; } @@ -191,7 +246,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, .type_of_data = USB_TOD_SETUP, }; - if (usb_do_packet(&setup_packet)) { + if (usb_do_packet(&setup_packet, 1)) { return NULL; } @@ -209,7 +264,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, DEV_TO_HOST ? USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), }; - rval = usb_do_packet(&data_packet); + rval = usb_do_packet(&data_packet, 1); } if (rval == USB_NO_ERROR) { @@ -228,7 +283,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, ack_packet.type_of_data = USB_TOD_SETUP_ACK_OUT; } - rval = usb_do_packet(&ack_packet); + rval = usb_do_packet(&ack_packet, 1); } return (rval == USB_NO_ERROR) ? buffer : NULL; } diff --git a/usb/msd.c b/usb/msd.c index f91df4d..4191002 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -241,7 +241,7 @@ static int write_cmd(struct usb_device* usbdev, void* src) .type_of_data = USB_TOD_COMMAND, }; - return usb_do_packet(&cmd_packet); + return usb_do_packet(&cmd_packet, 1); } /** @@ -268,7 +268,7 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) .type_of_data = USB_TOD_STATUS, }; - error = usb_do_packet(&status_packet); + error = usb_do_packet(&status_packet, 1); if (error != USB_NO_ERROR) { return error; } @@ -321,7 +321,7 @@ static int msd_get_capacity(struct usb_device* usbdev, uint32_t* block_size, .type_of_data = USB_TOD_DATA_IN, }; - if (usb_do_packet(&in_packet) != USB_NO_ERROR) { + if (usb_do_packet(&in_packet, 1) != USB_NO_ERROR) { return 0; } @@ -463,7 +463,7 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, .type_of_data = USB_TOD_DATA_IN, }; - error = usb_do_packet(&in_packet); + error = usb_do_packet(&in_packet, 1); if (error != USB_NO_ERROR) { return error; } @@ -564,7 +564,7 @@ static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, .type_of_data = USB_TOD_DATA_OUT, }; - error = usb_do_packet(&out_packet); + error = usb_do_packet(&out_packet, 1); if (error != USB_NO_ERROR) { return error; } -- 1.6.0.2 From max at tyndur.org Sat Aug 29 14:40:27 2009 From: max at tyndur.org (Max Reitz) Date: Sat, 29 Aug 2009 14:40:27 +0200 Subject: [cdi-devel] [PATCH] usb: Handle multiple packets in usb_do_packet In-Reply-To: <1251548526-17488-1-git-send-email-kevin@tyndur.org> References: <1251548526-17488-1-git-send-email-kevin@tyndur.org> Message-ID: <4A9921BB.4070405@tyndur.org> On Sat, Aug 29 14:22, Kevin Wolf wrote: > Signed-off-by: Kevin Wolf > --- > usb/include/usb.h | 2 +- > usb/main.c | 93 ++++++++++++++++++++++++++++++++++++++++++----------- > usb/msd.c | 10 +++--- > 3 files changed, 80 insertions(+), 25 deletions(-) > > diff --git a/usb/include/usb.h b/usb/include/usb.h > index 3882601..5f0f2cc 100644 > --- a/usb/include/usb.h > +++ b/usb/include/usb.h > @@ -334,7 +334,7 @@ struct usb_pipe { > #define HCI_STRUCT_SIZE max(sizeof(struct ohci), sizeof(struct uhci)) > > > -int usb_do_packet(struct usb_packet* packet); > +int usb_do_packet(struct usb_packet* packets, int num_packets); > void enumerate_hci(struct hci*); > struct cdi_driver* init_uhcd(void); > void init_msc_driver(void); > diff --git a/usb/main.c b/usb/main.c > index cfab9a5..da767cb 100644 > --- a/usb/main.c > +++ b/usb/main.c > @@ -99,20 +99,38 @@ static const int next_data_type[9] = { > "STATUS" > };*/ > > +#define MIN(a, b) ((a) < (b) ? (a) : (b)) > + > /** > * Verarbeitet ein USB-Paket. > * > * @param packet Das Paket > */ > > -int usb_do_packet(struct usb_packet* packet) > +int usb_do_packet(struct usb_packet* packets, int num_packets) > { > int error = USB_NAK; > - int tod_short, tod = packet->type_of_data; > + int tod_short, tod; > int i = 0, mps; > - struct usb_pipe* pipe = packet->pipe; > - struct usb_device* device = pipe->device; > + int num_out_packets; > + struct usb_pipe* pipe; > + struct usb_device* device; > + struct usb_packet* out_packets; > + > + if (num_packets < 1) { > + return USB_TRIVIAL_ERROR; > + } > + > + // Alle Pakete muessen vom selben Typ sein und dieselbe Pipe benutzen > + tod = packets[0].type_of_data; > + pipe = packets[0].pipe; > + for (i = 1; i < num_packets; i++) { > + if ((packets[i].type_of_data != tod) || (packets[i].pipe != pipe)) { > + return USB_TRIVIAL_ERROR; > + } > + } > > + device = pipe->device; > if (!(device->expects & tod)) { > dprintf("0x%04X erwartet, 0x%04X bekommen...?\n", device->expects, tod); > // FIXME Das ist keine elegante Loesung des Problems > @@ -125,31 +143,63 @@ int usb_do_packet(struct usb_packet* packet) > } > //dprintf("TOD: %s\n", tod_name[tod_short]); > device->expects = next_data_type[tod_short]; > - packet->type &= 0xFF; > > - struct usb_packet send_packet = { > - .pipe = packet->pipe, > - .type = packet->type, > - .data = packet->data > - }; > + // Pruefen, wie viele Pakete wir draus machen muessen > mps = pipe->endpoint->max_packet_size; > + num_out_packets = 0; > + for (i = 0; i < num_packets; i++) { > + int num = (packets[i].length + mps - 1) / mps; > + if (num > 0) { > + num_out_packets += num; > + } else { > + // Pakete ohne Daten wollen auch gesendet werden > + num_out_packets++; > + } > + } > + > + // Ggf. Pakete kopieren und splitten > + if (num_out_packets == num_packets) { > + out_packets = packets; > + } else { > + int j; > + int cur = 0; > + > + out_packets = calloc(num_out_packets, sizeof(*out_packets)); > + for (i = 0; i < num_packets; i++) { > + int num = (packets[i].length + mps - 1) / mps; > + if (num == 0) { > + num = 1; > + } > + > + for (j = 0; j < num; j++) { > + out_packets[cur].pipe = pipe; > + out_packets[cur].type = packets[i].type; > + out_packets[cur].data = packets[i].data; > + out_packets[cur].length = MIN(packets[i].length, mps); > + out_packets[cur].type_of_data = tod; > > - for (i = 0; (i < packet->length) || (!i && !packet->length); i += mps) { > - send_packet.length = > - (packet->length - i > mps) ? mps : (packet->length - i); > + packets[i].data += out_packets[cur].length; > + packets[i].length -= out_packets[cur].length; > + cur++; > + } > + } > + } > + > + // Pakete (FIXME: einzeln) senden > + for (i = 0; i < num_out_packets; i++) { > error = USB_NAK; > while (error == USB_NAK) { > - error = device->hci->do_packet(&send_packet); > + error = device->hci->do_packet(&out_packets[i]); > if (error == USB_NAK) { > cdi_sleep_ms(5); > } > } > if (error != USB_NO_ERROR) { > - i -= mps; > + i--; > } else { > pipe->data_toggle ^= 1; > - send_packet.data += mps; > } > + > if (error == USB_STALLED) { > printf("[usb] ENDPOINT 0x%02X DES GER??TS %i STALLED!\n", > pipe->endpoint->endpoint_address, > @@ -159,6 +209,11 @@ int usb_do_packet(struct usb_packet* packet) > pipe->endpoint->endpoint_address); > } > } > + > + if (out_packets != packets) { > + free(out_packets); > + } > + > return error; > } > > @@ -191,7 +246,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, > .type_of_data = USB_TOD_SETUP, > }; > > - if (usb_do_packet(&setup_packet)) { > + if (usb_do_packet(&setup_packet, 1)) { > return NULL; > } > > @@ -209,7 +264,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, > DEV_TO_HOST ? USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), > }; > > - rval = usb_do_packet(&data_packet); > + rval = usb_do_packet(&data_packet, 1); > } > > if (rval == USB_NO_ERROR) { > @@ -228,7 +283,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, > ack_packet.type_of_data = USB_TOD_SETUP_ACK_OUT; > } > > - rval = usb_do_packet(&ack_packet); > + rval = usb_do_packet(&ack_packet, 1); > } > return (rval == USB_NO_ERROR) ? buffer : NULL; > } > diff --git a/usb/msd.c b/usb/msd.c > index f91df4d..4191002 100644 > --- a/usb/msd.c > +++ b/usb/msd.c > @@ -241,7 +241,7 @@ static int write_cmd(struct usb_device* usbdev, void* src) > .type_of_data = USB_TOD_COMMAND, > }; > > - return usb_do_packet(&cmd_packet); > + return usb_do_packet(&cmd_packet, 1); > } > > /** > @@ -268,7 +268,7 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) > .type_of_data = USB_TOD_STATUS, > }; > > - error = usb_do_packet(&status_packet); > + error = usb_do_packet(&status_packet, 1); > if (error != USB_NO_ERROR) { > return error; > } > @@ -321,7 +321,7 @@ static int msd_get_capacity(struct usb_device* usbdev, uint32_t* block_size, > .type_of_data = USB_TOD_DATA_IN, > }; > > - if (usb_do_packet(&in_packet) != USB_NO_ERROR) { > + if (usb_do_packet(&in_packet, 1) != USB_NO_ERROR) { > return 0; > } > > @@ -463,7 +463,7 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, > .type_of_data = USB_TOD_DATA_IN, > }; > > - error = usb_do_packet(&in_packet); > + error = usb_do_packet(&in_packet, 1); > if (error != USB_NO_ERROR) { > return error; > } > @@ -564,7 +564,7 @@ static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, > .type_of_data = USB_TOD_DATA_OUT, > }; > > - error = usb_do_packet(&out_packet); > + error = usb_do_packet(&out_packet, 1); > if (error != USB_NO_ERROR) { > return error; > } > Acked-by: Max Reitz From max at tyndur.org Sat Aug 29 21:03:10 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 29 Aug 2009 21:03:10 +0200 Subject: [cdi-devel] [PATCH] Multiple packets Message-ID: <1251572590-31603-1-git-send-email-max@tyndur.org> From: Max Reitz + OHCD can send more than one packet at once. Now it is a lot faster but unfortunately, you get a lot of STALLs that cannot be recovered. * UHCD takes multiple packets but sends them one by one - although that is not very nice. Signed-off-by: Max Reitz --- usb/include/usb.h | 12 +++- usb/main.c | 46 +++++----- usb/msd.c | 9 ++- usb/ohci.c | 248 +++++++++++++++++++++++++++++++---------------------- usb/uhci.c | 23 +++++- 5 files changed, 210 insertions(+), 128 deletions(-) diff --git a/usb/include/usb.h b/usb/include/usb.h index 5f0f2cc..4b37dc5 100644 --- a/usb/include/usb.h +++ b/usb/include/usb.h @@ -169,7 +169,7 @@ struct hci { * * @return Gibt den Status an (z. B. USB_NO_ERROR f??r keinen Fehler) */ - int (* do_packet)(struct usb_packet* packet); + int (* do_packets)(struct usb_packet* packets, int num_packets); /** * Erstellt eine neue Pipe @@ -299,6 +299,12 @@ struct msclass_data { struct usb_pipe* bulk_out; }; +enum usb_toggle { + TOGGLE_UNSPECIFIC = 0, + TOGGLE_0 = 1, + TOGGLE_1 = 2 +}; + /** Beschreibt ein USB-Paket */ struct usb_packet { /// Die gewuenschte Pipe @@ -311,6 +317,10 @@ struct usb_packet { int length; /// Typ der Daten (zur Sicherheit, damit es kein doofes STALL gibt) int type_of_data; + /// Fehlertyp (oder USB_NO_ERROR) + int condition; + /// Bestimmtes Data-Toggle benuten + enum usb_toggle use_toggle; }; /// Beschreibt eine Pipe diff --git a/usb/main.c b/usb/main.c index da767cb..4639369 100644 --- a/usb/main.c +++ b/usb/main.c @@ -177,6 +177,7 @@ int usb_do_packet(struct usb_packet* packets, int num_packets) out_packets[cur].data = packets[i].data; out_packets[cur].length = MIN(packets[i].length, mps); out_packets[cur].type_of_data = tod; + out_packets[cur].condition = USB_NAK; packets[i].data += out_packets[cur].length; packets[i].length -= out_packets[cur].length; @@ -185,28 +186,27 @@ int usb_do_packet(struct usb_packet* packets, int num_packets) } } - // Pakete (FIXME: einzeln) senden - for (i = 0; i < num_out_packets; i++) { - error = USB_NAK; - while (error == USB_NAK) { - error = device->hci->do_packet(&out_packets[i]); - if (error == USB_NAK) { - cdi_sleep_ms(5); + error = device->hci->do_packets(out_packets, num_out_packets); + if (error & USB_STALLED) { + printf("[usb] ENDPOINT 0x%02X DES GER??TS %i STALLED!\n", + pipe->endpoint->endpoint_address, + device->id); + do_control(device, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, + REC_ENDPOINT, CLEAR_FEATURE, 0, + pipe->endpoint->endpoint_address); + } else if (error & USB_NAK) { + // Nicht angenommene Pakete (FIXME: einzeln) senden + for (i = 0; i < num_out_packets; i++) { + if (!(out_packets[i].condition & USB_NAK)) { + continue; + } + error = USB_NAK; + while (error & USB_NAK) { + error = device->hci->do_packets(&out_packets[i], 1); + if (error & USB_NAK) { + cdi_sleep_ms(5); + } } - } - if (error != USB_NO_ERROR) { - i--; - } else { - pipe->data_toggle ^= 1; - } - - if (error == USB_STALLED) { - printf("[usb] ENDPOINT 0x%02X DES GER??TS %i STALLED!\n", - pipe->endpoint->endpoint_address, - device->id); - do_control(device, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, - REC_ENDPOINT, CLEAR_FEATURE, 0, - pipe->endpoint->endpoint_address); } } @@ -244,6 +244,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, .data = setup, .length = sizeof(*setup), .type_of_data = USB_TOD_SETUP, + .use_toggle = TOGGLE_UNSPECIFIC }; if (usb_do_packet(&setup_packet, 1)) { @@ -262,6 +263,7 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, .type_of_data = (direction == DEV_TO_HOST ? USB_TOD_SETUP_DATA_IN : USB_TOD_SETUP_DATA_OUT), + .use_toggle = TOGGLE_UNSPECIFIC }; rval = usb_do_packet(&data_packet, 1); @@ -272,8 +274,8 @@ static void* do_control(struct usb_device* device, int direction, void* buffer, .pipe = device->ep0, .data = NULL, .length = 0, + .use_toggle = TOGGLE_1 }; - device->ep0->data_toggle = 1; if (no_data || (direction == HOST_TO_DEV)) { ack_packet.type = PACKET_IN; diff --git a/usb/msd.c b/usb/msd.c index 4191002..34e6e04 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -34,7 +34,7 @@ //DEBUG definieren, um einige Debugmeldungen anzuzeigen #define DEBUG //FULLDEBUG definieren, um ALLE Debugmeldungen anzuzeigen -#define FULLDEBUG +//#define FULLDEBUG #if defined FULLDEBUG && !defined DEBUG #define DEBUG @@ -239,6 +239,7 @@ static int write_cmd(struct usb_device* usbdev, void* src) .data = src, .length = 0x1F, .type_of_data = USB_TOD_COMMAND, + .use_toggle = TOGGLE_UNSPECIFIC }; return usb_do_packet(&cmd_packet, 1); @@ -266,6 +267,7 @@ static int read_status(struct usb_device* usbdev, uint32_t expected_tag) .data = csw, .length = 0x0D, .type_of_data = USB_TOD_STATUS, + .use_toggle = TOGGLE_UNSPECIFIC }; error = usb_do_packet(&status_packet, 1); @@ -319,6 +321,7 @@ static int msd_get_capacity(struct usb_device* usbdev, uint32_t* block_size, .data = cap, .length = sizeof(*cap), .type_of_data = USB_TOD_DATA_IN, + .use_toggle = TOGGLE_UNSPECIFIC }; if (usb_do_packet(&in_packet, 1) != USB_NO_ERROR) { @@ -373,7 +376,7 @@ static inline int tsl(volatile int* variable) return rval; } -#define MAX_ACCESS_BLOCKS 32 +#define MAX_ACCESS_BLOCKS 4 static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, uint16_t sectors, void* buffer, @@ -461,6 +464,7 @@ static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, .data = buffer, .length = length, .type_of_data = USB_TOD_DATA_IN, + .use_toggle = TOGGLE_UNSPECIFIC }; error = usb_do_packet(&in_packet, 1); @@ -562,6 +566,7 @@ static uint32_t msd_write(struct usb_device* usbdev, uint32_t lba, .data = buffer, .length = length, .type_of_data = USB_TOD_DATA_OUT, + .use_toggle = TOGGLE_UNSPECIFIC }; error = usb_do_packet(&out_packet, 1); diff --git a/usb/ohci.c b/usb/ohci.c index 9e0a2aa..d95f330 100644 --- a/usb/ohci.c +++ b/usb/ohci.c @@ -53,7 +53,7 @@ static void ohci_kill(struct cdi_driver* cdi_hcd); static void ohci_deinit(struct cdi_device* cdi_hci); void ohci_init(struct cdi_device* cdi_hci); static void ohci_handler(struct cdi_device* dev); -static int ohci_do_packet(struct usb_packet* packet); +static int ohci_do_packets(struct usb_packet* packet, int num_packets); static cdi_list_t get_devices(struct hci* gen_hci); static void activate_device(struct usb_device* device); static void ohci_reset_device(struct usb_device* device); @@ -109,7 +109,7 @@ void ohci_init(struct cdi_device* cdi_hci) dprintf("Speicher nicht gefunden!\n"); return; } - dprintf("Speicher @ 0x%08x\n", (uintptr_t) ohci->memory); + dprintf("Speicher @ 0x%08X\n", (uintptr_t) ohci->memory); if ((ohci->memory->hc_revision & 0xFF) != 0x10) { dprintf( "Dieses OHCI k??nnte inkompatibel mit dem Treiber sein (HCI %i.%i, HCD 1.0)\n", @@ -134,7 +134,7 @@ void ohci_init(struct cdi_device* cdi_hci) return; } - ohci->transfer_pool = mempool_create(32 * (1024 + sizeof(struct ohci_td)), + ohci->transfer_pool = mempool_create(56 * (1024 + sizeof(struct ohci_td)), 1024 + sizeof(struct ohci_td)); if (ohci->transfer_pool == NULL) { dprintf("Transfer-Speicherpool konnte nicht erzeugt werden!\n"); @@ -229,24 +229,33 @@ void ohci_init(struct cdi_device* cdi_hci) gen_hci->find_devices = &get_devices; gen_hci->activate_device = &activate_device; - gen_hci->do_packet = &ohci_do_packet; + gen_hci->do_packets = &ohci_do_packets; gen_hci->add_pipe = &ohci_establish_pipe; enumerate_hci(gen_hci); } -static int ohci_do_packet(struct usb_packet* packet) +static inline int tsl(volatile int* variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} + +static int ohci_do_packets(struct usb_packet* packets, int num_packets) { struct ohci_ed_desc* edsc; struct ohci_td_desc* tdsc, * otdsc; - int i, cond; - struct ohci* ohci = (struct ohci*) packet->pipe->device->hci; - struct ohci_td* vtd; - uintptr_t ptd; + int i, j, cond, toggle, done_packets; + struct ohci* ohci = (struct ohci*) packets[0].pipe->device->hci; + struct ohci_td* vtd[num_packets]; + uintptr_t ptd[num_packets]; + static volatile int locked = 0; for (i = 0; (edsc = cdi_list_get(ohci->ed_list, i)) != NULL; i++) { - if ((edsc->function == packet->pipe->device->id) && - (edsc->endpoint == packet->pipe->endpoint->endpoint_address)) + if ((edsc->function == packets[0].pipe->device->id) && + (edsc->endpoint == packets[0].pipe->endpoint->endpoint_address)) { break; } @@ -254,100 +263,143 @@ static int ohci_do_packet(struct usb_packet* packet) if (edsc == NULL) { return USB_TIMEOUT; //Genau das w??rde passieren } - if (mempool_get(ohci->transfer_pool, (void**) &vtd, &ptd) == -1) { - return USB_TRIVIAL_ERROR; - } - if ((packet->data != NULL) && (packet->type != PACKET_IN)) { - memcpy((void*) vtd + sizeof(struct ohci_td), packet->data, - packet->length); - } - vtd->rounding = 1; //Warum nicht - switch (packet->type) { - case PACKET_SETUP: - vtd->direction = OHC_TD_DIR_SETUP; - break; - case PACKET_IN: - vtd->direction = OHC_TD_DIR_IN; - break; - case PACKET_OUT: - vtd->direction = OHC_TD_DIR_OUT; - break; - default: - mempool_put(ohci->transfer_pool, vtd); - return USB_TRIVIAL_ERROR; //Hm, passt nicht ganz - } - vtd->di = 0; - vtd->toggle = 0x2 | packet->pipe->data_toggle; - vtd->error = 0; - vtd->condition = 15; - if ((packet->data == NULL) || !packet->length) { - vtd->current_buffer_pointer = 0; - vtd->buffer_end = 0; - } else { - vtd->current_buffer_pointer = ptd + sizeof(struct ohci_td); - vtd->buffer_end = ptd + sizeof(struct ohci_td) + packet->length - 1; - } - vtd->next_td = 0; - tdsc = malloc(sizeof(*tdsc)); - if (tdsc == NULL) { - mempool_put(ohci->transfer_pool, vtd); - return USB_TRIVIAL_ERROR; - } - tdsc->virt = vtd; - tdsc->phys = ptd; - tdsc->endpoint = edsc; - if (cdi_list_size(edsc->transfers)) { - otdsc = cdi_list_get(edsc->transfers, cdi_list_size( - edsc->transfers) - 1); - otdsc->virt->next_td = ptd; - } - cdi_list_insert(edsc->transfers, cdi_list_size(edsc->transfers), tdsc); - if (!edsc->virt->td_queue_head) { - edsc->virt->td_queue_head = ptd; + toggle = packets[0].pipe->data_toggle; + for (i = 0; i < num_packets; i++) { + if (mempool_get(ohci->transfer_pool, (void**) &vtd[i], + &ptd[i]) == -1) + { + return USB_TRIVIAL_ERROR; + } + if ((packets[i].data != NULL) && (packets[i].type != PACKET_IN)) { + memcpy((void*) vtd[i] + sizeof(struct ohci_td), packets[i].data, + packets[i].length); + } + vtd[i]->rounding = 1; //Warum nicht + switch (packets[i].type) { + case PACKET_SETUP: + vtd[i]->direction = OHC_TD_DIR_SETUP; + break; + case PACKET_IN: + vtd[i]->direction = OHC_TD_DIR_IN; + break; + case PACKET_OUT: + vtd[i]->direction = OHC_TD_DIR_OUT; + break; + default: //Hm, passt nicht ganz + for (j = 0; j <= i; j++) { + mempool_put(ohci->transfer_pool, vtd[j]); + } + return USB_TRIVIAL_ERROR; + } + vtd[i]->di = 0; + if (packets[i].use_toggle == TOGGLE_0) { + toggle = 0; + } else if (packets[i].use_toggle == TOGGLE_1) { + toggle = 1; + } + vtd[i]->toggle = 0x2 | toggle; + toggle ^= 1; + vtd[i]->error = 0; + vtd[i]->condition = 15; + if ((packets[i].data == NULL) || !packets[i].length) { + vtd[i]->current_buffer_pointer = 0; + vtd[i]->buffer_end = 0; + } else { + vtd[i]->current_buffer_pointer = ptd[i] + sizeof(struct ohci_td); + vtd[i]->buffer_end = ptd[i] + sizeof(struct ohci_td) + + packets[i].length - 1; + } + vtd[i]->next_td = 0; + tdsc = malloc(sizeof(*tdsc)); + if (tdsc == NULL) { + for (j = 0; j <= i; j++) { + mempool_put(ohci->transfer_pool, vtd[j]); + } + return USB_TRIVIAL_ERROR; + } + tdsc->virt = vtd[i]; + tdsc->phys = ptd[i]; + tdsc->endpoint = edsc; + + while (tsl(&locked)) { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + if (cdi_list_size(edsc->transfers)) { + otdsc = cdi_list_get(edsc->transfers, 0); + otdsc->virt->next_td = ptd[i]; + } + if (!edsc->virt->td_queue_head) { + edsc->virt->td_queue_head = ptd[i]; + } + locked = 0; + + cdi_list_push(edsc->transfers, tdsc); } + packets[0].pipe->data_toggle = toggle; if (edsc->type == USB_CONTROL) { ohci->memory->hc_command_status |= OHC_CMST_CLF; } else if (edsc->type == USB_BULK) { ohci->memory->hc_command_status |= OHC_CMST_BLF; } - while (vtd->condition == 15) { + done_packets = 0; + cond = USB_NO_ERROR; + while (done_packets < num_packets) { cdi_sleep_ms(1); + for (i = 0; i < num_packets; i++) { + if (vtd[i]->condition != 15) { + done_packets++; + switch (vtd[i]->condition) { + case 0x00: + packets[i].condition = USB_NO_ERROR; + break; + case 0x02: + packets[i].condition = USB_BITSTUFF; + break; + case 0x04: + packets[i].condition = USB_STALLED; + for (j = 0; j < num_packets; j++) { + vtd[j]->condition = 0x04; //Allet, was nicht fertig ist, ist STALLed + } + break; + case 0x05: + packets[i].condition = USB_TIMEOUT; + break; + case 0x08: + packets[i].condition = USB_BABBLE; + break; + case 0x0C: + case 0x0D: + packets[i].condition = USB_BUFFER_ERROR; + break; + default: + packets[i].condition = USB_CRC; + break; + } + cond |= packets[i].condition; + vtd[i]->condition = 15; //Nicht nochmal ??berpr??fen + } + } } - cond = vtd->condition; - if ((packet->data != NULL) && (packet->type == PACKET_IN)) { - memcpy(packet->data, (void*) vtd + sizeof(struct ohci_td), - packet->length); + for (i = 0; i < num_packets; i++) { + if ((packets[i].data != NULL) && (packets[i].type == PACKET_IN)) { + memcpy(packets[i].data, (void*) vtd[i] + sizeof(struct ohci_td), + packets[i].length); + } + if (vtd[i]->next_td == 0) { + edsc->virt->td_queue_head = 0; + } + mempool_put(ohci->transfer_pool, vtd[i]); } - if (vtd->next_td == 0) { + if (cond & USB_STALLED) { //Transferliste f??r diesen Endpoint leeren edsc->virt->td_queue_head = 0; + while ((tdsc = cdi_list_pop(edsc->transfers)) != NULL) { + free(tdsc); + } + edsc->virt->td_queue_tail = 0; //Weitermachen } - mempool_put(ohci->transfer_pool, vtd); - switch (cond) { - case 0x00: - return USB_NO_ERROR; - case 0x01: - return USB_CRC; - case 0x02: - return USB_BITSTUFF; - case 0x03: //Datatoggle-Fehler - return USB_CRC; - case 0x04: - return USB_STALLED; - case 0x05: - return USB_TIMEOUT; - case 0x06: //PID-Fehler - case 0x07: //Ebenso - return USB_CRC; - case 0x08: - return USB_BABBLE; - case 0x09: //Data underrun - return USB_CRC; - case 0x0C: //Buffer overrun - case 0x0D: //Buffer underrun - return USB_BUFFER_ERROR; - default: //??hm... - return USB_CRC; - } + return cond; } static cdi_list_t get_devices(struct hci* gen_hci) @@ -440,13 +492,6 @@ static void ohci_establish_pipe(struct usb_pipe* pipe) dsc->transfers = cdi_list_create(); cdi_list_push(ohci->ed_list, dsc); - dprintf( - "%s-Endpoint %i (Ger??t %i) hinzugef??gt (%s)\n", - iscontrol ? "Control" : "Bulk", ved->endpoint, ved->function, - (ved->direction == - OHC_ED_DIR_TD) ? "IN/OUT/SETUP" : ((ved->direction == - OHC_ED_DIR_IN) ? "IN" : "OUT")); - if (iscontrol) { ohci->memory->hc_control_head_ed = ped; } else { @@ -485,6 +530,7 @@ static void ohci_handler(struct cdi_device* dev) { if (tdsc->phys == phys) { //Das ist der fertige Transfer cdi_list_remove(edsc->transfers, j); + free(tdsc); break; } } diff --git a/usb/uhci.c b/usb/uhci.c index 405a879..bad63bb 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -54,7 +54,7 @@ static struct cdi_driver cdi_driver; static cdi_list_t active_transfers; static void uhci_handler(struct cdi_device* dev); -static int uhci_do_packet(struct usb_packet* packet); +static int uhci_do_packets(struct usb_packet* packets, int num_packets); static cdi_list_t get_devices(struct hci* gen_hci); static void activate_device(struct usb_device* device); static void uhci_deinit(struct cdi_device* cdi_hci); @@ -184,7 +184,7 @@ void uhci_init(struct cdi_device* cdi_hci) dprintf(" Fertig\n"); gen_hci->find_devices = &get_devices; gen_hci->activate_device = &activate_device; - gen_hci->do_packet = &uhci_do_packet; + gen_hci->do_packets = &uhci_do_packets; gen_hci->add_pipe = &uhci_establish_pipe; enumerate_hci(gen_hci); @@ -262,7 +262,13 @@ static int uhci_do_packet(struct usb_packet* packet) td.next = 1; //Invalid td.active = 1; td.ioc = 1; + if (packet->use_toggle == TOGGLE_0) { + packet->pipe->data_toggle = 0; + } else if (packet->use_toggle == TOGGLE_1) { + packet->pipe->data_toggle = 1; + } td.data_toggle = packet->pipe->data_toggle; + packet->pipe->data_toggle ^= 1; td.low_speed = usbdev->low_speed; td.errors = 1; td.pid = packet->type; @@ -330,6 +336,19 @@ static int uhci_do_packet(struct usb_packet* packet) return addr.error; } +static int uhci_do_packets(struct usb_packet* packet, int num_packets) +{ + int i, cond = 0; + + //TODO + for (i = 0; i < num_packets; i++) { + packet[i].condition = uhci_do_packet(&packet[i]); + cond |= packet[i].condition; + } + + return cond; +} + static void uhci_handler(struct cdi_device* cdi_hci) { struct uhci* uhci = (struct uhci*) ((struct cdi_hci*) cdi_hci)->hci; -- 1.6.3.3 From max at tyndur.org Sun Aug 30 11:35:56 2009 From: max at tyndur.org (max at tyndur.org) Date: Sun, 30 Aug 2009 11:35:56 +0200 Subject: [cdi-devel] [PATCH] usb: A bit faster Message-ID: <1251624956-7021-1-git-send-email-max@tyndur.org> From: Max Reitz * Made faster (OHCI is now limited to 48 packets at a time, so the MSC driver may send or receive more than 4 sectors consecutively) Signed-off-by: Max Reitz --- usb/msd.c | 2 +- usb/ohci.c | 22 +++++++++++++++++++--- usb/uhci.c | 4 ++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/usb/msd.c b/usb/msd.c index 34e6e04..b3f303c 100644 --- a/usb/msd.c +++ b/usb/msd.c @@ -376,7 +376,7 @@ static inline int tsl(volatile int* variable) return rval; } -#define MAX_ACCESS_BLOCKS 4 +#define MAX_ACCESS_BLOCKS 32 static uint32_t msd_read(struct usb_device* usbdev, uint32_t lba, uint16_t sectors, void* buffer, diff --git a/usb/ohci.c b/usb/ohci.c index d95f330..cc88169 100644 --- a/usb/ohci.c +++ b/usb/ohci.c @@ -53,7 +53,8 @@ static void ohci_kill(struct cdi_driver* cdi_hcd); static void ohci_deinit(struct cdi_device* cdi_hci); void ohci_init(struct cdi_device* cdi_hci); static void ohci_handler(struct cdi_device* dev); -static int ohci_do_packets(struct usb_packet* packet, int num_packets); +static int ohci_do_all_packets(struct usb_packet* packet, int num_packets); +static int ohci_do_some_packets(struct usb_packet* packets, int num_packets); static cdi_list_t get_devices(struct hci* gen_hci); static void activate_device(struct usb_device* device); static void ohci_reset_device(struct usb_device* device); @@ -229,7 +230,7 @@ void ohci_init(struct cdi_device* cdi_hci) gen_hci->find_devices = &get_devices; gen_hci->activate_device = &activate_device; - gen_hci->do_packets = &ohci_do_packets; + gen_hci->do_packets = &ohci_do_all_packets; gen_hci->add_pipe = &ohci_establish_pipe; enumerate_hci(gen_hci); @@ -243,7 +244,22 @@ static inline int tsl(volatile int* variable) return rval; } -static int ohci_do_packets(struct usb_packet* packets, int num_packets) +static int ohci_do_all_packets(struct usb_packet* packets, int num_packets) +{ + int error = USB_NO_ERROR; + + //Mehr als 48 sind wohl nicht ratsam (vor allem wegen des Mempools) + while ((num_packets > 0) && (error == USB_NO_ERROR)) { + error = ohci_do_some_packets(packets, + (num_packets > 48) ? 48 : num_packets); + packets = &packets[48]; + num_packets -= (num_packets > 48) ? 48 : num_packets; + } + + return error; +} + +static int ohci_do_some_packets(struct usb_packet* packets, int num_packets) { struct ohci_ed_desc* edsc; struct ohci_td_desc* tdsc, * otdsc; diff --git a/usb/uhci.c b/usb/uhci.c index bad63bb..bd9169f 100644 --- a/usb/uhci.c +++ b/usb/uhci.c @@ -341,9 +341,9 @@ static int uhci_do_packets(struct usb_packet* packet, int num_packets) int i, cond = 0; //TODO - for (i = 0; i < num_packets; i++) { + for (i = 0; (i < num_packets) && !cond; i++) { packet[i].condition = uhci_do_packet(&packet[i]); - cond |= packet[i].condition; + cond = packet[i].condition; } return cond; -- 1.6.3.3 From max at tyndur.org Sun Aug 30 13:51:07 2009 From: max at tyndur.org (max at tyndur.org) Date: Sun, 30 Aug 2009 13:51:07 +0200 Subject: [cdi-devel] [PATCH] Mehrere ISRs pro IRQ Message-ID: <1251633067-21048-1-git-send-email-max@tyndur.org> From: Max Reitz Signed-off-by: Max Reitz --- src/kernel/src/intr.c | 76 +++++++++++++++++++++++++++++++------------------ 1 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/kernel/src/intr.c b/src/kernel/src/intr.c index 9fa3ff3..be90e8c 100644 --- a/src/kernel/src/intr.c +++ b/src/kernel/src/intr.c @@ -38,6 +38,7 @@ */ #include "cpu.h" +#include #include "ports.h" #include "types.h" #include "kernel.h" @@ -65,8 +66,8 @@ typedef struct { gate_descriptor idt[IDT_SIZE]; -pfIrqHandler irq_handlers[16] = { 0 }; // f?r jeden IRQ genau ein Handler. Wenn es keinen Handler gibt, ist der entsprechende Wert 0. -struct task * intr_handling_task[256] = { NULL }; +static list_t* irq_handlers[16] = { NULL }; +list_t* intr_handling_task[256] = { NULL }; void handle_irq(int irq, dword* esp); @@ -120,7 +121,7 @@ dword irqs_to_send[16]; extern dword kernel_pd_id; /* - * gibt einen neuen Wert f?r esp zurck + * gibt einen neuen Wert fuer esp zurck */ dword handle_int(dword esp) { @@ -146,7 +147,7 @@ dword handle_int(dword esp) } else { - // TODO: Abst?rzen, denn diese Interrupts d?rften nicht aufgerufen werden + // TODO: Abstuerzen, denn diese Interrupts duerften nicht aufgerufen werden } send_irqs(&esp); @@ -186,11 +187,11 @@ dword handle_int(dword esp) /** * Legt eine IDT an und installiert die Exception Handler. Nicht genutzte - * Eintr?ge werden mit Verweisen auf den Null-Handler initalisiert. - * Anschlie?end wird die IDT geladen. + * Eintraege werden mit Verweisen auf den Null-Handler initalisiert. + * Anschliessend wird die IDT geladen. */ void init_idt() { - int i; + int i; // Tabelle initialisieren for (i = 0; i < IDT_SIZE; i++) @@ -198,7 +199,7 @@ void init_idt() { set_intr(i, 0x08, &null_handler, 0, IDT_INTERRUPT_GATE); } - // Register befllen + // Register befuellen struct { word size; dword base; @@ -265,9 +266,9 @@ void init_idt() { outb_wait(PIC1_DATA, 0x00); outb_wait(PIC2_DATA, 0x00); - // Handler f?r den System Call installieren + // Handler fuer den System Call installieren // TODO: SOllte eigentlich ein IDT_TRAP_GATE sein, aber irgendwo ist da - // noch ein Problem, das dann f?r einen Page Fault sorgt, wenn ich zu + // noch ein Problem, das dann fuer einen Page Fault sorgt, wenn ich zu // schnell tippe... set_intr(SYSCALL_INTERRUPT, 0x08, syscall_stub, 3, IDT_INTERRUPT_GATE); //set_intr(SYSCALL_INTERRUPT, 0x08, syscall_stub, 3, IDT_TRAP_GATE); @@ -323,26 +324,29 @@ int request_irq(int irq, void * handler) return 1; } - if(irq_handlers[irq] != 0) - { - return 1; + if (irq_handlers[irq] == NULL) { + irq_handlers[irq] = list_create(); } - irq_handlers[irq] = handler; + list_push(irq_handlers[irq], handler); return 0; } int release_irq(int irq) { - if(irq < 0 || irq > 15) + /*if(irq < 0 || irq > 15) { return 1; } irq_handlers[irq] = 0; - return 0; + return 0;*/ + + //FIXME Ich weiss ja nicht, wer das braucht. Aber wenn es noetig ist, dann + //muesste man das in release_irq(int irq, void* handler) oder sowas aendern + return 1; } @@ -361,7 +365,7 @@ void handle_irq(int irq, dword* esp) } }*/ - // IRQs 7 und 15 k?nnen unabsichtlich aufgerufen werden + // IRQs 7 und 15 koennen unabsichtlich aufgerufen werden // In diesem Fall beim PIC nachschauen, ob wirklich was zu verarbeiten // ist, ansonsten gleich wieder abbrechen. if ((irq == 7) || (irq == 15)) @@ -386,9 +390,16 @@ void handle_irq(int irq, dword* esp) //printf("disable IRQ %d\n", irq); disable_irq(irq); } - else if (irq_handlers[irq] != 0) + else if (irq_handlers[irq] != NULL) { - irq_handlers[irq](irq, esp); + int i; + pfIrqHandler handler; + + for (i = 0; (handler = list_get_element_at(irq_handlers[irq], i)) + != NULL; i++) + { + handler(irq, esp); + } } send_eoi: @@ -402,7 +413,10 @@ send_eoi: void set_intr_handling_task(byte intr, struct task * task) { //kprintf("Interrupt %d wird jetzt von Task %d behandelt\n", intr, task->pid); - intr_handling_task[intr] = task; + if (intr_handling_task[intr] == NULL) { + intr_handling_task[intr] = list_create(); + } + list_push(intr_handling_task[intr], task); } void handle_exception(dword* esp) @@ -426,7 +440,7 @@ void handle_exception(dword* esp) case 14: cr2 = read_cr2(); diff = (cr2 >> PAGE_SHIFT) - (isf->esp >> PAGE_SHIFT); - // Ueberpr?fen ob der Pagefault durch einen Stackoverflow + // Ueberpruefen, ob der Pagefault durch einen Stackoverflow // hervorgerufen wurde if ((diff >= -2) && (diff <= 2) && (current_task != NULL) @@ -440,7 +454,7 @@ void handle_exception(dword* esp) break; } - // Pr?fen, ob ein VM86-Task die Exception ausgel?st hat, und evtl reagieren + // Pruefen, ob ein VM86-Task die Exception ausgeloest hat, und evtl reagieren if (isf->eflags & 0x20000) { if (vm86_exception(esp)) { return; @@ -512,19 +526,25 @@ void handle_exception(dword* esp) void send_irqs(dword* esp) { + int i; + struct task* tsk; dword irq; for (irq = 0; irq < 16; irq++) { dword rpc_data = irq + IRQ_BASE; while (irqs_to_send[irq] > 0) { - schedule_to_task(intr_handling_task[irq + IRQ_BASE], esp); - if (!fastrpc_irq(intr_handling_task[irq + IRQ_BASE], 0, 0, 4, - (char*) &rpc_data, irq)) + for (i = 0; (tsk = list_get_element_at( + intr_handling_task[irq + IRQ_BASE], i)) != NULL; i++) { - //kprintf("SYSCALL_RPC = FALSE"); - break; + schedule_to_task(tsk, esp); + if (!fastrpc_irq(tsk, 0, 0, 4, + (char*) &rpc_data, irq)) + { + //kprintf("SYSCALL_RPC = FALSE"); + break; + } + //kprintf("IRQ-Handler aufgerufen"); } - //kprintf("IRQ-Handler aufgerufen"); irqs_to_send[irq]--; } } -- 1.6.3.3