From kevin at tyndur.org Sat Dec 5 17:09:03 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 5 Dec 2009 17:09:03 +0100 Subject: [cdi-devel] [PATCH 0/4] Restructure driver initialization Message-ID: <1260029347-15604-1-git-send-email-kevin@tyndur.org> Currently drivers have some init function and need to actively register with the CDI library by a function call. Only afterwards they transfer control to CDI. The following patches change drivers over to a passive registration and initialization model, i.e. drivers are registered by a CDI_DRIVER macro and their initialization routine is called as a callback. Hopefully this makes it easy for Pedigree to drop the pedigree.cc file they had to add to each driver. This series looks like quite a bit of change, but the interesting part is really just a few lines of changed headers. Most changes are only mechanical conversion of drivers. Unless there are any reservations left, I'm going to commit these patches to the CDI repository soon. Kevin Wolf (4): New header cdi-osdep.h Use the CDI_DRIVER macro Call driver init function as a callback Simplify driver declarations ata/main.c | 90 +++++++++++++++++++++++++++++++------------------- e1000/main.c | 64 +++++++++++++++--------------------- ext2/main.c | 58 ++++++++++++--------------------- floppy/main.c | 70 +++++++++++++++------------------------ include/cdi-osdep.h | 35 ++++++++++++++++++++ include/cdi.h | 9 +++-- iso9660/main.c | 54 ++++++++++++------------------ ne2k/main.c | 59 +++++++++++++-------------------- pcnet/main.c | 63 +++++++++++++---------------------- ramdisk/main.c | 57 ++++++++++++-------------------- rtl8139/main.c | 63 +++++++++++++---------------------- serial/main.c | 57 ++++++++++++-------------------- sis900/main.c | 66 +++++++++++++++---------------------- 13 files changed, 329 insertions(+), 416 deletions(-) create mode 100644 include/cdi-osdep.h From kevin at tyndur.org Sat Dec 5 17:09:04 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 5 Dec 2009 17:09:04 +0100 Subject: [cdi-devel] [PATCH 1/4] New header cdi-osdep.h In-Reply-To: <1260029347-15604-1-git-send-email-kevin@tyndur.org> References: <1260029347-15604-1-git-send-email-kevin@tyndur.org> Message-ID: <1260029347-15604-2-git-send-email-kevin@tyndur.org> * cdi: OS independent parts of cdi.h are now in cdi-osdep.h + cdi: CDI_DRIVER macro which is used to declare drivers (in preparation of the removal of main() from the drivers) Signed-off-by: Kevin Wolf --- include/cdi-osdep.h | 35 +++++++++++++++++++++++++++++++++++ include/cdi.h | 3 +-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 include/cdi-osdep.h diff --git a/include/cdi-osdep.h b/include/cdi-osdep.h new file mode 100644 index 0000000..6227e91 --- /dev/null +++ b/include/cdi-osdep.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2009 Kevin Wolf + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/projects/COPYING.WTFPL for more details. + */ + +#ifndef _CDI_OSDEP_H_ +#define _CDI_OSDEP_H_ + +/** + * \german + * Muss fuer jeden CDI-Treiber genau einmal verwendet werden, um ihn bei der + * CDI-Bibliothek zu registrieren + * + * @param name Name des Treibers + * @param drv Pointer auf eine Treiberbeschreibung (struct cdi_driver*) + * @param deps Liste von Namen anderer Treiber, von denen der Treiber abhaengt + * \endgerman + * + * \english + * CDI_DRIVER shall be used exactly once for each CDI driver. It registers + * the driver with the CDI library. + * + * @param name Name of the driver + * @param drv Pointer to a driver description (struct cdi_driver*) + * @param deps List of names of other drivers on which this driver depends + * \endenglish + */ +#define CDI_DRIVER(name, drv, deps...) /* TODO */ + +#endif diff --git a/include/cdi.h b/include/cdi.h index 06e1904..ed28c78 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -16,10 +16,9 @@ #include +#include "cdi-osdep.h" #include "cdi/lists.h" -#define CDI_STANDALONE - typedef enum { CDI_UNKNOWN = 0, CDI_NETWORK = 1, -- 1.6.0.2 From kevin at tyndur.org Sat Dec 5 17:09:05 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 5 Dec 2009 17:09:05 +0100 Subject: [cdi-devel] [PATCH 2/4] Use the CDI_DRIVER macro In-Reply-To: <1260029347-15604-1-git-send-email-kevin@tyndur.org> References: <1260029347-15604-1-git-send-email-kevin@tyndur.org> Message-ID: <1260029347-15604-3-git-send-email-kevin@tyndur.org> * cdi: Initialize driver structures statically instead of using init functions that set the values + cdi: Add a CDI_DRIVER definition to each driver Signed-off-by: Kevin Wolf --- ata/main.c | 48 +++++++++++++++++++++++++++++------------------- e1000/main.c | 26 ++++++++++++++++---------- ext2/main.c | 24 ++++++++++++++++-------- floppy/main.c | 30 ++++++++++++++++++------------ iso9660/main.c | 23 ++++++++++++++++------- ne2k/main.c | 31 +++++++++++++++++++------------ pcnet/main.c | 31 +++++++++++++++++++------------ ramdisk/main.c | 24 ++++++++++++++++-------- rtl8139/main.c | 28 +++++++++++++++++----------- serial/main.c | 24 ++++++++++++++++-------- sis900/main.c | 38 ++++++++++++++++++++++---------------- 11 files changed, 204 insertions(+), 123 deletions(-) diff --git a/ata/main.c b/ata/main.c index d5200c2..e7fec85 100644 --- a/ata/main.c +++ b/ata/main.c @@ -37,10 +37,11 @@ #include "device.h" +#define DRIVER_STORAGE_NAME "ata" +#define DRIVER_SCSI_NAME "atapi" + static struct cdi_storage_driver driver_storage; static struct cdi_scsi_driver driver_scsi; -static const char* driver_storage_name = "ata"; -static const char* driver_scsi_name = "atapi"; static cdi_list_t controller_list = NULL; static void ata_driver_init(int argc, char* argv[]); @@ -78,23 +79,7 @@ static void ata_driver_init(int argc, char* argv[]) // Konstruktor der Vaterklasse cdi_storage_driver_init((struct cdi_storage_driver*) &driver_storage); cdi_scsi_driver_init((struct cdi_scsi_driver*) &driver_scsi); - - // Namen setzen - driver_storage.drv.name = driver_storage_name; - driver_scsi.drv.name = driver_scsi_name; - - // Funktionspointer initialisieren - driver_storage.drv.destroy = ata_driver_destroy; - driver_storage.drv.init_device = ata_init_device; - driver_storage.drv.remove_device = ata_remove_device; - driver_storage.read_blocks = ata_read_blocks; - driver_storage.write_blocks = ata_write_blocks; - - driver_scsi.drv.destroy = atapi_driver_destroy; - driver_scsi.drv.init_device = atapi_init_device; - driver_scsi.drv.remove_device = atapi_remove_device; - driver_scsi.request = atapi_request; - + // Liste mit Controllern initialisieren controller_list = cdi_list_create(); @@ -180,3 +165,28 @@ static void atapi_driver_destroy(struct cdi_driver* driver) { cdi_scsi_driver_destroy((struct cdi_scsi_driver*) driver); } + + +static struct cdi_storage_driver driver_storage = { + .drv = { + .name = DRIVER_STORAGE_NAME, + .destroy = ata_driver_destroy, + .init_device = ata_init_device, + .remove_device = ata_remove_device, + }, + .read_blocks = ata_read_blocks, + .write_blocks = ata_write_blocks, +}; + +static struct cdi_scsi_driver driver_scsi = { + .drv = { + .name = DRIVER_SCSI_NAME, + .destroy = atapi_driver_destroy, + .init_device = atapi_init_device, + .remove_device = atapi_remove_device, + }, + .request = atapi_request, +}; + +CDI_DRIVER(DRIVER_STORAGE_NAME, driver_storage) +CDI_DRIVER(DRIVER_SCSI_NAME, driver_scsi) diff --git a/e1000/main.c b/e1000/main.c index a96e055..3affced 100644 --- a/e1000/main.c +++ b/e1000/main.c @@ -37,13 +37,13 @@ #include "device.h" +#define DRIVER_NAME "e1000" + struct e1000_driver { struct cdi_net_driver net; }; static struct e1000_driver driver; -static const char* driver_name = "e1000"; - static void e1000_driver_init(void); static void e1000_driver_destroy(struct cdi_driver* driver); @@ -71,14 +71,6 @@ static void e1000_driver_init() // Konstruktor der Vaterklasse cdi_net_driver_init((struct cdi_net_driver*) &driver); - // Namen setzen - driver.net.drv.name = driver_name; - - // Funktionspointer initialisieren - driver.net.drv.destroy = e1000_driver_destroy; - driver.net.drv.init_device = e1000_init_device; - driver.net.drv.remove_device = e1000_remove_device; - // Passende PCI-Geraete suchen cdi_list_t pci_devices = cdi_list_create(); cdi_pci_get_all_devices(pci_devices); @@ -117,3 +109,17 @@ static void e1000_driver_destroy(struct cdi_driver* driver) // TODO Alle Karten deinitialisieren } + + +static struct e1000_driver driver = { + .net = { + .drv = { + .name = DRIVER_NAME, + .destroy = e1000_driver_destroy, + .init_device = e1000_init_device, + .remove_device = e1000_remove_device, + }, + }, +}; + +CDI_DRIVER(DRIVER_NAME, driver) diff --git a/ext2/main.c b/ext2/main.c index acbe6db..ae0c094 100644 --- a/ext2/main.c +++ b/ext2/main.c @@ -41,13 +41,13 @@ #include "ext2_cdi.h" +#define DRIVER_NAME "ext2" + struct ext2_driver { struct cdi_fs_driver fs; }; static struct ext2_driver ext2_driver; -static const char* driver_name = "ext2"; - static int ext2_driver_init(struct ext2_driver* driver); static void ext2_driver_destroy(struct cdi_driver* driver); @@ -77,12 +77,6 @@ static int ext2_driver_init(struct ext2_driver* driver) // Konstruktor der Vaterklasse cdi_fs_driver_init((struct cdi_fs_driver*) driver); - // Namen setzen - driver->fs.drv.name = driver_name; - driver->fs.fs_init = ext2_fs_init; - driver->fs.fs_destroy = ext2_fs_destroy; - - driver->fs.drv.destroy = ext2_driver_destroy; return 0; } @@ -93,3 +87,17 @@ static void ext2_driver_destroy(struct cdi_driver* driver) { cdi_fs_driver_destroy((struct cdi_fs_driver*) driver); } + + +static struct ext2_driver ext2_driver = { + .fs = { + .drv = { + .name = DRIVER_NAME, + .destroy = ext2_driver_destroy, + }, + .fs_init = ext2_fs_init, + .fs_destroy = ext2_fs_destroy, + }, +}; + +CDI_DRIVER(DRIVER_NAME, ext2_driver) diff --git a/floppy/main.c b/floppy/main.c index 8ecd8cd..1f44ed6 100644 --- a/floppy/main.c +++ b/floppy/main.c @@ -35,6 +35,8 @@ #include "device.h" +#define DRIVER_NAME "floppy" + struct floppy_driver { struct cdi_storage_driver storage; }; @@ -45,8 +47,6 @@ static struct floppy_controller floppy_controller = { .use_dma = 1 }; -static const char* driver_name = "floppy"; - static int floppy_driver_init(struct floppy_driver* driver); static void floppy_driver_destroy(struct cdi_driver* driver); @@ -79,16 +79,6 @@ static int floppy_driver_init(struct floppy_driver* driver) // Konstruktor der Vaterklasse cdi_storage_driver_init((struct cdi_storage_driver*) driver); - // Namen setzen - driver->storage.drv.name = driver_name; - - // Funktionspointer initialisieren - driver->storage.drv.destroy = floppy_driver_destroy; - driver->storage.drv.init_device = floppy_init_device; - driver->storage.drv.remove_device = floppy_remove_device; - driver->storage.read_blocks = floppy_read_blocks; - driver->storage.write_blocks = floppy_write_blocks; - // Geraete erstellen (TODO: Was wenn eines oder beide nicht vorhanden // sind?) for (i = 0; i < 2; i++) { @@ -131,3 +121,19 @@ static void floppy_driver_destroy(struct cdi_driver* driver) // TODO Alle Laufwerke deinitialisieren } + + +static struct floppy_driver floppy_driver = { + .storage = { + .drv = { + .name = DRIVER_NAME, + .destroy = floppy_driver_destroy, + .init_device = floppy_init_device, + .remove_device = floppy_remove_device, + }, + .read_blocks = floppy_read_blocks, + .write_blocks = floppy_write_blocks, + }, +}; + +CDI_DRIVER(DRIVER_NAME, floppy_driver) diff --git a/iso9660/main.c b/iso9660/main.c index 3d21a96..01424d9 100644 --- a/iso9660/main.c +++ b/iso9660/main.c @@ -24,13 +24,13 @@ #include "iso9660_cdi.h" #include "iso9660def.h" +#define DRIVER_NAME "iso9660" + struct iso9660_driver { struct cdi_fs_driver drv; }; static struct iso9660_driver iso9660_driver; -static const char* driver_name = "iso9660"; - static int iso9660_driver_init(struct iso9660_driver *driver); static void iso9660_driver_destroy(struct cdi_driver* driver); @@ -57,11 +57,6 @@ static int iso9660_driver_init(struct iso9660_driver *driver) { // Konstruktor der Vaterklasse cdi_fs_driver_init((struct cdi_fs_driver*)driver); - // Namen setzen - driver->drv.drv.name = driver_name; - driver->drv.fs_init = iso9660_fs_init; - driver->drv.fs_destroy = iso9660_fs_destroy; - driver->drv.drv.destroy = iso9660_driver_destroy; return 0; } @@ -91,3 +86,17 @@ int debug(const char *fmt,...) { return 0; #endif } + + +static struct iso9660_driver iso9660_driver = { + .drv = { + .drv = { + .name = DRIVER_NAME, + .destroy = iso9660_driver_destroy, + }, + .fs_init = iso9660_fs_init, + .fs_destroy = iso9660_fs_destroy, + }, +}; + +CDI_DRIVER(DRIVER_NAME, iso9660_driver) diff --git a/ne2k/main.c b/ne2k/main.c index 3a8a87e..170157a 100644 --- a/ne2k/main.c +++ b/ne2k/main.c @@ -35,12 +35,13 @@ #include "ne2k.h" -static struct { - struct cdi_net_driver net; -} driver; +#define DRIVER_NAME "ne2k" -static const char* driver_name = "ne2k"; +struct ne2k_driver { + struct cdi_net_driver net; +}; +static struct ne2k_driver driver; static int ne2k_driver_init(int argc, char* argv[]); static void ne2k_driver_destroy(struct cdi_driver* driver); @@ -65,14 +66,6 @@ static int ne2k_driver_init(int argc, char* argv[]) // Konstruktor der Vaterklasse cdi_net_driver_init((struct cdi_net_driver*) &driver); - // Namen setzen - driver.net.drv.name = driver_name; - - // Funktionspointer initialisieren - driver.net.drv.destroy = ne2k_driver_destroy; - driver.net.drv.init_device = ne2k_init_device; - driver.net.drv.remove_device = ne2k_remove_device; - // Passende PCI-Geraete suchen cdi_list_t pci_devices = cdi_list_create(); cdi_pci_get_all_devices(pci_devices); @@ -110,3 +103,17 @@ static void ne2k_driver_destroy(struct cdi_driver* driver) // TODO Alle Karten deinitialisieren } + + +static struct ne2k_driver ne2k_driver = { + .net = { + .drv = { + .name = DRIVER_NAME, + .destroy = ne2k_driver_destroy, + .init_device = ne2k_init_device, + .remove_device = ne2k_remove_device, + }, + }, +}; + +CDI_DRIVER(DRIVER_NAME, ne2k_driver) diff --git a/pcnet/main.c b/pcnet/main.c index c37454a..b6bf7e4 100644 --- a/pcnet/main.c +++ b/pcnet/main.c @@ -36,16 +36,17 @@ #include "pcnet.h" +#define DRIVER_NAME "pcnet" + struct module_options { uint32_t ip; }; -static struct { +struct pcnet_driver { struct cdi_net_driver net; -} driver; - -static const char* driver_name = "pcnet"; +}; +static struct pcnet_driver driver; static int pcnet_driver_init(int argc, char* argv[]); static void pcnet_driver_destroy(struct cdi_driver* driver); @@ -73,14 +74,6 @@ static int pcnet_driver_init(int argc, char* argv[]) // Konstruktor der Vaterklasse cdi_net_driver_init((struct cdi_net_driver*) &driver); - // Namen setzen - driver.net.drv.name = driver_name; - - // Funktionspointer initialisieren - driver.net.drv.destroy = pcnet_driver_destroy; - driver.net.drv.init_device = pcnet_init_device; - driver.net.drv.remove_device = pcnet_remove_device; - // Passende PCI-Geraete suchen cdi_list_t pci_devices = cdi_list_create(); cdi_pci_get_all_devices(pci_devices); @@ -115,3 +108,17 @@ static void pcnet_driver_destroy(struct cdi_driver* driver) // TODO Alle Karten deinitialisieren } + + +static struct pcnet_driver driver = { + .net = { + .drv = { + .name = DRIVER_NAME, + .destroy = pcnet_driver_destroy, + .init_device = pcnet_init_device, + .remove_device = pcnet_remove_device, + }, + }, +}; + +CDI_DRIVER(DRIVER_NAME, driver) diff --git a/ramdisk/main.c b/ramdisk/main.c index 73a7e0c..422cd80 100644 --- a/ramdisk/main.c +++ b/ramdisk/main.c @@ -35,13 +35,13 @@ #include "ramdisk_cdi.h" +#define DRIVER_NAME "ramdisk" + struct ramdisk_driver { struct cdi_fs_driver fs; }; static struct ramdisk_driver ramdisk_driver; -static const char* driver_name = "ramdisk"; - static int ramdisk_driver_init(struct ramdisk_driver* driver); static void ramdisk_driver_destroy(struct cdi_driver* driver); @@ -71,12 +71,6 @@ static int ramdisk_driver_init(struct ramdisk_driver* driver) // Konstruktor der Vaterklasse cdi_fs_driver_init((struct cdi_fs_driver*) driver); - // Namen setzen - driver->fs.drv.name = driver_name; - driver->fs.fs_init = ramdisk_fs_init; - driver->fs.fs_destroy = ramdisk_fs_destroy; - - driver->fs.drv.destroy = ramdisk_driver_destroy; return 0; } @@ -87,3 +81,17 @@ static void ramdisk_driver_destroy(struct cdi_driver* driver) { cdi_fs_driver_destroy((struct cdi_fs_driver*) driver); } + + +static struct ramdisk_driver ramdisk_driver = { + .fs = { + .drv = { + .name = DRIVER_NAME, + .destroy = ramdisk_driver_destroy, + }, + .fs_init = ramdisk_fs_init, + .fs_destroy = ramdisk_fs_destroy, + }, +}; + +CDI_DRIVER(DRIVER_NAME, ramdisk_driver) diff --git a/rtl8139/main.c b/rtl8139/main.c index 07c1adf..b9d7728 100644 --- a/rtl8139/main.c +++ b/rtl8139/main.c @@ -35,16 +35,16 @@ #include "rtl8139.h" +#define DRIVER_NAME "rtl8139" + struct module_options { uint32_t ip; }; -static struct { +static struct rtl8139_driver { struct cdi_net_driver net; } driver; -static const char* driver_name = "rtl8139"; - static int rtl8139_driver_init(int argc, char* argv[]); static void rtl8139_driver_destroy(struct cdi_driver* driver); @@ -72,14 +72,6 @@ static int rtl8139_driver_init(int argc, char* argv[]) // Konstruktor der Vaterklasse cdi_net_driver_init((struct cdi_net_driver*) &driver); - // Namen setzen - driver.net.drv.name = driver_name; - - // Funktionspointer initialisieren - driver.net.drv.destroy = rtl8139_driver_destroy; - driver.net.drv.init_device = rtl8139_init_device; - driver.net.drv.remove_device = rtl8139_remove_device; - // Passende PCI-Geraete suchen cdi_list_t pci_devices = cdi_list_create(); cdi_pci_get_all_devices(pci_devices); @@ -117,3 +109,17 @@ static void rtl8139_driver_destroy(struct cdi_driver* driver) // TODO Alle Karten deinitialisieren } + + +static struct rtl8139_driver driver = { + .net = { + .drv = { + .name = DRIVER_NAME, + .destroy = rtl8139_driver_destroy, + .init_device = rtl8139_init_device, + .remove_device = rtl8139_remove_device, + }, + }, +}; + +CDI_DRIVER(DRIVER_NAME, driver) diff --git a/serial/main.c b/serial/main.c index 2ebc62a..3fd71ba 100644 --- a/serial/main.c +++ b/serial/main.c @@ -35,13 +35,13 @@ #include "serial_cdi.h" +#define DRIVER_NAME "serial" + struct serial_driver { struct cdi_fs_driver fs; }; static struct serial_driver serial_driver; -static const char* driver_name = "serial"; - static int serial_driver_init(struct serial_driver* driver); static void serial_driver_destroy(struct cdi_driver* driver); @@ -71,12 +71,6 @@ static int serial_driver_init(struct serial_driver* driver) // Konstruktor der Vaterklasse cdi_fs_driver_init((struct cdi_fs_driver*) driver); - // Namen setzen - driver->fs.drv.name = driver_name; - driver->fs.fs_init = serial_fs_init; - driver->fs.fs_destroy = serial_fs_destroy; - - driver->fs.drv.destroy = serial_driver_destroy; return 0; } @@ -87,3 +81,17 @@ static void serial_driver_destroy(struct cdi_driver* driver) { cdi_fs_driver_destroy((struct cdi_fs_driver*) driver); } + + +static struct serial_driver serial_driver = { + .fs = { + .drv = { + .name = DRIVER_NAME, + .destroy = serial_driver_destroy, + }, + .fs_init = serial_fs_init, + .fs_destroy = serial_fs_destroy, + }, +}; + +CDI_DRIVER(DRIVER_NAME, serial_driver) diff --git a/sis900/main.c b/sis900/main.c index 550dd0a..0ae47ca 100644 --- a/sis900/main.c +++ b/sis900/main.c @@ -37,13 +37,13 @@ #include "device.h" +#define DRIVER_NAME "sis900" + struct sis900_driver { struct cdi_net_driver net; }; -static struct sis900_driver driver; -static const char* driver_name = "sis900"; - +static struct sis900_driver sis900_driver; static void sis900_driver_init(int argc, char* argv[]); static void sis900_driver_destroy(struct cdi_driver* driver); @@ -56,7 +56,7 @@ int init_sis900(int argc, char* argv[]) cdi_init(); sis900_driver_init(argc, argv); - cdi_driver_register((struct cdi_driver*) &driver); + cdi_driver_register((struct cdi_driver*) &sis900_driver); cdi_run_drivers(); @@ -69,15 +69,7 @@ int init_sis900(int argc, char* argv[]) static void sis900_driver_init(int argc, char* argv[]) { // Konstruktor der Vaterklasse - cdi_net_driver_init((struct cdi_net_driver*) &driver); - - // Namen setzen - driver.net.drv.name = driver_name; - - // Funktionspointer initialisieren - driver.net.drv.destroy = sis900_driver_destroy; - driver.net.drv.init_device = sis900_init_device; - driver.net.drv.remove_device = sis900_remove_device; + cdi_net_driver_init((struct cdi_net_driver*) &sis900_driver); // Passende PCI-Geraete suchen cdi_list_t pci_devices = cdi_list_create(); @@ -96,14 +88,14 @@ static void sis900_driver_init(int argc, char* argv[]) device->phys = phys_device; device->pci = dev; - cdi_list_push(driver.net.drv.devices, device); + cdi_list_push(sis900_driver.net.drv.devices, device); } else { cdi_pci_device_destroy(dev); } } - printf("sis900: %d Karten gefunden.\n", - cdi_list_size(driver.net.drv.devices)); + printf("sis900: %d Karten gefunden.\n", + cdi_list_size(sis900_driver.net.drv.devices)); cdi_list_destroy(pci_devices); } @@ -117,3 +109,17 @@ static void sis900_driver_destroy(struct cdi_driver* driver) // TODO Alle Karten deinitialisieren } + + +static struct sis900_driver sis900_driver = { + .net = { + .drv = { + .name = DRIVER_NAME, + .destroy = sis900_driver_destroy, + .init_device = sis900_init_device, + .remove_device = sis900_remove_device, + }, + }, +}; + +CDI_DRIVER(DRIVER_NAME, sis900_driver) -- 1.6.0.2 From kevin at tyndur.org Sat Dec 5 17:09:06 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 5 Dec 2009 17:09:06 +0100 Subject: [cdi-devel] [PATCH 3/4] Call driver init function as a callback In-Reply-To: <1260029347-15604-1-git-send-email-kevin@tyndur.org> References: <1260029347-15604-1-git-send-email-kevin@tyndur.org> Message-ID: <1260029347-15604-4-git-send-email-kevin@tyndur.org> * cdi: Drivers register their initialization function in the generic driver structur now. They no longer have a main function (if possible), instead the entry point is moved to somewhere in the OS specific CDI library. ! cdi: Drivers need to specify their type Signed-off-by: Kevin Wolf --- ata/main.c | 31 ++++++++++++++++++++----------- e1000/main.c | 24 +++++------------------- ext2/main.c | 27 ++++----------------------- floppy/main.c | 26 ++++---------------------- include/cdi.h | 4 +++- iso9660/main.c | 24 ++++-------------------- ne2k/main.c | 22 +++------------------- pcnet/main.c | 22 +++------------------- ramdisk/main.c | 26 ++++---------------------- rtl8139/main.c | 22 +++------------------- serial/main.c | 26 ++++---------------------- sis900/main.c | 24 +++++------------------- 12 files changed, 62 insertions(+), 216 deletions(-) diff --git a/ata/main.c b/ata/main.c index e7fec85..4699c2d 100644 --- a/ata/main.c +++ b/ata/main.c @@ -44,22 +44,19 @@ static struct cdi_storage_driver driver_storage; static struct cdi_scsi_driver driver_scsi; static cdi_list_t controller_list = NULL; -static void ata_driver_init(int argc, char* argv[]); -static void ata_driver_destroy(struct cdi_driver* driver); -static void atapi_driver_destroy(struct cdi_driver* driver); +static int argc; +static char** argv; #ifdef CDI_STANDALONE -int main(int argc, char* argv[]) +int main(int _argc, char* _argv[]) #else -int init_ata(int argc, char* argv[]) +int init_ata(int _argc, char* _argv[]) #endif { - cdi_init(); - ata_driver_init(argc, argv); - cdi_storage_driver_register((struct cdi_storage_driver*) &driver_storage); - cdi_scsi_driver_register((struct cdi_scsi_driver*) &driver_scsi); + argc = _argc; + argv = _argv; - cdi_run_drivers(); + cdi_init(); return 0; } @@ -67,7 +64,7 @@ int init_ata(int argc, char* argv[]) /** * Initialisiert die Datenstrukturen fuer den sis900-Treiber */ -static void ata_driver_init(int argc, char* argv[]) +static int ata_driver_init(void) { struct ata_controller* controller; uint16_t busmaster_regbase = 0; @@ -149,6 +146,8 @@ static void ata_driver_init(int argc, char* argv[]) controller->scsi = (struct cdi_scsi_driver*) &driver_scsi; ata_init_controller(controller); cdi_list_push(controller_list, controller); + + return 0; } /** @@ -161,6 +160,12 @@ static void ata_driver_destroy(struct cdi_driver* driver) // TODO Alle Karten deinitialisieren } +static int atapi_driver_init(void) +{ + // TODO ATAPI-Initialisierungscode hierher verschieben + return 0; +} + static void atapi_driver_destroy(struct cdi_driver* driver) { cdi_scsi_driver_destroy((struct cdi_scsi_driver*) driver); @@ -169,7 +174,9 @@ static void atapi_driver_destroy(struct cdi_driver* driver) static struct cdi_storage_driver driver_storage = { .drv = { + .type = CDI_STORAGE, .name = DRIVER_STORAGE_NAME, + .init = ata_driver_init, .destroy = ata_driver_destroy, .init_device = ata_init_device, .remove_device = ata_remove_device, @@ -180,7 +187,9 @@ static struct cdi_storage_driver driver_storage = { static struct cdi_scsi_driver driver_scsi = { .drv = { + .type = CDI_SCSI, .name = DRIVER_SCSI_NAME, + .init = atapi_driver_init, .destroy = atapi_driver_destroy, .init_device = atapi_init_device, .remove_device = atapi_remove_device, diff --git a/e1000/main.c b/e1000/main.c index 3affced..4f3bb9d 100644 --- a/e1000/main.c +++ b/e1000/main.c @@ -44,29 +44,11 @@ struct e1000_driver { }; static struct e1000_driver driver; -static void e1000_driver_init(void); -static void e1000_driver_destroy(struct cdi_driver* driver); - -#ifdef CDI_STANDALONE -int main(void) -#else -int init_e1000(void) -#endif -{ - cdi_init(); - - e1000_driver_init(); - cdi_driver_register((struct cdi_driver*) &driver); - - cdi_run_drivers(); - - return 0; -} /** * Initialisiert die Datenstrukturen fuer den e1000-Treiber */ -static void e1000_driver_init() +static int e1000_driver_init(void) { // Konstruktor der Vaterklasse cdi_net_driver_init((struct cdi_net_driver*) &driver); @@ -98,6 +80,8 @@ static void e1000_driver_init() cdi_list_size(driver.net.drv.devices)); cdi_list_destroy(pci_devices); + + return 0; } /** @@ -114,7 +98,9 @@ static void e1000_driver_destroy(struct cdi_driver* driver) static struct e1000_driver driver = { .net = { .drv = { + .type = CDI_NETWORK, .name = DRIVER_NAME, + .init = e1000_driver_init, .destroy = e1000_driver_destroy, .init_device = e1000_init_device, .remove_device = e1000_remove_device, diff --git a/ext2/main.c b/ext2/main.c index ae0c094..bdef05b 100644 --- a/ext2/main.c +++ b/ext2/main.c @@ -48,35 +48,14 @@ struct ext2_driver { }; static struct ext2_driver ext2_driver; -static int ext2_driver_init(struct ext2_driver* driver); -static void ext2_driver_destroy(struct cdi_driver* driver); - -#ifdef CDI_STANDALONE -int main(void) -#else -int init_ext2(void) -#endif -{ - cdi_init(); - - if (ext2_driver_init(&ext2_driver) != 0) { - return -1; - } - cdi_fs_driver_register((struct cdi_fs_driver*) &ext2_driver); - - cdi_run_drivers(); - - return 0; -} /** * Initialisiert die Datenstrukturen fuer den ext2-Treiber */ -static int ext2_driver_init(struct ext2_driver* driver) +static int ext2_driver_init(void) { // Konstruktor der Vaterklasse - cdi_fs_driver_init((struct cdi_fs_driver*) driver); - + cdi_fs_driver_init((struct cdi_fs_driver*) &ext2_driver); return 0; } @@ -92,7 +71,9 @@ static void ext2_driver_destroy(struct cdi_driver* driver) static struct ext2_driver ext2_driver = { .fs = { .drv = { + .type = CDI_FILESYSTEM, .name = DRIVER_NAME, + .init = ext2_driver_init, .destroy = ext2_driver_destroy, }, .fs_init = ext2_fs_init, diff --git a/floppy/main.c b/floppy/main.c index 1f44ed6..f888eb0 100644 --- a/floppy/main.c +++ b/floppy/main.c @@ -47,34 +47,14 @@ static struct floppy_controller floppy_controller = { .use_dma = 1 }; -static int floppy_driver_init(struct floppy_driver* driver); -static void floppy_driver_destroy(struct cdi_driver* driver); - -#ifdef CDI_STANDALONE -int main(void) -#else -int init_floppy(void) -#endif -{ - cdi_init(); - - if (floppy_driver_init(&floppy_driver) != 0) { - return -1; - } - cdi_storage_driver_register((struct cdi_storage_driver*) &floppy_driver); - - cdi_run_drivers(); - - return 0; -} - /** * Initialisiert die Datenstrukturen fuer den floppy-Treiber */ -static int floppy_driver_init(struct floppy_driver* driver) +static int floppy_driver_init(void) { int i; struct floppy_device* device; + struct floppy_driver* driver = &floppy_driver; // Konstruktor der Vaterklasse cdi_storage_driver_init((struct cdi_storage_driver*) driver); @@ -126,7 +106,9 @@ static void floppy_driver_destroy(struct cdi_driver* driver) static struct floppy_driver floppy_driver = { .storage = { .drv = { + .type = CDI_STORAGE, .name = DRIVER_NAME, + .init = floppy_driver_init, .destroy = floppy_driver_destroy, .init_device = floppy_init_device, .remove_device = floppy_remove_device, diff --git a/include/cdi.h b/include/cdi.h index ed28c78..c3fdf8d 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -29,6 +29,7 @@ typedef enum { CDI_AUDIO_MIXER = 6, CDI_USB_HCD = 7, CDI_USB = 8, + CDI_FILESYSTEM = 9, } cdi_device_type_t; struct cdi_driver; @@ -49,13 +50,14 @@ struct cdi_driver { void (*init_device)(struct cdi_device* device); void (*remove_device)(struct cdi_device* device); + int (*init)(void); void (*destroy)(struct cdi_driver* driver); }; /** * Muss vor dem ersten Aufruf einer anderen CDI-Funktion aufgerufen werden. * Initialisiert interne Datenstruktur der Implementierung fuer das jeweilige - * Betriebssystem. + * Betriebssystem und startet anschliessend alle Treiber. * * Ein wiederholter Aufruf bleibt ohne Effekt. */ diff --git a/iso9660/main.c b/iso9660/main.c index 01424d9..a935db9 100644 --- a/iso9660/main.c +++ b/iso9660/main.c @@ -31,31 +31,13 @@ struct iso9660_driver { }; static struct iso9660_driver iso9660_driver; -static int iso9660_driver_init(struct iso9660_driver *driver); -static void iso9660_driver_destroy(struct cdi_driver* driver); - -#ifdef CDI_STANDALONE -int main(void) -#else -int init_iso9660(void) -#endif -{ - cdi_init(); - - if (iso9660_driver_init(&iso9660_driver)!=0) return -1; - cdi_fs_driver_register((struct cdi_fs_driver*)&iso9660_driver); - - cdi_run_drivers(); - - return 0; -} /** * Initializes the data structures for the iso9660 driver */ -static int iso9660_driver_init(struct iso9660_driver *driver) { +static int iso9660_driver_init(void) { // Konstruktor der Vaterklasse - cdi_fs_driver_init((struct cdi_fs_driver*)driver); + cdi_fs_driver_init((struct cdi_fs_driver*) &iso9660_driver); return 0; } @@ -91,7 +73,9 @@ int debug(const char *fmt,...) { static struct iso9660_driver iso9660_driver = { .drv = { .drv = { + .type = CDI_FILESYSTEM, .name = DRIVER_NAME, + .init = iso9660_driver_init, .destroy = iso9660_driver_destroy, }, .fs_init = iso9660_fs_init, diff --git a/ne2k/main.c b/ne2k/main.c index 170157a..e1ecd06 100644 --- a/ne2k/main.c +++ b/ne2k/main.c @@ -42,26 +42,8 @@ struct ne2k_driver { }; static struct ne2k_driver driver; -static int ne2k_driver_init(int argc, char* argv[]); -static void ne2k_driver_destroy(struct cdi_driver* driver); - -#ifdef CDI_STANDALONE -int main(int argc, char* argv[]) -#else -int init_ne2k(int argc, char* argv[]) -#endif -{ - cdi_init(); - - ne2k_driver_init(argc, argv); - cdi_driver_register((struct cdi_driver*) &driver); - - cdi_run_drivers(); - - return 0; -} -static int ne2k_driver_init(int argc, char* argv[]) +static int ne2k_driver_init(void) { // Konstruktor der Vaterklasse cdi_net_driver_init((struct cdi_net_driver*) &driver); @@ -108,7 +90,9 @@ static void ne2k_driver_destroy(struct cdi_driver* driver) static struct ne2k_driver ne2k_driver = { .net = { .drv = { + .type = CDI_NETWORK, .name = DRIVER_NAME, + .init = ne2k_driver_init, .destroy = ne2k_driver_destroy, .init_device = ne2k_init_device, .remove_device = ne2k_remove_device, diff --git a/pcnet/main.c b/pcnet/main.c index b6bf7e4..39f3fe9 100644 --- a/pcnet/main.c +++ b/pcnet/main.c @@ -47,26 +47,8 @@ struct pcnet_driver { }; static struct pcnet_driver driver; -static int pcnet_driver_init(int argc, char* argv[]); -static void pcnet_driver_destroy(struct cdi_driver* driver); - -#ifdef CDI_STANDALONE -int main(int argc, char* argv[]) -#else -int init_pcnet(int argc, char* argv[]) -#endif -{ - cdi_init(); - - pcnet_driver_init(argc, argv); - cdi_driver_register((struct cdi_driver*) &driver); - - cdi_run_drivers(); - - return 0; -} -static int pcnet_driver_init(int argc, char* argv[]) +static int pcnet_driver_init(void) { // TODO Auf pci-Service warten // TODO Auf tcpip-Service warten @@ -113,7 +95,9 @@ static void pcnet_driver_destroy(struct cdi_driver* driver) static struct pcnet_driver driver = { .net = { .drv = { + .type = CDI_NETWORK, .name = DRIVER_NAME, + .init = pcnet_driver_init, .destroy = pcnet_driver_destroy, .init_device = pcnet_init_device, .remove_device = pcnet_remove_device, diff --git a/ramdisk/main.c b/ramdisk/main.c index 422cd80..b4417c8 100644 --- a/ramdisk/main.c +++ b/ramdisk/main.c @@ -42,34 +42,14 @@ struct ramdisk_driver { }; static struct ramdisk_driver ramdisk_driver; -static int ramdisk_driver_init(struct ramdisk_driver* driver); -static void ramdisk_driver_destroy(struct cdi_driver* driver); - -#ifdef CDI_STANDALONE -int main(void) -#else -int init_ramdisk(void) -#endif -{ - cdi_init(); - - if (ramdisk_driver_init(&ramdisk_driver) != 0) { - return -1; - } - cdi_fs_driver_register((struct cdi_fs_driver*) &ramdisk_driver); - - cdi_run_drivers(); - - return 0; -} /** * Initialisiert die Datenstrukturen fuer den ramdisk-Treiber */ -static int ramdisk_driver_init(struct ramdisk_driver* driver) +static int ramdisk_driver_init(void) { // Konstruktor der Vaterklasse - cdi_fs_driver_init((struct cdi_fs_driver*) driver); + cdi_fs_driver_init((struct cdi_fs_driver*) &ramdisk_driver); return 0; } @@ -86,7 +66,9 @@ static void ramdisk_driver_destroy(struct cdi_driver* driver) static struct ramdisk_driver ramdisk_driver = { .fs = { .drv = { + .type = CDI_FILESYSTEM, .name = DRIVER_NAME, + .init = ramdisk_driver_init, .destroy = ramdisk_driver_destroy, }, .fs_init = ramdisk_fs_init, diff --git a/rtl8139/main.c b/rtl8139/main.c index b9d7728..a9a74b3 100644 --- a/rtl8139/main.c +++ b/rtl8139/main.c @@ -45,26 +45,8 @@ static struct rtl8139_driver { struct cdi_net_driver net; } driver; -static int rtl8139_driver_init(int argc, char* argv[]); -static void rtl8139_driver_destroy(struct cdi_driver* driver); - -#ifdef CDI_STANDALONE -int main(int argc, char* argv[]) -#else -int init_rtl8139(int argc, char* argv[]) -#endif -{ - cdi_init(); - - rtl8139_driver_init(argc, argv); - cdi_driver_register((struct cdi_driver*) &driver); - - cdi_run_drivers(); - - return 0; -} -static int rtl8139_driver_init(int argc, char* argv[]) +static int rtl8139_driver_init(void) { // TODO Auf pci-Service warten // TODO Auf tcpip-Service warten @@ -114,7 +96,9 @@ static void rtl8139_driver_destroy(struct cdi_driver* driver) static struct rtl8139_driver driver = { .net = { .drv = { + .type = CDI_NETWORK, .name = DRIVER_NAME, + .init = rtl8139_driver_init, .destroy = rtl8139_driver_destroy, .init_device = rtl8139_init_device, .remove_device = rtl8139_remove_device, diff --git a/serial/main.c b/serial/main.c index 3fd71ba..e498990 100644 --- a/serial/main.c +++ b/serial/main.c @@ -42,34 +42,14 @@ struct serial_driver { }; static struct serial_driver serial_driver; -static int serial_driver_init(struct serial_driver* driver); -static void serial_driver_destroy(struct cdi_driver* driver); - -#ifdef CDI_STANDALONE -int main(void) -#else -int init_serial(void) -#endif -{ - cdi_init(); - - if (serial_driver_init(&serial_driver) != 0) { - return -1; - } - cdi_fs_driver_register((struct cdi_fs_driver*) &serial_driver); - - cdi_run_drivers(); - - return 0; -} /** * Initialisiert die Datenstrukturen fuer den serial-Treiber */ -static int serial_driver_init(struct serial_driver* driver) +static int serial_driver_init(void) { // Konstruktor der Vaterklasse - cdi_fs_driver_init((struct cdi_fs_driver*) driver); + cdi_fs_driver_init((struct cdi_fs_driver*) &serial_driver); return 0; } @@ -86,7 +66,9 @@ static void serial_driver_destroy(struct cdi_driver* driver) static struct serial_driver serial_driver = { .fs = { .drv = { + .type = CDI_FILESYSTEM, .name = DRIVER_NAME, + .init = serial_driver_init, .destroy = serial_driver_destroy, }, .fs_init = serial_fs_init, diff --git a/sis900/main.c b/sis900/main.c index 0ae47ca..d4a0d93 100644 --- a/sis900/main.c +++ b/sis900/main.c @@ -44,29 +44,11 @@ struct sis900_driver { }; static struct sis900_driver sis900_driver; -static void sis900_driver_init(int argc, char* argv[]); -static void sis900_driver_destroy(struct cdi_driver* driver); - -#ifdef CDI_STANDALONE -int main(int argc, char* argv[]) -#else -int init_sis900(int argc, char* argv[]) -#endif -{ - cdi_init(); - - sis900_driver_init(argc, argv); - cdi_driver_register((struct cdi_driver*) &sis900_driver); - - cdi_run_drivers(); - - return 0; -} /** * Initialisiert die Datenstrukturen fuer den sis900-Treiber */ -static void sis900_driver_init(int argc, char* argv[]) +static int sis900_driver_init(void) { // Konstruktor der Vaterklasse cdi_net_driver_init((struct cdi_net_driver*) &sis900_driver); @@ -98,6 +80,8 @@ static void sis900_driver_init(int argc, char* argv[]) cdi_list_size(sis900_driver.net.drv.devices)); cdi_list_destroy(pci_devices); + + return 0; } /** @@ -114,7 +98,9 @@ static void sis900_driver_destroy(struct cdi_driver* driver) static struct sis900_driver sis900_driver = { .net = { .drv = { + .type = CDI_NETWORK, .name = DRIVER_NAME, + .init = sis900_driver_init, .destroy = sis900_driver_destroy, .init_device = sis900_init_device, .remove_device = sis900_remove_device, -- 1.6.0.2 From kevin at tyndur.org Sat Dec 5 17:09:07 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 5 Dec 2009 17:09:07 +0100 Subject: [cdi-devel] [PATCH 4/4] Simplify driver declarations In-Reply-To: <1260029347-15604-1-git-send-email-kevin@tyndur.org> References: <1260029347-15604-1-git-send-email-kevin@tyndur.org> Message-ID: <1260029347-15604-5-git-send-email-kevin@tyndur.org> * cdi: The destroy function doesn't need to get the driver struct passed as an argument. The driver struct is static anyway. * cdi: Drivers don't need to wrap cdi_{fs,net,storage}_driver if they don't extend the struct (and there is no reason to do so). Signed-off-by: Kevin Wolf --- ata/main.c | 11 +++++++---- e1000/main.c | 36 ++++++++++++++++-------------------- ext2/main.c | 29 ++++++++++++----------------- floppy/main.c | 40 ++++++++++++++++++---------------------- include/cdi.h | 2 +- iso9660/main.c | 31 +++++++++++++------------------ ne2k/main.c | 34 +++++++++++++++------------------- pcnet/main.c | 40 ++++++++++++++++------------------------ ramdisk/main.c | 31 +++++++++++++------------------ rtl8139/main.c | 39 ++++++++++++++++----------------------- serial/main.c | 31 +++++++++++++------------------ sis900/main.c | 36 ++++++++++++++++-------------------- 12 files changed, 156 insertions(+), 204 deletions(-) diff --git a/ata/main.c b/ata/main.c index 4699c2d..3cc502d 100644 --- a/ata/main.c +++ b/ata/main.c @@ -153,11 +153,13 @@ static int ata_driver_init(void) /** * Deinitialisiert die Datenstrukturen fuer den ata-Treiber */ -static void ata_driver_destroy(struct cdi_driver* driver) +static int ata_driver_destroy(void) { - cdi_storage_driver_destroy((struct cdi_storage_driver*) driver); + cdi_storage_driver_destroy(&driver_storage); // TODO Alle Karten deinitialisieren + + return 0; } static int atapi_driver_init(void) @@ -166,9 +168,10 @@ static int atapi_driver_init(void) return 0; } -static void atapi_driver_destroy(struct cdi_driver* driver) +static int atapi_driver_destroy(void) { - cdi_scsi_driver_destroy((struct cdi_scsi_driver*) driver); + cdi_scsi_driver_destroy(&driver_scsi); + return 0; } diff --git a/e1000/main.c b/e1000/main.c index 4f3bb9d..26ce832 100644 --- a/e1000/main.c +++ b/e1000/main.c @@ -39,11 +39,7 @@ #define DRIVER_NAME "e1000" -struct e1000_driver { - struct cdi_net_driver net; -}; - -static struct e1000_driver driver; +static struct cdi_net_driver driver; /** * Initialisiert die Datenstrukturen fuer den e1000-Treiber @@ -51,7 +47,7 @@ static struct e1000_driver driver; static int e1000_driver_init(void) { // Konstruktor der Vaterklasse - cdi_net_driver_init((struct cdi_net_driver*) &driver); + cdi_net_driver_init(&driver); // Passende PCI-Geraete suchen cdi_list_t pci_devices = cdi_list_create(); @@ -70,14 +66,14 @@ static int e1000_driver_init(void) device->phys = phys_device; device->pci = dev; - cdi_list_push(driver.net.drv.devices, device); + cdi_list_push(driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); } } printf("e1000: %d Karten gefunden.\n", - cdi_list_size(driver.net.drv.devices)); + cdi_list_size(driver.drv.devices)); cdi_list_destroy(pci_devices); @@ -87,24 +83,24 @@ static int e1000_driver_init(void) /** * Deinitialisiert die Datenstrukturen fuer den e1000-Treiber */ -static void e1000_driver_destroy(struct cdi_driver* driver) +static int e1000_driver_destroy(void) { - cdi_net_driver_destroy((struct cdi_net_driver*) driver); + cdi_net_driver_destroy(&driver); // TODO Alle Karten deinitialisieren + + return 0; } -static struct e1000_driver driver = { - .net = { - .drv = { - .type = CDI_NETWORK, - .name = DRIVER_NAME, - .init = e1000_driver_init, - .destroy = e1000_driver_destroy, - .init_device = e1000_init_device, - .remove_device = e1000_remove_device, - }, +static struct cdi_net_driver driver = { + .drv = { + .type = CDI_NETWORK, + .name = DRIVER_NAME, + .init = e1000_driver_init, + .destroy = e1000_driver_destroy, + .init_device = e1000_init_device, + .remove_device = e1000_remove_device, }, }; diff --git a/ext2/main.c b/ext2/main.c index bdef05b..a1d597f 100644 --- a/ext2/main.c +++ b/ext2/main.c @@ -43,11 +43,7 @@ #define DRIVER_NAME "ext2" -struct ext2_driver { - struct cdi_fs_driver fs; -}; - -static struct ext2_driver ext2_driver; +static struct cdi_fs_driver ext2_driver; /** * Initialisiert die Datenstrukturen fuer den ext2-Treiber @@ -62,23 +58,22 @@ static int ext2_driver_init(void) /** * Deinitialisiert die Datenstrukturen fuer den sis900-Treiber */ -static void ext2_driver_destroy(struct cdi_driver* driver) +static int ext2_driver_destroy(void) { - cdi_fs_driver_destroy((struct cdi_fs_driver*) driver); + cdi_fs_driver_destroy(&ext2_driver); + return 0; } -static struct ext2_driver ext2_driver = { - .fs = { - .drv = { - .type = CDI_FILESYSTEM, - .name = DRIVER_NAME, - .init = ext2_driver_init, - .destroy = ext2_driver_destroy, - }, - .fs_init = ext2_fs_init, - .fs_destroy = ext2_fs_destroy, +static struct cdi_fs_driver ext2_driver = { + .drv = { + .type = CDI_FILESYSTEM, + .name = DRIVER_NAME, + .init = ext2_driver_init, + .destroy = ext2_driver_destroy, }, + .fs_init = ext2_fs_init, + .fs_destroy = ext2_fs_destroy, }; CDI_DRIVER(DRIVER_NAME, ext2_driver) diff --git a/floppy/main.c b/floppy/main.c index f888eb0..e6208a6 100644 --- a/floppy/main.c +++ b/floppy/main.c @@ -37,11 +37,8 @@ #define DRIVER_NAME "floppy" -struct floppy_driver { - struct cdi_storage_driver storage; -}; +static struct cdi_storage_driver floppy_driver; -static struct floppy_driver floppy_driver; static struct floppy_controller floppy_controller = { // Standardmaessig DMA benutzen .use_dma = 1 @@ -54,10 +51,9 @@ static int floppy_driver_init(void) { int i; struct floppy_device* device; - struct floppy_driver* driver = &floppy_driver; // Konstruktor der Vaterklasse - cdi_storage_driver_init((struct cdi_storage_driver*) driver); + cdi_storage_driver_init(&floppy_driver); // Geraete erstellen (TODO: Was wenn eines oder beide nicht vorhanden // sind?) @@ -72,7 +68,7 @@ static int floppy_driver_init(void) // Geraet nur eintragen wenn es existiert if (floppy_device_probe(device) != 0) { - cdi_list_push(driver->storage.drv.devices, device); + cdi_list_push(floppy_driver.drv.devices, device); } else { free(device); } @@ -81,7 +77,7 @@ static int floppy_driver_init(void) // IRQ fuer FDC registrieren(ist hier egal fuer welches Geraet, da er eh // nur in der Controller-Struktur etwas macht). cdi_register_irq(6, floppy_handle_interrupt, (struct cdi_device*) - cdi_list_get(driver->storage.drv.devices, 0)); + cdi_list_get(floppy_driver.drv.devices, 0)); // Kontroller initialisieren, falls dabei ein Fehler auftritt kann der // Treiber gleich wieder beendet werden @@ -95,27 +91,27 @@ static int floppy_driver_init(void) /** * Deinitialisiert die Datenstrukturen fuer den sis900-Treiber */ -static void floppy_driver_destroy(struct cdi_driver* driver) +static int floppy_driver_destroy(void) { - cdi_storage_driver_destroy((struct cdi_storage_driver*) driver); + cdi_storage_driver_destroy(&floppy_driver); // TODO Alle Laufwerke deinitialisieren + + return 0; } -static struct floppy_driver floppy_driver = { - .storage = { - .drv = { - .type = CDI_STORAGE, - .name = DRIVER_NAME, - .init = floppy_driver_init, - .destroy = floppy_driver_destroy, - .init_device = floppy_init_device, - .remove_device = floppy_remove_device, - }, - .read_blocks = floppy_read_blocks, - .write_blocks = floppy_write_blocks, +static struct cdi_storage_driver floppy_driver = { + .drv = { + .type = CDI_STORAGE, + .name = DRIVER_NAME, + .init = floppy_driver_init, + .destroy = floppy_driver_destroy, + .init_device = floppy_init_device, + .remove_device = floppy_remove_device, }, + .read_blocks = floppy_read_blocks, + .write_blocks = floppy_write_blocks, }; CDI_DRIVER(DRIVER_NAME, floppy_driver) diff --git a/include/cdi.h b/include/cdi.h index c3fdf8d..f45181a 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -51,7 +51,7 @@ struct cdi_driver { void (*remove_device)(struct cdi_device* device); int (*init)(void); - void (*destroy)(struct cdi_driver* driver); + int (*destroy)(void); }; /** diff --git a/iso9660/main.c b/iso9660/main.c index a935db9..1e81779 100644 --- a/iso9660/main.c +++ b/iso9660/main.c @@ -26,18 +26,14 @@ #define DRIVER_NAME "iso9660" -struct iso9660_driver { - struct cdi_fs_driver drv; -}; - -static struct iso9660_driver iso9660_driver; +static struct cdi_fs_driver iso9660_driver; /** * Initializes the data structures for the iso9660 driver */ static int iso9660_driver_init(void) { // Konstruktor der Vaterklasse - cdi_fs_driver_init((struct cdi_fs_driver*) &iso9660_driver); + cdi_fs_driver_init(&iso9660_driver); return 0; } @@ -45,9 +41,10 @@ static int iso9660_driver_init(void) { /** * Deinitialize the data structures for the iso9660 driver */ -static void iso9660_driver_destroy(struct cdi_driver* driver) +static int iso9660_driver_destroy(void) { - cdi_fs_driver_destroy((struct cdi_fs_driver*)driver); + cdi_fs_driver_destroy(&iso9660_driver); + return 0; } /** @@ -70,17 +67,15 @@ int debug(const char *fmt,...) { } -static struct iso9660_driver iso9660_driver = { - .drv = { - .drv = { - .type = CDI_FILESYSTEM, - .name = DRIVER_NAME, - .init = iso9660_driver_init, - .destroy = iso9660_driver_destroy, - }, - .fs_init = iso9660_fs_init, - .fs_destroy = iso9660_fs_destroy, +static struct cdi_fs_driver iso9660_driver = { + .drv = { + .type = CDI_FILESYSTEM, + .name = DRIVER_NAME, + .init = iso9660_driver_init, + .destroy = iso9660_driver_destroy, }, + .fs_init = iso9660_fs_init, + .fs_destroy = iso9660_fs_destroy, }; CDI_DRIVER(DRIVER_NAME, iso9660_driver) diff --git a/ne2k/main.c b/ne2k/main.c index e1ecd06..229bccb 100644 --- a/ne2k/main.c +++ b/ne2k/main.c @@ -37,16 +37,12 @@ #define DRIVER_NAME "ne2k" -struct ne2k_driver { - struct cdi_net_driver net; -}; - -static struct ne2k_driver driver; +static struct cdi_net_driver ne2k_driver; static int ne2k_driver_init(void) { // Konstruktor der Vaterklasse - cdi_net_driver_init((struct cdi_net_driver*) &driver); + cdi_net_driver_init(&ne2k_driver); // Passende PCI-Geraete suchen cdi_list_t pci_devices = cdi_list_create(); @@ -65,7 +61,7 @@ static int ne2k_driver_init(void) device->phys = phys_device; device->pci = dev; - cdi_list_push(driver.net.drv.devices, device); + cdi_list_push(ne2k_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); } @@ -79,24 +75,24 @@ static int ne2k_driver_init(void) /** * Deinitialisiert die Datenstrukturen fuer den ne2k-Treiber */ -static void ne2k_driver_destroy(struct cdi_driver* driver) +static int ne2k_driver_destroy(void) { - cdi_net_driver_destroy((struct cdi_net_driver*) driver); + cdi_net_driver_destroy(&ne2k_driver); // TODO Alle Karten deinitialisieren + + return 0; } -static struct ne2k_driver ne2k_driver = { - .net = { - .drv = { - .type = CDI_NETWORK, - .name = DRIVER_NAME, - .init = ne2k_driver_init, - .destroy = ne2k_driver_destroy, - .init_device = ne2k_init_device, - .remove_device = ne2k_remove_device, - }, +static struct cdi_net_driver ne2k_driver = { + .drv = { + .type = CDI_NETWORK, + .name = DRIVER_NAME, + .init = ne2k_driver_init, + .destroy = ne2k_driver_destroy, + .init_device = ne2k_init_device, + .remove_device = ne2k_remove_device, }, }; diff --git a/pcnet/main.c b/pcnet/main.c index 39f3fe9..342ac39 100644 --- a/pcnet/main.c +++ b/pcnet/main.c @@ -38,15 +38,7 @@ #define DRIVER_NAME "pcnet" -struct module_options { - uint32_t ip; -}; - -struct pcnet_driver { - struct cdi_net_driver net; -}; - -static struct pcnet_driver driver; +static struct cdi_net_driver pcnet_driver; static int pcnet_driver_init(void) { @@ -54,7 +46,7 @@ static int pcnet_driver_init(void) // TODO Auf tcpip-Service warten // Konstruktor der Vaterklasse - cdi_net_driver_init((struct cdi_net_driver*) &driver); + cdi_net_driver_init(&pcnet_driver); // Passende PCI-Geraete suchen cdi_list_t pci_devices = cdi_list_create(); @@ -70,7 +62,7 @@ static int pcnet_driver_init(void) memset(device, 0, sizeof(struct pcnet_device)); device->pci = dev; - cdi_list_push(driver.net.drv.devices, device); + cdi_list_push(pcnet_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); } @@ -84,25 +76,25 @@ static int pcnet_driver_init(void) /** * Deinitialisiert die Datenstrukturen fuer den pcnet-Treiber */ -static void pcnet_driver_destroy(struct cdi_driver* driver) +static int pcnet_driver_destroy(void) { - cdi_net_driver_destroy((struct cdi_net_driver*) driver); + cdi_net_driver_destroy(&pcnet_driver); // TODO Alle Karten deinitialisieren + + return 0; } -static struct pcnet_driver driver = { - .net = { - .drv = { - .type = CDI_NETWORK, - .name = DRIVER_NAME, - .init = pcnet_driver_init, - .destroy = pcnet_driver_destroy, - .init_device = pcnet_init_device, - .remove_device = pcnet_remove_device, - }, +static struct cdi_net_driver pcnet_driver = { + .drv = { + .type = CDI_NETWORK, + .name = DRIVER_NAME, + .init = pcnet_driver_init, + .destroy = pcnet_driver_destroy, + .init_device = pcnet_init_device, + .remove_device = pcnet_remove_device, }, }; -CDI_DRIVER(DRIVER_NAME, driver) +CDI_DRIVER(DRIVER_NAME, pcnet_driver) diff --git a/ramdisk/main.c b/ramdisk/main.c index b4417c8..c0d8b3e 100644 --- a/ramdisk/main.c +++ b/ramdisk/main.c @@ -37,11 +37,7 @@ #define DRIVER_NAME "ramdisk" -struct ramdisk_driver { - struct cdi_fs_driver fs; -}; - -static struct ramdisk_driver ramdisk_driver; +static struct cdi_fs_driver ramdisk_driver; /** * Initialisiert die Datenstrukturen fuer den ramdisk-Treiber @@ -49,7 +45,7 @@ static struct ramdisk_driver ramdisk_driver; static int ramdisk_driver_init(void) { // Konstruktor der Vaterklasse - cdi_fs_driver_init((struct cdi_fs_driver*) &ramdisk_driver); + cdi_fs_driver_init(&ramdisk_driver); return 0; } @@ -57,23 +53,22 @@ static int ramdisk_driver_init(void) /** * Deinitialisiert die Datenstrukturen fuer den ramdisk-Treiber */ -static void ramdisk_driver_destroy(struct cdi_driver* driver) +static int ramdisk_driver_destroy(void) { - cdi_fs_driver_destroy((struct cdi_fs_driver*) driver); + cdi_fs_driver_destroy(&ramdisk_driver); + return 0; } -static struct ramdisk_driver ramdisk_driver = { - .fs = { - .drv = { - .type = CDI_FILESYSTEM, - .name = DRIVER_NAME, - .init = ramdisk_driver_init, - .destroy = ramdisk_driver_destroy, - }, - .fs_init = ramdisk_fs_init, - .fs_destroy = ramdisk_fs_destroy, +static struct cdi_fs_driver ramdisk_driver = { + .drv = { + .type = CDI_FILESYSTEM, + .name = DRIVER_NAME, + .init = ramdisk_driver_init, + .destroy = ramdisk_driver_destroy, }, + .fs_init = ramdisk_fs_init, + .fs_destroy = ramdisk_fs_destroy, }; CDI_DRIVER(DRIVER_NAME, ramdisk_driver) diff --git a/rtl8139/main.c b/rtl8139/main.c index a9a74b3..131c67b 100644 --- a/rtl8139/main.c +++ b/rtl8139/main.c @@ -37,14 +37,7 @@ #define DRIVER_NAME "rtl8139" -struct module_options { - uint32_t ip; -}; - -static struct rtl8139_driver { - struct cdi_net_driver net; -} driver; - +static struct cdi_net_driver rtl8139_driver; static int rtl8139_driver_init(void) { @@ -52,7 +45,7 @@ static int rtl8139_driver_init(void) // TODO Auf tcpip-Service warten // Konstruktor der Vaterklasse - cdi_net_driver_init((struct cdi_net_driver*) &driver); + cdi_net_driver_init(&rtl8139_driver); // Passende PCI-Geraete suchen cdi_list_t pci_devices = cdi_list_create(); @@ -71,7 +64,7 @@ static int rtl8139_driver_init(void) device->phys = phys_device; device->pci = dev; - cdi_list_push(driver.net.drv.devices, device); + cdi_list_push(rtl8139_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); } @@ -85,25 +78,25 @@ static int rtl8139_driver_init(void) /** * Deinitialisiert die Datenstrukturen fuer den rtl8139-Treiber */ -static void rtl8139_driver_destroy(struct cdi_driver* driver) +static int rtl8139_driver_destroy(void) { - cdi_net_driver_destroy((struct cdi_net_driver*) driver); + cdi_net_driver_destroy(&rtl8139_driver); // TODO Alle Karten deinitialisieren + + return 0; } -static struct rtl8139_driver driver = { - .net = { - .drv = { - .type = CDI_NETWORK, - .name = DRIVER_NAME, - .init = rtl8139_driver_init, - .destroy = rtl8139_driver_destroy, - .init_device = rtl8139_init_device, - .remove_device = rtl8139_remove_device, - }, +static struct cdi_net_driver rtl8139_driver = { + .drv = { + .type = CDI_NETWORK, + .name = DRIVER_NAME, + .init = rtl8139_driver_init, + .destroy = rtl8139_driver_destroy, + .init_device = rtl8139_init_device, + .remove_device = rtl8139_remove_device, }, }; -CDI_DRIVER(DRIVER_NAME, driver) +CDI_DRIVER(DRIVER_NAME, rtl8139_driver) diff --git a/serial/main.c b/serial/main.c index e498990..890ba3b 100644 --- a/serial/main.c +++ b/serial/main.c @@ -37,11 +37,7 @@ #define DRIVER_NAME "serial" -struct serial_driver { - struct cdi_fs_driver fs; -}; - -static struct serial_driver serial_driver; +static struct cdi_fs_driver serial_driver; /** * Initialisiert die Datenstrukturen fuer den serial-Treiber @@ -49,7 +45,7 @@ static struct serial_driver serial_driver; static int serial_driver_init(void) { // Konstruktor der Vaterklasse - cdi_fs_driver_init((struct cdi_fs_driver*) &serial_driver); + cdi_fs_driver_init(&serial_driver); return 0; } @@ -57,23 +53,22 @@ static int serial_driver_init(void) /** * Deinitialisiert die Datenstrukturen fuer den serial-Treiber */ -static void serial_driver_destroy(struct cdi_driver* driver) +static int serial_driver_destroy(void) { - cdi_fs_driver_destroy((struct cdi_fs_driver*) driver); + cdi_fs_driver_destroy(&serial_driver); + return 0; } -static struct serial_driver serial_driver = { - .fs = { - .drv = { - .type = CDI_FILESYSTEM, - .name = DRIVER_NAME, - .init = serial_driver_init, - .destroy = serial_driver_destroy, - }, - .fs_init = serial_fs_init, - .fs_destroy = serial_fs_destroy, +static struct cdi_fs_driver serial_driver = { + .drv = { + .type = CDI_FILESYSTEM, + .name = DRIVER_NAME, + .init = serial_driver_init, + .destroy = serial_driver_destroy, }, + .fs_init = serial_fs_init, + .fs_destroy = serial_fs_destroy, }; CDI_DRIVER(DRIVER_NAME, serial_driver) diff --git a/sis900/main.c b/sis900/main.c index d4a0d93..b03511c 100644 --- a/sis900/main.c +++ b/sis900/main.c @@ -39,11 +39,7 @@ #define DRIVER_NAME "sis900" -struct sis900_driver { - struct cdi_net_driver net; -}; - -static struct sis900_driver sis900_driver; +static struct cdi_net_driver sis900_driver; /** * Initialisiert die Datenstrukturen fuer den sis900-Treiber @@ -51,7 +47,7 @@ static struct sis900_driver sis900_driver; static int sis900_driver_init(void) { // Konstruktor der Vaterklasse - cdi_net_driver_init((struct cdi_net_driver*) &sis900_driver); + cdi_net_driver_init(&sis900_driver); // Passende PCI-Geraete suchen cdi_list_t pci_devices = cdi_list_create(); @@ -70,14 +66,14 @@ static int sis900_driver_init(void) device->phys = phys_device; device->pci = dev; - cdi_list_push(sis900_driver.net.drv.devices, device); + cdi_list_push(sis900_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); } } printf("sis900: %d Karten gefunden.\n", - cdi_list_size(sis900_driver.net.drv.devices)); + cdi_list_size(sis900_driver.drv.devices)); cdi_list_destroy(pci_devices); @@ -87,24 +83,24 @@ static int sis900_driver_init(void) /** * Deinitialisiert die Datenstrukturen fuer den sis900-Treiber */ -static void sis900_driver_destroy(struct cdi_driver* driver) +static int sis900_driver_destroy(void) { - cdi_net_driver_destroy((struct cdi_net_driver*) driver); + cdi_net_driver_destroy(&sis900_driver); // TODO Alle Karten deinitialisieren + + return 0; } -static struct sis900_driver sis900_driver = { - .net = { - .drv = { - .type = CDI_NETWORK, - .name = DRIVER_NAME, - .init = sis900_driver_init, - .destroy = sis900_driver_destroy, - .init_device = sis900_init_device, - .remove_device = sis900_remove_device, - }, +static struct cdi_net_driver sis900_driver = { + .drv = { + .type = CDI_NETWORK, + .name = DRIVER_NAME, + .init = sis900_driver_init, + .destroy = sis900_driver_destroy, + .init_device = sis900_init_device, + .remove_device = sis900_remove_device, }, }; -- 1.6.0.2 From max at tyndur.org Tue Dec 8 22:05:02 2009 From: max at tyndur.org (max at tyndur.org) Date: Tue, 8 Dec 2009 22:05:02 +0100 Subject: [cdi-devel] [PATCH] CDI.usb, CDI.usb-hcd Message-ID: <1260306302-13766-1-git-send-email-max@tyndur.org> From: Max Reitz + Added proposals for CDI.usb (USB drivers (bus drivers)) and CDI.usb-hcd (USB host controller drivers) Signed-off-by: Max Reitz --- include/cdi/usb-hcd.h | 60 ++++++++++ include/cdi/usb.h | 311 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 371 insertions(+), 0 deletions(-) create mode 100644 include/cdi/usb-hcd.h create mode 100644 include/cdi/usb.h diff --git a/include/cdi/usb-hcd.h b/include/cdi/usb-hcd.h new file mode 100644 index 0000000..70210ee --- /dev/null +++ b/include/cdi/usb-hcd.h @@ -0,0 +1,60 @@ +#ifndef _CDI_USB_HCD_H_ +#define _CDI_USB_HCD_H_ + +#include "cdi/misc.h" +#include "cdi/usb.h" + +struct cdi_usb_hc { + struct cdi_device dev; + + ///USB-Geschwindigkeit + cdi_usb_speed_t speed; +}; + +struct cdi_usb_hcd { + struct cdi_driver drv; + + /** + * Listet alle Ger??te auf, die an den Rootports h??ngen und ruft ggf. + * cdi_usb_device_init auf + * + * @param device Der Hostcontroller + */ + void (*find_devices)(struct cdi_usb_hc* device); + + /** + * Aktiviert den zum angegebenen Ger??t geh??rigen Rootport + * + * @param usb_device Das USB-Ger??t (muss an einem Rootport h??ngen) + */ + void (*activate_device)(struct cdi_usb_device* usb_device); + + /** + * Verarbeitet USB-Pakete + * + * @param packets Array zu sendender Pakete + * @param num_packets Anzahl der Pakete + * + * @return Gibt den Status an (z. B. USB_NO_ERROR f??r keinen Fehler) + */ + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, + size_t num_packets); + + /** + * Erstellt eine neue Pipe + * + * @param pipe Die Pipe + */ + void (*add_pipe)(struct cdi_usb_pipe* pipe); +}; + +/// Initialisiert einen HCD +void cdi_usb_hcd_init(struct cdi_usb_hcd* driver); + +/// Gibt einen HCD frei +void cdi_usb_hcd_destroy(struct cdi_usb_hcd* driver); + +/// Registriert einen HCD +void cdi_usb_hcd_register(struct cdi_usb_hcd* driver); + +#endif diff --git a/include/cdi/usb.h b/include/cdi/usb.h new file mode 100644 index 0000000..e2b8547 --- /dev/null +++ b/include/cdi/usb.h @@ -0,0 +1,311 @@ +#ifndef _CDI_USB_H_ +#define _CDI_USB_H_ + +#include "stddef.h" +#include "stdint.h" + +typedef enum { + /// Low-Speed (1,5 Mbit/s) + CDI_USB_LOW_SPEED = 0x01, + /// Full-Speed (12 Mbit/s) + CDI_USB_FULL_SPEED = 0x02, + /// High-Speed (480 Mbit/s) + CDI_USB_HIGH_SPEED = 0x04, + /// SuperSpeed (5 Gbit/s) + CDI_USB_SUPER_SPEED = 0x08, +} cdi_usb_speed_t; + +typedef enum { + /// SETUP-Paket + CDI_USB_PACKET_SETUP = 0x2D, + /// Daten vom Geraet zum Host + CDI_USB_PACKET_IN = 0x69, + /// Daten vom Host zum Geraet + CDI_USB_PACKET_OUT = 0xE1, +} cdi_usb_packet_type_t; + +typedef enum { + /// Kein Fehler + CDI_USB_NO_ERROR = 0x0000, + /// Stalled + CDI_USB_STALLED = 0x0001, + /// Bufferfehler (Overflow, underrun, ...) + CDI_USB_BUFFER_ERROR = 0x0002, + /// Babble (Geraet hat zu viele Daten gesendet) + CDI_USB_BABBLE = 0x0004, + /// NAK (Daten sind erneut zu senden) + CDI_USB_NAK = 0x0008, + /// CRC-Fehler + CDI_USB_CRC = 0x0010, + /// Timeout + CDI_USB_TIMEOUT = 0x0020, + /// Bitstuff/-destuff-Fehler + CDI_USB_BITSTUFF = 0x0040, + /// Reservieren von Speicher fehlgeschlagen, ... + CDI_USB_TRIVIAL_ERROR = 0x0080, +} cdi_usb_status_t; + +struct cdi_usb_hc; +struct cdi_usb_pipe; +struct cdi_usb_interface; +struct cdi_usb_driver; + +struct cdi_usb_device { + /// Der USB-Bustreiber + struct cdi_usb_driver* drv; + /// Der Hostcontroller, an dem das Geraet im Endeffekt haengt + struct cdi_usb_hc* hc; + /** + * Haengt das Geraet an einem Hub, dann ist dieses Feld ein Pointer auf + * das entsprechende USB-Geraet, haengt es an einem Rootport, dann ist + * dieses Feld NULL. + */ + struct cdi_usb_device* hub; + /// Geschwindigkeit(-en) dieses Geraets + cdi_usb_speed_t speed; + /// ID + int id; + /// Entweder der Rootport oder der Port des Hubs, an dem das Geraet haengt + int port; + /// Hier darf der Geraetetreiber seine Daten unterbringen + void* driver_data; + + /** + * Felder, die so auch bei PCI existieren: Vendor-, Device-, Class-, + * Subclass- und Protocol-ID + */ + int vendor_id; + int device_id; + int class_id; + int subclass_id; + int protocol_id; + + /** + * Setzt das USB-Geraet zurueck (kann sowohl vom HCD als auch vom Hub + * driver ausgefuehrt werden) + * + * @param usb_device Das Geraet + */ + void (*reset)(struct cdi_usb_device* usb_device); + + /// Ziemlich lowlevelig. Gehoert das hier wirklich rein? + /// Im einzelnen sind das: Pipe fuer Endpoint 0 + struct cdi_usb_pipe* ep0; + /// Device descriptor + void* device_desc; + /// Configuration descriptor + void* config_desc; + /// Interface descriptor + struct cdi_usb_interface* interface_desc; + /// Class descriptor + void* class_desc; +}; + +enum cdi_usb_toggle { + /// Das Toggle nehmen, das jetzt dran ist + TOGGLE_UNSPECIFIC = 0, + /// Geloeschtes Togglebit erzwingen + TOGGLE_0 = 1, + /// Gesetztes Togglebit erzwingen + TOGGLE_1 = 2 +}; + +struct cdi_usb_endpoint { + /// sizeof(struct cdi_usb_endpoint) + uint8_t length; + /// 5 + uint8_t descriptor_type; + /// Nummer des Endpoints + uint8_t endpoint_address; + /// Eigenschaften (IN/OUT, Bulk/Interrupt/..., ...) + uint8_t attributes; + /// Maximale Paketgroesse in Bytes + uint16_t max_packet_size; + /// ??? + uint8_t interval; +} __attribute__((packed)); + +struct cdi_usb_interface { + /// sizeof(struct cdi_usb_interface) + uint8_t length; + /// 4 + uint8_t descriptor_type; + /// Nummer des Interfaces + uint8_t interface_number; + /// ??? + uint8_t alternate_setting; + /// Anzahl der Endpoints fuer dieses Interface + uint8_t num_endpoints; + /// Class-ID (bei Composite Devices) + uint8_t interface_class; + /// Subclass-ID + uint8_t interface_subclass; + /// Protocol-ID + uint8_t interface_protocol; + /// String-Index, der das Interface beschreibt + uint8_t iInterface; +} __attribute__((packed)); + +/// Beschreibt eine Pipe +struct cdi_usb_pipe { + /// Ziel + struct cdi_usb_device* device; + /// Ein Pointer auf eine den Endpoint beschreibende Struktur + /// TODO Das ist eigentlich auch sehr lowlevelig. + struct cdi_usb_endpoint* endpoint; + /// Data-Toggle + int data_toggle; +}; + +/** Beschreibt ein USB-Paket */ +struct cdi_usb_packet { + /// Die gewuenschte Pipe + struct cdi_usb_pipe* pipe; + /// Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) + cdi_usb_packet_type_t type; + /// Zeiger auf den Datenpuffer + void* data; + /// Die Laenge des Puffers + size_t length; + /// Fehlertyp (oder USB_NO_ERROR) + cdi_usb_status_t condition; + /// Bestimmtes Data-Toggle benuten + enum cdi_usb_toggle use_toggle; +}; + +typedef enum { + /// Host -> Geraet + CDI_USB_CONTROL_OUT = 0x00, + /// Geraet -> Host + CDI_USB_CONTROL_IN = 0x80 +} cdi_usb_control_direction_t; + +typedef enum { + /// Standard-Request + CDI_USB_CONTROL_RQ_STANDARD = 0x00, + /// Klassenspezifischer Request + CDI_USB_CONTROL_RQ_CLASS = 0x20, + /// Herstellerspezifischer Request + CDI_USB_CONTROL_RQ_VENDOR = 0x40 +} cdi_usb_control_request_type_t; + +typedef enum { + /// Empfaenger: Geraet + CDI_USB_CONTROL_REC_DEVICE = 0x00, + /// Empfaenger: Interface + CDI_USB_CONTROL_REC_INTERFACE = 0x01, + /// Empfaenger: Endpoint + CDI_USB_CONTROL_REC_ENDPOINT = 0x02, + /// Empfaenger ist jemand anderes + CDI_USB_CONTROL_REC_OTHER = 0x03 +} cdi_usb_control_recipient_t; + +struct cdi_usb_control_transfer { + /// Wenn Daten zum Geraet gesendet werden sollen, dann ist hier der + /// Speicherbereich anzugeben + void *buffer; + /// Laenge der erwarteten bzw. zu sendenden Daten + size_t length; + /// Richtung + cdi_usb_control_direction_t direction; + /// Art des Requests (Standardrequest, klassenspezifisch, + /// herstellerspezifisch) + cdi_usb_control_request_type_t rtype; + /// Ziel (Device, Interface, Endpoint, anderes) + cdi_usb_control_recipient_t recipient; + /// Requestcode (Je nach rtype und recipient) + int request; + /// Ein zu sendender Wert + int value; + /// Ein zu sendender Index + int index; +}; + +#include "cdi/usb-hcd.h" + +struct cdi_usb_driver { + struct cdi_driver drv; + + /// Unterstuetzte Geschwindigkeiten + cdi_usb_speed_t speeds; + + /** + * Enumeriert ein USB-Geraet + * + * @param device Das zu enumerierende Geraet + */ + void (*enumerate_device)(struct cdi_usb_device* device); + + /** + * Sendet USB-Pakete + * + * @param packets Array zu sendender Pakete + * @param num_packets Anzahl der Pakete + * + * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) + */ + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, + size_t num_packets); + + /** + * Fuehrt einen Control-Transfer durch + * + * @param device Das Zielgeraet + * @param transfer Eine den Transfer beschreibende Struktur + * + * @return Gibt einen Puffer zur??ck, der die ausgetauschen Daten enthaelt, + * im Fehlerfall hingegen NULL, gab es keine Daten, dann + * (void *)0xFFFFFFFF. + */ + void* (*do_control)(struct cdi_usb_device* device, + struct cdi_usb_control_transfer* transfer); +}; + +/** + * Ein USB-Geraetetreiber uebergibt dem Betriebssystem eine solche Struktur, + * damit ein bestimmter Handler beim Auffinden solcher Geraete aufgerufen wird. + * Jedes Feld enthaelt entweder eine Zahl, die mit der des Geraetes + * uebereinstimmen muss oder -1, dann wird dieses Feld ignoriert. + */ +struct cdi_usb_device_pattern { + int vendor_id; + int device_id; + int class_id; + int subclass_id; + int protocol_id; + + /** + * Ueberprueft das angegebene USB-Geraet + * + * @param device Das Geraet + * + * @return 0, wenn dieses Geraet nicht vom Treiber verarbeitet werden kann, + * sonst 1 + */ + int (*take_usb_device)(struct cdi_usb_device* device); +}; + +void cdi_usb_driver_destroy(struct cdi_usb_driver* driver); + +/** + * Initialisiert und registriert einen USB-Bustreiber beim System + * + * @param driver Der Treiber + */ +void cdi_usb_driver_init_and_register(struct cdi_usb_driver* driver); + +/** + * Registriert ein gefundenes USB-Geraet beim Betriebssystem + * + * @param device Das zu registrierende Geraet + */ +void cdi_usb_device_init(struct cdi_usb_device* device); + +/** + * Registriert ein Geraetemuster + * + * @param pattern Das Muster + */ +void cdi_usb_register_device_pattern(struct cdi_usb_device_pattern* pattern); + +#endif -- 1.6.4.2 From max at tyndur.org Tue Dec 8 22:12:26 2009 From: max at tyndur.org (max at tyndur.org) Date: Tue, 8 Dec 2009 22:12:26 +0100 Subject: [cdi-devel] [PATCH v2] CDI.usb, CDI.usb-hcd Message-ID: <1260306746-14267-1-git-send-email-max@tyndur.org> From: Max Reitz + Added proposals for CDI.usb (USB drivers (bus drivers)) and CDI.usb-hcd (USB host controller drivers) Signed-off-by: Max Reitz --- include/cdi/usb-hcd.h | 60 ++++++++++ include/cdi/usb.h | 311 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 371 insertions(+), 0 deletions(-) create mode 100644 include/cdi/usb-hcd.h create mode 100644 include/cdi/usb.h diff --git a/include/cdi/usb-hcd.h b/include/cdi/usb-hcd.h new file mode 100644 index 0000000..a5fcc51 --- /dev/null +++ b/include/cdi/usb-hcd.h @@ -0,0 +1,60 @@ +#ifndef _CDI_USB_HCD_H_ +#define _CDI_USB_HCD_H_ + +#include "cdi/misc.h" +#include "cdi/usb.h" + +struct cdi_usb_hc { + struct cdi_device dev; + + ///USB-Geschwindigkeit + cdi_usb_speed_t speed; +}; + +struct cdi_usb_hcd { + struct cdi_driver drv; + + /** + * Listet alle Geraete auf, die an den Rootports haengen und ruft ggf. + * cdi_usb_device_init auf + * + * @param device Der Hostcontroller + */ + void (*find_devices)(struct cdi_usb_hc* device); + + /** + * Aktiviert den zum angegebenen Geraet gehoerigen Rootport + * + * @param usb_device Das USB-Geraet (muss an einem Rootport haengen) + */ + void (*activate_device)(struct cdi_usb_device* usb_device); + + /** + * Verarbeitet USB-Pakete + * + * @param packets Array zu sendender Pakete + * @param num_packets Anzahl der Pakete + * + * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) + */ + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, + size_t num_packets); + + /** + * Erstellt eine neue Pipe + * + * @param pipe Die Pipe + */ + void (*add_pipe)(struct cdi_usb_pipe* pipe); +}; + +/// Initialisiert einen HCD +void cdi_usb_hcd_init(struct cdi_usb_hcd* driver); + +/// Gibt einen HCD frei +void cdi_usb_hcd_destroy(struct cdi_usb_hcd* driver); + +/// Registriert einen HCD +void cdi_usb_hcd_register(struct cdi_usb_hcd* driver); + +#endif diff --git a/include/cdi/usb.h b/include/cdi/usb.h new file mode 100644 index 0000000..df695fa --- /dev/null +++ b/include/cdi/usb.h @@ -0,0 +1,311 @@ +#ifndef _CDI_USB_H_ +#define _CDI_USB_H_ + +#include "stddef.h" +#include "stdint.h" + +typedef enum { + /// Low-Speed (1,5 Mbit/s) + CDI_USB_LOW_SPEED = 0x01, + /// Full-Speed (12 Mbit/s) + CDI_USB_FULL_SPEED = 0x02, + /// High-Speed (480 Mbit/s) + CDI_USB_HIGH_SPEED = 0x04, + /// SuperSpeed (5 Gbit/s) + CDI_USB_SUPER_SPEED = 0x08, +} cdi_usb_speed_t; + +typedef enum { + /// SETUP-Paket + CDI_USB_PACKET_SETUP = 0x2D, + /// Daten vom Geraet zum Host + CDI_USB_PACKET_IN = 0x69, + /// Daten vom Host zum Geraet + CDI_USB_PACKET_OUT = 0xE1, +} cdi_usb_packet_type_t; + +typedef enum { + /// Kein Fehler + CDI_USB_NO_ERROR = 0x0000, + /// Stalled + CDI_USB_STALLED = 0x0001, + /// Bufferfehler (Overflow, underrun, ...) + CDI_USB_BUFFER_ERROR = 0x0002, + /// Babble (Geraet hat zu viele Daten gesendet) + CDI_USB_BABBLE = 0x0004, + /// NAK (Daten sind erneut zu senden) + CDI_USB_NAK = 0x0008, + /// CRC-Fehler + CDI_USB_CRC = 0x0010, + /// Timeout + CDI_USB_TIMEOUT = 0x0020, + /// Bitstuff/-destuff-Fehler + CDI_USB_BITSTUFF = 0x0040, + /// Reservieren von Speicher fehlgeschlagen, ... + CDI_USB_TRIVIAL_ERROR = 0x0080, +} cdi_usb_status_t; + +struct cdi_usb_hc; +struct cdi_usb_pipe; +struct cdi_usb_interface; +struct cdi_usb_driver; + +struct cdi_usb_device { + /// Der USB-Bustreiber + struct cdi_usb_driver* drv; + /// Der Hostcontroller, an dem das Geraet im Endeffekt haengt + struct cdi_usb_hc* hc; + /** + * Haengt das Geraet an einem Hub, dann ist dieses Feld ein Pointer auf + * das entsprechende USB-Geraet, haengt es an einem Rootport, dann ist + * dieses Feld NULL. + */ + struct cdi_usb_device* hub; + /// Geschwindigkeit(-en) dieses Geraets + cdi_usb_speed_t speed; + /// ID + int id; + /// Entweder der Rootport oder der Port des Hubs, an dem das Geraet haengt + int port; + /// Hier darf der Geraetetreiber seine Daten unterbringen + void* driver_data; + + /** + * Felder, die so auch bei PCI existieren: Vendor-, Device-, Class-, + * Subclass- und Protocol-ID + */ + int vendor_id; + int device_id; + int class_id; + int subclass_id; + int protocol_id; + + /** + * Setzt das USB-Geraet zurueck (kann sowohl vom HCD als auch vom Hub + * driver ausgefuehrt werden) + * + * @param usb_device Das Geraet + */ + void (*reset)(struct cdi_usb_device* usb_device); + + /// Ziemlich lowlevelig. Gehoert das hier wirklich rein? + /// Im einzelnen sind das: Pipe fuer Endpoint 0 + struct cdi_usb_pipe* ep0; + /// Device descriptor + void* device_desc; + /// Configuration descriptor + void* config_desc; + /// Interface descriptor + struct cdi_usb_interface* interface_desc; + /// Class descriptor + void* class_desc; +}; + +enum cdi_usb_toggle { + /// Das Toggle nehmen, das jetzt dran ist + TOGGLE_UNSPECIFIC = 0, + /// Geloeschtes Togglebit erzwingen + TOGGLE_0 = 1, + /// Gesetztes Togglebit erzwingen + TOGGLE_1 = 2 +}; + +struct cdi_usb_endpoint { + /// sizeof(struct cdi_usb_endpoint) + uint8_t length; + /// 5 + uint8_t descriptor_type; + /// Nummer des Endpoints + uint8_t endpoint_address; + /// Eigenschaften (IN/OUT, Bulk/Interrupt/..., ...) + uint8_t attributes; + /// Maximale Paketgroesse in Bytes + uint16_t max_packet_size; + /// ??? + uint8_t interval; +} __attribute__((packed)); + +struct cdi_usb_interface { + /// sizeof(struct cdi_usb_interface) + uint8_t length; + /// 4 + uint8_t descriptor_type; + /// Nummer des Interfaces + uint8_t interface_number; + /// ??? + uint8_t alternate_setting; + /// Anzahl der Endpoints fuer dieses Interface + uint8_t num_endpoints; + /// Class-ID (bei Composite Devices) + uint8_t interface_class; + /// Subclass-ID + uint8_t interface_subclass; + /// Protocol-ID + uint8_t interface_protocol; + /// String-Index, der das Interface beschreibt + uint8_t iInterface; +} __attribute__((packed)); + +/// Beschreibt eine Pipe +struct cdi_usb_pipe { + /// Ziel + struct cdi_usb_device* device; + /// Ein Pointer auf eine den Endpoint beschreibende Struktur + /// TODO Das ist eigentlich auch sehr lowlevelig. + struct cdi_usb_endpoint* endpoint; + /// Data-Toggle + int data_toggle; +}; + +/** Beschreibt ein USB-Paket */ +struct cdi_usb_packet { + /// Die gewuenschte Pipe + struct cdi_usb_pipe* pipe; + /// Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) + cdi_usb_packet_type_t type; + /// Zeiger auf den Datenpuffer + void* data; + /// Die Laenge des Puffers + size_t length; + /// Fehlertyp (oder USB_NO_ERROR) + cdi_usb_status_t condition; + /// Bestimmtes Data-Toggle benuten + enum cdi_usb_toggle use_toggle; +}; + +typedef enum { + /// Host -> Geraet + CDI_USB_CONTROL_OUT = 0x00, + /// Geraet -> Host + CDI_USB_CONTROL_IN = 0x80 +} cdi_usb_control_direction_t; + +typedef enum { + /// Standard-Request + CDI_USB_CONTROL_RQ_STANDARD = 0x00, + /// Klassenspezifischer Request + CDI_USB_CONTROL_RQ_CLASS = 0x20, + /// Herstellerspezifischer Request + CDI_USB_CONTROL_RQ_VENDOR = 0x40 +} cdi_usb_control_request_type_t; + +typedef enum { + /// Empfaenger: Geraet + CDI_USB_CONTROL_REC_DEVICE = 0x00, + /// Empfaenger: Interface + CDI_USB_CONTROL_REC_INTERFACE = 0x01, + /// Empfaenger: Endpoint + CDI_USB_CONTROL_REC_ENDPOINT = 0x02, + /// Empfaenger ist jemand anderes + CDI_USB_CONTROL_REC_OTHER = 0x03 +} cdi_usb_control_recipient_t; + +struct cdi_usb_control_transfer { + /// Wenn Daten zum Geraet gesendet werden sollen, dann ist hier der + /// Speicherbereich anzugeben + void *buffer; + /// Laenge der erwarteten bzw. zu sendenden Daten + size_t length; + /// Richtung + cdi_usb_control_direction_t direction; + /// Art des Requests (Standardrequest, klassenspezifisch, + /// herstellerspezifisch) + cdi_usb_control_request_type_t rtype; + /// Ziel (Device, Interface, Endpoint, anderes) + cdi_usb_control_recipient_t recipient; + /// Requestcode (Je nach rtype und recipient) + int request; + /// Ein zu sendender Wert + int value; + /// Ein zu sendender Index + int index; +}; + +#include "cdi/usb-hcd.h" + +struct cdi_usb_driver { + struct cdi_driver drv; + + /// Unterstuetzte Geschwindigkeiten + cdi_usb_speed_t speeds; + + /** + * Enumeriert ein USB-Geraet + * + * @param device Das zu enumerierende Geraet + */ + void (*enumerate_device)(struct cdi_usb_device* device); + + /** + * Sendet USB-Pakete + * + * @param packets Array zu sendender Pakete + * @param num_packets Anzahl der Pakete + * + * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) + */ + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, + size_t num_packets); + + /** + * Fuehrt einen Control-Transfer durch + * + * @param device Das Zielgeraet + * @param transfer Eine den Transfer beschreibende Struktur + * + * @return Gibt einen Puffer zurueck, der die ausgetauschen Daten enthaelt, + * im Fehlerfall hingegen NULL, gab es keine Daten, dann + * (void *)0xFFFFFFFF. + */ + void* (*do_control)(struct cdi_usb_device* device, + struct cdi_usb_control_transfer* transfer); +}; + +/** + * Ein USB-Geraetetreiber uebergibt dem Betriebssystem eine solche Struktur, + * damit ein bestimmter Handler beim Auffinden solcher Geraete aufgerufen wird. + * Jedes Feld enthaelt entweder eine Zahl, die mit der des Geraetes + * uebereinstimmen muss oder -1, dann wird dieses Feld ignoriert. + */ +struct cdi_usb_device_pattern { + int vendor_id; + int device_id; + int class_id; + int subclass_id; + int protocol_id; + + /** + * Ueberprueft das angegebene USB-Geraet + * + * @param device Das Geraet + * + * @return 0, wenn dieses Geraet nicht vom Treiber verarbeitet werden kann, + * sonst 1 + */ + int (*take_usb_device)(struct cdi_usb_device* device); +}; + +void cdi_usb_driver_destroy(struct cdi_usb_driver* driver); + +/** + * Initialisiert und registriert einen USB-Bustreiber beim System + * + * @param driver Der Treiber + */ +void cdi_usb_driver_init_and_register(struct cdi_usb_driver* driver); + +/** + * Registriert ein gefundenes USB-Geraet beim Betriebssystem + * + * @param device Das zu registrierende Geraet + */ +void cdi_usb_device_init(struct cdi_usb_device* device); + +/** + * Registriert ein Geraetemuster + * + * @param pattern Das Muster + */ +void cdi_usb_register_device_pattern(struct cdi_usb_device_pattern* pattern); + +#endif -- 1.6.4.2 From matthew at theiselins.net Wed Dec 9 13:48:07 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Wed, 9 Dec 2009 22:48:07 +1000 Subject: [cdi-devel] [PATCH] Various cleanups and portability fixes Message-ID: <1260362887-24133-1-git-send-email-matthew@theiselins.net> Hi, This patch addresses several problems: * Added a few structures to cdi-osdep.h to clean up some tyndur-specific elements in some functions * Added extern "C" to header files where necessary in order to handle C++ linkage problems * This commit contains a draft for a solution (cdi-osdep.h) to get around the const error with CDI_DRIVER on some operating systems. This is merely an RFC change, and has not been propagated into actual driver code yet. The idea of the CDI_DRIVER workaround is that both macros are implemented in drivers and the OS provides only one of the macros (whichever it supports best). There may well be a better solution; this is merely what I deem the most portable and simple solution. Cheers, Matt Signed-off-by: Matthew Iselin --- include/cdi-osdep.h | 36 +++++++++++++++++++++++++++++++++++- include/cdi/dma.h | 5 ++--- include/cdi/misc.h | 8 ++++++++ include/cdi/net.h | 8 ++++++++ include/cdi/pci.h | 3 +++ 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/include/cdi-osdep.h b/include/cdi-osdep.h index 6227e91..08132c8 100644 --- a/include/cdi-osdep.h +++ b/include/cdi-osdep.h @@ -26,10 +26,44 @@ * the driver with the CDI library. * * @param name Name of the driver - * @param drv Pointer to a driver description (struct cdi_driver*) + * @param drv A driver description (struct cdi_driver) * @param deps List of names of other drivers on which this driver depends * \endenglish */ #define CDI_DRIVER(name, drv, deps...) /* TODO */ +/** + * \english + * CDI_DRIVER_CONST shall be used exactly once for each CDI driver. It registers + * the driver with the CDI library. If CDI_DRIVER is defined, CDI_DRIVER_CONST + * must not be defined. + * + * @param name Name of the driver + * @param init Function to be called to initialise the driver + * @param destroy Function to be called to destroy the driver + * @param init_device Function to be called when initialising a device + * @param remove_device Function to be called when removing a device + * @param deps List of names of other drivers on which this driver depends + * \endenglish + */ +#define CDI_DRIVER_CONST(name, init, destroy, init_device, remove_device, deps...) /* TODO */ + +/** + * \english + * OS-specific PCI metadata. + * \endenglish + */ +typedef struct +{ +} os_pci_meta; + +/** + * \english + * OS-specific DMA metadata. + * \endenglish + */ +typedef struct +{ +} os_dma_meta; + #endif diff --git a/include/cdi/dma.h b/include/cdi/dma.h index 24f53a8..2ff19ec 100644 --- a/include/cdi/dma.h +++ b/include/cdi/dma.h @@ -20,9 +20,8 @@ struct cdi_dma_handle { size_t length; uint8_t mode; void* buffer; - - // LOST-Implementation... - FILE* file; + + os_dma_meta meta; }; // Geraet => Speicher diff --git a/include/cdi/misc.h b/include/cdi/misc.h index 2a130f6..2049c62 100644 --- a/include/cdi/misc.h +++ b/include/cdi/misc.h @@ -15,6 +15,10 @@ #include "cdi.h" +#ifdef __cplusplus +extern "C" { +#endif + /** * Registiert einen neuen IRQ-Handler. * @@ -89,5 +93,9 @@ int cdi_ioports_free(uint16_t start, uint16_t count); */ void cdi_sleep_ms(uint32_t ms); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif diff --git a/include/cdi/net.h b/include/cdi/net.h index 89628bd..1fddab6 100644 --- a/include/cdi/net.h +++ b/include/cdi/net.h @@ -36,6 +36,10 @@ struct cdi_net_driver { }; +#ifdef __cplusplus +extern "C" { +#endif + /** * Initialisiert die Datenstrukturen fuer einen Netzerktreiber * (erzeugt die devices-Liste) @@ -60,6 +64,10 @@ void cdi_net_device_init(struct cdi_net_device* device); void cdi_net_receive( struct cdi_net_device* device, void* buffer, size_t size); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif /*\@}*/ diff --git a/include/cdi/pci.h b/include/cdi/pci.h index 8a9cc18..6a3be7a 100644 --- a/include/cdi/pci.h +++ b/include/cdi/pci.h @@ -14,6 +14,7 @@ #include #include "cdi.h" +#include "cdi-osdep.h" #include "cdi/lists.h" struct cdi_pci_device { @@ -33,6 +34,8 @@ struct cdi_pci_device { uint8_t irq; cdi_list_t resources; + + os_pci_meta meta; }; typedef enum { -- 1.6.0.4 From max at tyndur.org Wed Dec 9 20:50:15 2009 From: max at tyndur.org (max at tyndur.org) Date: Wed, 9 Dec 2009 20:50:15 +0100 Subject: [cdi-devel] [PATCH] CDI.usb, mempool Message-ID: <1260388215-7377-1-git-send-email-max@tyndur.org> From: Max Reitz * Split the CDI.usb function "cdi_usb_driver_init_and_register" into an init and a register function, because a driver using the new driver registering method (via CDI_DRIVER macros) may not register itself + mempool as CDI header (has been used in the old USB driver) Signed-off-by: Max Reitz --- include/cdi/mempool.h | 35 +++++++++++++++++++++++++++++++++++ include/cdi/usb.h | 11 +++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 include/cdi/mempool.h diff --git a/include/cdi/mempool.h b/include/cdi/mempool.h new file mode 100644 index 0000000..83d2ab2 --- /dev/null +++ b/include/cdi/mempool.h @@ -0,0 +1,35 @@ +/****************************************************************************** +* Copyright (c) 2009 Kevin Wolf * +* * +* 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 MEMPOOL_H +#define MEMPOOL_H + +#include +#include + +struct mempool; + +struct mempool* mempool_create(size_t pool_size, size_t object_size); +int mempool_get(struct mempool* pool, void** obj, uintptr_t* phys_obj); +int mempool_put(struct mempool* pool, void* obj); + +#endif diff --git a/include/cdi/usb.h b/include/cdi/usb.h index df695fa..c4406c7 100644 --- a/include/cdi/usb.h +++ b/include/cdi/usb.h @@ -288,11 +288,18 @@ struct cdi_usb_device_pattern { void cdi_usb_driver_destroy(struct cdi_usb_driver* driver); /** - * Initialisiert und registriert einen USB-Bustreiber beim System + * Initialisiert einen USB-Bustreiber * * @param driver Der Treiber */ -void cdi_usb_driver_init_and_register(struct cdi_usb_driver* driver); +void cdi_usb_driver_init(struct cdi_usb_driver* driver); + +/** + * Registriert einen USB-Bustreiber beim System + * + * @param driver Der Treiber + */ +void cdi_usb_driver_register(struct cdi_usb_driver* driver); /** * Registriert ein gefundenes USB-Geraet beim Betriebssystem -- 1.6.4.2 From max at tyndur.org Wed Dec 9 21:09:52 2009 From: max at tyndur.org (max at tyndur.org) Date: Wed, 9 Dec 2009 21:09:52 +0100 Subject: [cdi-devel] [PATCH] USB 1.1 Message-ID: <1260389392-11936-1-git-send-email-max@tyndur.org> From: Max Reitz + Added USB 1.1 driver based on CDI.usb Signed-off-by: Max Reitz --- usb11/control.c | 242 +++++++++++++++++++++++++++++++++++++++++++ usb11/include/control.h | 31 ++++++ usb11/include/transfer.h | 38 +++++++ usb11/include/usb-structs.h | 184 ++++++++++++++++++++++++++++++++ usb11/include/usb11.h | 41 +++++++ usb11/main.c | 58 ++++++++++ usb11/transfer.c | 223 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 817 insertions(+), 0 deletions(-) create mode 100644 usb11/control.c create mode 100644 usb11/include/control.h create mode 100644 usb11/include/transfer.h create mode 100644 usb11/include/usb-structs.h create mode 100644 usb11/include/usb11.h create mode 100644 usb11/main.c create mode 100644 usb11/transfer.c diff --git a/usb11/control.c b/usb11/control.c new file mode 100644 index 0000000..4151aa5 --- /dev/null +++ b/usb11/control.c @@ -0,0 +1,242 @@ +/***************************************************************************** +* 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 "usb11.h" +#include "usb-structs.h" +#include "transfer.h" + +static int usb_dev_ids = 1; + +static void enumerate_hub(struct cdi_usb_device *usbdev); + +void usb11_enumerate(struct cdi_usb_device *device) +{ + struct endpoint_desc *ep0; + struct device_desc *dev_desc; + struct cdi_usb_hcd *hcd; + int id; + char *name; + void *position; + struct config_desc *conf_desc; + struct interface_desc *if_desc, *best_if; + + hcd = (struct cdi_usb_hcd *)device->hc->dev.driver; + + //EP0 initialisieren + device->ep0 = malloc(sizeof(*device->ep0)); + device->ep0->endpoint = malloc(sizeof(struct endpoint_desc)); + ep0 = (struct endpoint_desc *)device->ep0->endpoint; + ep0->length = sizeof(struct endpoint_desc); + ep0->descriptor_type = DESC_ENDPOINT; + ep0->endpoint_address = 0; + ep0->attributes = 0; + ep0->max_packet_size = 8; + ep0->interval = 0; + device->ep0->device = device; + device->ep0->data_toggle = 0; + + //Zur??cksetzen + device->reset(device); + + //Pipe erzeugen + hcd->add_pipe(device->ep0); + + //Erste acht Bytes des Device-Descriptors abrufen und die maximale + //Paketgr????e von EP0 feststellen + dev_desc = usb11_do_control(device, DEV_TO_HOST, NULL, 8, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); + if (dev_desc == NULL) { + return; + } + ep0->max_packet_size = dev_desc->max_packet_size0; + + //Nochmals zur??cksetzen + device->reset(device); + + //USB-Adresse zuweisen + usb11_do_control(device, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, + REC_DEVICE, SET_ADDRESS, (id = usb_dev_ids++), 0); + device->id = id; + if (usb_dev_ids >= 128) { + d0printf("Warning: USB device ID overflow.\n"); + usb_dev_ids = 1; + } + + //So, jetzt haben wir eine Adresse, da gibts auch eine neue Pipe + hcd->add_pipe(device->ep0); + + //Den ganzen Device-Descriptor einlesen + dev_desc = usb11_do_control(device, DEV_TO_HOST, NULL, sizeof(*dev_desc), + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); + device->device_desc = dev_desc; + device->vendor_id = dev_desc->vendor_id; + device->device_id = dev_desc->device_id; + + if (dev_desc->iManufacturer) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iManufacturer, 0); + if (name == NULL) { + return; + } + d1printf(" -> Manufacturer: "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf("\n"); + } + if (dev_desc->iProduct) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iProduct, 0); + if (name == NULL) { + return; + } + d1printf(" -> Product: "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf("\n"); + } + //TODO: Man kann doch nicht immer den ersten nehmen... + conf_desc = usb11_do_control(device, 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 = usb11_do_control(device, DEV_TO_HOST, NULL, + conf_desc->total_length, STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return; + } + device->config_desc = conf_desc; + usb11_do_control(device, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_CONFIGURATION, conf_desc->config_value, 0); + if (conf_desc->iConfiguration) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | conf_desc->iConfiguration, 0); + if (name == NULL) { + return; + } + d1printf("Using configuration "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf(".\n"); + } + position = conf_desc; + position = (void *)((uintptr_t)position + sizeof(struct config_desc)); + best_if = position; //Standard-IF + for (int i = 0; i < conf_desc->num_interfaces; i++) { + if_desc = position; + //TODO MSD ist zwar h??bsch. Aber andere auch ;-) + if (if_desc->interface_class == 8) { + best_if = if_desc; + break; + } + position = (void *)((uintptr_t)position + + sizeof(struct interface_desc) + + if_desc->num_endpoints * sizeof(struct endpoint_desc)); + } + usb11_do_control(device, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_INTERFACE, best_if->interface_number, 0); + device->interface_desc = (void *)best_if; + if (best_if->iInterface) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | best_if->iInterface, 0); + if (name == NULL) { + return; + } + d1printf("Using interface "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf(".\n"); + } + d1printf("Configuration: %i:%i (%i)\n", conf_desc->config_value, + best_if->interface_number, best_if->interface_class); + + if (!dev_desc->class_id) { //Composite Device + device->class_id = best_if->interface_class; + device->subclass_id = best_if->interface_subclass; + device->protocol_id = best_if->interface_protocol; + } else { + device->class_id = dev_desc->class_id; + device->subclass_id = dev_desc->subclass_id; + device->protocol_id = dev_desc->protocol_id; + } + + if (best_if->interface_class == USBDC_HUB) { + enumerate_hub(device); + } +} + +static void reset_hub_device(struct cdi_usb_device *usbdev) +{ + usb11_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 cdi_usb_device *usbdev) +{ + struct hub_desc *hub_desc; + struct cdi_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 = usb11_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 = usb11_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 + usb11_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); + down = malloc(sizeof(struct cdi_usb_device)); + down->hc = usbdev->hc; + down->hub = usbdev; + down->id = 0; + down->port = i; + down->speed = (port_status[0] & PORT_LOWSPEED) ? + CDI_USB_LOW_SPEED : CDI_USB_FULL_SPEED; + down->reset = &reset_hub_device; + cdi_usb_device_init(down); + } +} diff --git a/usb11/include/control.h b/usb11/include/control.h new file mode 100644 index 0000000..18b9db4 --- /dev/null +++ b/usb11/include/control.h @@ -0,0 +1,31 @@ +/***************************************************************************** +* 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 CONTROL_H +#define CONTROL_H + +#include +#include + +void usb11_enumerate(struct cdi_usb_device *device); + +#endif diff --git a/usb11/include/transfer.h b/usb11/include/transfer.h new file mode 100644 index 0000000..3b0f086 --- /dev/null +++ b/usb11/include/transfer.h @@ -0,0 +1,38 @@ +/***************************************************************************** +* 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 TRANSFER_H +#define TRANSFER_H + +#include + +#include + +void *usb11_do_control(struct cdi_usb_device *device, int direction, + void *buffer, int length, int rtype, int recipient, int request, int value, + int index); +void *usb11_export_control(struct cdi_usb_device *device, + struct cdi_usb_control_transfer *transfer); +cdi_usb_status_t usb11_send_packets(struct cdi_usb_packet *packets, + size_t num_packets); + +#endif diff --git a/usb11/include/usb-structs.h b/usb11/include/usb-structs.h new file mode 100644 index 0000000..427d0e5 --- /dev/null +++ b/usb11/include/usb-structs.h @@ -0,0 +1,184 @@ +/***************************************************************************** +* 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 USB_STRUCTS_H +#define USB_STRUCTS_H + +#include + + +#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 + + +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; + +#endif diff --git a/usb11/include/usb11.h b/usb11/include/usb11.h new file mode 100644 index 0000000..77634e9 --- /dev/null +++ b/usb11/include/usb11.h @@ -0,0 +1,41 @@ +/***************************************************************************** +* 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 USB11_H +#define USB11_H + +#include + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[usb 1.1] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[usb 1.1] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + +#endif diff --git a/usb11/main.c b/usb11/main.c new file mode 100644 index 0000000..e381062 --- /dev/null +++ b/usb11/main.c @@ -0,0 +1,58 @@ +/***************************************************************************** +* 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 "control.h" +#include "transfer.h" + +#define DRIVER_NAME "USB 1.1" + +int init_usb11(void); + +static struct cdi_usb_driver driver = { + .drv = { + .type = CDI_USB, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = NULL, + .remove_device = NULL, + .init = &init_usb11, + .destroy = NULL + }, + .speeds = CDI_USB_LOW_SPEED | CDI_USB_FULL_SPEED, + .enumerate_device = &usb11_enumerate, + .send_packets = &usb11_send_packets, + .do_control = &usb11_export_control +}; + +CDI_DRIVER(DRIVER_NAME, driver) + +int init_usb11() +{ + cdi_init(); + + cdi_usb_driver_init(&driver); + + return 0; +} diff --git a/usb11/transfer.c b/usb11/transfer.c new file mode 100644 index 0000000..eb4f949 --- /dev/null +++ b/usb11/transfer.c @@ -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. * +*****************************************************************************/ + +#include +#include + +#include + +#include "transfer.h" +#include "usb11.h" +#include "usb-structs.h" + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +/** + * Verarbeitet USB-Pakete. + * + * @param packet Die Paketliste + * @param num_packets Anzahl der Pakete + * + * @return Status + */ + +cdi_usb_status_t usb11_send_packets(struct cdi_usb_packet *packets, + size_t num_packets) +{ + cdi_usb_status_t error = CDI_USB_NAK; + int i = 0, mps; + int num_out_packets; + struct cdi_usb_pipe *pipe; + struct cdi_usb_device *device; + struct cdi_usb_packet *out_packets; + struct endpoint_desc *ep; + struct cdi_usb_hcd *hcd; + + if (num_packets < 1) { + return CDI_USB_TRIVIAL_ERROR; + } + + // Alle Pakete muessen dieselbe Pipe benutzen + pipe = packets[0].pipe; + for (i = 1; i < (int)num_packets; i++) { + if (packets[i].pipe != pipe) { + return CDI_USB_TRIVIAL_ERROR; + } + } + + device = pipe->device; + hcd = (struct cdi_usb_hcd *)device->hc->dev.driver; + ep = (struct endpoint_desc *)pipe->endpoint; + + // Pruefen, wie viele Pakete wir draus machen muessen + mps = ep->max_packet_size; + num_out_packets = 0; + for (i = 0; i < (int)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 == (int)num_packets) { + out_packets = packets; + } else { + int j; + int cur = 0; + + out_packets = calloc(num_out_packets, sizeof(*out_packets)); + for (i = 0; i < (int)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((int)packets[i].length, mps); + out_packets[cur].condition = CDI_USB_NAK; + + packets[i].data = (void *)((uintptr_t)packets[i].data + + out_packets[cur].length); + packets[i].length -= out_packets[cur].length; + cur++; + } + } + } + + error = hcd->send_packets(out_packets, num_out_packets); + if (error & CDI_USB_STALLED) { + d0printf("ENDPOINT 0x%02X OF DEVICE %i STALLED!\n", + ep->endpoint_address, device->id); + usb11_do_control(device, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, + REC_ENDPOINT, CLEAR_FEATURE, 0, ep->endpoint_address); + } else if (error & CDI_USB_NAK) { + // Nicht angenommene Pakete (FIXME: einzeln) senden + for (i = 0; i < num_out_packets; i++) { + if (!(out_packets[i].condition & CDI_USB_NAK)) { + continue; + } + error = CDI_USB_NAK; + while (error & CDI_USB_NAK) { + error = hcd->send_packets(&out_packets[i], 1); + if (error & CDI_USB_NAK) { + cdi_sleep_ms(5); + } + } + } + } + + if (out_packets != packets) { + free(out_packets); + } + + return error; +} + +void *usb11_do_control(struct cdi_usb_device *device, int direction, + void *buffer, int length, int rtype, int recipient, int request, int value, + int index) +{ + int no_data, alloced_here = 0; + cdi_usb_status_t rval; + struct setup_packet _setup; + struct setup_packet *setup = &_setup; + + no_data = direction & NO_DATA; + direction &= 0x80; + + setup->request_type = direction | (rtype & 0x60) | (recipient & 0x1F); + setup->request = request; + setup->value = value; + setup->index = index; + setup->length = length; + + struct cdi_usb_packet setup_packet = { + .pipe = device->ep0, + .type = CDI_USB_PACKET_SETUP, + .data = setup, + .length = sizeof(*setup), + .use_toggle = TOGGLE_UNSPECIFIC + }; + + if (usb11_send_packets(&setup_packet, 1)) { + return NULL; + } + + if (no_data) { + rval = CDI_USB_NO_ERROR; + buffer = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten + } else { + if ((direction != HOST_TO_DEV) || (length && (buffer == NULL))) { + buffer = malloc(length); + alloced_here = 1; + } + + struct cdi_usb_packet data_packet = { + .pipe = device->ep0, + .type = (direction == DEV_TO_HOST ? + CDI_USB_PACKET_IN : CDI_USB_PACKET_OUT), + .data = buffer, + .length = length, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + rval = usb11_send_packets(&data_packet, 1); + } + + if (rval == CDI_USB_NO_ERROR) { + struct cdi_usb_packet ack_packet = { + .pipe = device->ep0, + .data = NULL, + .length = 0, + .use_toggle = TOGGLE_1 + }; + + if (no_data || (direction == HOST_TO_DEV)) { + ack_packet.type = CDI_USB_PACKET_IN; + } else { + ack_packet.type = CDI_USB_PACKET_OUT; + } + + rval = usb11_send_packets(&ack_packet, 1); + } + + if ((rval != CDI_USB_NO_ERROR) && alloced_here) { + free(buffer); + } + + return (rval == CDI_USB_NO_ERROR) ? buffer : NULL; +} + +void *usb11_export_control(struct cdi_usb_device *device, + struct cdi_usb_control_transfer *transfer) +{ + return usb11_do_control(device, transfer->direction, transfer->buffer, + transfer->length, transfer->rtype, transfer->recipient, + transfer->request, transfer->value, transfer->index); +} -- 1.6.4.2 From max at tyndur.org Wed Dec 9 21:10:12 2009 From: max at tyndur.org (max at tyndur.org) Date: Wed, 9 Dec 2009 21:10:12 +0100 Subject: [cdi-devel] [PATCH] UHCI Message-ID: <1260389412-11978-1-git-send-email-max@tyndur.org> From: Max Reitz + Added UHCI driver based on CDI.usb-hcd Signed-off-by: Max Reitz --- uhci/hcd.c | 215 +++++++++++++++++++++++++++++++++++++ uhci/include/uhci-hcd.h | 17 +++ uhci/include/uhci-init.h | 9 ++ uhci/include/uhci-transfer.h | 8 ++ uhci/include/uhci.h | 138 ++++++++++++++++++++++++ uhci/init.c | 242 ++++++++++++++++++++++++++++++++++++++++++ uhci/main.c | 34 ++++++ uhci/transfer.c | 91 ++++++++++++++++ 8 files changed, 754 insertions(+), 0 deletions(-) create mode 100644 uhci/hcd.c create mode 100644 uhci/include/uhci-hcd.h create mode 100644 uhci/include/uhci-init.h create mode 100644 uhci/include/uhci-transfer.h create mode 100644 uhci/include/uhci.h create mode 100644 uhci/init.c create mode 100644 uhci/main.c create mode 100644 uhci/transfer.c diff --git a/uhci/hcd.c b/uhci/hcd.c new file mode 100644 index 0000000..bda4715 --- /dev/null +++ b/uhci/hcd.c @@ -0,0 +1,215 @@ +/***************************************************************************** +* 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 +#include +#include + +#include "uhci.h" +#include "uhci-hcd.h" + +extern cdi_list_t uhci_active_transfers; + +static void reset_device(struct cdi_usb_device *device); + +static void find_devices(struct cdi_usb_hc *device) +{ + struct uhci *uhci = (struct uhci *)device; + struct cdi_usb_device *dev; + int i; + + d0printf("Scanning root ports\n"); + 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)) { + continue; + } + dev = malloc(sizeof(struct cdi_usb_device)); + dev->hc = device; + dev->id = 0; + dev->port = i; + dev->speed = (cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & + RPORT_LOSPD) ? CDI_USB_LOW_SPEED : CDI_USB_FULL_SPEED; + dev->hub = NULL; + dev->reset = &reset_device; + cdi_usb_device_init(dev); + } +} + +static void activate_device(struct cdi_usb_device *device) +{ + struct uhci *uhci = (struct uhci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Activating device on port %i.\n", device->port); + cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, + RPORT_ENABLE | RPORT_CSC); + cdi_sleep_ms(20); +} + +static void reset_device(struct cdi_usb_device *device) +{ + struct uhci *uhci = (struct uhci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Resetting device on port %i.\n", device->port); + cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, + RPORT_ENABLE | RPORT_RESET | RPORT_CSC); + cdi_sleep_ms(20); +} + +static void add_pipe(struct cdi_usb_pipe *pipe) +{ + //Macht nix, weil das bei UHCI nicht n??tig ist + pipe = NULL; //Aber GCC k??nnen wir trotzdem gl??cklich machen +} + +static inline int _tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} + +static volatile int locked = 0; + +static cdi_usb_status_t do_packet(struct cdi_usb_packet *packet) +{ + struct cdi_usb_device *usbdev = packet->pipe->device; + struct uhci *uhci = (struct uhci *)usbdev->hc; + struct uhci_td td; + struct transfer addr; + int timeout, frame; + + void *buf; + uintptr_t pbuf; + + memset(&td, 0, sizeof(td)); + 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->speed == CDI_USB_LOW_SPEED); + td.errors = 1; + td.pid = packet->type; + td.device = usbdev->id; + td.endpoint = packet->pipe->endpoint->endpoint_address & 0x07; + td.maxlen = packet->length ? packet->length - 1 : 0x7FF; + while (_tsl(&locked)); + frame = (cdi_inw(uhci->pbase + UHCI_FRNUM) + 5) & 0x3FF; + while (!(uhci->queue_heads[frame].transfer & 1)) { + frame++; + frame &= 0x3FF; + } + + if (mempool_get(uhci->buffers, &buf, &pbuf) < 0) { + return CDI_USB_TRIVIAL_ERROR; + } + + addr.virt = buf; + addr.phys = pbuf; + addr.error = 0xFFFF; + + buf = (void *)((uintptr_t)buf + sizeof(td)); + pbuf += sizeof(td); + + if (!packet->length) { + td.buffer = 0; + } else { + td.buffer = pbuf; + if (packet->type != CDI_USB_PACKET_IN) { + memcpy(buf, packet->data, packet->length); + } + } + + memcpy(addr.virt, &td, sizeof(td)); + + + if (uhci_active_transfers == NULL) { + uhci_active_transfers = cdi_list_create(); + } + uhci->queue_heads[frame].transfer = addr.phys; + cdi_list_push(uhci_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)) { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + if (packet->type == CDI_USB_PACKET_IN) { + memcpy(packet->data, buf, packet->length); + } + if (addr.error == 0xFFFF) { + addr.error = CDI_USB_TIMEOUT; + } + + mempool_put(uhci->buffers, addr.virt); + + return addr.error; +} + +static cdi_usb_status_t do_packets(struct cdi_usb_packet *packets, + size_t num_packets) +{ + size_t i; + cdi_usb_status_t cond = CDI_USB_NO_ERROR; + + //TODO + for (i = 0; (i < num_packets) && !cond; i++) { + packets[i].condition = do_packet(&packets[i]); + cond |= packets[i].condition; + } + + return cond; +} + + +const struct uhci_functions uhci_functions = { + .find_devices = &find_devices, + .activate_device = &activate_device, + .send_packets = &do_packets, + .add_pipe = &add_pipe +}; diff --git a/uhci/include/uhci-hcd.h b/uhci/include/uhci-hcd.h new file mode 100644 index 0000000..e01ce9f --- /dev/null +++ b/uhci/include/uhci-hcd.h @@ -0,0 +1,17 @@ +#ifndef UHCI_CONTROL_H +#define UHCI_CONTROL_H + +#include +#include + +struct uhci_functions { + void (*find_devices)(struct cdi_usb_hc *device); + void (*activate_device)(struct cdi_usb_device *device); + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet *packets, + size_t num_packets); + void (*add_pipe)(struct cdi_usb_pipe *pipe); +}; + +cdi_list_t uhci_find_devices(struct cdi_usb_hc *device); + +#endif diff --git a/uhci/include/uhci-init.h b/uhci/include/uhci-init.h new file mode 100644 index 0000000..33ecf66 --- /dev/null +++ b/uhci/include/uhci-init.h @@ -0,0 +1,9 @@ +#ifndef UHCI_INIT_H +#define UHCI_INIT_H + +#include "uhci.h" + +void find_and_init_uhcis(void); +void uhci_init(struct cdi_device *dev); + +#endif diff --git a/uhci/include/uhci-transfer.h b/uhci/include/uhci-transfer.h new file mode 100644 index 0000000..89062ff --- /dev/null +++ b/uhci/include/uhci-transfer.h @@ -0,0 +1,8 @@ +#ifndef USB_TRANSFER_H +#define USB_TRANSFER_H + +#include + +void uhci_handler(struct cdi_device *cdi_hci); + +#endif diff --git a/uhci/include/uhci.h b/uhci/include/uhci.h new file mode 100644 index 0000000..678b2a1 --- /dev/null +++ b/uhci/include/uhci.h @@ -0,0 +1,138 @@ +/***************************************************************************** +* 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 UHCI_H +#define UHCI_H + +#include +#include + +#include +#include +#include +#include + + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[uhci] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[uhci] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + + +#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 CONFIGURE 0x0040 +#define GLOB_SUSP_MODE 0x0008 +#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 + +//Legacy support +#define UHCI_PCI_LEGSUP 0xC0 //w + +#define UHCI_LEGSUP_STATUS 0x8F00 //Statusbits, die mittels schreiben einer 1 + //gel??scht werden +#define UHCI_LEGSUP_NO_CHG 0x5040 //Bits, die nicht ver??ndert werden k??nnen + //(RO bzw. reserviert) +#define UHCI_LEGSUP_PIRQ 0x2000 //Interrupt wird als PCI-IRQ ausgef??hrt + + +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 { + 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; +}; + +struct uhci { + struct cdi_usb_hc cdidev; + struct cdi_pci_device *pcidev; + int 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 mempool *buffers; +}; + +#endif diff --git a/uhci/init.c b/uhci/init.c new file mode 100644 index 0000000..89f9881 --- /dev/null +++ b/uhci/init.c @@ -0,0 +1,242 @@ +/***************************************************************************** +* 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 +#include +#include +#include + +#include "uhci.h" +#include "uhci-hcd.h" +#include "uhci-init.h" +#include "uhci-transfer.h" + +extern const struct uhci_functions uhci_functions; +extern int init_uhci(void); + +#define DRIVER_NAME "uhcd" + +static struct cdi_usb_hcd driver = { + .drv = { + .type = CDI_USB_HCD, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = &uhci_init, + .remove_device = NULL, + .init = &init_uhci, + .destroy = NULL + } +}; + +CDI_DRIVER(DRIVER_NAME, driver) + +void find_and_init_uhcis(void) +{ + struct uhci *hci; + char *name; + int uhci_counter = 0; + struct cdi_pci_device *dev; + + cdi_usb_hcd_init(&driver); + + cdi_list_t pci_devices = cdi_list_create(); + cdi_pci_get_all_devices(pci_devices); + for (int i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) { + if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03) + || (dev->interface_id != 0x00)) + { + cdi_pci_device_destroy(dev); + } else { + hci = malloc(sizeof(struct uhci)); + hci->cdidev.dev.type = CDI_USB_HCD; + name = malloc(7); + sprintf(name, "uhci%i", uhci_counter++); + hci->cdidev.dev.name = (const char *)name; + hci->cdidev.speed = CDI_USB_LOW_SPEED | CDI_USB_FULL_SPEED; + hci->pcidev = dev; + cdi_list_push(driver.drv.devices, hci); + } + } + + cdi_list_destroy(pci_devices); + + driver.find_devices = uhci_functions.find_devices; + driver.activate_device = uhci_functions.activate_device; + driver.send_packets = uhci_functions.send_packets; + driver.add_pipe = uhci_functions.add_pipe; +} + +void uhci_init(struct cdi_device *dev) +{ + struct uhci *uhci = (struct uhci *)dev; + struct cdi_pci_resource *res; + int size = 0, request_size, legsup, cmd, intr, i; + + d0printf("Trying to init %s at %02x:%02x.%02x...\n", uhci->cdidev.dev.name, + uhci->pcidev->bus, uhci->pcidev->dev, uhci->pcidev->function); + + uhci->pbase = 0; + for (i = 0; (res = cdi_list_get(uhci->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) { + d0printf("Cannot allocate I/O ports.\n"); + return; + } + uhci->pbase = res->start; + break; + } + } + if (!uhci->pbase) { + d0printf("No I/O space found.\n"); + return; + } + request_size = + // Maximale Paketl??nge (Full Speed) ist 1023, Alignment au??erdem + 1024 + + // Transferdeskriptor + sizeof(struct uhci_td); + + uhci->buffers = mempool_create(8192, request_size); + if (uhci->buffers == NULL) { + d0printf("Cannot create mempool.\n"); + return; + } + + if (cdi_alloc_phys_mem(sizeof(*uhci->queue_heads) * 1024, + (void **)&uhci->queue_heads, (void **)&uhci->phys_queue_heads) == -1) + { + d0printf("Cannot allocate QH memory.\n"); + return; + } + + if (cdi_alloc_phys_mem(4096, (void **)&uhci->frame_list, + (void **)&uhci->phys_frame_list) == -1) + { + d0printf("Cannot allocate frame list.\n"); + return; + } + + cdi_pci_config_outw(uhci->pcidev, 4, 0x05); + + cdi_register_irq(uhci->pcidev->irq, &uhci_handler, dev); + uhci->root_ports = (size - 0x10) >> 1; + //Erstmal testen, wie viele wir genau haben... + for (i = 2; i < uhci->root_ports; i++) { + if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + i * 2) & 0x0080) || + (cdi_inw(uhci->pbase + UHCI_RPORTS + i * 2) == 0xFFFF)) + { + uhci->root_ports = i; + break; + } + } + //Mehr als sieben Rootports, klingt lustig + if (uhci->root_ports > 7) { + uhci->root_ports = 2; + } + + d1printf("I/O 0x%04X (%i ports), IRQ %i\n", uhci->pbase, uhci->root_ports, + uhci->pcidev->irq); + +#ifdef CDI_PCI_DIRECT_ACCESS + legsup = cdi_pci_config_inw(uhci->pcidev, UHCI_PCI_LEGSUP); +#endif + cmd = cdi_inw(uhci->pbase + UHCI_USBCMD); + intr = cdi_inw(uhci->pbase + UHCI_USBINTR); + + //Wenn der HC l??uft, das Configurebit gesetzt ist, der nicht runtergefahren + //ist oder dieses Interrupt-enable-Bit gesetzt ist, dann war wohl schon + //jemand dran. + //(oder wenn der Legacy Support aktiviert ist, das hei??t, wenn in LEGSUP + //irgendein Bit gesetzt ist, dass nicht RO/reserviert oder ein Statusbit + //ist) + if ((legsup & ~(UHCI_LEGSUP_STATUS | UHCI_LEGSUP_NO_CHG)) || + (cmd & USB_RUN) || !(cmd & CONFIGURE) || !(cmd & GLOB_SUSP_MODE) || + (intr & 0x0002)) + { + //Na, wer hat denn da am HC rumgefummelt. B????????ses BIOS. Oder SMM. Oder + //wer auch immer. +#ifdef CDI_PCI_DIRECT_ACCESS + //Alle LEGSUP-Statusbits l??schen + cdi_pci_config_outw(uhci->pcidev, UHCI_PCI_LEGSUP, + UHCI_LEGSUP_STATUS); +#endif + //Resetten + cdi_outw(uhci->pbase + UHCI_USBCMD, HCRESET); + //Warten, bis der Reset beendet ist + for (i = 0; (cdi_inw(uhci->pbase + UHCI_USBCMD) & HCRESET) && + (i < 50); i++) + { + cdi_sleep_ms(10); + } + if (i == 50) { + d1printf("Reset not finished...\n"); + } + + //Interrupts erstmal deaktivieren + cdi_outw(uhci->pbase + UHCI_USBINTR, 0); + //Den gesamten HC ebenso + cdi_outw(uhci->pbase + UHCI_USBCMD, 0); + + //Alle Ports "runterfahren" + for (int port = 0; port < uhci->root_ports; port++) { + cdi_outw(uhci->pbase + UHCI_RPORTS + (port * 2), 0); + } + } + + for (i = 0; i < 1024; i++) { + 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; + } + + //Standardwert, jede Millisekunde ein Frame + cdi_outb(uhci->pbase + UHCI_SOFMOD, 0x40); + //Unsere Frameliste eintragen + cdi_outl(uhci->pbase + UHCI_FRBASEADD, uhci->phys_frame_list); + //Frame zur??cksetzen + cdi_outw(uhci->pbase + UHCI_FRNUM, 0); +#ifdef CDI_PCI_DIRECT_ACCESS + //Hier setzen wir das PIRQ-Bit und deaktivieren s??mtlichen Legacy Support. + cdi_pci_config_outw(uhci->pcidev, UHCI_PCI_LEGSUP, UHCI_LEGSUP_PIRQ); +#else + d0printf("Failed to deactivate USB legacy support.\n"); +#endif + + //HC starten + cdi_outw(uhci->pbase + UHCI_USBCMD, USB_RUN | CONFIGURE | MAXP); + //Alle Interrupts aktivieren + cdi_outw(uhci->pbase + UHCI_USBINTR, 0xF); + + //Connect status change l??schen + for (i = 0; i < uhci->root_ports; i++) { + cdi_outw(uhci->pbase + UHCI_RPORTS + i * 2, RPORT_CSC); + } +} diff --git a/uhci/main.c b/uhci/main.c new file mode 100644 index 0000000..8c5fb07 --- /dev/null +++ b/uhci/main.c @@ -0,0 +1,34 @@ +/***************************************************************************** +* 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 "uhci-init.h" + +int init_uhci() +{ + cdi_init(); + + find_and_init_uhcis(); + + return 0; +} diff --git a/uhci/transfer.c b/uhci/transfer.c new file mode 100644 index 0000000..52b95bb --- /dev/null +++ b/uhci/transfer.c @@ -0,0 +1,91 @@ +/***************************************************************************** +* 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 "uhci.h" + +cdi_list_t uhci_active_transfers = NULL; + +void uhci_handler(struct cdi_device *cdi_hci) +{ + struct uhci *uhci = (struct uhci *)cdi_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; + } + //Ist Bit 0 gesetzt, dann ist das ein normaler IOC (Interrupt on + //completion), bei Bit 1 ist ein Fehler aufgetreten, aber das werden wir ja + //unten sehen. + if (status & ~0x0003) { + d1printf("Unerwarteter IRQ 0x%04X von %s\n", status, cdi_hci->name); + } + if (status & 0x10) { + d0printf("SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n"); + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! + } else { + for (i = 0; (addr = cdi_list_get(uhci_active_transfers, i)) != NULL; + i++) + { + td = addr->virt; + if (td->active) { + continue; + } + addr->error = CDI_USB_NO_ERROR; + cdi_list_remove(uhci_active_transfers, i--); + if (td->stalled_err) { + d0printf("ENDPOINT STALLED\n"); + addr->error |= CDI_USB_STALLED; + } + if (td->buf_err) { + d1printf("Puffer??berlauf oder Underrun\n"); + addr->error |= CDI_USB_BUFFER_ERROR; + } + if (td->babble) { + d1printf("Da war ein Ger??t wohl sehr gespr??chig: Babble.\n"); + addr->error |= CDI_USB_BABBLE; + } + if (td->nak) { + d1printf("NAK empfangen\n"); + addr->error |= CDI_USB_NAK; + } + if (td->crc_time_err) { + d1printf("CRC-Fehler oder Timeout\n"); + addr->error |= CDI_USB_CRC | CDI_USB_TIMEOUT; + } + if (td->bitstuff_err) { + d1printf("Bitstufffehler\n"); + addr->error |= CDI_USB_BITSTUFF; + } + //TODO: free(td) - dazu w??re eine CDI-Funktion n??tzlich + } + } + cdi_outw(uhci->pbase + UHCI_USBSTS, status); +} -- 1.6.4.2 From max at tyndur.org Wed Dec 9 21:10:17 2009 From: max at tyndur.org (max at tyndur.org) Date: Wed, 9 Dec 2009 21:10:17 +0100 Subject: [cdi-devel] [PATCH] OHCI Message-ID: <1260389417-12012-1-git-send-email-max@tyndur.org> From: Max Reitz + Added OHCI driver based on CDI.usb-hcd Signed-off-by: Max Reitz --- ohci/hcd.c | 315 ++++++++++++++++++++++++++++++++++++++++++ ohci/include/ohci-hcd.h | 17 +++ ohci/include/ohci-init.h | 31 ++++ ohci/include/ohci-transfer.h | 8 + ohci/include/ohci.h | 283 +++++++++++++++++++++++++++++++++++++ ohci/init.c | 241 ++++++++++++++++++++++++++++++++ ohci/main.c | 34 +++++ ohci/transfer.c | 84 +++++++++++ 8 files changed, 1013 insertions(+), 0 deletions(-) create mode 100644 ohci/hcd.c create mode 100644 ohci/include/ohci-hcd.h create mode 100644 ohci/include/ohci-init.h create mode 100644 ohci/include/ohci-transfer.h create mode 100644 ohci/include/ohci.h create mode 100644 ohci/init.c create mode 100644 ohci/main.c create mode 100644 ohci/transfer.c diff --git a/ohci/hcd.c b/ohci/hcd.c new file mode 100644 index 0000000..8296ba9 --- /dev/null +++ b/ohci/hcd.c @@ -0,0 +1,315 @@ +/***************************************************************************** +* 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 +#include +#include + +#include "ohci.h" +#include "ohci-hcd.h" + +static void reset_device(struct cdi_usb_device *device); + +static void find_devices(struct cdi_usb_hc *device) +{ + struct ohci *ohci = (struct ohci *)device; + struct cdi_usb_device *dev; + int i; + + d0printf("Scanning root ports\n"); + for (i = 0; i < ohci->root_ports; i++) { + if (!(ohci->memory->hc_rh_port_status[i] & OHC_RP_CCS)) { + continue; + } + dev = malloc(sizeof(struct cdi_usb_device)); + dev->hc = device; + dev->id = 0; + dev->port = i; + dev->speed = (ohci->memory->hc_rh_port_status[i] & OHC_RP_LSDA) ? + CDI_USB_LOW_SPEED : CDI_USB_FULL_SPEED; + dev->reset = &reset_device; + cdi_usb_device_init(dev); + } +} + +static void activate_device(struct cdi_usb_device *device) +{ + struct ohci *ohci = (struct ohci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Activating device on port %i.\n", device->port); + ohci->memory->hc_rh_port_status[device->port] |= OHC_RP_PES; + cdi_sleep_ms(20); +} + +static void reset_device(struct cdi_usb_device *device) +{ + struct ohci *ohci = (struct ohci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Resetting device on port %i.\n", device->port); + ohci->memory->hc_rh_port_status[device->port] |= OHC_RP_PRS; + cdi_sleep_ms(20); +} + +static void add_pipe(struct cdi_usb_pipe *pipe) +{ + struct ohci *ohci = (struct ohci *)pipe->device->hc; + 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->speed == CDI_USB_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); + + if (iscontrol) { + ohci->memory->hc_control_head_ed = ped; + } else { + ohci->memory->hc_bulk_head_ed = ped; + } +} + +static inline int _tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} + +static volatile int locked = 0; + +static cdi_usb_status_t do_packets(struct cdi_usb_packet *packets, + size_t num_packets) +{ + struct ohci_ed_desc *edsc; + struct ohci_td_desc *tdsc, *otdsc; + int i, j, cond, toggle, done_packets; + struct ohci *ohci = (struct ohci *)packets[0].pipe->device->hc; + struct ohci_td *vtd[num_packets]; + uintptr_t ptd[num_packets]; + + for (i = 0; (edsc = cdi_list_get(ohci->ed_list, i)) != NULL; i++) { + if ((edsc->function == packets[0].pipe->device->id) && + (edsc->endpoint == packets[0].pipe->endpoint->endpoint_address)) + { + break; + } + } + if (edsc == NULL) { + return CDI_USB_TIMEOUT; //Genau das w??rde passieren + } + + toggle = packets[0].pipe->data_toggle; + for (i = 0; i < (int)num_packets; i++) { + if (mempool_get(ohci->transfer_pool, (void **)&vtd[i], &ptd[i]) == -1) + { + return CDI_USB_TRIVIAL_ERROR; + } + if ((packets[i].data != NULL) && + (packets[i].type != CDI_USB_PACKET_IN)) + { + memcpy((void *)((uintptr_t)vtd[i] + sizeof(struct ohci_td)), + packets[i].data, packets[i].length); + } + vtd[i]->rounding = 1; //Warum nicht + switch (packets[i].type) { + case CDI_USB_PACKET_SETUP: + vtd[i]->direction = OHC_TD_DIR_SETUP; + break; + case CDI_USB_PACKET_IN: + vtd[i]->direction = OHC_TD_DIR_IN; + break; + case CDI_USB_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 CDI_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 CDI_USB_TRIVIAL_ERROR; + } + tdsc->virt = vtd[i]; + tdsc->phys = ptd[i]; + tdsc->endpoint = edsc; + + 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]; + } + + 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; + } + done_packets = 0; + cond = CDI_USB_NO_ERROR; + while (done_packets < (int)num_packets) { + cdi_sleep_ms(1); + for (i = 0; i < (int)num_packets; i++) { + if (vtd[i]->condition != 15) { + done_packets++; + switch (vtd[i]->condition) { + case 0x00: + packets[i].condition = CDI_USB_NO_ERROR; + break; + case 0x02: + packets[i].condition = CDI_USB_BITSTUFF; + break; + case 0x04: + packets[i].condition = CDI_USB_STALLED; + for (j = 0; j < (int)num_packets; j++) { + //Allet, was nicht fertig ist, ist STALLed + vtd[j]->condition = 0x04; + } + break; + case 0x05: + packets[i].condition = CDI_USB_TIMEOUT; + break; + case 0x08: + packets[i].condition = CDI_USB_BABBLE; + break; + case 0x0C: + case 0x0D: + packets[i].condition = CDI_USB_BUFFER_ERROR; + break; + default: + packets[i].condition = CDI_USB_CRC; + break; + } + cond |= packets[i].condition; + vtd[i]->condition = 15; //Nicht nochmal ??berpr??fen + } + } + } + for (i = 0; i < (int)num_packets; i++) { + if ((packets[i].data != NULL) && + (packets[i].type == CDI_USB_PACKET_IN)) + { + memcpy(packets[i].data, (void *)((uintptr_t)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 (cond & CDI_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 + } + return cond; +} + + +const struct ohci_functions ohci_functions = { + .find_devices = &find_devices, + .activate_device = &activate_device, + .send_packets = &do_packets, + .add_pipe = &add_pipe +}; diff --git a/ohci/include/ohci-hcd.h b/ohci/include/ohci-hcd.h new file mode 100644 index 0000000..162b357 --- /dev/null +++ b/ohci/include/ohci-hcd.h @@ -0,0 +1,17 @@ +#ifndef UHCI_CONTROL_H +#define UHCI_CONTROL_H + +#include +#include + +struct ohci_functions { + void (*find_devices)(struct cdi_usb_hc *device); + void (*activate_device)(struct cdi_usb_device *device); + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet *packets, + size_t num_packets); + void (*add_pipe)(struct cdi_usb_pipe *pipe); +}; + +cdi_list_t ohci_find_devices(struct cdi_usb_hc *device); + +#endif diff --git a/ohci/include/ohci-init.h b/ohci/include/ohci-init.h new file mode 100644 index 0000000..2f19d2c --- /dev/null +++ b/ohci/include/ohci-init.h @@ -0,0 +1,31 @@ +/***************************************************************************** +* 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 OHCI_INIT_H +#define OHCI_INIT_H + +#include "ohci.h" + +void find_and_init_ohcis(void); +void ohci_init(struct cdi_device *dev); + +#endif diff --git a/ohci/include/ohci-transfer.h b/ohci/include/ohci-transfer.h new file mode 100644 index 0000000..06267e6 --- /dev/null +++ b/ohci/include/ohci-transfer.h @@ -0,0 +1,8 @@ +#ifndef USB_TRANSFER_H +#define USB_TRANSFER_H + +#include + +void ohci_handler(struct cdi_device *cdi_hci); + +#endif diff --git a/ohci/include/ohci.h b/ohci/include/ohci.h new file mode 100644 index 0000000..b06a280 --- /dev/null +++ b/ohci/include/ohci.h @@ -0,0 +1,283 @@ +/***************************************************************************** +* 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 OHCI_H +#define OHCI_H + +#include +#include + +#include +#include +#include + +#include "ohci.h" + + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[ohci] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[ohci] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + + +#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_legsup +{ + uint32_t hce_control; + uint32_t hce_input; + uint32_t hce_output; + uint32_t hce_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 cdi_usb_hc cdidev; + struct cdi_pci_device *pcidev; + + struct ohci_registers *memory; + struct ohci_legsup *legsup; + int root_ports; + struct ohci_hcca *hcca; + + struct mempool *ed_pool; + struct mempool *transfer_pool; + + cdi_list_t ed_list; +}; + +#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 + 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 + +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) + 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); + +#endif diff --git a/ohci/init.c b/ohci/init.c new file mode 100644 index 0000000..a1ea5a5 --- /dev/null +++ b/ohci/init.c @@ -0,0 +1,241 @@ +/***************************************************************************** +* 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 +#include +#include +#include +#include + +#include "ohci.h" +#include "ohci-hcd.h" +#include "ohci-init.h" +#include "ohci-transfer.h" + +extern const struct ohci_functions ohci_functions; +extern int init_ohci(void); + +#define DRIVER_NAME "ohcd" + +static struct cdi_usb_hcd driver = { + .drv = { + .type = CDI_USB_HCD, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = &ohci_init, + .remove_device = NULL, + .init = &init_ohci, + .destroy = NULL + } +}; + +CDI_DRIVER(DRIVER_NAME, driver) + +void find_and_init_ohcis(void) +{ + struct ohci *hci; + char *name; + int ohci_counter = 0; + struct cdi_pci_device *dev; + + cdi_usb_hcd_init(&driver); + + cdi_list_t pci_devices = cdi_list_create(); + cdi_pci_get_all_devices(pci_devices); + for (int i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) { + if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03) + || (dev->interface_id != 0x10)) + { + cdi_pci_device_destroy(dev); + } else { + hci = malloc(sizeof(struct ohci)); + hci->cdidev.dev.type = CDI_USB_HCD; + name = malloc(7); + sprintf(name, "ohci%i", ohci_counter++); + hci->cdidev.dev.name = (const char *)name; + hci->cdidev.speed = CDI_USB_LOW_SPEED | CDI_USB_FULL_SPEED; + hci->pcidev = dev; + cdi_list_push(driver.drv.devices, hci); + } + } + + cdi_list_destroy(pci_devices); + + driver.find_devices = ohci_functions.find_devices; + driver.activate_device = ohci_functions.activate_device; + driver.send_packets = ohci_functions.send_packets; + driver.add_pipe = ohci_functions.add_pipe; +} + +void ohci_init(struct cdi_device *dev) +{ + struct ohci *ohci = (struct ohci *)dev; + struct cdi_pci_resource *res; + int i; + uintptr_t phys_hcca; + + ohci->memory = NULL; + for (i = 0; (res = cdi_list_get(ohci->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) { + d0printf("Wasn't able to allocate memory.\n"); + return; + } + ohci->legsup = (void *)((uintptr_t)ohci->memory + 0x100); + break; + } + } + if (ohci->memory == NULL) { + d1printf("Memory not found!\n"); + return; + } + d1printf("Memory @ 0x%08X\n", (uintptr_t)ohci->memory); + if ((ohci->memory->hc_revision & 0xFF) != 0x10) { + d0printf("This OHCI might be incompatible with this driver" + "(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) + { + d0printf("Wasn't able to allocate HCCA.\n"); + return; + } + memset(ohci->hcca, 0, sizeof(*ohci->hcca)); + + d1printf("IRQ: %i\n", ohci->pcidev->irq); + cdi_register_irq(ohci->pcidev->irq, &ohci_handler, dev); + + ohci->ed_pool = mempool_create(64 * sizeof(struct ohci_ed), + sizeof(struct ohci_ed)); + if (ohci->ed_pool == NULL) { + d0printf("Wasn't able to create ED memory pool!\n"); + return; + } + + ohci->transfer_pool = mempool_create(48 * (1024 + sizeof(struct ohci_td)), + 1024 + sizeof(struct ohci_td)); + if (ohci->transfer_pool == NULL) { + d0printf("Wasn't able to create transfer memory pool!\n"); + return; + } + + ohci->memory->hc_interrupt_disable = OHC_INT_MIE; + + if (ohci->memory->hc_control & OHC_CTRL_IR) { + d1printf("Kicking out 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) { + _d1printf("Went fine.\n"); + } else { + _d1printf("Idiot.\n"); + ohci->memory->hc_control &= ~OHC_CTRL_IR; + } + } else if ((ohci->memory->hc_control & OHC_CTRL_CBSR) != OHC_USB_RESET) { + d1printf("BIOS active\n"); + if ((ohci->memory->hc_control & OHC_CTRL_CBSR) != OHC_USB_OPERATIONAL) + { + d1printf("Activating RESUME...\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 + cdi_sleep_ms(10); //Reset abwarten + } + + if (ohci->legsup->hce_control & 1) { + d1printf("USB legacy support activated, but that won't last for" + "long...\n"); + } + //Harhar, that's mine now! + ohci->legsup->hce_control = 0; + + 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... + d1printf("Reset not finished yet...\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; + + d1printf("Activating HC.\n"); + + //DO IT, FAGGOT! + 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); + + d0printf("Found %i root ports.\n", ohci->root_ports); + + for (i = 0; i < ohci->root_ports; i++) { + ohci->memory->hc_rh_port_status[i] |= OHC_RP_CCS; //Deaktivieren + } + + ohci->ed_list = cdi_list_create(); +} diff --git a/ohci/main.c b/ohci/main.c new file mode 100644 index 0000000..4b6cd67 --- /dev/null +++ b/ohci/main.c @@ -0,0 +1,34 @@ +/***************************************************************************** +* 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 "ohci-init.h" + +int init_ohci() +{ + cdi_init(); + + find_and_init_ohcis(); + + return 0; +} diff --git a/ohci/transfer.c b/ohci/transfer.c new file mode 100644 index 0000000..dc5d3df --- /dev/null +++ b/ohci/transfer.c @@ -0,0 +1,84 @@ +/***************************************************************************** +* 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 "ohci.h" + +void ohci_handler(struct cdi_device *cdi_hci) +{ + struct ohci *ohci = (struct ohci *)cdi_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; + if (!status) { + return; //Wohl nicht f??r uns + } + + if (status & OHC_INT_RHSC) { + done |= OHC_INT_RHSC; + } + if (status & OHC_INT_UE) { + d0printf("Fatal HC error.\n"); + ohci->memory->hc_command_status |= OHC_CMST_RESET; + done |= OHC_INT_UE; + } + if (status & OHC_INT_SF) { + //??hm... Na ja. Manchmal fragt man sich, ob sich der HC so einsam + //f??hlt, dass er IRQs feuert, obwohl sie deaktiviert sind. + 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); + free(tdsc); + break; + } + } + if (tdsc != NULL) { + //Gefunden + break; + } + } + } + + if (status & ~done) { + d1printf("Non-handled interrupt: 0x%08X\n", status & ~done); + } + + ohci->memory->hc_interrupt_status = status; +} -- 1.6.4.2 From max at tyndur.org Wed Dec 9 21:10:28 2009 From: max at tyndur.org (max at tyndur.org) Date: Wed, 9 Dec 2009 21:10:28 +0100 Subject: [cdi-devel] [PATCH] USB mass storage device driver Message-ID: <1260389428-12055-1-git-send-email-max@tyndur.org> From: Max Reitz + Added USB mass storage device driver (currently just for "bulk only") Signed-off-by: Max Reitz --- usb-msd/include/msd.h | 121 ++++++++++++ usb-msd/main.c | 68 +++++++ usb-msd/msd.c | 485 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 674 insertions(+), 0 deletions(-) create mode 100644 usb-msd/include/msd.h create mode 100644 usb-msd/main.c create mode 100644 usb-msd/msd.c diff --git a/usb-msd/include/msd.h b/usb-msd/include/msd.h new file mode 100644 index 0000000..bb6cf51 --- /dev/null +++ b/usb-msd/include/msd.h @@ -0,0 +1,121 @@ +/***************************************************************************** +* 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 MSD_H +#define MSD_H + +#include +#include + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[usb-msd] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[usb-msd] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + +//"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 cdi_usb_device *usb_device; + struct cdi_usb_pipe *bulk_in; + struct cdi_usb_pipe *bulk_out; + 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 + +int check_msd(struct cdi_usb_device *device); +int msd_read_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer); +int msd_write_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer); + +#endif diff --git a/usb-msd/main.c b/usb-msd/main.c new file mode 100644 index 0000000..c5c401f --- /dev/null +++ b/usb-msd/main.c @@ -0,0 +1,68 @@ +/***************************************************************************** +* 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 "msd.h" + +#define DRIVER_NAME "usb-msd" + +static int init_msd(void); + +struct cdi_storage_driver msc_driver = { + .drv = { + .type = CDI_STORAGE, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = NULL, + .remove_device = NULL, + .init = &init_msd, + .destroy = NULL + }, + .read_blocks = &msd_read_blocks, + .write_blocks = &msd_write_blocks +}; + +CDI_DRIVER(DRIVER_NAME, msc_driver) + +static struct cdi_usb_device_pattern msd_pattern = { + .vendor_id = -1, + .device_id = -1, + .class_id = 8, + .subclass_id = -1, + .protocol_id = -1, + + .take_usb_device = &check_msd +}; + +static int init_msd() +{ + cdi_init(); + + cdi_storage_driver_init(&msc_driver); + + cdi_usb_register_device_pattern(&msd_pattern); + + return 0; +} diff --git a/usb-msd/msd.c b/usb-msd/msd.c new file mode 100644 index 0000000..e29d679 --- /dev/null +++ b/usb-msd/msd.c @@ -0,0 +1,485 @@ +/***************************************************************************** +* 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 +#include + +#include "msd.h" + +//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 + +extern struct cdi_storage_driver msc_driver; +static uint32_t cbw_tag = 1; + +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); + +int msd_read_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer) +{ + return msd_cdi_read(device, start, count, buffer); +} + +int msd_write_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer) +{ + return msd_cdi_write(device, start, count, buffer); +} + +static int msd_get_capacity(struct cdi_usb_device *usbdev, + uint32_t *block_size, uint32_t *block_count); + +int check_msd(struct cdi_usb_device *device) +{ + struct cdi_storage_device *strgdev; + struct cdi_usb_endpoint *ep_desc; + struct cdi_msd *cdimsd; + struct cdi_usb_hcd *hcd; + void *address; + int i; + uint32_t bs, bc; + uint64_t size; + static int msdnum = 0; + + //Nur Bulk-Only + if (device->protocol_id != 0x50) { + return 0; + } + + d0printf("MSD detected.\n"); + + cdimsd = malloc(sizeof(struct cdi_msd)); + if (cdimsd == NULL) { + return 0; + } + cdimsd->usb_device = device; + 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 0; + } + sprintf((char *)strgdev->dev.name, "usb-msd%i", msdnum++); + strgdev->dev.driver = (struct cdi_driver *)&msc_driver; + device->driver_data = cdimsd; + cdimsd->bulk_in = malloc(sizeof(*cdimsd->bulk_in)); + cdimsd->bulk_in->device = device; + cdimsd->bulk_in->endpoint = NULL; + cdimsd->bulk_in->data_toggle = 0; + cdimsd->bulk_out = malloc(sizeof(*cdimsd->bulk_out)); + cdimsd->bulk_out->device = device; + cdimsd->bulk_out->endpoint = NULL; + cdimsd->bulk_out->data_toggle = 0; + address = (void *)((uintptr_t)device->interface_desc + + sizeof(struct cdi_usb_interface)); + for (i = 0; i < device->interface_desc->num_endpoints; i++) { + ep_desc = address; + if ((ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (cdimsd->bulk_in->endpoint == NULL)) + { + //BULK-IN + cdimsd->bulk_in->endpoint = ep_desc; + } + else if (!(ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (cdimsd->bulk_out->endpoint == NULL)) + { + //BULK-OUT + cdimsd->bulk_out->endpoint = ep_desc; + } + address = (void *)((uintptr_t)address + + sizeof(struct cdi_usb_endpoint)); + } + if ((cdimsd->bulk_in->endpoint == NULL) || + (cdimsd->bulk_out->endpoint == NULL)) + { + d0printf("Not enough endpoints found.\n"); + return 0; + } + hcd = (struct cdi_usb_hcd *)device->hc->dev.driver; + hcd->add_pipe(cdimsd->bulk_in); + hcd->add_pipe(cdimsd->bulk_out); + if (!msd_get_capacity(device, &bs, &bc)) { + strgdev->block_size = 0; + strgdev->block_count = 0; + d1printf("Wasn't able to determine size of %s.\n", strgdev->dev.name); + } else { + strgdev->block_size = bs; + strgdev->block_count = bc; + size = bs; + size *= bc; + d1printf("%s: %i * %i B (about %d MB).\n", strgdev->dev.name, bc, bs, + (int)(size >> 20)); + } + cdi_storage_device_init(strgdev); + cdi_list_push(msc_driver.drv.devices, strgdev); + + return 1; +} + +static int write_cmd(struct cdi_usb_device *usbdev, void *src) +{ + struct cdi_msd *cdimsd = usbdev->driver_data; + struct cdi_usb_packet cmd_packet = { + .pipe = cdimsd->bulk_out, + .type = CDI_USB_PACKET_OUT, + .data = src, + .length = 0x1F, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + return usbdev->drv->send_packets(&cmd_packet, 1); +} + +/** + * 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 cdi_usb_device *usbdev, uint32_t expected_tag) +{ + struct cdi_msd *msc = usbdev->driver_data; + struct command_status_wrapper _csw; + struct command_status_wrapper *csw = &_csw; + cdi_usb_status_t error; + + struct cdi_usb_packet status_packet = { + .pipe = msc->bulk_in, + .type = CDI_USB_PACKET_IN, + .data = csw, + .length = 0x0D, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + error = usbdev->drv->send_packets(&status_packet, 1); + if (error != CDI_USB_NO_ERROR) { + return error; + } + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || csw->csw_status) + { + d1printf("0x%08X %i==%i 0x%08X\n", csw->csw_signature, csw->csw_tag, + expected_tag, csw->csw_status); + return CDI_USB_TRIVIAL_ERROR; + } + + return CDI_USB_NO_ERROR; +} + +static int msd_get_capacity(struct cdi_usb_device *usbdev, + uint32_t *block_size, uint32_t *block_count) +{ + struct cdi_msd *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper *cbw = &_cbw; + struct msd_capacity _cap; + struct msd_capacity *cap = &_cap; + uint32_t expected_tag; + + if ((usbdev == NULL) || (block_size == NULL) || (block_count == NULL)) { + return 0; + } + + msc = usbdev->driver_data; + 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 (write_cmd(usbdev, cbw) != CDI_USB_NO_ERROR) { + return 0; + } + + struct cdi_usb_packet in_packet = { + .pipe = msc->bulk_in, + .type = CDI_USB_PACKET_IN, + .data = cap, + .length = sizeof(*cap), + .use_toggle = TOGGLE_UNSPECIFIC + }; + + if (usbdev->drv->send_packets(&in_packet, 1) != CDI_USB_NO_ERROR) { + return 0; + } + + if (read_status(usbdev, expected_tag) != CDI_USB_NO_ERROR) { + 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 cdi_usb_device *usbdev) +{ + struct msclass_data *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper *cbw = &_cbw; + uint32_t expected_tag; + + msc = (struct msclass_data *)usbdev->driver_data; + 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, cbw) != USB_NO_ERROR) { + return 0; + } + + if (read_status(usbdev, expected_tag) != USB_NO_ERROR) { + return 0; + } + + return 1; +} +#endif + +#define MAX_ACCESS_BLOCKS 32 + +static uint32_t msd_read(struct cdi_usb_device *usbdev, uint32_t lba, + 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) +{ + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int i; +#endif + struct cdi_usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + int j, bbs; + + d1printf("read(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + + if (!count) { + d1printf("Empty read request.\n"); + return 0; + } + for (j = 0; j < (int)count; j += MAX_ACCESS_BLOCKS) { +#ifdef WAIT_FOR_MSD_READY + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { + cdi_sleep_ms(20); + } +#endif + bbs = (count - j > MAX_ACCESS_BLOCKS) ? MAX_ACCESS_BLOCKS : count - j; + error = msd_read(usbdev, start + j, bbs, + (void *)((uintptr_t)buffer + j * bs), bbs * bs); + if (error != CDI_USB_NO_ERROR) { + d0printf("Read error 0x%X at block %lld.\n", error, start + j); + return -1; + } + } + return 0; +} + +static uint32_t msd_read(struct cdi_usb_device *usbdev, uint32_t lba, + uint16_t sectors, void *buffer, size_t length) +{ + struct cdi_msd *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper *cbw = &_cbw; + uint32_t expected_tag; + int error; + + if ((buffer == NULL) || !length) { + return CDI_USB_TRIVIAL_ERROR; + } + msc = usbdev->driver_data; + 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 = write_cmd(usbdev, cbw); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + struct cdi_usb_packet in_packet = { + .pipe = msc->bulk_in, + .type = CDI_USB_PACKET_IN, + .data = buffer, + .length = length, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + error = usbdev->drv->send_packets(&in_packet, 1); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + error = read_status(usbdev, expected_tag); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + return CDI_USB_NO_ERROR; +} + +static uint32_t msd_write(struct cdi_usb_device *usbdev, uint32_t lba, + 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) +{ + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int i; +#endif + struct cdi_usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + int j, bbs; + + d1printf("write(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + + if (!count) { + d1printf("Empty write request.\n"); + return 0; + } + for (j = 0; j < (int)count; j += MAX_ACCESS_BLOCKS) { +#ifdef WAIT_FOR_MSD_READY + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) + cdi_sleep_ms(20); +#endif + bbs = (count - j > MAX_ACCESS_BLOCKS) ? MAX_ACCESS_BLOCKS : count - j; + error = msd_write(usbdev, start + j, bbs, + (void *)((uintptr_t)buffer + j * bs), bbs * bs); + if (error != CDI_USB_NO_ERROR) { + d0printf("Write error 0x%X at block %lld.\n", error, start + j); + return -1; + } + } + return 0; +} + +static uint32_t msd_write(struct cdi_usb_device *usbdev, uint32_t lba, + uint16_t sectors, void *buffer, size_t length) +{ + struct cdi_msd *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper* cbw = &_cbw; + uint32_t expected_tag; + int error; + + if ((buffer == NULL) || !length) { + return CDI_USB_TRIVIAL_ERROR; + } + msc = usbdev->driver_data; + 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, cbw); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + struct cdi_usb_packet out_packet = { + .pipe = msc->bulk_out, + .type = CDI_USB_PACKET_OUT, + .data = buffer, + .length = length, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + error = usbdev->drv->send_packets(&out_packet, 1); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + error = read_status(usbdev, expected_tag); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + return CDI_USB_NO_ERROR; +} -- 1.6.4.2 From matthew at theiselins.net Thu Dec 10 00:38:05 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Thu, 10 Dec 2009 09:38:05 +1000 Subject: [cdi-devel] [PATCH v2] Various cleanups and portability fixes Message-ID: <1260401885-601-1-git-send-email-matthew@theiselins.net> Hi, This patch removes the CDI_DRIVER_CONST macro created previously, as well as the following: * Added a few structures to cdi-osdep.h to clean up some tyndur-specific elements in some functions * Added extern "C" to header files where necessary in order to handle C++ linkage problems Cheers, Matt Signed-off-by: Matthew Iselin --- include/cdi-osdep.h | 20 +++++++++++++++++++- include/cdi/dma.h | 5 ++--- include/cdi/lists.h | 8 ++++++++ include/cdi/misc.h | 8 ++++++++ include/cdi/net.h | 8 ++++++++ include/cdi/pci.h | 3 +++ include/cdi/storage.h | 6 ++++++ 7 files changed, 54 insertions(+), 4 deletions(-) diff --git a/include/cdi-osdep.h b/include/cdi-osdep.h index 6227e91..57a34a7 100644 --- a/include/cdi-osdep.h +++ b/include/cdi-osdep.h @@ -26,10 +26,28 @@ * the driver with the CDI library. * * @param name Name of the driver - * @param drv Pointer to a driver description (struct cdi_driver*) + * @param drv A driver description (struct cdi_driver) * @param deps List of names of other drivers on which this driver depends * \endenglish */ #define CDI_DRIVER(name, drv, deps...) /* TODO */ +/** + * \english + * OS-specific PCI metadata. + * \endenglish + */ +typedef struct +{ +} os_pci_meta; + +/** + * \english + * OS-specific DMA metadata. + * \endenglish + */ +typedef struct +{ +} os_dma_meta; + #endif diff --git a/include/cdi/dma.h b/include/cdi/dma.h index 24f53a8..2ff19ec 100644 --- a/include/cdi/dma.h +++ b/include/cdi/dma.h @@ -20,9 +20,8 @@ struct cdi_dma_handle { size_t length; uint8_t mode; void* buffer; - - // LOST-Implementation... - FILE* file; + + os_dma_meta meta; }; // Geraet => Speicher diff --git a/include/cdi/lists.h b/include/cdi/lists.h index 9a3403c..53c3def 100644 --- a/include/cdi/lists.h +++ b/include/cdi/lists.h @@ -21,6 +21,10 @@ */ typedef struct cdi_list_implementation* cdi_list_t; +#ifdef __cplusplus +extern "C" { +#endif + /** * Erzeugt eine neue Liste * @@ -106,4 +110,8 @@ void* cdi_list_remove(cdi_list_t list, size_t index); */ size_t cdi_list_size(cdi_list_t list); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif diff --git a/include/cdi/misc.h b/include/cdi/misc.h index 2a130f6..2049c62 100644 --- a/include/cdi/misc.h +++ b/include/cdi/misc.h @@ -15,6 +15,10 @@ #include "cdi.h" +#ifdef __cplusplus +extern "C" { +#endif + /** * Registiert einen neuen IRQ-Handler. * @@ -89,5 +93,9 @@ int cdi_ioports_free(uint16_t start, uint16_t count); */ void cdi_sleep_ms(uint32_t ms); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif diff --git a/include/cdi/net.h b/include/cdi/net.h index 89628bd..1fddab6 100644 --- a/include/cdi/net.h +++ b/include/cdi/net.h @@ -36,6 +36,10 @@ struct cdi_net_driver { }; +#ifdef __cplusplus +extern "C" { +#endif + /** * Initialisiert die Datenstrukturen fuer einen Netzerktreiber * (erzeugt die devices-Liste) @@ -60,6 +64,10 @@ void cdi_net_device_init(struct cdi_net_device* device); void cdi_net_receive( struct cdi_net_device* device, void* buffer, size_t size); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif /*\@}*/ diff --git a/include/cdi/pci.h b/include/cdi/pci.h index 8a9cc18..6a3be7a 100644 --- a/include/cdi/pci.h +++ b/include/cdi/pci.h @@ -14,6 +14,7 @@ #include #include "cdi.h" +#include "cdi-osdep.h" #include "cdi/lists.h" struct cdi_pci_device { @@ -33,6 +34,8 @@ struct cdi_pci_device { uint8_t irq; cdi_list_t resources; + + os_pci_meta meta; }; typedef enum { diff --git a/include/cdi/storage.h b/include/cdi/storage.h index d19fc6f..4963b68 100644 --- a/include/cdi/storage.h +++ b/include/cdi/storage.h @@ -57,6 +57,9 @@ struct cdi_storage_driver { uint64_t count, void* buffer); }; +#ifdef __cplusplus +extern "C" { +#endif /** * Initialisiert die Datenstrukturen fuer einen Massenspeichertreiber @@ -80,6 +83,9 @@ void cdi_storage_driver_register(struct cdi_storage_driver* driver); */ void cdi_storage_device_init(struct cdi_storage_device* device); +#ifdef __cplusplus +}; // extern "C" +#endif #endif -- 1.6.0.4 From matthew at theiselins.net Thu Dec 10 00:52:10 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Thu, 10 Dec 2009 09:52:10 +1000 Subject: [cdi-devel] [PATCH] Update cdi-osdep.h to contain OS-specific structs for PCI and DMA Message-ID: <1260402731-1489-1-git-send-email-matthew@theiselins.net> Signed-off-by: Matthew Iselin --- include/cdi-osdep.h | 20 +++++++++++++++++++- 1 files changed, 19 insertions(+), 1 deletions(-) diff --git a/include/cdi-osdep.h b/include/cdi-osdep.h index 6227e91..2095580 100644 --- a/include/cdi-osdep.h +++ b/include/cdi-osdep.h @@ -26,10 +26,28 @@ * the driver with the CDI library. * * @param name Name of the driver - * @param drv Pointer to a driver description (struct cdi_driver*) + * @param drv A driver description (struct cdi_driver) * @param deps List of names of other drivers on which this driver depends * \endenglish */ #define CDI_DRIVER(name, drv, deps...) /* TODO */ +/** + * \english + * OS-specific PCI data. + * \endenglish + */ +typedef struct +{ +} cdi_pci_device_osdep; + +/** + * \english + * OS-specific DMA data. + * \endenglish + */ +typedef struct +{ +} cdi_dma_osdep; + #endif -- 1.6.0.4 From matthew at theiselins.net Thu Dec 10 00:52:11 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Thu, 10 Dec 2009 09:52:11 +1000 Subject: [cdi-devel] [PATCH] Add extern "C" to header files. In-Reply-To: <1260402731-1489-1-git-send-email-matthew@theiselins.net> References: <1260402731-1489-1-git-send-email-matthew@theiselins.net> Message-ID: <1260402731-1489-2-git-send-email-matthew@theiselins.net> NOTE: This patch depends on the "Update cdi-osdep.h to contain OS-specific structs for PCI and DMA" commit. Signed-off-by: Matthew Iselin --- include/cdi/bios.h | 8 ++++++++ include/cdi/cache.h | 7 +++++++ include/cdi/dma.h | 13 ++++++++++--- include/cdi/fs.h | 7 ++++++- include/cdi/io.h | 8 ++++++++ include/cdi/lists.h | 8 ++++++++ include/cdi/misc.h | 8 ++++++++ include/cdi/net.h | 8 ++++++++ include/cdi/pci.h | 11 +++++++++++ include/cdi/scsi.h | 8 ++++++++ include/cdi/storage.h | 6 ++++++ 11 files changed, 88 insertions(+), 4 deletions(-) diff --git a/include/cdi/bios.h b/include/cdi/bios.h index 43ec828..461a6c5 100644 --- a/include/cdi/bios.h +++ b/include/cdi/bios.h @@ -47,6 +47,10 @@ struct cdi_bios_memory { uint16_t size; }; +#ifdef __cplusplus +extern "C" { +#endif + /** * Ruft den BIOS-Interrupt 0x10 auf. * @param registers Pointer auf eine Register-Struktur. Diese wird beim Aufruf @@ -59,5 +63,9 @@ struct cdi_bios_memory { */ int cdi_bios_int10(struct cdi_bios_registers *registers, cdi_list_t memory); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif diff --git a/include/cdi/cache.h b/include/cdi/cache.h index 4b9effd..d378599 100644 --- a/include/cdi/cache.h +++ b/include/cdi/cache.h @@ -40,6 +40,9 @@ typedef int (cdi_cache_read_block_t)(struct cdi_cache* cache, uint64_t block, typedef int (cdi_cache_write_block_t)(struct cdi_cache* cache, uint64_t block, size_t count, const void* src, void* prv); +#ifdef __cplusplus +extern "C" { +#endif /** * Cache erstellen @@ -108,4 +111,8 @@ void cdi_cache_block_release(struct cdi_cache* cache, void cdi_cache_block_dirty(struct cdi_cache* cache, struct cdi_cache_block* block); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif diff --git a/include/cdi/dma.h b/include/cdi/dma.h index 24f53a8..8d50640 100644 --- a/include/cdi/dma.h +++ b/include/cdi/dma.h @@ -20,9 +20,8 @@ struct cdi_dma_handle { size_t length; uint8_t mode; void* buffer; - - // LOST-Implementation... - FILE* file; + + cdi_dma_osdep meta; }; // Geraet => Speicher @@ -34,6 +33,10 @@ struct cdi_dma_handle { #define CDI_DMA_MODE_BLOCK (2 << 6) +#ifdef __cplusplus +extern "C" { +#endif + /** * Initialisiert einen Transport per DMA * @@ -63,5 +66,9 @@ int cdi_dma_write(struct cdi_dma_handle* handle); */ int cdi_dma_close(struct cdi_dma_handle* handle); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif diff --git a/include/cdi/fs.h b/include/cdi/fs.h index c898b08..c464d0d 100644 --- a/include/cdi/fs.h +++ b/include/cdi/fs.h @@ -617,7 +617,9 @@ struct cdi_fs_acl_entry_grp_str { char* group_name; }; - +#ifdef __cplusplus +extern "C" { +#endif /** * Dateisystemtreiber-Struktur initialisieren @@ -670,6 +672,9 @@ size_t cdi_fs_data_read(struct cdi_fs_filesystem* fs, uint64_t start, size_t cdi_fs_data_write(struct cdi_fs_filesystem* fs, uint64_t start, size_t size, const void* buffer); +#ifdef __cplusplus +}; // extern "C" +#endif #endif diff --git a/include/cdi/io.h b/include/cdi/io.h index 64534f8..5b0cad5 100644 --- a/include/cdi/io.h +++ b/include/cdi/io.h @@ -13,6 +13,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + static inline uint16_t cdi_inw(uint16_t _port) { uint16_t result; @@ -51,5 +55,9 @@ static inline void cdi_outl(uint16_t _port, uint32_t _data) asm volatile ("outl %0, %1" : : "a"(_data), "Nd" (_port)); } +#ifdef __cplusplus +}; // extern "C" +#endif + #endif diff --git a/include/cdi/lists.h b/include/cdi/lists.h index 9a3403c..53c3def 100644 --- a/include/cdi/lists.h +++ b/include/cdi/lists.h @@ -21,6 +21,10 @@ */ typedef struct cdi_list_implementation* cdi_list_t; +#ifdef __cplusplus +extern "C" { +#endif + /** * Erzeugt eine neue Liste * @@ -106,4 +110,8 @@ void* cdi_list_remove(cdi_list_t list, size_t index); */ size_t cdi_list_size(cdi_list_t list); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif diff --git a/include/cdi/misc.h b/include/cdi/misc.h index 2a130f6..2049c62 100644 --- a/include/cdi/misc.h +++ b/include/cdi/misc.h @@ -15,6 +15,10 @@ #include "cdi.h" +#ifdef __cplusplus +extern "C" { +#endif + /** * Registiert einen neuen IRQ-Handler. * @@ -89,5 +93,9 @@ int cdi_ioports_free(uint16_t start, uint16_t count); */ void cdi_sleep_ms(uint32_t ms); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif diff --git a/include/cdi/net.h b/include/cdi/net.h index 89628bd..1fddab6 100644 --- a/include/cdi/net.h +++ b/include/cdi/net.h @@ -36,6 +36,10 @@ struct cdi_net_driver { }; +#ifdef __cplusplus +extern "C" { +#endif + /** * Initialisiert die Datenstrukturen fuer einen Netzerktreiber * (erzeugt die devices-Liste) @@ -60,6 +64,10 @@ void cdi_net_device_init(struct cdi_net_device* device); void cdi_net_receive( struct cdi_net_device* device, void* buffer, size_t size); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif /*\@}*/ diff --git a/include/cdi/pci.h b/include/cdi/pci.h index 8a9cc18..b5ab5c3 100644 --- a/include/cdi/pci.h +++ b/include/cdi/pci.h @@ -14,6 +14,7 @@ #include #include "cdi.h" +#include "cdi-osdep.h" #include "cdi/lists.h" struct cdi_pci_device { @@ -33,6 +34,8 @@ struct cdi_pci_device { uint8_t irq; cdi_list_t resources; + + cdi_pci_device_osdep meta; }; typedef enum { @@ -49,6 +52,10 @@ struct cdi_pci_resource { }; +#ifdef __cplusplus +extern "C" { +#endif + /** * Gibt alle PCI-Geraete im System zurueck. Die Geraete werden dazu * in die uebergebene Liste eingefuegt. @@ -80,5 +87,9 @@ void cdi_pci_alloc_memory(struct cdi_pci_device* device); */ void cdi_pci_free_memory(struct cdi_pci_device* device); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif diff --git a/include/cdi/scsi.h b/include/cdi/scsi.h index 7b80975..6d12b91 100644 --- a/include/cdi/scsi.h +++ b/include/cdi/scsi.h @@ -74,6 +74,10 @@ struct cdi_scsi_driver { struct cdi_scsi_packet *packet); }; +#ifdef __cplusplus +extern "C" { +#endif + /** * Ein SCSI-Paket allozieren * @@ -112,6 +116,10 @@ void cdi_scsi_driver_register(struct cdi_scsi_driver* driver); */ void cdi_scsi_device_init(struct cdi_scsi_device* device); +#ifdef __cplusplus +}; // extern "C" +#endif + #endif /*\@}*/ diff --git a/include/cdi/storage.h b/include/cdi/storage.h index d19fc6f..4963b68 100644 --- a/include/cdi/storage.h +++ b/include/cdi/storage.h @@ -57,6 +57,9 @@ struct cdi_storage_driver { uint64_t count, void* buffer); }; +#ifdef __cplusplus +extern "C" { +#endif /** * Initialisiert die Datenstrukturen fuer einen Massenspeichertreiber @@ -80,6 +83,9 @@ void cdi_storage_driver_register(struct cdi_storage_driver* driver); */ void cdi_storage_device_init(struct cdi_storage_device* device); +#ifdef __cplusplus +}; // extern "C" +#endif #endif -- 1.6.0.4 From matthew at theiselins.net Thu Dec 10 02:53:49 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Thu, 10 Dec 2009 11:53:49 +1000 Subject: [cdi-devel] [PATCH] Remove dependency on cdi-osdep.h in cdi.h Message-ID: <1260410029-3239-1-git-send-email-matthew@theiselins.net> Hi, This patch removes the dependency on cdi-osdep.h in cdi.h, in order to handle situations where cdi-osdep.h may need to define OS-specific functions and structs which need cdi.h (and other headers which might include cdi.h). Cheers, Matt Signed-off-by: Matthew Iselin --- include/cdi.h | 1 - include/cdi/dma.h | 1 + 2 files changed, 1 insertions(+), 1 deletions(-) diff --git a/include/cdi.h b/include/cdi.h index f45181a..bf72639 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -16,7 +16,6 @@ #include -#include "cdi-osdep.h" #include "cdi/lists.h" typedef enum { diff --git a/include/cdi/dma.h b/include/cdi/dma.h index 8d50640..7b3d159 100644 --- a/include/cdi/dma.h +++ b/include/cdi/dma.h @@ -14,6 +14,7 @@ #include #include #include "cdi.h" +#include "cdi-osdep.h" struct cdi_dma_handle { uint8_t channel; -- 1.6.0.4 From matthew at theiselins.net Fri Dec 11 10:24:42 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Fri, 11 Dec 2009 19:24:42 +1000 Subject: [cdi-devel] [PATCH] CDI.usb, mempool In-Reply-To: <1260388215-7377-1-git-send-email-max@tyndur.org> References: <1260388215-7377-1-git-send-email-max@tyndur.org> Message-ID: On Thu, Dec 10, 2009 at 5:50 AM, wrote: > From: Max Reitz > > * Split the CDI.usb function "cdi_usb_driver_init_and_register" > ?into an init and a register function, because a driver using the > ?new driver registering method (via CDI_DRIVER macros) may not > ?register itself > + mempool as CDI header (has been used in the old USB driver) > > Signed-off-by: Max Reitz Minor nitpick: at some point you may want to document the mempool header. Acked-by: Matthew Iselin From matthew at theiselins.net Fri Dec 11 10:38:43 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Fri, 11 Dec 2009 19:38:43 +1000 Subject: [cdi-devel] [PATCH] USB 1.1 In-Reply-To: <1260389392-11936-1-git-send-email-max@tyndur.org> References: <1260389392-11936-1-git-send-email-max@tyndur.org> Message-ID: Hi, On Thu, Dec 10, 2009 at 6:09 AM, wrote: > From: Max Reitz > > + > + ? ?//EP0 initialisieren > + ? ?device->ep0 = malloc(sizeof(*device->ep0)); > + ? ?device->ep0->endpoint = malloc(sizeof(struct endpoint_desc)); > + ? ?ep0 = (struct endpoint_desc *)device->ep0->endpoint; > + ? ?ep0->length = sizeof(struct endpoint_desc); > + ? ?ep0->descriptor_type = DESC_ENDPOINT; > + ? ?ep0->endpoint_address = 0; > + ? ?ep0->attributes = 0; > + ? ?ep0->max_packet_size = 8; > + ? ?ep0->interval = 0; > + ? ?device->ep0->device = device; > + ? ?device->ep0->data_toggle = 0; Hmm, wouldn't device->ep0 and device->ep0->endpoint leak here: > + > + ? ?if (dev_desc->iManufacturer) { > + ? ? ? ?name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, > + ? ? ? ? ? ?REC_DEVICE, GET_DESCRIPTOR, > + ? ? ? ? ? ?(DESC_STRING << 8) | dev_desc->iManufacturer, 0); > + ? ? ? ?if (name == NULL) { > + ? ? ? ? ? ?return; > + ? ? ? ?} > + ? ? ? ?d1printf(" -> Manufacturer: "); > + ? ? ? ?for (int i = 2; i < name[0]; i += 2) { > + ? ? ? ? ? ?_d1printf("%c", name[i]); > + ? ? ? ?} > + ? ? ? ?_d1printf("\n"); > + ? ?} There doesn't appear to be any memory freed in the case of name == NULL here? Is this intentional? Looks good otherwise! Cheers, Matt From max at tyndur.org Fri Dec 11 16:41:11 2009 From: max at tyndur.org (max at tyndur.org) Date: Fri, 11 Dec 2009 16:41:11 +0100 Subject: [cdi-devel] [PATCH] Memleak in USB 1.1 Message-ID: <1260546071-8964-1-git-send-email-max@tyndur.org> From: Max Reitz ! Fixed the memleak mentioned by Matthew Signed-off-by: Max Reitz --- usb11/control.c | 26 ++++++++++++++++++-------- 1 files changed, 18 insertions(+), 8 deletions(-) diff --git a/usb11/control.c b/usb11/control.c index 4151aa5..d7723c8 100644 --- a/usb11/control.c +++ b/usb11/control.c @@ -34,7 +34,7 @@ static int usb_dev_ids = 1; static void enumerate_hub(struct cdi_usb_device *usbdev); -void usb11_enumerate(struct cdi_usb_device *device) +static int usb11_real_enumerate(struct cdi_usb_device *device) { struct endpoint_desc *ep0; struct device_desc *dev_desc; @@ -71,7 +71,7 @@ void usb11_enumerate(struct cdi_usb_device *device) dev_desc = usb11_do_control(device, DEV_TO_HOST, NULL, 8, STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); if (dev_desc == NULL) { - return; + return 0; } ep0->max_packet_size = dev_desc->max_packet_size0; @@ -102,7 +102,7 @@ void usb11_enumerate(struct cdi_usb_device *device) REC_DEVICE, GET_DESCRIPTOR, (DESC_STRING << 8) | dev_desc->iManufacturer, 0); if (name == NULL) { - return; + return 0; } d1printf(" -> Manufacturer: "); for (int i = 2; i < name[0]; i += 2) { @@ -115,7 +115,7 @@ void usb11_enumerate(struct cdi_usb_device *device) REC_DEVICE, GET_DESCRIPTOR, (DESC_STRING << 8) | dev_desc->iProduct, 0); if (name == NULL) { - return; + return 0; } d1printf(" -> Product: "); for (int i = 2; i < name[0]; i += 2) { @@ -128,13 +128,13 @@ void usb11_enumerate(struct cdi_usb_device *device) sizeof(struct config_desc), STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); if (conf_desc == NULL) { - return; + return 0; } conf_desc = usb11_do_control(device, DEV_TO_HOST, NULL, conf_desc->total_length, STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_CONFIGURATION << 8, 0); if (conf_desc == NULL) { - return; + return 0; } device->config_desc = conf_desc; usb11_do_control(device, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, @@ -144,7 +144,7 @@ void usb11_enumerate(struct cdi_usb_device *device) REC_DEVICE, GET_DESCRIPTOR, (DESC_STRING << 8) | conf_desc->iConfiguration, 0); if (name == NULL) { - return; + return 0; } d1printf("Using configuration "); for (int i = 2; i < name[0]; i += 2) { @@ -174,7 +174,7 @@ void usb11_enumerate(struct cdi_usb_device *device) REC_DEVICE, GET_DESCRIPTOR, (DESC_STRING << 8) | best_if->iInterface, 0); if (name == NULL) { - return; + return 0; } d1printf("Using interface "); for (int i = 2; i < name[0]; i += 2) { @@ -198,6 +198,16 @@ void usb11_enumerate(struct cdi_usb_device *device) if (best_if->interface_class == USBDC_HUB) { enumerate_hub(device); } + + return 1; +} + +void usb11_enumerate(struct cdi_usb_device *device) +{ + if (!usb11_real_enumerate(device)) { + free(device->ep0->endpoint); + free(device->ep0); + } } static void reset_hub_device(struct cdi_usb_device *usbdev) -- 1.6.4.2 From kevin at tyndur.org Fri Dec 11 22:53:39 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Fri, 11 Dec 2009 22:53:39 +0100 Subject: [cdi-devel] [PATCH] CDI.usb, mempool In-Reply-To: References: <1260388215-7377-1-git-send-email-max@tyndur.org> Message-ID: <20091211215338.GA11303@silvanus> On Fri, Dec 11, 2009 at 07:24:42PM +1000, Matthew Iselin wrote: > On Thu, Dec 10, 2009 at 5:50 AM, wrote: > > From: Max Reitz > > > > * Split the CDI.usb function "cdi_usb_driver_init_and_register" > > ?into an init and a register function, because a driver using the > > ?new driver registering method (via CDI_DRIVER macros) may not > > ?register itself > > + mempool as CDI header (has been used in the old USB driver) > > > > Signed-off-by: Max Reitz > > Minor nitpick: at some point you may want to document the mempool header. > > Acked-by: Matthew Iselin This is actually not a minor nitpick, but a good reason to reject a patch right away. I'll consider the patch as an exception only because this interface is going to be changed anyway when we introduce cdi_mem_area as discussed in the audio/IDC thread. Kevin From kevin at tyndur.org Fri Dec 11 23:17:15 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Fri, 11 Dec 2009 23:17:15 +0100 Subject: [cdi-devel] [PATCH] Update cdi-osdep.h to contain OS-specific structs for PCI and DMA In-Reply-To: <1260402731-1489-1-git-send-email-matthew@theiselins.net> References: <1260402731-1489-1-git-send-email-matthew@theiselins.net> Message-ID: <20091211221715.GB11303@silvanus> On Thu, Dec 10, 2009 at 09:52:10AM +1000, Matthew Iselin wrote: > > Signed-off-by: Matthew Iselin Added some hunks from the other patches which belong here logically (like introducing fields of the new types in existing structs) and German comments. Looks fine otherwise and is committed now. > --- > include/cdi-osdep.h | 20 +++++++++++++++++++- > 1 files changed, 19 insertions(+), 1 deletions(-) > > diff --git a/include/cdi-osdep.h b/include/cdi-osdep.h > index 6227e91..2095580 100644 > --- a/include/cdi-osdep.h > +++ b/include/cdi-osdep.h > @@ -26,10 +26,28 @@ > * the driver with the CDI library. > * > * @param name Name of the driver > - * @param drv Pointer to a driver description (struct cdi_driver*) > + * @param drv A driver description (struct cdi_driver) > * @param deps List of names of other drivers on which this driver depends > * \endenglish > */ > #define CDI_DRIVER(name, drv, deps...) /* TODO */ > > +/** > + * \english > + * OS-specific PCI data. > + * \endenglish > + */ > +typedef struct > +{ > +} cdi_pci_device_osdep; > + > +/** > + * \english > + * OS-specific DMA data. > + * \endenglish > + */ > +typedef struct > +{ > +} cdi_dma_osdep; > + > #endif > -- > 1.6.0.4 > > _______________________________________________ > cdi-devel mailing list > cdi-devel at tyndur.org > http://list.tyndur.org/cgi-bin/mailman/listinfo/cdi-devel From max at tyndur.org Fri Dec 11 23:19:35 2009 From: max at tyndur.org (max at tyndur.org) Date: Fri, 11 Dec 2009 23:19:35 +0100 Subject: [cdi-devel] [PATCH] English comments for usb.h and usb-hcd.h Message-ID: <1260569975-7394-1-git-send-email-max@tyndur.org> From: Max Reitz + Added english comments for those two files Signed-off-by: Max Reitz --- include/cdi/usb-hcd.h | 138 ++++++++-- include/cdi/usb.h | 745 +++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 740 insertions(+), 143 deletions(-) diff --git a/include/cdi/usb-hcd.h b/include/cdi/usb-hcd.h index a5fcc51..10e8bd4 100644 --- a/include/cdi/usb-hcd.h +++ b/include/cdi/usb-hcd.h @@ -7,7 +7,13 @@ struct cdi_usb_hc { struct cdi_device dev; - ///USB-Geschwindigkeit + /** + * \if german + * USB-Geschwindigkeit + * \elseif english + * USB speed + * \endif + */ cdi_usb_speed_t speed; }; @@ -15,46 +21,130 @@ struct cdi_usb_hcd { struct cdi_driver drv; /** - * Listet alle Geraete auf, die an den Rootports haengen und ruft ggf. - * cdi_usb_device_init auf - * - * @param device Der Hostcontroller - */ + * \if german + * + * Listet alle Geraete auf, die an den Rootports haengen und ruft ggf. + * cdi_usb_device_init auf + * + * @param device Der Hostcontroller + * + * \elseif english + * + * Lists all devices hanging at the root ports and calls (if necessary) + * cdi_usb_device_init + * + * @param device The host controller + * + * \endif + */ void (*find_devices)(struct cdi_usb_hc* device); /** - * Aktiviert den zum angegebenen Geraet gehoerigen Rootport - * - * @param usb_device Das USB-Geraet (muss an einem Rootport haengen) - */ + * \if german + * + * Aktiviert den zum angegebenen Geraet gehoerigen Rootport + * + * @param usb_device Das USB-Geraet (muss an einem Rootport haengen) + * + * \elseif english + * + * Activates the root port corresponding to the device + * + * @param usb_device The USB device (must be located at a root port) + * + * \endif + */ void (*activate_device)(struct cdi_usb_device* usb_device); /** - * Verarbeitet USB-Pakete - * - * @param packets Array zu sendender Pakete - * @param num_packets Anzahl der Pakete - * - * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) - */ + * \if german + * + * Verarbeitet USB-Pakete + * + * @param packets Array zu sendender Pakete + * @param num_packets Anzahl der Pakete + * + * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) + * + * \elseif english + * + * Sends USB packets + * + * @param packets Array of packages to be sent + * @param num_packages Number of packages + * + * \endif + */ cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, size_t num_packets); /** - * Erstellt eine neue Pipe - * - * @param pipe Die Pipe - */ + * \if german + * + * Erstellt eine neue Pipe + * + * @param pipe Die Pipe + * + * \elseif english + * + * Creates a new pipe + * + * @param pipe The pipe + * + * \endif + */ void (*add_pipe)(struct cdi_usb_pipe* pipe); }; -/// Initialisiert einen HCD +/** + * \if german + * + * Initialisiert einen HCD + * + * @param driver Der HCD + * + * \elseif english + * + * Initializes an HCD + * + * @param driver The HCD + * + * \endif + */ void cdi_usb_hcd_init(struct cdi_usb_hcd* driver); -/// Gibt einen HCD frei +/** + * \if german + * + * Gibt einen HCD frei + * + * @param driver Freizugebender HCD + * + * \elseif english + * + * Removes an HCD + * + * @param driver HCD to be removed + * + * \endif + */ void cdi_usb_hcd_destroy(struct cdi_usb_hcd* driver); -/// Registriert einen HCD +/** + * \if german + * + * Registriert einen HCD + * + * @param driver Zu registrierender HCD + * + * \elseif english + * + * Registers an HCD + * + * @param driver HCD to be registered + * + * \endif + */ void cdi_usb_hcd_register(struct cdi_usb_hcd* driver); #endif diff --git a/include/cdi/usb.h b/include/cdi/usb.h index c4406c7..76723fb 100644 --- a/include/cdi/usb.h +++ b/include/cdi/usb.h @@ -16,32 +16,102 @@ typedef enum { } cdi_usb_speed_t; typedef enum { - /// SETUP-Paket + /** + * \if german + * SETUP-Paket + * \elseif english + * SETUP packet + * \endif + */ CDI_USB_PACKET_SETUP = 0x2D, - /// Daten vom Geraet zum Host + + /** + * \if german + * Daten vom Geraet zum Host + * \elseif english + * Data from the device to the host + * \endif + */ CDI_USB_PACKET_IN = 0x69, - /// Daten vom Host zum Geraet + + /** + * \if german + * Daten vom Host zum Geraet + * \elseif english + * Data from the host to the device + * \endif + */ CDI_USB_PACKET_OUT = 0xE1, } cdi_usb_packet_type_t; typedef enum { - /// Kein Fehler + /** + * \if german + * Kein Fehler + * \elseif english + * No error + * \endif + */ CDI_USB_NO_ERROR = 0x0000, + /// Stalled CDI_USB_STALLED = 0x0001, - /// Bufferfehler (Overflow, underrun, ...) + + /** + * \if german + * Bufferfehler (Overflow, underrun, ...) + * \elseif english + * Buffer error (overflow, underrun, ...) + * \endif + */ CDI_USB_BUFFER_ERROR = 0x0002, - /// Babble (Geraet hat zu viele Daten gesendet) + + /** + * \if german + * Babble (Geraet hat zu viele Daten gesendet) + * \elseif english + * Babble (device has sent more data than expected) + * \endif + */ CDI_USB_BABBLE = 0x0004, - /// NAK (Daten sind erneut zu senden) + + /** + * \if german + * NAK (Daten sind erneut zu senden) + * \elseif english + * NAK (data has to be sent again) + * \endif + */ CDI_USB_NAK = 0x0008, - /// CRC-Fehler + + /** + * \if german + * CRC-Fehler + * \elseif english + * CRC error + * \endif + */ CDI_USB_CRC = 0x0010, + /// Timeout CDI_USB_TIMEOUT = 0x0020, - /// Bitstuff/-destuff-Fehler + + /** + * \if german + * Bitstuff/-destuff-Fehler + * \elseif english + * bitstuff/-destuff error + * \endif + */ CDI_USB_BITSTUFF = 0x0040, - /// Reservieren von Speicher fehlgeschlagen, ... + + /** + * \if german + * Reservieren von Speicher fehlgeschlagen, ... + * \elseif english + * Allocating of memory failed, ... + * \endif + */ CDI_USB_TRIVIAL_ERROR = 0x0080, } cdi_usb_status_t; @@ -51,29 +121,76 @@ struct cdi_usb_interface; struct cdi_usb_driver; struct cdi_usb_device { - /// Der USB-Bustreiber + /** + * \if german + * Der USB-Bustreiber + * \elseif english + * The USB bus driver + * \endif + */ struct cdi_usb_driver* drv; - /// Der Hostcontroller, an dem das Geraet im Endeffekt haengt + + /** + * \if german + * Der Hostcontroller, an dem das Geraet im Endeffekt haengt + * \elseif english + * The host controller where the device is eventually located + * \endif + */ struct cdi_usb_hc* hc; + /** - * Haengt das Geraet an einem Hub, dann ist dieses Feld ein Pointer auf - * das entsprechende USB-Geraet, haengt es an einem Rootport, dann ist - * dieses Feld NULL. - */ + * \if german + * Haengt das Geraet an einem Hub, dann ist dieses Feld ein Pointer auf + * das entsprechende USB-Geraet, haengt es an einem Rootport, dann ist + * dieses Feld NULL. + * \elseif english + * If the device is located at a hub, then this field contains a pointer to + * the corresponding USB device, if it's located at a root port, then this + * field is set to NULL. + * \endif + */ struct cdi_usb_device* hub; - /// Geschwindigkeit(-en) dieses Geraets + + /** + * \if german + * Geschwindigkeit(-en) dieses Geraets + * \elseif english + * Speed(-s) supported by this device + * \endif + */ cdi_usb_speed_t speed; + /// ID int id; - /// Entweder der Rootport oder der Port des Hubs, an dem das Geraet haengt + + /** + * \if german + * Entweder der Rootport oder der Port des Hubs, an dem das Geraet haengt + * \elseif english + * Either the root port or the port hub's port where the device is located + * \endif + */ int port; - /// Hier darf der Geraetetreiber seine Daten unterbringen + + /** + * \if german + * Hier darf der Geraetetreiber seine Daten unterbringen + * \elseif english + * The device driver may store its data here + * \endif + */ void* driver_data; /** - * Felder, die so auch bei PCI existieren: Vendor-, Device-, Class-, - * Subclass- und Protocol-ID - */ + * \if german + * Felder, die so auch bei PCI existieren: Vendor-, Device-, Class-, + * Subclass- und Protocol-ID + * \elseif english + * Fields existing at PCI, too: vendor, device, class, subclass and + * protocol ID. + * \endif + */ int vendor_id; int device_id; int class_id; @@ -81,46 +198,117 @@ struct cdi_usb_device { int protocol_id; /** - * Setzt das USB-Geraet zurueck (kann sowohl vom HCD als auch vom Hub - * driver ausgefuehrt werden) - * - * @param usb_device Das Geraet - */ + * \if german + * + * Setzt das USB-Geraet zurueck (kann sowohl vom HCD als auch vom Hub + * driver ausgefuehrt werden) + * + * @param usb_device Das Geraet + * + * \elseif english + * + * Resets the USB device (may be executed either by the HCD or the hub + * driver) + * + * @param usb_device The device + * + * \endif + */ void (*reset)(struct cdi_usb_device* usb_device); - /// Ziemlich lowlevelig. Gehoert das hier wirklich rein? - /// Im einzelnen sind das: Pipe fuer Endpoint 0 + /** + * \if german + * Ziemlich lowlevelig. Gehoert das hier wirklich rein? + * \elseif english + * Very low level. Does it really fit in here? + * \endif + */ + + /** + * \if german + * Im einzelnen sind das: Pipe fuer Endpoint 0 + * \elseif english + * In particular these things are: Pipe for endpoint 0 + * \endif + */ struct cdi_usb_pipe* ep0; + /// Device descriptor void* device_desc; + /// Configuration descriptor void* config_desc; + /// Interface descriptor struct cdi_usb_interface* interface_desc; + /// Class descriptor void* class_desc; }; enum cdi_usb_toggle { - /// Das Toggle nehmen, das jetzt dran ist + /** + * \if german + * Das Toggle nehmen, das jetzt dran ist + * \elseif english + * Use the toggle to be used right now + * \endif + */ TOGGLE_UNSPECIFIC = 0, - /// Geloeschtes Togglebit erzwingen + + /** + * \if german + * Geloeschtes Togglebit erzwingen + * \elseif english + * Force unset toggle bit + * \endif + */ TOGGLE_0 = 1, - /// Gesetztes Togglebit erzwingen + + /** + * \if german + * Gesetztes Togglebit erzwingen + * \elseif english + * Force set toggle bit + * \endif + */ TOGGLE_1 = 2 }; struct cdi_usb_endpoint { /// sizeof(struct cdi_usb_endpoint) uint8_t length; + /// 5 uint8_t descriptor_type; - /// Nummer des Endpoints + + /** + * \if german + * Nummer des Endpoints + * \elseif english + * Endpoint's number + * \endif + */ uint8_t endpoint_address; - /// Eigenschaften (IN/OUT, Bulk/Interrupt/..., ...) + + /** + * \if german + * Eigenschaften (IN/OUT, Bulk/Interrupt/..., ...) + * \elseif english + * Attributes (IN/OUT, bulk/interrupt/..., ...) + * \endif + */ uint8_t attributes; - /// Maximale Paketgroesse in Bytes + + /** + * \if german + * Maximale Paketgroesse in Bytes + * \elseif english + * Maximum packet size in bytes + * \endif + */ uint16_t max_packet_size; + /// ??? uint8_t interval; } __attribute__((packed)); @@ -128,96 +316,311 @@ struct cdi_usb_endpoint { struct cdi_usb_interface { /// sizeof(struct cdi_usb_interface) uint8_t length; + /// 4 uint8_t descriptor_type; - /// Nummer des Interfaces + + /** + * \if german + * Nummer des Interface + * \elseif english + * Interface's number + * \endif + */ uint8_t interface_number; + /// ??? uint8_t alternate_setting; - /// Anzahl der Endpoints fuer dieses Interface + + /** + * \if german + * Anzahl der Endpoints fuer dieses Interface + * \elseif english + * Number of endpoints for this interface + * \endif + */ uint8_t num_endpoints; - /// Class-ID (bei Composite Devices) + + /** + * \if german + * Class-ID (bei Composite Devices) + * \elseif english + * Class ID (used with composite devices) + * \endif + */ uint8_t interface_class; + /// Subclass-ID uint8_t interface_subclass; + /// Protocol-ID uint8_t interface_protocol; - /// String-Index, der das Interface beschreibt + + /** + * \if german + * String-Index, der das Interface beschreibt + * \elseif english + * String index describing the interface + * \endif + */ uint8_t iInterface; } __attribute__((packed)); -/// Beschreibt eine Pipe +/** + * \if german + * Beschreibt eine Pipe + * \elseif english + * Describes a pipe + * \endif + */ struct cdi_usb_pipe { - /// Ziel + /** + * \if german + * Ziel + * \elseif english + * Destination + * \endif + */ struct cdi_usb_device* device; - /// Ein Pointer auf eine den Endpoint beschreibende Struktur - /// TODO Das ist eigentlich auch sehr lowlevelig. + + /** + * \if german + * Ein Pointer auf eine den Endpoint beschreibende Struktur + * TODO Das ist eigentlich auch sehr lowlevelig. + * \elseif english + * A pointer to a structure describing the endpoint + * TODO This is on a very low level, too. + */ struct cdi_usb_endpoint* endpoint; + /// Data-Toggle int data_toggle; }; -/** Beschreibt ein USB-Paket */ +/** + * \if german + * Beschreibt ein USB-Paket + * \elseif english + * Describes a USB packet + * \endif + */ struct cdi_usb_packet { - /// Die gewuenschte Pipe + /** + * \if german + * Die gewuenschte Pipe + * \elseif english + * The desired pipe + * \endif + */ struct cdi_usb_pipe* pipe; - /// Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) + + /** + * \if german + * Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) + * \elseif english + * Type of packet (PACKET_IN, PACKET_OUT, PACKET_SETUP) + * \endif + */ cdi_usb_packet_type_t type; - /// Zeiger auf den Datenpuffer + + /** + * \if german + * Zeiger auf den Datenpuffer + * \elseif english + * Pointer to the data buffer + * \endif + */ void* data; - /// Die Laenge des Puffers + + /** + * \if german + * Die Laenge des Puffers + * \elseif english + * The buffer's length + * \endif + */ size_t length; - /// Fehlertyp (oder USB_NO_ERROR) + + /** + * \if german + * Fehlertyp (oder USB_NO_ERROR) + * \elseif english + * Error type (or USB_NO_ERROR) + * \endif + */ cdi_usb_status_t condition; - /// Bestimmtes Data-Toggle benuten + + /** + * \if german + * Bestimmtes Data-Toggle benuten + * \elseif english + * Use specific data toggle + * \endif + */ enum cdi_usb_toggle use_toggle; }; typedef enum { - /// Host -> Geraet + /** + * \if german + * Host -> Geraet + * \elseif english + * Host -> device + * \endif + */ CDI_USB_CONTROL_OUT = 0x00, - /// Geraet -> Host + + /** + * \if german + * Geraet -> Host + * \elseif english + * Device -> host + * \endif + */ CDI_USB_CONTROL_IN = 0x80 } cdi_usb_control_direction_t; typedef enum { - /// Standard-Request + /** + * \if german + * Standard-Request + * \elseif english + * Standard request + * \endif + */ CDI_USB_CONTROL_RQ_STANDARD = 0x00, - /// Klassenspezifischer Request + + /** + * \if german + * Klassenspezifischer Request + * \elseif english + * Class specific request + * \endif + */ CDI_USB_CONTROL_RQ_CLASS = 0x20, - /// Herstellerspezifischer Request + + /** + * \if german + * Herstellerspezifischer Request + * \elseif english + * Vendor specific request + * \endif + */ CDI_USB_CONTROL_RQ_VENDOR = 0x40 } cdi_usb_control_request_type_t; typedef enum { - /// Empfaenger: Geraet + /** + * \if german + * Empfaenger: Geraet + * \elseif english + * Recipient: device + * \endif + */ CDI_USB_CONTROL_REC_DEVICE = 0x00, - /// Empfaenger: Interface + + /** + * \if german + * Empfaenger: Interface + * \elseif english + * Recipient: interface + * \endif + */ CDI_USB_CONTROL_REC_INTERFACE = 0x01, - /// Empfaenger: Endpoint + + /** + * \if german + * Empfaenger: Endpoint + * \elseif english + * Recipient: endpoint + * \endif + */ CDI_USB_CONTROL_REC_ENDPOINT = 0x02, - /// Empfaenger ist jemand anderes + + /** + * \if german + * Empfaenger ist jemand anderes + * \elseif english + * Recipient is another one + * \endif + */ CDI_USB_CONTROL_REC_OTHER = 0x03 } cdi_usb_control_recipient_t; struct cdi_usb_control_transfer { - /// Wenn Daten zum Geraet gesendet werden sollen, dann ist hier der - /// Speicherbereich anzugeben - void *buffer; - /// Laenge der erwarteten bzw. zu sendenden Daten + /** + * \if german + * Wenn Daten zum Geraet gesendet werden sollen, dann ist hier der + * Speicherbereich anzugeben + * \elseif english + * If data has to be sent to the device, then the memory buffer has to be + * given here + * \endif + */ + void* buffer; + + /** + * \if german + * Laenge der erwarteten bzw. zu sendenden Daten + * \elseif english + * Length of the expected data respectively the data to be sent + * \endif + */ size_t length; - /// Richtung + + /** + * \if german + * Richtung + * \elseif english + * Direction + * \endif + */ cdi_usb_control_direction_t direction; - /// Art des Requests (Standardrequest, klassenspezifisch, - /// herstellerspezifisch) + + /** + * \if german + * Art des Requests (Standardrequest, klassenspezifisch, + * herstellerspezifisch) + * \elseif english + * Request type (standard request, class specific, vendor specific) + * \endif + */ cdi_usb_control_request_type_t rtype; - /// Ziel (Device, Interface, Endpoint, anderes) + + /** + * \if german + * Ziel (Device, Interface, Endpoint, anderes) + * \elseif english + * Recipient (device, interface, endpoint, other) + * \endif + */ cdi_usb_control_recipient_t recipient; - /// Requestcode (Je nach rtype und recipient) + + /** + * \if german + * Requestcode (Je nach rtype und recipient) + * \elseif english + * Request code (specific to rtype and recipient) + * \endif + */ int request; - /// Ein zu sendender Wert + + /** + * \if german + * Ein zu sendender Wert + * \elseif english + * A value to be sent + * \endif + */ int value; - /// Ein zu sendender Index + + /** + * \if german + * Ein zu sendender Index + * \elseif english + * An index to be sent + * \endif + */ int index; }; @@ -226,47 +629,98 @@ struct cdi_usb_control_transfer { struct cdi_usb_driver { struct cdi_driver drv; - /// Unterstuetzte Geschwindigkeiten + /** + * \if german + * Unterstuetzte Geschwindigkeiten + * \elseif english + * Supported speeds + * \endif + */ cdi_usb_speed_t speeds; /** - * Enumeriert ein USB-Geraet - * - * @param device Das zu enumerierende Geraet - */ + * \if german + * + * Enumeriert ein USB-Geraet + * + * @param device Das zu enumerierende Geraet + * + * \elseif english + * + * Enumerates a USB device + * + * @param device The device to be enumerated + * + * \endif + */ void (*enumerate_device)(struct cdi_usb_device* device); /** - * Sendet USB-Pakete - * - * @param packets Array zu sendender Pakete - * @param num_packets Anzahl der Pakete - * - * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) - */ + * \if german + * + * Sendet USB-Pakete + * + * @param packets Array zu sendender Pakete + * @param num_packets Anzahl der Pakete + * + * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) + * + * \elseif english + * + * Sends USB packets + * + * @param packets Array of packets to be sent + * @param num_packets Number of packets + * + * @return Returns the status (i. e. USB_NO_ERROR to indicate no error) + * + * \endif + */ cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, size_t num_packets); /** - * Fuehrt einen Control-Transfer durch - * - * @param device Das Zielgeraet - * @param transfer Eine den Transfer beschreibende Struktur - * - * @return Gibt einen Puffer zurueck, der die ausgetauschen Daten enthaelt, - * im Fehlerfall hingegen NULL, gab es keine Daten, dann - * (void *)0xFFFFFFFF. - */ + * \if german + * + * Fuehrt einen Control-Transfer durch + * + * @param device Das Zielgeraet + * @param transfer Eine den Transfer beschreibende Struktur + * + * @return Gibt einen Puffer zurueck, der die ausgetauschen Daten enthaelt, + * im Fehlerfall hingegen NULL, gab es keine Daten, dann + * (void *)0xFFFFFFFF. + * + * \elseif english + * + * Performs a control transfer + * + * @param device The destination device + * @param transfer A structure describing this transfer + * + * @return Returns a buffer containing the transferred data, in case of an + * error the value is NULL and if there was no data, it returns + * (void *)0xFFFFFFFF. + * + * \endif + */ void* (*do_control)(struct cdi_usb_device* device, struct cdi_usb_control_transfer* transfer); }; /** - * Ein USB-Geraetetreiber uebergibt dem Betriebssystem eine solche Struktur, - * damit ein bestimmter Handler beim Auffinden solcher Geraete aufgerufen wird. - * Jedes Feld enthaelt entweder eine Zahl, die mit der des Geraetes - * uebereinstimmen muss oder -1, dann wird dieses Feld ignoriert. - */ + * \if german + * Ein USB-Geraetetreiber uebergibt dem Betriebssystem eine solche Struktur, + * damit ein bestimmter Handler beim Auffinden solcher Geraete aufgerufen wird. + * Jedes Feld enthaelt entweder eine Zahl, die mit der des Geraetes + * uebereinstimmen muss oder -1, dann wird dieses Feld ignoriert. + * \elseif english + * A USB device driver passes this structure to the OS so that a specific + * handler is called upon the detection of such a device. Each field either + * contains a number that has to be equal to that of the driver or -1, so that + * the field will be ignored. + * \endif + */ struct cdi_usb_device_pattern { int vendor_id; int device_id; @@ -275,44 +729,97 @@ struct cdi_usb_device_pattern { int protocol_id; /** - * Ueberprueft das angegebene USB-Geraet - * - * @param device Das Geraet - * - * @return 0, wenn dieses Geraet nicht vom Treiber verarbeitet werden kann, - * sonst 1 - */ + * \if german + * + * Ueberprueft das angegebene USB-Geraet + * + * @param device Das Geraet + * + * @return 0, wenn dieses Geraet nicht vom Treiber verarbeitet werden kann, + * sonst 1 + * + * \elseif english + * + * Checks the given USB device + * + * @param device The device + * + * @return 0 if the driver is unable to handle this device, else 1 + * + * \endif + */ int (*take_usb_device)(struct cdi_usb_device* device); }; void cdi_usb_driver_destroy(struct cdi_usb_driver* driver); /** - * Initialisiert einen USB-Bustreiber - * - * @param driver Der Treiber - */ + * \if german + * + * Initialisiert einen USB-Bustreiber + * + * @param driver Der Treiber + * + * \elseif english + * + * Initializes a USB bus driver + * + * @param driver The driver + * + * \endif + */ void cdi_usb_driver_init(struct cdi_usb_driver* driver); /** - * Registriert einen USB-Bustreiber beim System - * - * @param driver Der Treiber - */ + * \if german + * + * Registriert einen USB-Bustreiber beim System + * + * @param driver Der Treiber + * + * \elseif english + * + * Registers a USB bus driver at the OS + * + * @param driver The driver + * + * \endif + */ void cdi_usb_driver_register(struct cdi_usb_driver* driver); /** - * Registriert ein gefundenes USB-Geraet beim Betriebssystem - * - * @param device Das zu registrierende Geraet - */ + * \if german + * + * Registriert ein gefundenes USB-Geraet beim Betriebssystem - diese Funktion + * wird z. B. vom Host-Controller-Treiber nach einem Aufruf von find_devices + * fuer jedes gefundene Geraet aufgerufen + * + * @param device Das zu registrierende Geraet + * + * \elseif english + * + * Registers a detected USB device at the OS - this function is called i. e. by + * the host controller driver after a find_devices call for every device found + * + * @param device The device to be registered + */ void cdi_usb_device_init(struct cdi_usb_device* device); /** - * Registriert ein Geraetemuster - * - * @param pattern Das Muster - */ + * \if german + * + * Registriert ein Geraetemuster + * + * @param pattern Das Muster + * + * \elseif english + * + * Registers a device pattern + * + * @param pattern The pattern + * + * \endif + */ void cdi_usb_register_device_pattern(struct cdi_usb_device_pattern* pattern); #endif -- 1.6.4.2 From kevin at tyndur.org Fri Dec 11 23:20:20 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Fri, 11 Dec 2009 23:20:20 +0100 Subject: [cdi-devel] [PATCH] Add extern "C" to header files. In-Reply-To: <1260402731-1489-2-git-send-email-matthew@theiselins.net> References: <1260402731-1489-1-git-send-email-matthew@theiselins.net> <1260402731-1489-2-git-send-email-matthew@theiselins.net> Message-ID: <20091211222019.GC11303@silvanus> On Thu, Dec 10, 2009 at 09:52:11AM +1000, Matthew Iselin wrote: > NOTE: This patch depends on the "Update cdi-osdep.h to contain OS-specific structs for PCI and DMA" commit. > > Signed-off-by: Matthew Iselin Thanks, applied. When you send patches that depend on previous patches, you best send them as a proper patch series (e.g. "git format-patch -n -2 --cover-letter" and then "git send-email *"). The subject of the mails looks like [PATCH 1/2] then and makes it clear that the patches need to be applied in a specific order. From kevin at tyndur.org Fri Dec 11 23:26:10 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Fri, 11 Dec 2009 23:26:10 +0100 Subject: [cdi-devel] [PATCH] Remove dependency on cdi-osdep.h in cdi.h In-Reply-To: <1260410029-3239-1-git-send-email-matthew@theiselins.net> References: <1260410029-3239-1-git-send-email-matthew@theiselins.net> Message-ID: <20091211222610.GD11303@silvanus> On Thu, Dec 10, 2009 at 11:53:49AM +1000, Matthew Iselin wrote: > Hi, > > This patch removes the dependency on cdi-osdep.h in cdi.h, in order to handle situations where cdi-osdep.h may need to define OS-specific functions and structs which need cdi.h (and other headers which might include cdi.h). I'm hesitant to take this one. It means that driver need to include cdi-osdep.h explicitly to get CDI_DRIVER, CDI_STANDALONE and so on. Is there a real need for Pedigree to have this? If you just need to reference struct cdi_driver you could still add a forward declaration to cdi-osdep.h. Kevin From max at tyndur.org Fri Dec 11 23:36:21 2009 From: max at tyndur.org (Max Reitz) Date: Fri, 11 Dec 2009 23:36:21 +0100 Subject: [cdi-devel] [PATCH] Remove dependency on cdi-osdep.h in cdi.h In-Reply-To: <20091211222610.GD11303@silvanus> References: <1260410029-3239-1-git-send-email-matthew@theiselins.net> <20091211222610.GD11303@silvanus> Message-ID: <4B22C965.6080600@tyndur.org> Kevin Wolf wrote: > I'm hesitant to take this one. It means that driver need to include > cdi-osdep.h explicitly to get CDI_DRIVER, CDI_STANDALONE and so on. Do they really? Couldn't one just include cdi-osdep.h in cdi.h or cdi/misc.h? (Just asking) From matthew at theiselins.net Sat Dec 12 00:11:43 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Sat, 12 Dec 2009 09:11:43 +1000 Subject: [cdi-devel] [PATCH] Remove dependency on cdi-osdep.h in cdi.h In-Reply-To: <20091211222610.GD11303@silvanus> References: <1260410029-3239-1-git-send-email-matthew@theiselins.net> <20091211222610.GD11303@silvanus> Message-ID: Hi, > I'm hesitant to take this one. It means that driver need to include > cdi-osdep.h explicitly to get CDI_DRIVER, CDI_STANDALONE and so on. > > Is there a real need for Pedigree to have this? If you just need to > reference struct cdi_driver you could still add a forward declaration > to cdi-osdep.h. You are correct. This patch is now no longer required. Cheers, Matt From matthew at theiselins.net Sat Dec 12 00:45:37 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Sat, 12 Dec 2009 09:45:37 +1000 Subject: [cdi-devel] [PATCH] Remove dependency on cdi-osdep.h in cdi.h In-Reply-To: <4B22C965.6080600@tyndur.org> References: <1260410029-3239-1-git-send-email-matthew@theiselins.net> <20091211222610.GD11303@silvanus> <4B22C965.6080600@tyndur.org> Message-ID: Hi, > Do they really? Couldn't one just include cdi-osdep.h in cdi.h or > cdi/misc.h? (Just asking) Yes, but this patch's intent was to remove that dependency (ie, no #include "cdi-osdep.h" in cdi.h). As Kevin pointed out, forward declaring struct cdi_driver is a better solution anyway. Cheers, Matt From kevin at tyndur.org Sat Dec 12 17:34:36 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2009 17:34:36 +0100 Subject: [cdi-devel] [PATCH] USB 1.1 In-Reply-To: <1260389392-11936-1-git-send-email-max@tyndur.org> References: <1260389392-11936-1-git-send-email-max@tyndur.org> Message-ID: <20091212163435.GA20637@silvanus> On Wed, Dec 09, 2009 at 09:09:52PM +0100, max at tyndur.org wrote: > From: Max Reitz > > + Added USB 1.1 driver based on CDI.usb > > Signed-off-by: Max Reitz Doesn't compile for me: kevin at silvanus:~/entwicklung/cdi> LANG=C make CC usb11/control.c usb11/control.c:25:22: error: cdi/list.h: No such file or directory usb11/control.c: In function 'usb11_enumerate': usb11/control.c:108: error: 'for' loop initial declaration used outside C99 mode usb11/control.c:121: error: 'for' loop initial declaration used outside C99 mode usb11/control.c:150: error: 'for' loop initial declaration used outside C99 mode usb11/control.c:158: error: 'for' loop initial declaration used outside C99 mode usb11/control.c:180: error: 'for' loop initial declaration used outside C99 mode make: *** [usb11/control.o] Error 1 From max at tyndur.org Sat Dec 12 18:45:50 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 12 Dec 2009 18:45:50 +0100 Subject: [cdi-devel] [PATCH] Makefile Message-ID: <1260639957-11307-1-git-send-email-max@tyndur.org> From: Max Reitz * Use C99 (better: gnu99) instead of implicit C89 * Ignore errors upon make clean Signed-off-by: Max Reitz --- Makefile | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3d60c79..5360efb 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ SRCS = $(shell find -name '*.c') OBJS = $(addsuffix .o,$(basename $(SRCS))) CC = gcc -CFLAGS = -m32 -Wall -g -I include -Werror -D_GNU_SOURCE +CFLAGS = -m32 -Wall -g -I include -Werror -D_GNU_SOURCE -std=gnu99 run_command = $(if $(V),$(2), at echo $(1);$(2)) @@ -12,7 +12,7 @@ all: $(OBJS) $(call run_command,"CC $^",$(CC) $(CFLAGS) $(addprefix -I,$(addprefix $(dir $^),. include)) -c -o $@ $^) clean: - rm $(OBJS) + -rm $(OBJS) 2> /dev/null doc: for lang in german english; do \ -- 1.6.4.2 From max at tyndur.org Sat Dec 12 18:47:09 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 12 Dec 2009 18:47:09 +0100 Subject: [cdi-devel] [PATCH] Direct access to PCI configuration space Message-ID: <1260640035-11473-1-git-send-email-max@tyndur.org> From: Max Reitz + Some drivers need direct access to the PCI configuration space Signed-off-by: Max Reitz --- include/cdi/pci.h | 12 ++++++++++++ 1 files changed, 12 insertions(+), 0 deletions(-) diff --git a/include/cdi/pci.h b/include/cdi/pci.h index 6b1e6f0..4bf38ea 100644 --- a/include/cdi/pci.h +++ b/include/cdi/pci.h @@ -87,6 +87,18 @@ void cdi_pci_alloc_memory(struct cdi_pci_device* device); */ void cdi_pci_free_memory(struct cdi_pci_device* device); +/** + * Signalisiert CDI-Treibern Zugriff auf den PCI-Konfigurationsraum + */ +#define CDI_PCI_DIRECT_ACCESS + +uint8_t cdi_pci_config_inb(struct cdi_pci_device *device, int offset); +uint16_t cdi_pci_config_inw(struct cdi_pci_device *device, int offset); +uint32_t cdi_pci_config_inl(struct cdi_pci_device *device, int offset); +void cdi_pci_config_outb(struct cdi_pci_device *device, int offset, uint8_t value); +void cdi_pci_config_outw(struct cdi_pci_device *device, int offset, uint16_t value); +void cdi_pci_config_outl(struct cdi_pci_device *device, int offset, uint32_t value); + #ifdef __cplusplus }; // extern "C" #endif -- 1.6.4.2 From max at tyndur.org Sat Dec 12 18:47:10 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 12 Dec 2009 18:47:10 +0100 Subject: [cdi-devel] [PATCH 1/6] Mempool In-Reply-To: <1260640035-11473-1-git-send-email-max@tyndur.org> References: <1260640035-11473-1-git-send-email-max@tyndur.org> Message-ID: <1260640035-11473-2-git-send-email-max@tyndur.org> From: Max Reitz + Added mempool header written by Kevin as a temporary solution for the USB host controller drivers coming soon Signed-off-by: Max Reitz --- include/cdi/mempool.h | 35 +++++++++++++++++++++++++++++++++++ 1 files changed, 35 insertions(+), 0 deletions(-) create mode 100644 include/cdi/mempool.h diff --git a/include/cdi/mempool.h b/include/cdi/mempool.h new file mode 100644 index 0000000..83d2ab2 --- /dev/null +++ b/include/cdi/mempool.h @@ -0,0 +1,35 @@ +/****************************************************************************** +* Copyright (c) 2009 Kevin Wolf * +* * +* 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 MEMPOOL_H +#define MEMPOOL_H + +#include +#include + +struct mempool; + +struct mempool* mempool_create(size_t pool_size, size_t object_size); +int mempool_get(struct mempool* pool, void** obj, uintptr_t* phys_obj); +int mempool_put(struct mempool* pool, void* obj); + +#endif -- 1.6.4.2 From max at tyndur.org Sat Dec 12 18:47:11 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 12 Dec 2009 18:47:11 +0100 Subject: [cdi-devel] [PATCH 2/6] CDI.usb, CDI.usb-hcd In-Reply-To: <1260640035-11473-2-git-send-email-max@tyndur.org> References: <1260640035-11473-1-git-send-email-max@tyndur.org> <1260640035-11473-2-git-send-email-max@tyndur.org> Message-ID: <1260640035-11473-3-git-send-email-max@tyndur.org> From: Max Reitz + Added CDI.usb header + Added CDI.usb-hcd header Signed-off-by: Max Reitz --- include/cdi/usb-hcd.h | 178 ++++++++++ include/cdi/usb.h | 868 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1046 insertions(+), 0 deletions(-) create mode 100644 include/cdi/usb-hcd.h create mode 100644 include/cdi/usb.h diff --git a/include/cdi/usb-hcd.h b/include/cdi/usb-hcd.h new file mode 100644 index 0000000..ffeb061 --- /dev/null +++ b/include/cdi/usb-hcd.h @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2009 Max Reitz + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/projects/COPYING.WTFPL for more details. + */ + +/** + * \if german + * USB-Hostcontrollertreiber + * \elseif english + * USB host controller drivers + * \endif + * \defgroup USBHCD + */ +/*\@{*/ + +#ifndef _CDI_USB_HCD_H_ +#define _CDI_USB_HCD_H_ + +#include "cdi/misc.h" +#include "cdi/usb.h" + +struct cdi_usb_hc { + struct cdi_device dev; + + /** + * \if german + * USB-Geschwindigkeit + * \elseif english + * USB speed + * \endif + */ + cdi_usb_speed_t speed; +}; + +struct cdi_usb_hcd { + struct cdi_driver drv; + + /** + * \if german + * + * Listet alle Geraete auf, die an den Rootports haengen und ruft ggf. + * cdi_usb_device_init auf + * + * @param device Der Hostcontroller + * + * \elseif english + * + * Lists all devices hanging at the root ports and calls (if necessary) + * cdi_usb_device_init + * + * @param device The host controller + * + * \endif + */ + void (*find_devices)(struct cdi_usb_hc* device); + + /** + * \if german + * + * Aktiviert den zum angegebenen Geraet gehoerigen Rootport + * + * @param usb_device Das USB-Geraet (muss an einem Rootport haengen) + * + * \elseif english + * + * Activates the root port corresponding to the device + * + * @param usb_device The USB device (must be located at a root port) + * + * \endif + */ + void (*activate_device)(struct cdi_usb_device* usb_device); + + /** + * \if german + * + * Verarbeitet USB-Pakete + * + * @param packets Array zu sendender Pakete + * @param num_packets Anzahl der Pakete + * + * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) + * + * \elseif english + * + * Sends USB packets + * + * @param packets Array of packages to be sent + * @param num_packages Number of packages + * + * \endif + */ + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, + size_t num_packets); + + /** + * \if german + * + * Erstellt eine neue Pipe + * + * @param pipe Die Pipe + * + * \elseif english + * + * Creates a new pipe + * + * @param pipe The pipe + * + * \endif + */ + void (*add_pipe)(struct cdi_usb_pipe* pipe); +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \if german + * + * Initialisiert einen HCD + * + * @param driver Der HCD + * + * \elseif english + * + * Initializes an HCD + * + * @param driver The HCD + * + * \endif + */ +void cdi_usb_hcd_init(struct cdi_usb_hcd* driver); + +/** + * \if german + * + * Gibt einen HCD frei + * + * @param driver Freizugebender HCD + * + * \elseif english + * + * Removes an HCD + * + * @param driver HCD to be removed + * + * \endif + */ +void cdi_usb_hcd_destroy(struct cdi_usb_hcd* driver); + +/** + * \if german + * + * Registriert einen HCD + * + * @param driver Zu registrierender HCD + * + * \elseif english + * + * Registers an HCD + * + * @param driver HCD to be registered + * + * \endif + */ +void cdi_usb_hcd_register(struct cdi_usb_hcd* driver); + +#ifdef __cplusplus +} //extern "C" +#endif + +#endif diff --git a/include/cdi/usb.h b/include/cdi/usb.h new file mode 100644 index 0000000..73be915 --- /dev/null +++ b/include/cdi/usb.h @@ -0,0 +1,868 @@ +/* + * Copyright (c) 2009 Max Reitz + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/projects/COPYING.WTFPL for more details. + */ + +/** + * \if german + * USB-Bustreiber + * \elseif english + * USB bus drivers + * \endif + * \defgroup USB + */ +/*\@{*/ + +#ifndef _CDI_USB_H_ +#define _CDI_USB_H_ + +#include "stddef.h" +#include "stdint.h" + +typedef enum { + /// Low-Speed (1,5 Mbit/s) + CDI_USB_LOW_SPEED = 0x01, + /// Full-Speed (12 Mbit/s) + CDI_USB_FULL_SPEED = 0x02, + /// High-Speed (480 Mbit/s) + CDI_USB_HIGH_SPEED = 0x04, + /// SuperSpeed (5 Gbit/s) + CDI_USB_SUPER_SPEED = 0x08, +} cdi_usb_speed_t; + +typedef enum { + /** + * \if german + * SETUP-Paket + * \elseif english + * SETUP packet + * \endif + */ + CDI_USB_PACKET_SETUP = 0x2D, + + /** + * \if german + * Daten vom Geraet zum Host + * \elseif english + * Data from the device to the host + * \endif + */ + CDI_USB_PACKET_IN = 0x69, + + /** + * \if german + * Daten vom Host zum Geraet + * \elseif english + * Data from the host to the device + * \endif + */ + CDI_USB_PACKET_OUT = 0xE1, +} cdi_usb_packet_type_t; + +typedef enum { + /** + * \if german + * Kein Fehler + * \elseif english + * No error + * \endif + */ + CDI_USB_NO_ERROR = 0x0000, + + /// Stalled + CDI_USB_STALLED = 0x0001, + + /** + * \if german + * Bufferfehler (Overflow, underrun, ...) + * \elseif english + * Buffer error (overflow, underrun, ...) + * \endif + */ + CDI_USB_BUFFER_ERROR = 0x0002, + + /** + * \if german + * Babble (Geraet hat zu viele Daten gesendet) + * \elseif english + * Babble (device has sent more data than expected) + * \endif + */ + CDI_USB_BABBLE = 0x0004, + + /** + * \if german + * NAK (Daten sind erneut zu senden) + * \elseif english + * NAK (data has to be sent again) + * \endif + */ + CDI_USB_NAK = 0x0008, + + /** + * \if german + * CRC-Fehler + * \elseif english + * CRC error + * \endif + */ + CDI_USB_CRC = 0x0010, + + /// Timeout + CDI_USB_TIMEOUT = 0x0020, + + /** + * \if german + * Bitstuff/-destuff-Fehler + * \elseif english + * bitstuff/-destuff error + * \endif + */ + CDI_USB_BITSTUFF = 0x0040, + + /** + * \if german + * Reservieren von Speicher fehlgeschlagen, ... + * \elseif english + * Allocating of memory failed, ... + * \endif + */ + CDI_USB_TRIVIAL_ERROR = 0x0080, +} cdi_usb_status_t; + +struct cdi_usb_hc; +struct cdi_usb_pipe; +struct cdi_usb_interface; +struct cdi_usb_driver; + +struct cdi_usb_device { + /** + * \if german + * Der USB-Bustreiber + * \elseif english + * The USB bus driver + * \endif + */ + struct cdi_usb_driver* drv; + + /** + * \if german + * Der Hostcontroller, an dem das Geraet im Endeffekt haengt + * \elseif english + * The host controller where the device is eventually located + * \endif + */ + struct cdi_usb_hc* hc; + + /** + * \if german + * Haengt das Geraet an einem Hub, dann ist dieses Feld ein Pointer auf + * das entsprechende USB-Geraet, haengt es an einem Rootport, dann ist + * dieses Feld NULL. + * \elseif english + * If the device is located at a hub, then this field contains a pointer to + * the corresponding USB device, if it's located at a root port, then this + * field is set to NULL. + * \endif + */ + struct cdi_usb_device* hub; + + /** + * \if german + * Geschwindigkeit(-en) dieses Geraets + * \elseif english + * Speed(-s) supported by this device + * \endif + */ + cdi_usb_speed_t speed; + + /// ID + int id; + + /** + * \if german + * Entweder der Rootport oder der Port des Hubs, an dem das Geraet haengt + * \elseif english + * Either the root port or the port hub's port where the device is located + * \endif + */ + int port; + + /** + * \if german + * Hier darf der Geraetetreiber seine Daten unterbringen + * \elseif english + * The device driver may store its data here + * \endif + */ + void* driver_data; + + /** + * \if german + * Felder, die so auch bei PCI existieren: Vendor-, Device-, Class-, + * Subclass- und Protocol-ID + * \elseif english + * Fields existing at PCI, too: vendor, device, class, subclass and + * protocol ID. + * \endif + */ + int vendor_id; + int device_id; + int class_id; + int subclass_id; + int protocol_id; + + /** + * \if german + * + * Setzt das USB-Geraet zurueck (kann sowohl vom HCD als auch vom Hub + * driver ausgefuehrt werden) + * + * @param usb_device Das Geraet + * + * \elseif english + * + * Resets the USB device (may be executed either by the HCD or the hub + * driver) + * + * @param usb_device The device + * + * \endif + */ + void (*reset)(struct cdi_usb_device* usb_device); + + /** + * \if german + * Ziemlich lowlevelig. Gehoert das hier wirklich rein? + * \elseif english + * Very low level. Does it really fit in here? + * \endif + */ + + /** + * \if german + * Im einzelnen sind das: Pipe fuer Endpoint 0 + * \elseif english + * In particular these things are: Pipe for endpoint 0 + * \endif + */ + struct cdi_usb_pipe* ep0; + + /// Device descriptor + void* device_desc; + + /// Configuration descriptor + void* config_desc; + + /// Interface descriptor + struct cdi_usb_interface* interface_desc; + + /// Class descriptor + void* class_desc; +}; + +enum cdi_usb_toggle { + /** + * \if german + * Das Toggle nehmen, das jetzt dran ist + * \elseif english + * Use the toggle to be used right now + * \endif + */ + TOGGLE_UNSPECIFIC = 0, + + /** + * \if german + * Geloeschtes Togglebit erzwingen + * \elseif english + * Force unset toggle bit + * \endif + */ + TOGGLE_0 = 1, + + /** + * \if german + * Gesetztes Togglebit erzwingen + * \elseif english + * Force set toggle bit + * \endif + */ + TOGGLE_1 = 2 +}; + +struct cdi_usb_endpoint { + /// sizeof(struct cdi_usb_endpoint) + uint8_t length; + + /// 5 + uint8_t descriptor_type; + + /** + * \if german + * Nummer des Endpoints + * \elseif english + * Endpoint's number + * \endif + */ + uint8_t endpoint_address; + + /** + * \if german + * Eigenschaften (IN/OUT, Bulk/Interrupt/..., ...) + * \elseif english + * Attributes (IN/OUT, bulk/interrupt/..., ...) + * \endif + */ + uint8_t attributes; + + /** + * \if german + * Maximale Paketgroesse in Bytes + * \elseif english + * Maximum packet size in bytes + * \endif + */ + uint16_t max_packet_size; + + /// ??? + uint8_t interval; +} __attribute__((packed)); + +struct cdi_usb_interface { + /// sizeof(struct cdi_usb_interface) + uint8_t length; + + /// 4 + uint8_t descriptor_type; + + /** + * \if german + * Nummer des Interface + * \elseif english + * Interface's number + * \endif + */ + uint8_t interface_number; + + /// ??? + uint8_t alternate_setting; + + /** + * \if german + * Anzahl der Endpoints fuer dieses Interface + * \elseif english + * Number of endpoints for this interface + * \endif + */ + uint8_t num_endpoints; + + /** + * \if german + * Class-ID (bei Composite Devices) + * \elseif english + * Class ID (used with composite devices) + * \endif + */ + uint8_t interface_class; + + /// Subclass-ID + uint8_t interface_subclass; + + /// Protocol-ID + uint8_t interface_protocol; + + /** + * \if german + * String-Index, der das Interface beschreibt + * \elseif english + * String index describing the interface + * \endif + */ + uint8_t iInterface; +} __attribute__((packed)); + +/** + * \if german + * Beschreibt eine Pipe + * \elseif english + * Describes a pipe + * \endif + */ +struct cdi_usb_pipe { + /** + * \if german + * Ziel + * \elseif english + * Destination + * \endif + */ + struct cdi_usb_device* device; + + /** + * \if german + * Ein Pointer auf eine den Endpoint beschreibende Struktur + * TODO Das ist eigentlich auch sehr lowlevelig. + * \elseif english + * A pointer to a structure describing the endpoint + * TODO This is on a very low level, too. + */ + struct cdi_usb_endpoint* endpoint; + + /// Data-Toggle + int data_toggle; +}; + +/** + * \if german + * Beschreibt ein USB-Paket + * \elseif english + * Describes a USB packet + * \endif + */ +struct cdi_usb_packet { + /** + * \if german + * Die gewuenschte Pipe + * \elseif english + * The desired pipe + * \endif + */ + struct cdi_usb_pipe* pipe; + + /** + * \if german + * Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) + * \elseif english + * Type of packet (PACKET_IN, PACKET_OUT, PACKET_SETUP) + * \endif + */ + cdi_usb_packet_type_t type; + + /** + * \if german + * Zeiger auf den Datenpuffer + * \elseif english + * Pointer to the data buffer + * \endif + */ + void* data; + + /** + * \if german + * Die Laenge des Puffers + * \elseif english + * The buffer's length + * \endif + */ + size_t length; + + /** + * \if german + * Fehlertyp (oder USB_NO_ERROR) + * \elseif english + * Error type (or USB_NO_ERROR) + * \endif + */ + cdi_usb_status_t condition; + + /** + * \if german + * Bestimmtes Data-Toggle benuten + * \elseif english + * Use specific data toggle + * \endif + */ + enum cdi_usb_toggle use_toggle; +}; + +typedef enum { + /** + * \if german + * Host -> Geraet + * \elseif english + * Host -> device + * \endif + */ + CDI_USB_CONTROL_OUT = 0x00, + + /** + * \if german + * Geraet -> Host + * \elseif english + * Device -> host + * \endif + */ + CDI_USB_CONTROL_IN = 0x80 +} cdi_usb_control_direction_t; + +typedef enum { + /** + * \if german + * Standard-Request + * \elseif english + * Standard request + * \endif + */ + CDI_USB_CONTROL_RQ_STANDARD = 0x00, + + /** + * \if german + * Klassenspezifischer Request + * \elseif english + * Class specific request + * \endif + */ + CDI_USB_CONTROL_RQ_CLASS = 0x20, + + /** + * \if german + * Herstellerspezifischer Request + * \elseif english + * Vendor specific request + * \endif + */ + CDI_USB_CONTROL_RQ_VENDOR = 0x40 +} cdi_usb_control_request_type_t; + +typedef enum { + /** + * \if german + * Empfaenger: Geraet + * \elseif english + * Recipient: device + * \endif + */ + CDI_USB_CONTROL_REC_DEVICE = 0x00, + + /** + * \if german + * Empfaenger: Interface + * \elseif english + * Recipient: interface + * \endif + */ + CDI_USB_CONTROL_REC_INTERFACE = 0x01, + + /** + * \if german + * Empfaenger: Endpoint + * \elseif english + * Recipient: endpoint + * \endif + */ + CDI_USB_CONTROL_REC_ENDPOINT = 0x02, + + /** + * \if german + * Empfaenger ist jemand anderes + * \elseif english + * Recipient is another one + * \endif + */ + CDI_USB_CONTROL_REC_OTHER = 0x03 +} cdi_usb_control_recipient_t; + +struct cdi_usb_control_transfer { + /** + * \if german + * Wenn Daten zum Geraet gesendet werden sollen, dann ist hier der + * Speicherbereich anzugeben + * \elseif english + * If data has to be sent to the device, then the memory buffer has to be + * given here + * \endif + */ + void* buffer; + + /** + * \if german + * Laenge der erwarteten bzw. zu sendenden Daten + * \elseif english + * Length of the expected data respectively the data to be sent + * \endif + */ + size_t length; + + /** + * \if german + * Richtung + * \elseif english + * Direction + * \endif + */ + cdi_usb_control_direction_t direction; + + /** + * \if german + * Art des Requests (Standardrequest, klassenspezifisch, + * herstellerspezifisch) + * \elseif english + * Request type (standard request, class specific, vendor specific) + * \endif + */ + cdi_usb_control_request_type_t rtype; + + /** + * \if german + * Ziel (Device, Interface, Endpoint, anderes) + * \elseif english + * Recipient (device, interface, endpoint, other) + * \endif + */ + cdi_usb_control_recipient_t recipient; + + /** + * \if german + * Requestcode (Je nach rtype und recipient) + * \elseif english + * Request code (specific to rtype and recipient) + * \endif + */ + int request; + + /** + * \if german + * Ein zu sendender Wert + * \elseif english + * A value to be sent + * \endif + */ + int value; + + /** + * \if german + * Ein zu sendender Index + * \elseif english + * An index to be sent + * \endif + */ + int index; +}; + +#include "cdi/usb-hcd.h" + +struct cdi_usb_driver { + struct cdi_driver drv; + + /** + * \if german + * Unterstuetzte Geschwindigkeiten + * \elseif english + * Supported speeds + * \endif + */ + cdi_usb_speed_t speeds; + + /** + * \if german + * + * Enumeriert ein USB-Geraet + * + * @param device Das zu enumerierende Geraet + * + * \elseif english + * + * Enumerates a USB device + * + * @param device The device to be enumerated + * + * \endif + */ + void (*enumerate_device)(struct cdi_usb_device* device); + + /** + * \if german + * + * Sendet USB-Pakete + * + * @param packets Array zu sendender Pakete + * @param num_packets Anzahl der Pakete + * + * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) + * + * \elseif english + * + * Sends USB packets + * + * @param packets Array of packets to be sent + * @param num_packets Number of packets + * + * @return Returns the status (i. e. USB_NO_ERROR to indicate no error) + * + * \endif + */ + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, + size_t num_packets); + + /** + * \if german + * + * Fuehrt einen Control-Transfer durch + * + * @param device Das Zielgeraet + * @param transfer Eine den Transfer beschreibende Struktur + * + * @return Gibt einen Puffer zurueck, der die ausgetauschen Daten enthaelt, + * im Fehlerfall hingegen NULL, gab es keine Daten, dann + * (void *)0xFFFFFFFF. + * + * \elseif english + * + * Performs a control transfer + * + * @param device The destination device + * @param transfer A structure describing this transfer + * + * @return Returns a buffer containing the transferred data, in case of an + * error the value is NULL and if there was no data, it returns + * (void *)0xFFFFFFFF. + * + * \endif + */ + void* (*do_control)(struct cdi_usb_device* device, + struct cdi_usb_control_transfer* transfer); +}; + +/** + * \if german + * Ein USB-Geraetetreiber uebergibt dem Betriebssystem eine solche Struktur, + * damit ein bestimmter Handler beim Auffinden solcher Geraete aufgerufen wird. + * Jedes Feld enthaelt entweder eine Zahl, die mit der des Geraetes + * uebereinstimmen muss oder -1, dann wird dieses Feld ignoriert. + * \elseif english + * A USB device driver passes this structure to the OS so that a specific + * handler is called upon the detection of such a device. Each field either + * contains a number that has to be equal to that of the driver or -1, so that + * the field will be ignored. + * \endif + */ +struct cdi_usb_device_pattern { + int vendor_id; + int device_id; + int class_id; + int subclass_id; + int protocol_id; + + /** + * \if german + * + * Ueberprueft das angegebene USB-Geraet + * + * @param device Das Geraet + * + * @return 0, wenn dieses Geraet nicht vom Treiber verarbeitet werden kann, + * sonst 1 + * + * \elseif english + * + * Checks the given USB device + * + * @param device The device + * + * @return 0 if the driver is unable to handle this device, else 1 + * + * \endif + */ + int (*take_usb_device)(struct cdi_usb_device* device); +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \if german + * + * Entfernt einen USB-Bustreiber + * + * @param driver Der Treiber + * + * \elseif english + * + * Removes a USB bus driver + * + * @param driver The driver + * + * \endif + */ +void cdi_usb_driver_destroy(struct cdi_usb_driver* driver); + +/** + * \if german + * + * Initialisiert einen USB-Bustreiber + * + * @param driver Der Treiber + * + * \elseif english + * + * Initializes a USB bus driver + * + * @param driver The driver + * + * \endif + */ +void cdi_usb_driver_init(struct cdi_usb_driver* driver); + +/** + * \if german + * + * Registriert einen USB-Bustreiber beim System + * + * @param driver Der Treiber + * + * \elseif english + * + * Registers a USB bus driver at the OS + * + * @param driver The driver + * + * \endif + */ +void cdi_usb_driver_register(struct cdi_usb_driver* driver); + +/** + * \if german + * + * Registriert ein gefundenes USB-Geraet beim Betriebssystem - diese Funktion + * wird z. B. vom Host-Controller-Treiber nach einem Aufruf von find_devices + * fuer jedes gefundene Geraet aufgerufen + * + * @param device Das zu registrierende Geraet + * + * \elseif english + * + * Registers a detected USB device at the OS - this function is called i. e. by + * the host controller driver after a find_devices call for every device found + * + * @param device The device to be registered + */ +void cdi_usb_device_init(struct cdi_usb_device* device); + +/** + * \if german + * + * Registriert ein Geraetemuster + * + * @param pattern Das Muster + * + * \elseif english + * + * Registers a device pattern + * + * @param pattern The pattern + * + * \endif + */ +void cdi_usb_register_device_pattern(struct cdi_usb_device_pattern* pattern); + +#ifdef __cplusplus +} //extern "C" +#endif + +#endif -- 1.6.4.2 From max at tyndur.org Sat Dec 12 18:47:12 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 12 Dec 2009 18:47:12 +0100 Subject: [cdi-devel] [PATCH 3/6] OHCI In-Reply-To: <1260640035-11473-3-git-send-email-max@tyndur.org> References: <1260640035-11473-1-git-send-email-max@tyndur.org> <1260640035-11473-2-git-send-email-max@tyndur.org> <1260640035-11473-3-git-send-email-max@tyndur.org> Message-ID: <1260640035-11473-4-git-send-email-max@tyndur.org> From: Max Reitz + Added USB open host controller driver Signed-off-by: Max Reitz --- ohci/hcd.c | 315 ++++++++++++++++++++++++++++++++++++++++++ ohci/include/ohci-hcd.h | 39 +++++ ohci/include/ohci-init.h | 31 ++++ ohci/include/ohci-transfer.h | 30 ++++ ohci/include/ohci.h | 283 +++++++++++++++++++++++++++++++++++++ ohci/init.c | 241 ++++++++++++++++++++++++++++++++ ohci/main.c | 34 +++++ ohci/transfer.c | 85 +++++++++++ 8 files changed, 1058 insertions(+), 0 deletions(-) create mode 100644 ohci/hcd.c create mode 100644 ohci/include/ohci-hcd.h create mode 100644 ohci/include/ohci-init.h create mode 100644 ohci/include/ohci-transfer.h create mode 100644 ohci/include/ohci.h create mode 100644 ohci/init.c create mode 100644 ohci/main.c create mode 100644 ohci/transfer.c diff --git a/ohci/hcd.c b/ohci/hcd.c new file mode 100644 index 0000000..8296ba9 --- /dev/null +++ b/ohci/hcd.c @@ -0,0 +1,315 @@ +/***************************************************************************** +* 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 +#include +#include + +#include "ohci.h" +#include "ohci-hcd.h" + +static void reset_device(struct cdi_usb_device *device); + +static void find_devices(struct cdi_usb_hc *device) +{ + struct ohci *ohci = (struct ohci *)device; + struct cdi_usb_device *dev; + int i; + + d0printf("Scanning root ports\n"); + for (i = 0; i < ohci->root_ports; i++) { + if (!(ohci->memory->hc_rh_port_status[i] & OHC_RP_CCS)) { + continue; + } + dev = malloc(sizeof(struct cdi_usb_device)); + dev->hc = device; + dev->id = 0; + dev->port = i; + dev->speed = (ohci->memory->hc_rh_port_status[i] & OHC_RP_LSDA) ? + CDI_USB_LOW_SPEED : CDI_USB_FULL_SPEED; + dev->reset = &reset_device; + cdi_usb_device_init(dev); + } +} + +static void activate_device(struct cdi_usb_device *device) +{ + struct ohci *ohci = (struct ohci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Activating device on port %i.\n", device->port); + ohci->memory->hc_rh_port_status[device->port] |= OHC_RP_PES; + cdi_sleep_ms(20); +} + +static void reset_device(struct cdi_usb_device *device) +{ + struct ohci *ohci = (struct ohci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Resetting device on port %i.\n", device->port); + ohci->memory->hc_rh_port_status[device->port] |= OHC_RP_PRS; + cdi_sleep_ms(20); +} + +static void add_pipe(struct cdi_usb_pipe *pipe) +{ + struct ohci *ohci = (struct ohci *)pipe->device->hc; + 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->speed == CDI_USB_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); + + if (iscontrol) { + ohci->memory->hc_control_head_ed = ped; + } else { + ohci->memory->hc_bulk_head_ed = ped; + } +} + +static inline int _tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} + +static volatile int locked = 0; + +static cdi_usb_status_t do_packets(struct cdi_usb_packet *packets, + size_t num_packets) +{ + struct ohci_ed_desc *edsc; + struct ohci_td_desc *tdsc, *otdsc; + int i, j, cond, toggle, done_packets; + struct ohci *ohci = (struct ohci *)packets[0].pipe->device->hc; + struct ohci_td *vtd[num_packets]; + uintptr_t ptd[num_packets]; + + for (i = 0; (edsc = cdi_list_get(ohci->ed_list, i)) != NULL; i++) { + if ((edsc->function == packets[0].pipe->device->id) && + (edsc->endpoint == packets[0].pipe->endpoint->endpoint_address)) + { + break; + } + } + if (edsc == NULL) { + return CDI_USB_TIMEOUT; //Genau das w??rde passieren + } + + toggle = packets[0].pipe->data_toggle; + for (i = 0; i < (int)num_packets; i++) { + if (mempool_get(ohci->transfer_pool, (void **)&vtd[i], &ptd[i]) == -1) + { + return CDI_USB_TRIVIAL_ERROR; + } + if ((packets[i].data != NULL) && + (packets[i].type != CDI_USB_PACKET_IN)) + { + memcpy((void *)((uintptr_t)vtd[i] + sizeof(struct ohci_td)), + packets[i].data, packets[i].length); + } + vtd[i]->rounding = 1; //Warum nicht + switch (packets[i].type) { + case CDI_USB_PACKET_SETUP: + vtd[i]->direction = OHC_TD_DIR_SETUP; + break; + case CDI_USB_PACKET_IN: + vtd[i]->direction = OHC_TD_DIR_IN; + break; + case CDI_USB_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 CDI_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 CDI_USB_TRIVIAL_ERROR; + } + tdsc->virt = vtd[i]; + tdsc->phys = ptd[i]; + tdsc->endpoint = edsc; + + 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]; + } + + 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; + } + done_packets = 0; + cond = CDI_USB_NO_ERROR; + while (done_packets < (int)num_packets) { + cdi_sleep_ms(1); + for (i = 0; i < (int)num_packets; i++) { + if (vtd[i]->condition != 15) { + done_packets++; + switch (vtd[i]->condition) { + case 0x00: + packets[i].condition = CDI_USB_NO_ERROR; + break; + case 0x02: + packets[i].condition = CDI_USB_BITSTUFF; + break; + case 0x04: + packets[i].condition = CDI_USB_STALLED; + for (j = 0; j < (int)num_packets; j++) { + //Allet, was nicht fertig ist, ist STALLed + vtd[j]->condition = 0x04; + } + break; + case 0x05: + packets[i].condition = CDI_USB_TIMEOUT; + break; + case 0x08: + packets[i].condition = CDI_USB_BABBLE; + break; + case 0x0C: + case 0x0D: + packets[i].condition = CDI_USB_BUFFER_ERROR; + break; + default: + packets[i].condition = CDI_USB_CRC; + break; + } + cond |= packets[i].condition; + vtd[i]->condition = 15; //Nicht nochmal ??berpr??fen + } + } + } + for (i = 0; i < (int)num_packets; i++) { + if ((packets[i].data != NULL) && + (packets[i].type == CDI_USB_PACKET_IN)) + { + memcpy(packets[i].data, (void *)((uintptr_t)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 (cond & CDI_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 + } + return cond; +} + + +const struct ohci_functions ohci_functions = { + .find_devices = &find_devices, + .activate_device = &activate_device, + .send_packets = &do_packets, + .add_pipe = &add_pipe +}; diff --git a/ohci/include/ohci-hcd.h b/ohci/include/ohci-hcd.h new file mode 100644 index 0000000..ccb6345 --- /dev/null +++ b/ohci/include/ohci-hcd.h @@ -0,0 +1,39 @@ +/***************************************************************************** +* 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 UHCI_HCD_H +#define UHCI_HCD_H + +#include +#include + +struct ohci_functions { + void (*find_devices)(struct cdi_usb_hc *device); + void (*activate_device)(struct cdi_usb_device *device); + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet *packets, + size_t num_packets); + void (*add_pipe)(struct cdi_usb_pipe *pipe); +}; + +cdi_list_t ohci_find_devices(struct cdi_usb_hc *device); + +#endif diff --git a/ohci/include/ohci-init.h b/ohci/include/ohci-init.h new file mode 100644 index 0000000..2f19d2c --- /dev/null +++ b/ohci/include/ohci-init.h @@ -0,0 +1,31 @@ +/***************************************************************************** +* 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 OHCI_INIT_H +#define OHCI_INIT_H + +#include "ohci.h" + +void find_and_init_ohcis(void); +void ohci_init(struct cdi_device *dev); + +#endif diff --git a/ohci/include/ohci-transfer.h b/ohci/include/ohci-transfer.h new file mode 100644 index 0000000..a6dc6be --- /dev/null +++ b/ohci/include/ohci-transfer.h @@ -0,0 +1,30 @@ +/***************************************************************************** +* 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 OHCI_TRANSFER_H +#define OHCI_TRANSFER_H + +#include + +void ohci_handler(struct cdi_device *cdi_hci); + +#endif diff --git a/ohci/include/ohci.h b/ohci/include/ohci.h new file mode 100644 index 0000000..b06a280 --- /dev/null +++ b/ohci/include/ohci.h @@ -0,0 +1,283 @@ +/***************************************************************************** +* 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 OHCI_H +#define OHCI_H + +#include +#include + +#include +#include +#include + +#include "ohci.h" + + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[ohci] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[ohci] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + + +#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_legsup +{ + uint32_t hce_control; + uint32_t hce_input; + uint32_t hce_output; + uint32_t hce_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 cdi_usb_hc cdidev; + struct cdi_pci_device *pcidev; + + struct ohci_registers *memory; + struct ohci_legsup *legsup; + int root_ports; + struct ohci_hcca *hcca; + + struct mempool *ed_pool; + struct mempool *transfer_pool; + + cdi_list_t ed_list; +}; + +#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 + 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 + +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) + 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); + +#endif diff --git a/ohci/init.c b/ohci/init.c new file mode 100644 index 0000000..a1ea5a5 --- /dev/null +++ b/ohci/init.c @@ -0,0 +1,241 @@ +/***************************************************************************** +* 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 +#include +#include +#include +#include + +#include "ohci.h" +#include "ohci-hcd.h" +#include "ohci-init.h" +#include "ohci-transfer.h" + +extern const struct ohci_functions ohci_functions; +extern int init_ohci(void); + +#define DRIVER_NAME "ohcd" + +static struct cdi_usb_hcd driver = { + .drv = { + .type = CDI_USB_HCD, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = &ohci_init, + .remove_device = NULL, + .init = &init_ohci, + .destroy = NULL + } +}; + +CDI_DRIVER(DRIVER_NAME, driver) + +void find_and_init_ohcis(void) +{ + struct ohci *hci; + char *name; + int ohci_counter = 0; + struct cdi_pci_device *dev; + + cdi_usb_hcd_init(&driver); + + cdi_list_t pci_devices = cdi_list_create(); + cdi_pci_get_all_devices(pci_devices); + for (int i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) { + if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03) + || (dev->interface_id != 0x10)) + { + cdi_pci_device_destroy(dev); + } else { + hci = malloc(sizeof(struct ohci)); + hci->cdidev.dev.type = CDI_USB_HCD; + name = malloc(7); + sprintf(name, "ohci%i", ohci_counter++); + hci->cdidev.dev.name = (const char *)name; + hci->cdidev.speed = CDI_USB_LOW_SPEED | CDI_USB_FULL_SPEED; + hci->pcidev = dev; + cdi_list_push(driver.drv.devices, hci); + } + } + + cdi_list_destroy(pci_devices); + + driver.find_devices = ohci_functions.find_devices; + driver.activate_device = ohci_functions.activate_device; + driver.send_packets = ohci_functions.send_packets; + driver.add_pipe = ohci_functions.add_pipe; +} + +void ohci_init(struct cdi_device *dev) +{ + struct ohci *ohci = (struct ohci *)dev; + struct cdi_pci_resource *res; + int i; + uintptr_t phys_hcca; + + ohci->memory = NULL; + for (i = 0; (res = cdi_list_get(ohci->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) { + d0printf("Wasn't able to allocate memory.\n"); + return; + } + ohci->legsup = (void *)((uintptr_t)ohci->memory + 0x100); + break; + } + } + if (ohci->memory == NULL) { + d1printf("Memory not found!\n"); + return; + } + d1printf("Memory @ 0x%08X\n", (uintptr_t)ohci->memory); + if ((ohci->memory->hc_revision & 0xFF) != 0x10) { + d0printf("This OHCI might be incompatible with this driver" + "(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) + { + d0printf("Wasn't able to allocate HCCA.\n"); + return; + } + memset(ohci->hcca, 0, sizeof(*ohci->hcca)); + + d1printf("IRQ: %i\n", ohci->pcidev->irq); + cdi_register_irq(ohci->pcidev->irq, &ohci_handler, dev); + + ohci->ed_pool = mempool_create(64 * sizeof(struct ohci_ed), + sizeof(struct ohci_ed)); + if (ohci->ed_pool == NULL) { + d0printf("Wasn't able to create ED memory pool!\n"); + return; + } + + ohci->transfer_pool = mempool_create(48 * (1024 + sizeof(struct ohci_td)), + 1024 + sizeof(struct ohci_td)); + if (ohci->transfer_pool == NULL) { + d0printf("Wasn't able to create transfer memory pool!\n"); + return; + } + + ohci->memory->hc_interrupt_disable = OHC_INT_MIE; + + if (ohci->memory->hc_control & OHC_CTRL_IR) { + d1printf("Kicking out 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) { + _d1printf("Went fine.\n"); + } else { + _d1printf("Idiot.\n"); + ohci->memory->hc_control &= ~OHC_CTRL_IR; + } + } else if ((ohci->memory->hc_control & OHC_CTRL_CBSR) != OHC_USB_RESET) { + d1printf("BIOS active\n"); + if ((ohci->memory->hc_control & OHC_CTRL_CBSR) != OHC_USB_OPERATIONAL) + { + d1printf("Activating RESUME...\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 + cdi_sleep_ms(10); //Reset abwarten + } + + if (ohci->legsup->hce_control & 1) { + d1printf("USB legacy support activated, but that won't last for" + "long...\n"); + } + //Harhar, that's mine now! + ohci->legsup->hce_control = 0; + + 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... + d1printf("Reset not finished yet...\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; + + d1printf("Activating HC.\n"); + + //DO IT, FAGGOT! + 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); + + d0printf("Found %i root ports.\n", ohci->root_ports); + + for (i = 0; i < ohci->root_ports; i++) { + ohci->memory->hc_rh_port_status[i] |= OHC_RP_CCS; //Deaktivieren + } + + ohci->ed_list = cdi_list_create(); +} diff --git a/ohci/main.c b/ohci/main.c new file mode 100644 index 0000000..4b6cd67 --- /dev/null +++ b/ohci/main.c @@ -0,0 +1,34 @@ +/***************************************************************************** +* 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 "ohci-init.h" + +int init_ohci() +{ + cdi_init(); + + find_and_init_ohcis(); + + return 0; +} diff --git a/ohci/transfer.c b/ohci/transfer.c new file mode 100644 index 0000000..24f7c67 --- /dev/null +++ b/ohci/transfer.c @@ -0,0 +1,85 @@ +/***************************************************************************** +* 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 + +#include "ohci.h" + +void ohci_handler(struct cdi_device *cdi_hci) +{ + struct ohci *ohci = (struct ohci *)cdi_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; + if (!status) { + return; //Wohl nicht f??r uns + } + + if (status & OHC_INT_RHSC) { + done |= OHC_INT_RHSC; + } + if (status & OHC_INT_UE) { + d0printf("Fatal HC error.\n"); + ohci->memory->hc_command_status |= OHC_CMST_RESET; + done |= OHC_INT_UE; + } + if (status & OHC_INT_SF) { + //??hm... Na ja. Manchmal fragt man sich, ob sich der HC so einsam + //f??hlt, dass er IRQs feuert, obwohl sie deaktiviert sind. + 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); + free(tdsc); + break; + } + } + if (tdsc != NULL) { + //Gefunden + break; + } + } + } + + if (status & ~done) { + d1printf("Non-handled interrupt: 0x%08X\n", status & ~done); + } + + ohci->memory->hc_interrupt_status = status; +} -- 1.6.4.2 From max at tyndur.org Sat Dec 12 18:47:13 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 12 Dec 2009 18:47:13 +0100 Subject: [cdi-devel] [PATCH 4/6] UHCI In-Reply-To: <1260640035-11473-4-git-send-email-max@tyndur.org> References: <1260640035-11473-1-git-send-email-max@tyndur.org> <1260640035-11473-2-git-send-email-max@tyndur.org> <1260640035-11473-3-git-send-email-max@tyndur.org> <1260640035-11473-4-git-send-email-max@tyndur.org> Message-ID: <1260640035-11473-5-git-send-email-max@tyndur.org> From: Max Reitz + Added USB universal host controller driver Signed-off-by: Max Reitz --- uhci/hcd.c | 215 ++++++++++++++++++++++++++++++++++++ uhci/include/uhci-hcd.h | 39 +++++++ uhci/include/uhci-init.h | 31 +++++ uhci/include/uhci-transfer.h | 30 +++++ uhci/include/uhci.h | 138 +++++++++++++++++++++++ uhci/init.c | 249 ++++++++++++++++++++++++++++++++++++++++++ uhci/main.c | 34 ++++++ uhci/transfer.c | 91 +++++++++++++++ 8 files changed, 827 insertions(+), 0 deletions(-) create mode 100644 uhci/hcd.c create mode 100644 uhci/include/uhci-hcd.h create mode 100644 uhci/include/uhci-init.h create mode 100644 uhci/include/uhci-transfer.h create mode 100644 uhci/include/uhci.h create mode 100644 uhci/init.c create mode 100644 uhci/main.c create mode 100644 uhci/transfer.c diff --git a/uhci/hcd.c b/uhci/hcd.c new file mode 100644 index 0000000..bda4715 --- /dev/null +++ b/uhci/hcd.c @@ -0,0 +1,215 @@ +/***************************************************************************** +* 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 +#include +#include + +#include "uhci.h" +#include "uhci-hcd.h" + +extern cdi_list_t uhci_active_transfers; + +static void reset_device(struct cdi_usb_device *device); + +static void find_devices(struct cdi_usb_hc *device) +{ + struct uhci *uhci = (struct uhci *)device; + struct cdi_usb_device *dev; + int i; + + d0printf("Scanning root ports\n"); + 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)) { + continue; + } + dev = malloc(sizeof(struct cdi_usb_device)); + dev->hc = device; + dev->id = 0; + dev->port = i; + dev->speed = (cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & + RPORT_LOSPD) ? CDI_USB_LOW_SPEED : CDI_USB_FULL_SPEED; + dev->hub = NULL; + dev->reset = &reset_device; + cdi_usb_device_init(dev); + } +} + +static void activate_device(struct cdi_usb_device *device) +{ + struct uhci *uhci = (struct uhci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Activating device on port %i.\n", device->port); + cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, + RPORT_ENABLE | RPORT_CSC); + cdi_sleep_ms(20); +} + +static void reset_device(struct cdi_usb_device *device) +{ + struct uhci *uhci = (struct uhci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Resetting device on port %i.\n", device->port); + cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, + RPORT_ENABLE | RPORT_RESET | RPORT_CSC); + cdi_sleep_ms(20); +} + +static void add_pipe(struct cdi_usb_pipe *pipe) +{ + //Macht nix, weil das bei UHCI nicht n??tig ist + pipe = NULL; //Aber GCC k??nnen wir trotzdem gl??cklich machen +} + +static inline int _tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} + +static volatile int locked = 0; + +static cdi_usb_status_t do_packet(struct cdi_usb_packet *packet) +{ + struct cdi_usb_device *usbdev = packet->pipe->device; + struct uhci *uhci = (struct uhci *)usbdev->hc; + struct uhci_td td; + struct transfer addr; + int timeout, frame; + + void *buf; + uintptr_t pbuf; + + memset(&td, 0, sizeof(td)); + 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->speed == CDI_USB_LOW_SPEED); + td.errors = 1; + td.pid = packet->type; + td.device = usbdev->id; + td.endpoint = packet->pipe->endpoint->endpoint_address & 0x07; + td.maxlen = packet->length ? packet->length - 1 : 0x7FF; + while (_tsl(&locked)); + frame = (cdi_inw(uhci->pbase + UHCI_FRNUM) + 5) & 0x3FF; + while (!(uhci->queue_heads[frame].transfer & 1)) { + frame++; + frame &= 0x3FF; + } + + if (mempool_get(uhci->buffers, &buf, &pbuf) < 0) { + return CDI_USB_TRIVIAL_ERROR; + } + + addr.virt = buf; + addr.phys = pbuf; + addr.error = 0xFFFF; + + buf = (void *)((uintptr_t)buf + sizeof(td)); + pbuf += sizeof(td); + + if (!packet->length) { + td.buffer = 0; + } else { + td.buffer = pbuf; + if (packet->type != CDI_USB_PACKET_IN) { + memcpy(buf, packet->data, packet->length); + } + } + + memcpy(addr.virt, &td, sizeof(td)); + + + if (uhci_active_transfers == NULL) { + uhci_active_transfers = cdi_list_create(); + } + uhci->queue_heads[frame].transfer = addr.phys; + cdi_list_push(uhci_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)) { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + if (packet->type == CDI_USB_PACKET_IN) { + memcpy(packet->data, buf, packet->length); + } + if (addr.error == 0xFFFF) { + addr.error = CDI_USB_TIMEOUT; + } + + mempool_put(uhci->buffers, addr.virt); + + return addr.error; +} + +static cdi_usb_status_t do_packets(struct cdi_usb_packet *packets, + size_t num_packets) +{ + size_t i; + cdi_usb_status_t cond = CDI_USB_NO_ERROR; + + //TODO + for (i = 0; (i < num_packets) && !cond; i++) { + packets[i].condition = do_packet(&packets[i]); + cond |= packets[i].condition; + } + + return cond; +} + + +const struct uhci_functions uhci_functions = { + .find_devices = &find_devices, + .activate_device = &activate_device, + .send_packets = &do_packets, + .add_pipe = &add_pipe +}; diff --git a/uhci/include/uhci-hcd.h b/uhci/include/uhci-hcd.h new file mode 100644 index 0000000..46e4093 --- /dev/null +++ b/uhci/include/uhci-hcd.h @@ -0,0 +1,39 @@ +/***************************************************************************** +* 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 UHCI_HCD_H +#define UHCI_HCD_H + +#include +#include + +struct uhci_functions { + void (*find_devices)(struct cdi_usb_hc *device); + void (*activate_device)(struct cdi_usb_device *device); + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet *packets, + size_t num_packets); + void (*add_pipe)(struct cdi_usb_pipe *pipe); +}; + +cdi_list_t uhci_find_devices(struct cdi_usb_hc *device); + +#endif diff --git a/uhci/include/uhci-init.h b/uhci/include/uhci-init.h new file mode 100644 index 0000000..7b357bc --- /dev/null +++ b/uhci/include/uhci-init.h @@ -0,0 +1,31 @@ +/***************************************************************************** +* 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 UHCI_INIT_H +#define UHCI_INIT_H + +#include "uhci.h" + +void find_and_init_uhcis(void); +void uhci_init(struct cdi_device *dev); + +#endif diff --git a/uhci/include/uhci-transfer.h b/uhci/include/uhci-transfer.h new file mode 100644 index 0000000..53b46b0 --- /dev/null +++ b/uhci/include/uhci-transfer.h @@ -0,0 +1,30 @@ +/***************************************************************************** +* 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 UHCI_TRANSFER_H +#define UHCI_TRANSFER_H + +#include + +void uhci_handler(struct cdi_device *cdi_hci); + +#endif diff --git a/uhci/include/uhci.h b/uhci/include/uhci.h new file mode 100644 index 0000000..678b2a1 --- /dev/null +++ b/uhci/include/uhci.h @@ -0,0 +1,138 @@ +/***************************************************************************** +* 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 UHCI_H +#define UHCI_H + +#include +#include + +#include +#include +#include +#include + + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[uhci] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[uhci] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + + +#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 CONFIGURE 0x0040 +#define GLOB_SUSP_MODE 0x0008 +#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 + +//Legacy support +#define UHCI_PCI_LEGSUP 0xC0 //w + +#define UHCI_LEGSUP_STATUS 0x8F00 //Statusbits, die mittels schreiben einer 1 + //gel??scht werden +#define UHCI_LEGSUP_NO_CHG 0x5040 //Bits, die nicht ver??ndert werden k??nnen + //(RO bzw. reserviert) +#define UHCI_LEGSUP_PIRQ 0x2000 //Interrupt wird als PCI-IRQ ausgef??hrt + + +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 { + 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; +}; + +struct uhci { + struct cdi_usb_hc cdidev; + struct cdi_pci_device *pcidev; + int 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 mempool *buffers; +}; + +#endif diff --git a/uhci/init.c b/uhci/init.c new file mode 100644 index 0000000..4405e01 --- /dev/null +++ b/uhci/init.c @@ -0,0 +1,249 @@ +/***************************************************************************** +* 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 +#include +#include +#include + +#include "uhci.h" +#include "uhci-hcd.h" +#include "uhci-init.h" +#include "uhci-transfer.h" + +extern const struct uhci_functions uhci_functions; +extern int init_uhci(void); + +#define DRIVER_NAME "uhcd" + +static struct cdi_usb_hcd driver = { + .drv = { + .type = CDI_USB_HCD, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = &uhci_init, + .remove_device = NULL, + .init = &init_uhci, + .destroy = NULL + } +}; + +CDI_DRIVER(DRIVER_NAME, driver) + +void find_and_init_uhcis(void) +{ + struct uhci *hci; + char *name; + int uhci_counter = 0; + struct cdi_pci_device *dev; + + cdi_usb_hcd_init(&driver); + + cdi_list_t pci_devices = cdi_list_create(); + cdi_pci_get_all_devices(pci_devices); + for (int i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) { + if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03) + || (dev->interface_id != 0x00)) + { + cdi_pci_device_destroy(dev); + } else { + hci = malloc(sizeof(struct uhci)); + hci->cdidev.dev.type = CDI_USB_HCD; + name = malloc(7); + sprintf(name, "uhci%i", uhci_counter++); + hci->cdidev.dev.name = (const char *)name; + hci->cdidev.speed = CDI_USB_LOW_SPEED | CDI_USB_FULL_SPEED; + hci->pcidev = dev; + cdi_list_push(driver.drv.devices, hci); + } + } + + cdi_list_destroy(pci_devices); + + driver.find_devices = uhci_functions.find_devices; + driver.activate_device = uhci_functions.activate_device; + driver.send_packets = uhci_functions.send_packets; + driver.add_pipe = uhci_functions.add_pipe; +} + +void uhci_init(struct cdi_device *dev) +{ + struct uhci *uhci = (struct uhci *)dev; + struct cdi_pci_resource *res; + int size = 0, request_size, cmd, intr, i; +#ifdef CDI_PCI_DIRECT_ACCESS + int legsup; +#endif + + d0printf("Trying to init %s at %02x:%02x.%02x...\n", uhci->cdidev.dev.name, + uhci->pcidev->bus, uhci->pcidev->dev, uhci->pcidev->function); + + uhci->pbase = 0; + for (i = 0; (res = cdi_list_get(uhci->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) { + d0printf("Cannot allocate I/O ports.\n"); + return; + } + uhci->pbase = res->start; + break; + } + } + if (!uhci->pbase) { + d0printf("No I/O space found.\n"); + return; + } + request_size = + // Maximale Paketl??nge (Full Speed) ist 1023, Alignment au??erdem + 1024 + + // Transferdeskriptor + sizeof(struct uhci_td); + + uhci->buffers = mempool_create(8192, request_size); + if (uhci->buffers == NULL) { + d0printf("Cannot create mempool.\n"); + return; + } + + if (cdi_alloc_phys_mem(sizeof(*uhci->queue_heads) * 1024, + (void **)&uhci->queue_heads, (void **)&uhci->phys_queue_heads) == -1) + { + d0printf("Cannot allocate QH memory.\n"); + return; + } + + if (cdi_alloc_phys_mem(4096, (void **)&uhci->frame_list, + (void **)&uhci->phys_frame_list) == -1) + { + d0printf("Cannot allocate frame list.\n"); + return; + } + +#ifdef CDI_PCI_DIRECT_ACCESS + cdi_pci_config_outw(uhci->pcidev, 4, 0x05); +#endif + + cdi_register_irq(uhci->pcidev->irq, &uhci_handler, dev); + uhci->root_ports = (size - 0x10) >> 1; + //Erstmal testen, wie viele wir genau haben... + for (i = 2; i < uhci->root_ports; i++) { + if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + i * 2) & 0x0080) || + (cdi_inw(uhci->pbase + UHCI_RPORTS + i * 2) == 0xFFFF)) + { + uhci->root_ports = i; + break; + } + } + //Mehr als sieben Rootports, klingt lustig + if (uhci->root_ports > 7) { + uhci->root_ports = 2; + } + + d1printf("I/O 0x%04X (%i ports), IRQ %i\n", uhci->pbase, uhci->root_ports, + uhci->pcidev->irq); + +#ifdef CDI_PCI_DIRECT_ACCESS + legsup = cdi_pci_config_inw(uhci->pcidev, UHCI_PCI_LEGSUP); +#endif + cmd = cdi_inw(uhci->pbase + UHCI_USBCMD); + intr = cdi_inw(uhci->pbase + UHCI_USBINTR); + +#ifdef CDI_PCI_DIRECT_ACCESS + //Wenn der HC l??uft, das Configurebit gesetzt ist, der nicht runtergefahren + //ist oder dieses Interrupt-enable-Bit gesetzt ist, dann war wohl schon + //jemand dran. + //(oder wenn der Legacy Support aktiviert ist, das hei??t, wenn in LEGSUP + //irgendein Bit gesetzt ist, dass nicht RO/reserviert oder ein Statusbit + //ist) + if ((legsup & ~(UHCI_LEGSUP_STATUS | UHCI_LEGSUP_NO_CHG)) || + (cmd & USB_RUN) || !(cmd & CONFIGURE) || !(cmd & GLOB_SUSP_MODE) || + (intr & 0x0002)) + { + //Na, wer hat denn da am HC rumgefummelt. B????????ses BIOS. Oder SMM. Oder + //wer auch immer. + //Alle LEGSUP-Statusbits l??schen + cdi_pci_config_outw(uhci->pcidev, UHCI_PCI_LEGSUP, + UHCI_LEGSUP_STATUS); +#endif + //Resetten + cdi_outw(uhci->pbase + UHCI_USBCMD, HCRESET); + //Warten, bis der Reset beendet ist + for (i = 0; (cdi_inw(uhci->pbase + UHCI_USBCMD) & HCRESET) && + (i < 50); i++) + { + cdi_sleep_ms(10); + } + if (i == 50) { + d1printf("Reset not finished...\n"); + } + + //Interrupts erstmal deaktivieren + cdi_outw(uhci->pbase + UHCI_USBINTR, 0); + //Den gesamten HC ebenso + cdi_outw(uhci->pbase + UHCI_USBCMD, 0); + + //Alle Ports "runterfahren" + for (int port = 0; port < uhci->root_ports; port++) { + cdi_outw(uhci->pbase + UHCI_RPORTS + (port * 2), 0); + } +#ifdef CDI_PCI_DIRECT_ACCESS + } +#endif + + for (i = 0; i < 1024; i++) { + 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; + } + + //Standardwert, jede Millisekunde ein Frame + cdi_outb(uhci->pbase + UHCI_SOFMOD, 0x40); + //Unsere Frameliste eintragen + cdi_outl(uhci->pbase + UHCI_FRBASEADD, uhci->phys_frame_list); + //Frame zur??cksetzen + cdi_outw(uhci->pbase + UHCI_FRNUM, 0); +#ifdef CDI_PCI_DIRECT_ACCESS + //Hier setzen wir das PIRQ-Bit und deaktivieren s??mtlichen Legacy Support. + cdi_pci_config_outw(uhci->pcidev, UHCI_PCI_LEGSUP, UHCI_LEGSUP_PIRQ); +#else + d0printf("Failed to deactivate USB legacy support.\n"); +#endif + + //HC starten + cdi_outw(uhci->pbase + UHCI_USBCMD, USB_RUN | CONFIGURE | MAXP); + //Alle Interrupts aktivieren + cdi_outw(uhci->pbase + UHCI_USBINTR, 0xF); + + //Connect status change l??schen + for (i = 0; i < uhci->root_ports; i++) { + cdi_outw(uhci->pbase + UHCI_RPORTS + i * 2, RPORT_CSC); + } +} diff --git a/uhci/main.c b/uhci/main.c new file mode 100644 index 0000000..8c5fb07 --- /dev/null +++ b/uhci/main.c @@ -0,0 +1,34 @@ +/***************************************************************************** +* 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 "uhci-init.h" + +int init_uhci() +{ + cdi_init(); + + find_and_init_uhcis(); + + return 0; +} diff --git a/uhci/transfer.c b/uhci/transfer.c new file mode 100644 index 0000000..33c60bf --- /dev/null +++ b/uhci/transfer.c @@ -0,0 +1,91 @@ +/***************************************************************************** +* 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 "uhci.h" + +cdi_list_t uhci_active_transfers = NULL; + +void uhci_handler(struct cdi_device *cdi_hci) +{ + struct uhci *uhci = (struct uhci *)cdi_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; + } + //Ist Bit 0 gesetzt, dann ist das ein normaler IOC (Interrupt on + //completion), bei Bit 1 ist ein Fehler aufgetreten, aber das werden wir ja + //unten sehen. + if (status & ~0x0003) { + d1printf("Unerwarteter IRQ 0x%04X von %s\n", status, cdi_hci->name); + } + if (status & 0x10) { + d0printf("SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n"); + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! + } else { + for (i = 0; (addr = cdi_list_get(uhci_active_transfers, i)) != NULL; + i++) + { + td = addr->virt; + if (td->active) { + continue; + } + addr->error = CDI_USB_NO_ERROR; + cdi_list_remove(uhci_active_transfers, i--); + if (td->stalled_err) { + d0printf("ENDPOINT STALLED\n"); + addr->error |= CDI_USB_STALLED; + } + if (td->buf_err) { + d1printf("Puffer??berlauf oder Underrun\n"); + addr->error |= CDI_USB_BUFFER_ERROR; + } + if (td->babble) { + d1printf("Da war ein Ger??t wohl sehr gespr??chig: Babble.\n"); + addr->error |= CDI_USB_BABBLE; + } + if (td->nak) { + d1printf("NAK empfangen\n"); + addr->error |= CDI_USB_NAK; + } + if (td->crc_time_err) { + d1printf("CRC-Fehler oder Timeout\n"); + addr->error |= CDI_USB_CRC | CDI_USB_TIMEOUT; + } + if (td->bitstuff_err) { + d1printf("Bitstufffehler\n"); + addr->error |= CDI_USB_BITSTUFF; + } + //TODO: free(td) - dazu w??re eine CDI-Funktion n??tzlich + } + } + cdi_outw(uhci->pbase + UHCI_USBSTS, status); +} -- 1.6.4.2 From max at tyndur.org Sat Dec 12 18:47:14 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 12 Dec 2009 18:47:14 +0100 Subject: [cdi-devel] [PATCH 5/6] USB 1.1 In-Reply-To: <1260640035-11473-5-git-send-email-max@tyndur.org> References: <1260640035-11473-1-git-send-email-max@tyndur.org> <1260640035-11473-2-git-send-email-max@tyndur.org> <1260640035-11473-3-git-send-email-max@tyndur.org> <1260640035-11473-4-git-send-email-max@tyndur.org> <1260640035-11473-5-git-send-email-max@tyndur.org> Message-ID: <1260640035-11473-6-git-send-email-max@tyndur.org> From: Max Reitz + Added USB 1.1 bus driver Signed-off-by: Max Reitz --- usb11/control.c | 252 +++++++++++++++++++++++++++++++++++++++++++ usb11/include/control.h | 31 ++++++ usb11/include/transfer.h | 38 +++++++ usb11/include/usb-structs.h | 184 +++++++++++++++++++++++++++++++ usb11/include/usb11.h | 41 +++++++ usb11/main.c | 58 ++++++++++ usb11/transfer.c | 223 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 827 insertions(+), 0 deletions(-) create mode 100644 usb11/control.c create mode 100644 usb11/include/control.h create mode 100644 usb11/include/transfer.h create mode 100644 usb11/include/usb-structs.h create mode 100644 usb11/include/usb11.h create mode 100644 usb11/main.c create mode 100644 usb11/transfer.c diff --git a/usb11/control.c b/usb11/control.c new file mode 100644 index 0000000..170341d --- /dev/null +++ b/usb11/control.c @@ -0,0 +1,252 @@ +/***************************************************************************** +* 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 "usb11.h" +#include "usb-structs.h" +#include "transfer.h" + +static int usb_dev_ids = 1; + +static void enumerate_hub(struct cdi_usb_device *usbdev); + +static int usb11_real_enumerate(struct cdi_usb_device *device) +{ + struct endpoint_desc *ep0; + struct device_desc *dev_desc; + struct cdi_usb_hcd *hcd; + int id; + char *name; + void *position; + struct config_desc *conf_desc; + struct interface_desc *if_desc, *best_if; + + hcd = (struct cdi_usb_hcd *)device->hc->dev.driver; + + //EP0 initialisieren + device->ep0 = malloc(sizeof(*device->ep0)); + device->ep0->endpoint = malloc(sizeof(struct endpoint_desc)); + ep0 = (struct endpoint_desc *)device->ep0->endpoint; + ep0->length = sizeof(struct endpoint_desc); + ep0->descriptor_type = DESC_ENDPOINT; + ep0->endpoint_address = 0; + ep0->attributes = 0; + ep0->max_packet_size = 8; + ep0->interval = 0; + device->ep0->device = device; + device->ep0->data_toggle = 0; + + //Zur??cksetzen + device->reset(device); + + //Pipe erzeugen + hcd->add_pipe(device->ep0); + + //Erste acht Bytes des Device-Descriptors abrufen und die maximale + //Paketgr????e von EP0 feststellen + dev_desc = usb11_do_control(device, DEV_TO_HOST, NULL, 8, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); + if (dev_desc == NULL) { + return 0; + } + ep0->max_packet_size = dev_desc->max_packet_size0; + + //Nochmals zur??cksetzen + device->reset(device); + + //USB-Adresse zuweisen + usb11_do_control(device, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, + REC_DEVICE, SET_ADDRESS, (id = usb_dev_ids++), 0); + device->id = id; + if (usb_dev_ids >= 128) { + d0printf("Warning: USB device ID overflow.\n"); + usb_dev_ids = 1; + } + + //So, jetzt haben wir eine Adresse, da gibts auch eine neue Pipe + hcd->add_pipe(device->ep0); + + //Den ganzen Device-Descriptor einlesen + dev_desc = usb11_do_control(device, DEV_TO_HOST, NULL, sizeof(*dev_desc), + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); + device->device_desc = dev_desc; + device->vendor_id = dev_desc->vendor_id; + device->device_id = dev_desc->device_id; + + if (dev_desc->iManufacturer) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iManufacturer, 0); + if (name == NULL) { + return 0; + } + d1printf(" -> Manufacturer: "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf("\n"); + } + if (dev_desc->iProduct) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iProduct, 0); + if (name == NULL) { + return 0; + } + d1printf(" -> Product: "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf("\n"); + } + //TODO: Man kann doch nicht immer den ersten nehmen... + conf_desc = usb11_do_control(device, DEV_TO_HOST, NULL, + sizeof(struct config_desc), STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return 0; + } + conf_desc = usb11_do_control(device, DEV_TO_HOST, NULL, + conf_desc->total_length, STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return 0; + } + device->config_desc = conf_desc; + usb11_do_control(device, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_CONFIGURATION, conf_desc->config_value, 0); + if (conf_desc->iConfiguration) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | conf_desc->iConfiguration, 0); + if (name == NULL) { + return 0; + } + d1printf("Using configuration "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf(".\n"); + } + position = conf_desc; + position = (void *)((uintptr_t)position + sizeof(struct config_desc)); + best_if = position; //Standard-IF + for (int i = 0; i < conf_desc->num_interfaces; i++) { + if_desc = position; + //TODO MSD ist zwar h??bsch. Aber andere auch ;-) + if (if_desc->interface_class == 8) { + best_if = if_desc; + break; + } + position = (void *)((uintptr_t)position + + sizeof(struct interface_desc) + + if_desc->num_endpoints * sizeof(struct endpoint_desc)); + } + usb11_do_control(device, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_INTERFACE, best_if->interface_number, 0); + device->interface_desc = (void *)best_if; + if (best_if->iInterface) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | best_if->iInterface, 0); + if (name == NULL) { + return 0; + } + d1printf("Using interface "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf(".\n"); + } + d1printf("Configuration: %i:%i (%i)\n", conf_desc->config_value, + best_if->interface_number, best_if->interface_class); + + if (!dev_desc->class_id) { //Composite Device + device->class_id = best_if->interface_class; + device->subclass_id = best_if->interface_subclass; + device->protocol_id = best_if->interface_protocol; + } else { + device->class_id = dev_desc->class_id; + device->subclass_id = dev_desc->subclass_id; + device->protocol_id = dev_desc->protocol_id; + } + + if (best_if->interface_class == USBDC_HUB) { + enumerate_hub(device); + } + + return 1; +} + +void usb11_enumerate(struct cdi_usb_device *device) +{ + if (!usb11_real_enumerate(device)) { + free(device->ep0->endpoint); + free(device->ep0); + } +} + +static void reset_hub_device(struct cdi_usb_device *usbdev) +{ + usb11_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 cdi_usb_device *usbdev) +{ + struct hub_desc *hub_desc; + struct cdi_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 = usb11_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 = usb11_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 + usb11_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); + down = malloc(sizeof(struct cdi_usb_device)); + down->hc = usbdev->hc; + down->hub = usbdev; + down->id = 0; + down->port = i; + down->speed = (port_status[0] & PORT_LOWSPEED) ? + CDI_USB_LOW_SPEED : CDI_USB_FULL_SPEED; + down->reset = &reset_hub_device; + cdi_usb_device_init(down); + } +} diff --git a/usb11/include/control.h b/usb11/include/control.h new file mode 100644 index 0000000..97edefc --- /dev/null +++ b/usb11/include/control.h @@ -0,0 +1,31 @@ +/***************************************************************************** +* 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 CONTROL_H +#define CONTROL_H + +#include +#include + +void usb11_enumerate(struct cdi_usb_device *device); + +#endif diff --git a/usb11/include/transfer.h b/usb11/include/transfer.h new file mode 100644 index 0000000..3b0f086 --- /dev/null +++ b/usb11/include/transfer.h @@ -0,0 +1,38 @@ +/***************************************************************************** +* 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 TRANSFER_H +#define TRANSFER_H + +#include + +#include + +void *usb11_do_control(struct cdi_usb_device *device, int direction, + void *buffer, int length, int rtype, int recipient, int request, int value, + int index); +void *usb11_export_control(struct cdi_usb_device *device, + struct cdi_usb_control_transfer *transfer); +cdi_usb_status_t usb11_send_packets(struct cdi_usb_packet *packets, + size_t num_packets); + +#endif diff --git a/usb11/include/usb-structs.h b/usb11/include/usb-structs.h new file mode 100644 index 0000000..427d0e5 --- /dev/null +++ b/usb11/include/usb-structs.h @@ -0,0 +1,184 @@ +/***************************************************************************** +* 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 USB_STRUCTS_H +#define USB_STRUCTS_H + +#include + + +#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 + + +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; + +#endif diff --git a/usb11/include/usb11.h b/usb11/include/usb11.h new file mode 100644 index 0000000..77634e9 --- /dev/null +++ b/usb11/include/usb11.h @@ -0,0 +1,41 @@ +/***************************************************************************** +* 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 USB11_H +#define USB11_H + +#include + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[usb 1.1] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[usb 1.1] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + +#endif diff --git a/usb11/main.c b/usb11/main.c new file mode 100644 index 0000000..e381062 --- /dev/null +++ b/usb11/main.c @@ -0,0 +1,58 @@ +/***************************************************************************** +* 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 "control.h" +#include "transfer.h" + +#define DRIVER_NAME "USB 1.1" + +int init_usb11(void); + +static struct cdi_usb_driver driver = { + .drv = { + .type = CDI_USB, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = NULL, + .remove_device = NULL, + .init = &init_usb11, + .destroy = NULL + }, + .speeds = CDI_USB_LOW_SPEED | CDI_USB_FULL_SPEED, + .enumerate_device = &usb11_enumerate, + .send_packets = &usb11_send_packets, + .do_control = &usb11_export_control +}; + +CDI_DRIVER(DRIVER_NAME, driver) + +int init_usb11() +{ + cdi_init(); + + cdi_usb_driver_init(&driver); + + return 0; +} diff --git a/usb11/transfer.c b/usb11/transfer.c new file mode 100644 index 0000000..eb4f949 --- /dev/null +++ b/usb11/transfer.c @@ -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. * +*****************************************************************************/ + +#include +#include + +#include + +#include "transfer.h" +#include "usb11.h" +#include "usb-structs.h" + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +/** + * Verarbeitet USB-Pakete. + * + * @param packet Die Paketliste + * @param num_packets Anzahl der Pakete + * + * @return Status + */ + +cdi_usb_status_t usb11_send_packets(struct cdi_usb_packet *packets, + size_t num_packets) +{ + cdi_usb_status_t error = CDI_USB_NAK; + int i = 0, mps; + int num_out_packets; + struct cdi_usb_pipe *pipe; + struct cdi_usb_device *device; + struct cdi_usb_packet *out_packets; + struct endpoint_desc *ep; + struct cdi_usb_hcd *hcd; + + if (num_packets < 1) { + return CDI_USB_TRIVIAL_ERROR; + } + + // Alle Pakete muessen dieselbe Pipe benutzen + pipe = packets[0].pipe; + for (i = 1; i < (int)num_packets; i++) { + if (packets[i].pipe != pipe) { + return CDI_USB_TRIVIAL_ERROR; + } + } + + device = pipe->device; + hcd = (struct cdi_usb_hcd *)device->hc->dev.driver; + ep = (struct endpoint_desc *)pipe->endpoint; + + // Pruefen, wie viele Pakete wir draus machen muessen + mps = ep->max_packet_size; + num_out_packets = 0; + for (i = 0; i < (int)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 == (int)num_packets) { + out_packets = packets; + } else { + int j; + int cur = 0; + + out_packets = calloc(num_out_packets, sizeof(*out_packets)); + for (i = 0; i < (int)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((int)packets[i].length, mps); + out_packets[cur].condition = CDI_USB_NAK; + + packets[i].data = (void *)((uintptr_t)packets[i].data + + out_packets[cur].length); + packets[i].length -= out_packets[cur].length; + cur++; + } + } + } + + error = hcd->send_packets(out_packets, num_out_packets); + if (error & CDI_USB_STALLED) { + d0printf("ENDPOINT 0x%02X OF DEVICE %i STALLED!\n", + ep->endpoint_address, device->id); + usb11_do_control(device, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, + REC_ENDPOINT, CLEAR_FEATURE, 0, ep->endpoint_address); + } else if (error & CDI_USB_NAK) { + // Nicht angenommene Pakete (FIXME: einzeln) senden + for (i = 0; i < num_out_packets; i++) { + if (!(out_packets[i].condition & CDI_USB_NAK)) { + continue; + } + error = CDI_USB_NAK; + while (error & CDI_USB_NAK) { + error = hcd->send_packets(&out_packets[i], 1); + if (error & CDI_USB_NAK) { + cdi_sleep_ms(5); + } + } + } + } + + if (out_packets != packets) { + free(out_packets); + } + + return error; +} + +void *usb11_do_control(struct cdi_usb_device *device, int direction, + void *buffer, int length, int rtype, int recipient, int request, int value, + int index) +{ + int no_data, alloced_here = 0; + cdi_usb_status_t rval; + struct setup_packet _setup; + struct setup_packet *setup = &_setup; + + no_data = direction & NO_DATA; + direction &= 0x80; + + setup->request_type = direction | (rtype & 0x60) | (recipient & 0x1F); + setup->request = request; + setup->value = value; + setup->index = index; + setup->length = length; + + struct cdi_usb_packet setup_packet = { + .pipe = device->ep0, + .type = CDI_USB_PACKET_SETUP, + .data = setup, + .length = sizeof(*setup), + .use_toggle = TOGGLE_UNSPECIFIC + }; + + if (usb11_send_packets(&setup_packet, 1)) { + return NULL; + } + + if (no_data) { + rval = CDI_USB_NO_ERROR; + buffer = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten + } else { + if ((direction != HOST_TO_DEV) || (length && (buffer == NULL))) { + buffer = malloc(length); + alloced_here = 1; + } + + struct cdi_usb_packet data_packet = { + .pipe = device->ep0, + .type = (direction == DEV_TO_HOST ? + CDI_USB_PACKET_IN : CDI_USB_PACKET_OUT), + .data = buffer, + .length = length, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + rval = usb11_send_packets(&data_packet, 1); + } + + if (rval == CDI_USB_NO_ERROR) { + struct cdi_usb_packet ack_packet = { + .pipe = device->ep0, + .data = NULL, + .length = 0, + .use_toggle = TOGGLE_1 + }; + + if (no_data || (direction == HOST_TO_DEV)) { + ack_packet.type = CDI_USB_PACKET_IN; + } else { + ack_packet.type = CDI_USB_PACKET_OUT; + } + + rval = usb11_send_packets(&ack_packet, 1); + } + + if ((rval != CDI_USB_NO_ERROR) && alloced_here) { + free(buffer); + } + + return (rval == CDI_USB_NO_ERROR) ? buffer : NULL; +} + +void *usb11_export_control(struct cdi_usb_device *device, + struct cdi_usb_control_transfer *transfer) +{ + return usb11_do_control(device, transfer->direction, transfer->buffer, + transfer->length, transfer->rtype, transfer->recipient, + transfer->request, transfer->value, transfer->index); +} -- 1.6.4.2 From max at tyndur.org Sat Dec 12 18:47:15 2009 From: max at tyndur.org (max at tyndur.org) Date: Sat, 12 Dec 2009 18:47:15 +0100 Subject: [cdi-devel] [PATCH 6/6] USB MSD In-Reply-To: <1260640035-11473-6-git-send-email-max@tyndur.org> References: <1260640035-11473-1-git-send-email-max@tyndur.org> <1260640035-11473-2-git-send-email-max@tyndur.org> <1260640035-11473-3-git-send-email-max@tyndur.org> <1260640035-11473-4-git-send-email-max@tyndur.org> <1260640035-11473-5-git-send-email-max@tyndur.org> <1260640035-11473-6-git-send-email-max@tyndur.org> Message-ID: <1260640035-11473-7-git-send-email-max@tyndur.org> From: Max Reitz + Added USB mass storage driver (currently just bulk-only devices supported) Signed-off-by: Max Reitz --- usb-msd/include/msd.h | 121 ++++++++++++ usb-msd/main.c | 68 +++++++ usb-msd/msd.c | 486 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 675 insertions(+), 0 deletions(-) create mode 100644 usb-msd/include/msd.h create mode 100644 usb-msd/main.c create mode 100644 usb-msd/msd.c diff --git a/usb-msd/include/msd.h b/usb-msd/include/msd.h new file mode 100644 index 0000000..bb6cf51 --- /dev/null +++ b/usb-msd/include/msd.h @@ -0,0 +1,121 @@ +/***************************************************************************** +* 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 MSD_H +#define MSD_H + +#include +#include + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[usb-msd] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[usb-msd] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + +//"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 cdi_usb_device *usb_device; + struct cdi_usb_pipe *bulk_in; + struct cdi_usb_pipe *bulk_out; + 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 + +int check_msd(struct cdi_usb_device *device); +int msd_read_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer); +int msd_write_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer); + +#endif diff --git a/usb-msd/main.c b/usb-msd/main.c new file mode 100644 index 0000000..c5c401f --- /dev/null +++ b/usb-msd/main.c @@ -0,0 +1,68 @@ +/***************************************************************************** +* 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 "msd.h" + +#define DRIVER_NAME "usb-msd" + +static int init_msd(void); + +struct cdi_storage_driver msc_driver = { + .drv = { + .type = CDI_STORAGE, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = NULL, + .remove_device = NULL, + .init = &init_msd, + .destroy = NULL + }, + .read_blocks = &msd_read_blocks, + .write_blocks = &msd_write_blocks +}; + +CDI_DRIVER(DRIVER_NAME, msc_driver) + +static struct cdi_usb_device_pattern msd_pattern = { + .vendor_id = -1, + .device_id = -1, + .class_id = 8, + .subclass_id = -1, + .protocol_id = -1, + + .take_usb_device = &check_msd +}; + +static int init_msd() +{ + cdi_init(); + + cdi_storage_driver_init(&msc_driver); + + cdi_usb_register_device_pattern(&msd_pattern); + + return 0; +} diff --git a/usb-msd/msd.c b/usb-msd/msd.c new file mode 100644 index 0000000..336a250 --- /dev/null +++ b/usb-msd/msd.c @@ -0,0 +1,486 @@ +/***************************************************************************** +* 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 +#include +#include + +#include "msd.h" + +//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 + +extern struct cdi_storage_driver msc_driver; +static uint32_t cbw_tag = 1; + +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); + +int msd_read_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer) +{ + return msd_cdi_read(device, start, count, buffer); +} + +int msd_write_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer) +{ + return msd_cdi_write(device, start, count, buffer); +} + +static int msd_get_capacity(struct cdi_usb_device *usbdev, + uint32_t *block_size, uint32_t *block_count); + +int check_msd(struct cdi_usb_device *device) +{ + struct cdi_storage_device *strgdev; + struct cdi_usb_endpoint *ep_desc; + struct cdi_msd *cdimsd; + struct cdi_usb_hcd *hcd; + void *address; + int i; + uint32_t bs, bc; + uint64_t size; + static int msdnum = 0; + + //Nur Bulk-Only + if (device->protocol_id != 0x50) { + return 0; + } + + d0printf("MSD detected.\n"); + + cdimsd = malloc(sizeof(struct cdi_msd)); + if (cdimsd == NULL) { + return 0; + } + cdimsd->usb_device = device; + 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 0; + } + sprintf((char *)strgdev->dev.name, "usb-msd%i", msdnum++); + strgdev->dev.driver = (struct cdi_driver *)&msc_driver; + device->driver_data = cdimsd; + cdimsd->bulk_in = malloc(sizeof(*cdimsd->bulk_in)); + cdimsd->bulk_in->device = device; + cdimsd->bulk_in->endpoint = NULL; + cdimsd->bulk_in->data_toggle = 0; + cdimsd->bulk_out = malloc(sizeof(*cdimsd->bulk_out)); + cdimsd->bulk_out->device = device; + cdimsd->bulk_out->endpoint = NULL; + cdimsd->bulk_out->data_toggle = 0; + address = (void *)((uintptr_t)device->interface_desc + + sizeof(struct cdi_usb_interface)); + for (i = 0; i < device->interface_desc->num_endpoints; i++) { + ep_desc = address; + if ((ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (cdimsd->bulk_in->endpoint == NULL)) + { + //BULK-IN + cdimsd->bulk_in->endpoint = ep_desc; + } + else if (!(ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (cdimsd->bulk_out->endpoint == NULL)) + { + //BULK-OUT + cdimsd->bulk_out->endpoint = ep_desc; + } + address = (void *)((uintptr_t)address + + sizeof(struct cdi_usb_endpoint)); + } + if ((cdimsd->bulk_in->endpoint == NULL) || + (cdimsd->bulk_out->endpoint == NULL)) + { + d0printf("Not enough endpoints found.\n"); + return 0; + } + hcd = (struct cdi_usb_hcd *)device->hc->dev.driver; + hcd->add_pipe(cdimsd->bulk_in); + hcd->add_pipe(cdimsd->bulk_out); + if (!msd_get_capacity(device, &bs, &bc)) { + strgdev->block_size = 0; + strgdev->block_count = 0; + d1printf("Wasn't able to determine size of %s.\n", strgdev->dev.name); + } else { + strgdev->block_size = bs; + strgdev->block_count = bc; + size = bs; + size *= bc; + d1printf("%s: %i * %i B (about %d MB).\n", strgdev->dev.name, bc, bs, + (int)(size >> 20)); + } + cdi_storage_device_init(strgdev); + cdi_list_push(msc_driver.drv.devices, strgdev); + + return 1; +} + +static int write_cmd(struct cdi_usb_device *usbdev, void *src) +{ + struct cdi_msd *cdimsd = usbdev->driver_data; + struct cdi_usb_packet cmd_packet = { + .pipe = cdimsd->bulk_out, + .type = CDI_USB_PACKET_OUT, + .data = src, + .length = 0x1F, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + return usbdev->drv->send_packets(&cmd_packet, 1); +} + +/** + * 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 cdi_usb_device *usbdev, uint32_t expected_tag) +{ + struct cdi_msd *msc = usbdev->driver_data; + struct command_status_wrapper _csw; + struct command_status_wrapper *csw = &_csw; + cdi_usb_status_t error; + + struct cdi_usb_packet status_packet = { + .pipe = msc->bulk_in, + .type = CDI_USB_PACKET_IN, + .data = csw, + .length = 0x0D, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + error = usbdev->drv->send_packets(&status_packet, 1); + if (error != CDI_USB_NO_ERROR) { + return error; + } + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || csw->csw_status) + { + d1printf("0x%08X %i==%i 0x%08X\n", csw->csw_signature, csw->csw_tag, + expected_tag, csw->csw_status); + return CDI_USB_TRIVIAL_ERROR; + } + + return CDI_USB_NO_ERROR; +} + +static int msd_get_capacity(struct cdi_usb_device *usbdev, + uint32_t *block_size, uint32_t *block_count) +{ + struct cdi_msd *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper *cbw = &_cbw; + struct msd_capacity _cap; + struct msd_capacity *cap = &_cap; + uint32_t expected_tag; + + if ((usbdev == NULL) || (block_size == NULL) || (block_count == NULL)) { + return 0; + } + + msc = usbdev->driver_data; + 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 (write_cmd(usbdev, cbw) != CDI_USB_NO_ERROR) { + return 0; + } + + struct cdi_usb_packet in_packet = { + .pipe = msc->bulk_in, + .type = CDI_USB_PACKET_IN, + .data = cap, + .length = sizeof(*cap), + .use_toggle = TOGGLE_UNSPECIFIC + }; + + if (usbdev->drv->send_packets(&in_packet, 1) != CDI_USB_NO_ERROR) { + return 0; + } + + if (read_status(usbdev, expected_tag) != CDI_USB_NO_ERROR) { + 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 cdi_usb_device *usbdev) +{ + struct msclass_data *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper *cbw = &_cbw; + uint32_t expected_tag; + + msc = (struct msclass_data *)usbdev->driver_data; + 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, cbw) != USB_NO_ERROR) { + return 0; + } + + if (read_status(usbdev, expected_tag) != USB_NO_ERROR) { + return 0; + } + + return 1; +} +#endif + +#define MAX_ACCESS_BLOCKS 32 + +static uint32_t msd_read(struct cdi_usb_device *usbdev, uint32_t lba, + 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) +{ + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int i; +#endif + struct cdi_usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + int j, bbs; + + d1printf("read(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + + if (!count) { + d1printf("Empty read request.\n"); + return 0; + } + for (j = 0; j < (int)count; j += MAX_ACCESS_BLOCKS) { +#ifdef WAIT_FOR_MSD_READY + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { + cdi_sleep_ms(20); + } +#endif + bbs = (count - j > MAX_ACCESS_BLOCKS) ? MAX_ACCESS_BLOCKS : count - j; + error = msd_read(usbdev, start + j, bbs, + (void *)((uintptr_t)buffer + j * bs), bbs * bs); + if (error != CDI_USB_NO_ERROR) { + d0printf("Read error 0x%X at block %lld.\n", error, start + j); + return -1; + } + } + return 0; +} + +static uint32_t msd_read(struct cdi_usb_device *usbdev, uint32_t lba, + uint16_t sectors, void *buffer, size_t length) +{ + struct cdi_msd *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper *cbw = &_cbw; + uint32_t expected_tag; + int error; + + if ((buffer == NULL) || !length) { + return CDI_USB_TRIVIAL_ERROR; + } + msc = usbdev->driver_data; + 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 = write_cmd(usbdev, cbw); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + struct cdi_usb_packet in_packet = { + .pipe = msc->bulk_in, + .type = CDI_USB_PACKET_IN, + .data = buffer, + .length = length, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + error = usbdev->drv->send_packets(&in_packet, 1); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + error = read_status(usbdev, expected_tag); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + return CDI_USB_NO_ERROR; +} + +static uint32_t msd_write(struct cdi_usb_device *usbdev, uint32_t lba, + 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) +{ + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int i; +#endif + struct cdi_usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + int j, bbs; + + d1printf("write(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + + if (!count) { + d1printf("Empty write request.\n"); + return 0; + } + for (j = 0; j < (int)count; j += MAX_ACCESS_BLOCKS) { +#ifdef WAIT_FOR_MSD_READY + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) + cdi_sleep_ms(20); +#endif + bbs = (count - j > MAX_ACCESS_BLOCKS) ? MAX_ACCESS_BLOCKS : count - j; + error = msd_write(usbdev, start + j, bbs, + (void *)((uintptr_t)buffer + j * bs), bbs * bs); + if (error != CDI_USB_NO_ERROR) { + d0printf("Write error 0x%X at block %lld.\n", error, start + j); + return -1; + } + } + return 0; +} + +static uint32_t msd_write(struct cdi_usb_device *usbdev, uint32_t lba, + uint16_t sectors, void *buffer, size_t length) +{ + struct cdi_msd *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper* cbw = &_cbw; + uint32_t expected_tag; + int error; + + if ((buffer == NULL) || !length) { + return CDI_USB_TRIVIAL_ERROR; + } + msc = usbdev->driver_data; + 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, cbw); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + struct cdi_usb_packet out_packet = { + .pipe = msc->bulk_out, + .type = CDI_USB_PACKET_OUT, + .data = buffer, + .length = length, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + error = usbdev->drv->send_packets(&out_packet, 1); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + error = read_status(usbdev, expected_tag); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + return CDI_USB_NO_ERROR; +} -- 1.6.4.2 From kevin at tyndur.org Fri Dec 18 23:23:35 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Fri, 18 Dec 2009 23:23:35 +0100 Subject: [cdi-devel] [PATCH] Makefile In-Reply-To: <1260639957-11307-1-git-send-email-max@tyndur.org> References: <1260639957-11307-1-git-send-email-max@tyndur.org> Message-ID: <20091218222335.GC2463@silvanus> On Sat, Dec 12, 2009 at 06:45:50PM +0100, max at tyndur.org wrote: > From: Max Reitz > > * Use C99 (better: gnu99) instead of implicit C89 > * Ignore errors upon make clean > > Signed-off-by: Max Reitz Thanks, applied. From matthew at theiselins.net Sat Dec 19 07:40:39 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Sat, 19 Dec 2009 16:40:39 +1000 Subject: [cdi-devel] [PATCH] Fix a signed/unsigned comparison warning in e1000 Message-ID: <1261204839-3244-1-git-send-email-matthew@theiselins.net> Hi, This patch merely fixes a signed/unsigned comparison warning in the e1000 driver that was missed in the previous warning fix commit. Cheers, Matt Signed-off-by: Matthew Iselin --- e1000/device.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/e1000/device.h b/e1000/device.h index 899a06d..34f9dc1 100644 --- a/e1000/device.h +++ b/e1000/device.h @@ -149,7 +149,7 @@ struct e1000_device { struct e1000_tx_descriptor tx_desc[TX_BUFFER_NUM]; uint8_t tx_buffer[TX_BUFFER_NUM * TX_BUFFER_SIZE]; - int tx_cur_buffer; + uint32_t tx_cur_buffer; struct e1000_rx_descriptor rx_desc[RX_BUFFER_NUM]; uint8_t rx_buffer[RX_BUFFER_NUM * RX_BUFFER_SIZE]; -- 1.6.4.msysgit.0 From kevin at tyndur.org Sun Dec 20 01:05:06 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 20 Dec 2009 01:05:06 +0100 Subject: [cdi-devel] [PATCH] Fix a signed/unsigned comparison warning in e1000 In-Reply-To: <1261204839-3244-1-git-send-email-matthew@theiselins.net> References: <1261204839-3244-1-git-send-email-matthew@theiselins.net> Message-ID: <20091220000505.GA9805@silvanus> On Sat, Dec 19, 2009 at 04:40:39PM +1000, Matthew Iselin wrote: > Hi, > > This patch merely fixes a signed/unsigned comparison warning in the e1000 driver that was missed in the previous warning fix commit. > > Cheers, > Matt > > Signed-off-by: Matthew Iselin Thanks, applied. From kevin at tyndur.org Sun Dec 20 01:09:16 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 20 Dec 2009 01:09:16 +0100 Subject: [cdi-devel] [PATCH] Direct access to PCI configuration space In-Reply-To: <1260640035-11473-1-git-send-email-max@tyndur.org> References: <1260640035-11473-1-git-send-email-max@tyndur.org> Message-ID: <20091220000916.GB9805@silvanus> On Sat, Dec 12, 2009 at 06:47:09PM +0100, max at tyndur.org wrote: > From: Max Reitz > > + Some drivers need direct access to the PCI configuration space > > Signed-off-by: Max Reitz > --- > include/cdi/pci.h | 12 ++++++++++++ > 1 files changed, 12 insertions(+), 0 deletions(-) > > diff --git a/include/cdi/pci.h b/include/cdi/pci.h > index 6b1e6f0..4bf38ea 100644 > --- a/include/cdi/pci.h > +++ b/include/cdi/pci.h > @@ -87,6 +87,18 @@ void cdi_pci_alloc_memory(struct cdi_pci_device* device); > */ > void cdi_pci_free_memory(struct cdi_pci_device* device); > > +/** > + * Signalisiert CDI-Treibern Zugriff auf den PCI-Konfigurationsraum > + */ > +#define CDI_PCI_DIRECT_ACCESS > + > +uint8_t cdi_pci_config_inb(struct cdi_pci_device *device, int offset); > +uint16_t cdi_pci_config_inw(struct cdi_pci_device *device, int offset); > +uint32_t cdi_pci_config_inl(struct cdi_pci_device *device, int offset); > +void cdi_pci_config_outb(struct cdi_pci_device *device, int offset, uint8_t value); > +void cdi_pci_config_outw(struct cdi_pci_device *device, int offset, uint16_t value); > +void cdi_pci_config_outl(struct cdi_pci_device *device, int offset, uint32_t value); Seems Toni isn't going to reply himself, but he complained in IRC that offset should be an uint8_t instead of an int. My own complaint is that comments are missing. I said I was accepting missing comments for mempool because it will be changed anyway, but this code is going to stay, so it clearly needs appropriate documentation. From max at tyndur.org Sun Dec 20 11:56:44 2009 From: max at tyndur.org (max at tyndur.org) Date: Sun, 20 Dec 2009 11:56:44 +0100 Subject: [cdi-devel] [PATCH v2] Direct access to PCI configuration space Message-ID: <1261306604-7338-1-git-send-email-max@tyndur.org> From: Max Reitz + Some drivers need direct access to the PCI configuration space Signed-off-by: Max Reitz --- include/cdi/pci.h | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 114 insertions(+), 0 deletions(-) diff --git a/include/cdi/pci.h b/include/cdi/pci.h index 6b1e6f0..80c07fa 100644 --- a/include/cdi/pci.h +++ b/include/cdi/pci.h @@ -87,6 +87,120 @@ void cdi_pci_alloc_memory(struct cdi_pci_device* device); */ void cdi_pci_free_memory(struct cdi_pci_device* device); +/** + * \if german + * Signalisiert CDI-Treibern direkten Zugriff auf den PCI-Konfigurationsraum + * \elseif english + * Indicates direct access to the PCI configuration space to CDI drivers + * \endif + */ +#define CDI_PCI_DIRECT_ACCESS + +/** + * \if german + * Liest ein Byte (8 Bit) aus dem PCI-Konfigurationsraum eines PCI-Geraets + * + * @param device Das Geraet + * @param offset Der Offset im Konfigurationsraum + * + * @return Der Wert an diesem Offset + * \elseif english + * Reads a byte (8 bit) from the PCI configuration space of a PCI device + * + * @param device The device + * @param offset The offset in the configuration space + * + * @return The corresponding value + * \endif + */ +uint8_t cdi_pci_config_inb(struct cdi_pci_device* device, uint8_t offset); + +/** + * \if german + * Liest ein Word (16 Bit) aus dem PCI-Konfigurationsraum eines PCI-Geraets + * + * @param device Das Geraet + * @param offset Der Offset im Konfigurationsraum + * + * @return Der Wert an diesem Offset + * \elseif english + * Reads a word (16 bit) from the PCI configuration space of a PCI device + * + * @param device The device + * @param offset The offset in the configuration space + * + * @return The corresponding value + * \endif + */ +uint16_t cdi_pci_config_inw(struct cdi_pci_device* device, uint8_t offset); + +/** + * \if german + * Liest ein DWord (32 Bit) aus dem PCI-Konfigurationsraum eines PCI-Geraets + * + * @param device Das Geraet + * @param offset Der Offset im Konfigurationsraum + * + * @return Der Wert an diesem Offset + * \elseif english + * Reads a dword (32 bit) from the PCI configuration space of a PCI device + * + * @param device The device + * @param offset The offset in the configuration space + * + * @return The corresponding value + * \endif + */ +uint32_t cdi_pci_config_inl(struct cdi_pci_device* device, uint8_t offset); + +/** + * \if german + * Schreibt ein Byte (8 Bit) in den PCI-Konfigurationsraum eines PCI-Geraets + * + * @param device Das Geraet + * @param offset Der Offset im Konfigurationsraum + * @param value Der zu setzende Wert + * \elseif english + * Writes a byte (8 bit) into the PCI configuration space of a PCI device + * + * @param device The device + * @param offset The offset in the configuration space + * @param value Value to be set + */ +void cdi_pci_config_outb(struct cdi_pci_device* device, uint8_t offset, uint8_t value); + +/** + * \if german + * Schreibt ein Word (16 Bit) in den PCI-Konfigurationsraum eines PCI-Geraets + * + * @param device Das Geraet + * @param offset Der Offset im Konfigurationsraum + * @param value Der zu setzende Wert + * \elseif english + * Writes a word (16 bit) into the PCI configuration space of a PCI device + * + * @param device The device + * @param offset The offset in the configuration space + * @param value Value to be set + */ +void cdi_pci_config_outw(struct cdi_pci_device* device, uint8_t offset, uint16_t value); + +/** + * \if german + * Schreibt ein DWord (32 Bit) in den PCI-Konfigurationsraum eines PCI-Geraets + * + * @param device Das Geraet + * @param offset Der Offset im Konfigurationsraum + * @param value Der zu setzende Wert + * \elseif english + * Writes a dword (32 bit) into the PCI configuration space of a PCI device + * + * @param device The device + * @param offset The offset in the configuration space + * @param value Value to be set + */ +void cdi_pci_config_outl(struct cdi_pci_device* device, uint8_t offset, uint32_t value); + #ifdef __cplusplus }; // extern "C" #endif -- 1.6.4.2 From kevin at tyndur.org Sun Dec 20 13:27:58 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 20 Dec 2009 13:27:58 +0100 Subject: [cdi-devel] [PATCH v2] Direct access to PCI configuration space In-Reply-To: <1261306604-7338-1-git-send-email-max@tyndur.org> References: <1261306604-7338-1-git-send-email-max@tyndur.org> Message-ID: <20091220122758.GA6500@silvanus> On Sun, Dec 20, 2009 at 11:56:44AM +0100, max at tyndur.org wrote: > From: Max Reitz > > + Some drivers need direct access to the PCI configuration space > > Signed-off-by: Max Reitz > --- > include/cdi/pci.h | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 files changed, 114 insertions(+), 0 deletions(-) In some of the doxygen comment the \endif is missing. Also, I'd like to see the functions named readb/writeb instead of outb/inb (the latter is almost uniquely used for the x86 IO ports which is completely unrelated) No need to resend though. I'm going to make these changes to the patch myself and apply it if there are no objections. Kevin From kevin at tyndur.org Tue Dec 22 16:14:39 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 22 Dec 2009 16:14:39 +0100 Subject: [cdi-devel] [PATCH v2] Direct access to PCI configuration space In-Reply-To: <20091220122758.GA6500@silvanus> References: <1261306604-7338-1-git-send-email-max@tyndur.org> <20091220122758.GA6500@silvanus> Message-ID: <20091222151439.GA32651@silvanus> On Sun, Dec 20, 2009 at 01:27:58PM +0100, Kevin Wolf wrote: > On Sun, Dec 20, 2009 at 11:56:44AM +0100, max at tyndur.org wrote: > > From: Max Reitz > > > > + Some drivers need direct access to the PCI configuration space > > > > Signed-off-by: Max Reitz > > --- > > include/cdi/pci.h | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ > > 1 files changed, 114 insertions(+), 0 deletions(-) > > In some of the doxygen comment the \endif is missing. Also, I'd like to > see the functions named readb/writeb instead of outb/inb (the latter is > almost uniquely used for the x86 IO ports which is completely unrelated) > > No need to resend though. I'm going to make these changes to the patch > myself and apply it if there are no objections. I haven't heard back from anyone, so I applied this one now. From kevin at tyndur.org Tue Dec 22 16:15:56 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 22 Dec 2009 16:15:56 +0100 Subject: [cdi-devel] [PATCH 1/6] Mempool In-Reply-To: <1260640035-11473-2-git-send-email-max@tyndur.org> References: <1260640035-11473-1-git-send-email-max@tyndur.org> <1260640035-11473-2-git-send-email-max@tyndur.org> Message-ID: <20091222151556.GB32651@silvanus> On Sat, Dec 12, 2009 at 06:47:10PM +0100, max at tyndur.org wrote: > From: Max Reitz > > + Added mempool header written by Kevin as a temporary solution for > the USB host controller drivers coming soon > > Signed-off-by: Max Reitz Added a cdi_* prefix and extern "C" and applied. Thanks. From kevin at tyndur.org Tue Dec 22 16:41:13 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 22 Dec 2009 16:41:13 +0100 Subject: [cdi-devel] [PATCH v3 0/5] USB support Message-ID: <1261496478-20441-1-git-send-email-kevin@tyndur.org> Rebased the series on current master for further discussion and/or development. Max Reitz (5): CDI.usb, CDI.usb-hcd OHCI UHCI USB 1.1 USB MSD include/cdi/usb-hcd.h | 178 +++++++++ include/cdi/usb.h | 868 ++++++++++++++++++++++++++++++++++++++++++ ohci/hcd.c | 315 +++++++++++++++ ohci/include/ohci-hcd.h | 39 ++ ohci/include/ohci-init.h | 31 ++ ohci/include/ohci-transfer.h | 30 ++ ohci/include/ohci.h | 283 ++++++++++++++ ohci/init.c | 241 ++++++++++++ ohci/main.c | 34 ++ ohci/transfer.c | 85 ++++ uhci/hcd.c | 215 +++++++++++ uhci/include/uhci-hcd.h | 39 ++ uhci/include/uhci-init.h | 31 ++ uhci/include/uhci-transfer.h | 30 ++ uhci/include/uhci.h | 138 +++++++ uhci/init.c | 249 ++++++++++++ uhci/main.c | 34 ++ uhci/transfer.c | 91 +++++ usb-msd/include/msd.h | 121 ++++++ usb-msd/main.c | 68 ++++ usb-msd/msd.c | 486 +++++++++++++++++++++++ usb11/control.c | 252 ++++++++++++ usb11/include/control.h | 31 ++ usb11/include/transfer.h | 38 ++ usb11/include/usb-structs.h | 184 +++++++++ usb11/include/usb11.h | 41 ++ usb11/main.c | 58 +++ usb11/transfer.c | 223 +++++++++++ 28 files changed, 4433 insertions(+), 0 deletions(-) create mode 100644 include/cdi/usb-hcd.h create mode 100644 include/cdi/usb.h create mode 100644 ohci/hcd.c create mode 100644 ohci/include/ohci-hcd.h create mode 100644 ohci/include/ohci-init.h create mode 100644 ohci/include/ohci-transfer.h create mode 100644 ohci/include/ohci.h create mode 100644 ohci/init.c create mode 100644 ohci/main.c create mode 100644 ohci/transfer.c create mode 100644 uhci/hcd.c create mode 100644 uhci/include/uhci-hcd.h create mode 100644 uhci/include/uhci-init.h create mode 100644 uhci/include/uhci-transfer.h create mode 100644 uhci/include/uhci.h create mode 100644 uhci/init.c create mode 100644 uhci/main.c create mode 100644 uhci/transfer.c create mode 100644 usb-msd/include/msd.h create mode 100644 usb-msd/main.c create mode 100644 usb-msd/msd.c create mode 100644 usb11/control.c create mode 100644 usb11/include/control.h create mode 100644 usb11/include/transfer.h create mode 100644 usb11/include/usb-structs.h create mode 100644 usb11/include/usb11.h create mode 100644 usb11/main.c create mode 100644 usb11/transfer.c From kevin at tyndur.org Tue Dec 22 16:41:14 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 22 Dec 2009 16:41:14 +0100 Subject: [cdi-devel] [PATCH v3 1/5] CDI.usb, CDI.usb-hcd In-Reply-To: <1261496478-20441-1-git-send-email-kevin@tyndur.org> References: <1261496478-20441-1-git-send-email-kevin@tyndur.org> Message-ID: <1261496478-20441-2-git-send-email-kevin@tyndur.org> From: Max Reitz + Added CDI.usb header + Added CDI.usb-hcd header Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- include/cdi/usb-hcd.h | 178 ++++++++++ include/cdi/usb.h | 868 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1046 insertions(+), 0 deletions(-) create mode 100644 include/cdi/usb-hcd.h create mode 100644 include/cdi/usb.h diff --git a/include/cdi/usb-hcd.h b/include/cdi/usb-hcd.h new file mode 100644 index 0000000..60b0a01 --- /dev/null +++ b/include/cdi/usb-hcd.h @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2009 Max Reitz + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/projects/COPYING.WTFPL for more details. + */ + +/** + * \if german + * USB-Hostcontrollertreiber + * \elseif english + * USB host controller drivers + * \endif + * \defgroup USBHCD + */ +/*\@{*/ + +#ifndef _CDI_USB_HCD_H_ +#define _CDI_USB_HCD_H_ + +#include "cdi/misc.h" +#include "cdi/usb.h" + +struct cdi_usb_hc { + struct cdi_device dev; + + /** + * \if german + * USB-Geschwindigkeit + * \elseif english + * USB speed + * \endif + */ + cdi_usb_speed_t speed; +}; + +struct cdi_usb_hcd { + struct cdi_driver drv; + + /** + * \if german + * + * Listet alle Geraete auf, die an den Rootports haengen und ruft ggf. + * cdi_usb_device_init auf + * + * @param device Der Hostcontroller + * + * \elseif english + * + * Lists all devices hanging at the root ports and calls (if necessary) + * cdi_usb_device_init + * + * @param device The host controller + * + * \endif + */ + void (*find_devices)(struct cdi_usb_hc* device); + + /** + * \if german + * + * Aktiviert den zum angegebenen Geraet gehoerigen Rootport + * + * @param usb_device Das USB-Geraet (muss an einem Rootport haengen) + * + * \elseif english + * + * Activates the root port corresponding to the device + * + * @param usb_device The USB device (must be located at a root port) + * + * \endif + */ + void (*activate_device)(struct cdi_usb_device* usb_device); + + /** + * \if german + * + * Verarbeitet USB-Pakete + * + * @param packets Array zu sendender Pakete + * @param num_packets Anzahl der Pakete + * + * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) + * + * \elseif english + * + * Sends USB packets + * + * @param packets Array of packages to be sent + * @param num_packages Number of packages + * + * \endif + */ + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, + size_t num_packets); + + /** + * \if german + * + * Erstellt eine neue Pipe + * + * @param pipe Die Pipe + * + * \elseif english + * + * Creates a new pipe + * + * @param pipe The pipe + * + * \endif + */ + void (*add_pipe)(struct cdi_usb_pipe* pipe); +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \if german + * + * Initialisiert einen HCD + * + * @param driver Der HCD + * + * \elseif english + * + * Initializes an HCD + * + * @param driver The HCD + * + * \endif + */ +void cdi_usb_hcd_init(struct cdi_usb_hcd* driver); + +/** + * \if german + * + * Gibt einen HCD frei + * + * @param driver Freizugebender HCD + * + * \elseif english + * + * Removes an HCD + * + * @param driver HCD to be removed + * + * \endif + */ +void cdi_usb_hcd_destroy(struct cdi_usb_hcd* driver); + +/** + * \if german + * + * Registriert einen HCD + * + * @param driver Zu registrierender HCD + * + * \elseif english + * + * Registers an HCD + * + * @param driver HCD to be registered + * + * \endif + */ +void cdi_usb_hcd_register(struct cdi_usb_hcd* driver); + +#ifdef __cplusplus +} //extern "C" +#endif + +#endif diff --git a/include/cdi/usb.h b/include/cdi/usb.h new file mode 100644 index 0000000..54ce1a0 --- /dev/null +++ b/include/cdi/usb.h @@ -0,0 +1,868 @@ +/* + * Copyright (c) 2009 Max Reitz + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/projects/COPYING.WTFPL for more details. + */ + +/** + * \if german + * USB-Bustreiber + * \elseif english + * USB bus drivers + * \endif + * \defgroup USB + */ +/*\@{*/ + +#ifndef _CDI_USB_H_ +#define _CDI_USB_H_ + +#include "stddef.h" +#include "stdint.h" + +typedef enum { + /// Low-Speed (1,5 Mbit/s) + CDI_USB_LOW_SPEED = 0x01, + /// Full-Speed (12 Mbit/s) + CDI_USB_FULL_SPEED = 0x02, + /// High-Speed (480 Mbit/s) + CDI_USB_HIGH_SPEED = 0x04, + /// SuperSpeed (5 Gbit/s) + CDI_USB_SUPER_SPEED = 0x08, +} cdi_usb_speed_t; + +typedef enum { + /** + * \if german + * SETUP-Paket + * \elseif english + * SETUP packet + * \endif + */ + CDI_USB_PACKET_SETUP = 0x2D, + + /** + * \if german + * Daten vom Geraet zum Host + * \elseif english + * Data from the device to the host + * \endif + */ + CDI_USB_PACKET_IN = 0x69, + + /** + * \if german + * Daten vom Host zum Geraet + * \elseif english + * Data from the host to the device + * \endif + */ + CDI_USB_PACKET_OUT = 0xE1, +} cdi_usb_packet_type_t; + +typedef enum { + /** + * \if german + * Kein Fehler + * \elseif english + * No error + * \endif + */ + CDI_USB_NO_ERROR = 0x0000, + + /// Stalled + CDI_USB_STALLED = 0x0001, + + /** + * \if german + * Bufferfehler (Overflow, underrun, ...) + * \elseif english + * Buffer error (overflow, underrun, ...) + * \endif + */ + CDI_USB_BUFFER_ERROR = 0x0002, + + /** + * \if german + * Babble (Geraet hat zu viele Daten gesendet) + * \elseif english + * Babble (device has sent more data than expected) + * \endif + */ + CDI_USB_BABBLE = 0x0004, + + /** + * \if german + * NAK (Daten sind erneut zu senden) + * \elseif english + * NAK (data has to be sent again) + * \endif + */ + CDI_USB_NAK = 0x0008, + + /** + * \if german + * CRC-Fehler + * \elseif english + * CRC error + * \endif + */ + CDI_USB_CRC = 0x0010, + + /// Timeout + CDI_USB_TIMEOUT = 0x0020, + + /** + * \if german + * Bitstuff/-destuff-Fehler + * \elseif english + * bitstuff/-destuff error + * \endif + */ + CDI_USB_BITSTUFF = 0x0040, + + /** + * \if german + * Reservieren von Speicher fehlgeschlagen, ... + * \elseif english + * Allocating of memory failed, ... + * \endif + */ + CDI_USB_TRIVIAL_ERROR = 0x0080, +} cdi_usb_status_t; + +struct cdi_usb_hc; +struct cdi_usb_pipe; +struct cdi_usb_interface; +struct cdi_usb_driver; + +struct cdi_usb_device { + /** + * \if german + * Der USB-Bustreiber + * \elseif english + * The USB bus driver + * \endif + */ + struct cdi_usb_driver* drv; + + /** + * \if german + * Der Hostcontroller, an dem das Geraet im Endeffekt haengt + * \elseif english + * The host controller where the device is eventually located + * \endif + */ + struct cdi_usb_hc* hc; + + /** + * \if german + * Haengt das Geraet an einem Hub, dann ist dieses Feld ein Pointer auf + * das entsprechende USB-Geraet, haengt es an einem Rootport, dann ist + * dieses Feld NULL. + * \elseif english + * If the device is located at a hub, then this field contains a pointer to + * the corresponding USB device, if it's located at a root port, then this + * field is set to NULL. + * \endif + */ + struct cdi_usb_device* hub; + + /** + * \if german + * Geschwindigkeit(-en) dieses Geraets + * \elseif english + * Speed(-s) supported by this device + * \endif + */ + cdi_usb_speed_t speed; + + /// ID + int id; + + /** + * \if german + * Entweder der Rootport oder der Port des Hubs, an dem das Geraet haengt + * \elseif english + * Either the root port or the port hub's port where the device is located + * \endif + */ + int port; + + /** + * \if german + * Hier darf der Geraetetreiber seine Daten unterbringen + * \elseif english + * The device driver may store its data here + * \endif + */ + void* driver_data; + + /** + * \if german + * Felder, die so auch bei PCI existieren: Vendor-, Device-, Class-, + * Subclass- und Protocol-ID + * \elseif english + * Fields existing at PCI, too: vendor, device, class, subclass and + * protocol ID. + * \endif + */ + int vendor_id; + int device_id; + int class_id; + int subclass_id; + int protocol_id; + + /** + * \if german + * + * Setzt das USB-Geraet zurueck (kann sowohl vom HCD als auch vom Hub + * driver ausgefuehrt werden) + * + * @param usb_device Das Geraet + * + * \elseif english + * + * Resets the USB device (may be executed either by the HCD or the hub + * driver) + * + * @param usb_device The device + * + * \endif + */ + void (*reset)(struct cdi_usb_device* usb_device); + + /** + * \if german + * Ziemlich lowlevelig. Gehoert das hier wirklich rein? + * \elseif english + * Very low level. Does it really fit in here? + * \endif + */ + + /** + * \if german + * Im einzelnen sind das: Pipe fuer Endpoint 0 + * \elseif english + * In particular these things are: Pipe for endpoint 0 + * \endif + */ + struct cdi_usb_pipe* ep0; + + /// Device descriptor + void* device_desc; + + /// Configuration descriptor + void* config_desc; + + /// Interface descriptor + struct cdi_usb_interface* interface_desc; + + /// Class descriptor + void* class_desc; +}; + +enum cdi_usb_toggle { + /** + * \if german + * Das Toggle nehmen, das jetzt dran ist + * \elseif english + * Use the toggle to be used right now + * \endif + */ + TOGGLE_UNSPECIFIC = 0, + + /** + * \if german + * Geloeschtes Togglebit erzwingen + * \elseif english + * Force unset toggle bit + * \endif + */ + TOGGLE_0 = 1, + + /** + * \if german + * Gesetztes Togglebit erzwingen + * \elseif english + * Force set toggle bit + * \endif + */ + TOGGLE_1 = 2 +}; + +struct cdi_usb_endpoint { + /// sizeof(struct cdi_usb_endpoint) + uint8_t length; + + /// 5 + uint8_t descriptor_type; + + /** + * \if german + * Nummer des Endpoints + * \elseif english + * Endpoint's number + * \endif + */ + uint8_t endpoint_address; + + /** + * \if german + * Eigenschaften (IN/OUT, Bulk/Interrupt/..., ...) + * \elseif english + * Attributes (IN/OUT, bulk/interrupt/..., ...) + * \endif + */ + uint8_t attributes; + + /** + * \if german + * Maximale Paketgroesse in Bytes + * \elseif english + * Maximum packet size in bytes + * \endif + */ + uint16_t max_packet_size; + + /// ??? + uint8_t interval; +} __attribute__((packed)); + +struct cdi_usb_interface { + /// sizeof(struct cdi_usb_interface) + uint8_t length; + + /// 4 + uint8_t descriptor_type; + + /** + * \if german + * Nummer des Interface + * \elseif english + * Interface's number + * \endif + */ + uint8_t interface_number; + + /// ??? + uint8_t alternate_setting; + + /** + * \if german + * Anzahl der Endpoints fuer dieses Interface + * \elseif english + * Number of endpoints for this interface + * \endif + */ + uint8_t num_endpoints; + + /** + * \if german + * Class-ID (bei Composite Devices) + * \elseif english + * Class ID (used with composite devices) + * \endif + */ + uint8_t interface_class; + + /// Subclass-ID + uint8_t interface_subclass; + + /// Protocol-ID + uint8_t interface_protocol; + + /** + * \if german + * String-Index, der das Interface beschreibt + * \elseif english + * String index describing the interface + * \endif + */ + uint8_t iInterface; +} __attribute__((packed)); + +/** + * \if german + * Beschreibt eine Pipe + * \elseif english + * Describes a pipe + * \endif + */ +struct cdi_usb_pipe { + /** + * \if german + * Ziel + * \elseif english + * Destination + * \endif + */ + struct cdi_usb_device* device; + + /** + * \if german + * Ein Pointer auf eine den Endpoint beschreibende Struktur + * TODO Das ist eigentlich auch sehr lowlevelig. + * \elseif english + * A pointer to a structure describing the endpoint + * TODO This is on a very low level, too. + */ + struct cdi_usb_endpoint* endpoint; + + /// Data-Toggle + int data_toggle; +}; + +/** + * \if german + * Beschreibt ein USB-Paket + * \elseif english + * Describes a USB packet + * \endif + */ +struct cdi_usb_packet { + /** + * \if german + * Die gewuenschte Pipe + * \elseif english + * The desired pipe + * \endif + */ + struct cdi_usb_pipe* pipe; + + /** + * \if german + * Der Typ des Pakets (PACKET_IN, PACKET_OUT, PACKET_SETUP) + * \elseif english + * Type of packet (PACKET_IN, PACKET_OUT, PACKET_SETUP) + * \endif + */ + cdi_usb_packet_type_t type; + + /** + * \if german + * Zeiger auf den Datenpuffer + * \elseif english + * Pointer to the data buffer + * \endif + */ + void* data; + + /** + * \if german + * Die Laenge des Puffers + * \elseif english + * The buffer's length + * \endif + */ + size_t length; + + /** + * \if german + * Fehlertyp (oder USB_NO_ERROR) + * \elseif english + * Error type (or USB_NO_ERROR) + * \endif + */ + cdi_usb_status_t condition; + + /** + * \if german + * Bestimmtes Data-Toggle benuten + * \elseif english + * Use specific data toggle + * \endif + */ + enum cdi_usb_toggle use_toggle; +}; + +typedef enum { + /** + * \if german + * Host -> Geraet + * \elseif english + * Host -> device + * \endif + */ + CDI_USB_CONTROL_OUT = 0x00, + + /** + * \if german + * Geraet -> Host + * \elseif english + * Device -> host + * \endif + */ + CDI_USB_CONTROL_IN = 0x80 +} cdi_usb_control_direction_t; + +typedef enum { + /** + * \if german + * Standard-Request + * \elseif english + * Standard request + * \endif + */ + CDI_USB_CONTROL_RQ_STANDARD = 0x00, + + /** + * \if german + * Klassenspezifischer Request + * \elseif english + * Class specific request + * \endif + */ + CDI_USB_CONTROL_RQ_CLASS = 0x20, + + /** + * \if german + * Herstellerspezifischer Request + * \elseif english + * Vendor specific request + * \endif + */ + CDI_USB_CONTROL_RQ_VENDOR = 0x40 +} cdi_usb_control_request_type_t; + +typedef enum { + /** + * \if german + * Empfaenger: Geraet + * \elseif english + * Recipient: device + * \endif + */ + CDI_USB_CONTROL_REC_DEVICE = 0x00, + + /** + * \if german + * Empfaenger: Interface + * \elseif english + * Recipient: interface + * \endif + */ + CDI_USB_CONTROL_REC_INTERFACE = 0x01, + + /** + * \if german + * Empfaenger: Endpoint + * \elseif english + * Recipient: endpoint + * \endif + */ + CDI_USB_CONTROL_REC_ENDPOINT = 0x02, + + /** + * \if german + * Empfaenger ist jemand anderes + * \elseif english + * Recipient is another one + * \endif + */ + CDI_USB_CONTROL_REC_OTHER = 0x03 +} cdi_usb_control_recipient_t; + +struct cdi_usb_control_transfer { + /** + * \if german + * Wenn Daten zum Geraet gesendet werden sollen, dann ist hier der + * Speicherbereich anzugeben + * \elseif english + * If data has to be sent to the device, then the memory buffer has to be + * given here + * \endif + */ + void* buffer; + + /** + * \if german + * Laenge der erwarteten bzw. zu sendenden Daten + * \elseif english + * Length of the expected data respectively the data to be sent + * \endif + */ + size_t length; + + /** + * \if german + * Richtung + * \elseif english + * Direction + * \endif + */ + cdi_usb_control_direction_t direction; + + /** + * \if german + * Art des Requests (Standardrequest, klassenspezifisch, + * herstellerspezifisch) + * \elseif english + * Request type (standard request, class specific, vendor specific) + * \endif + */ + cdi_usb_control_request_type_t rtype; + + /** + * \if german + * Ziel (Device, Interface, Endpoint, anderes) + * \elseif english + * Recipient (device, interface, endpoint, other) + * \endif + */ + cdi_usb_control_recipient_t recipient; + + /** + * \if german + * Requestcode (Je nach rtype und recipient) + * \elseif english + * Request code (specific to rtype and recipient) + * \endif + */ + int request; + + /** + * \if german + * Ein zu sendender Wert + * \elseif english + * A value to be sent + * \endif + */ + int value; + + /** + * \if german + * Ein zu sendender Index + * \elseif english + * An index to be sent + * \endif + */ + int index; +}; + +#include "cdi/usb-hcd.h" + +struct cdi_usb_driver { + struct cdi_driver drv; + + /** + * \if german + * Unterstuetzte Geschwindigkeiten + * \elseif english + * Supported speeds + * \endif + */ + cdi_usb_speed_t speeds; + + /** + * \if german + * + * Enumeriert ein USB-Geraet + * + * @param device Das zu enumerierende Geraet + * + * \elseif english + * + * Enumerates a USB device + * + * @param device The device to be enumerated + * + * \endif + */ + void (*enumerate_device)(struct cdi_usb_device* device); + + /** + * \if german + * + * Sendet USB-Pakete + * + * @param packets Array zu sendender Pakete + * @param num_packets Anzahl der Pakete + * + * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) + * + * \elseif english + * + * Sends USB packets + * + * @param packets Array of packets to be sent + * @param num_packets Number of packets + * + * @return Returns the status (i. e. USB_NO_ERROR to indicate no error) + * + * \endif + */ + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, + size_t num_packets); + + /** + * \if german + * + * Fuehrt einen Control-Transfer durch + * + * @param device Das Zielgeraet + * @param transfer Eine den Transfer beschreibende Struktur + * + * @return Gibt einen Puffer zurueck, der die ausgetauschen Daten enthaelt, + * im Fehlerfall hingegen NULL, gab es keine Daten, dann + * (void *)0xFFFFFFFF. + * + * \elseif english + * + * Performs a control transfer + * + * @param device The destination device + * @param transfer A structure describing this transfer + * + * @return Returns a buffer containing the transferred data, in case of an + * error the value is NULL and if there was no data, it returns + * (void *)0xFFFFFFFF. + * + * \endif + */ + void* (*do_control)(struct cdi_usb_device* device, + struct cdi_usb_control_transfer* transfer); +}; + +/** + * \if german + * Ein USB-Geraetetreiber uebergibt dem Betriebssystem eine solche Struktur, + * damit ein bestimmter Handler beim Auffinden solcher Geraete aufgerufen wird. + * Jedes Feld enthaelt entweder eine Zahl, die mit der des Geraetes + * uebereinstimmen muss oder -1, dann wird dieses Feld ignoriert. + * \elseif english + * A USB device driver passes this structure to the OS so that a specific + * handler is called upon the detection of such a device. Each field either + * contains a number that has to be equal to that of the driver or -1, so that + * the field will be ignored. + * \endif + */ +struct cdi_usb_device_pattern { + int vendor_id; + int device_id; + int class_id; + int subclass_id; + int protocol_id; + + /** + * \if german + * + * Ueberprueft das angegebene USB-Geraet + * + * @param device Das Geraet + * + * @return 0, wenn dieses Geraet nicht vom Treiber verarbeitet werden kann, + * sonst 1 + * + * \elseif english + * + * Checks the given USB device + * + * @param device The device + * + * @return 0 if the driver is unable to handle this device, else 1 + * + * \endif + */ + int (*take_usb_device)(struct cdi_usb_device* device); +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \if german + * + * Entfernt einen USB-Bustreiber + * + * @param driver Der Treiber + * + * \elseif english + * + * Removes a USB bus driver + * + * @param driver The driver + * + * \endif + */ +void cdi_usb_driver_destroy(struct cdi_usb_driver* driver); + +/** + * \if german + * + * Initialisiert einen USB-Bustreiber + * + * @param driver Der Treiber + * + * \elseif english + * + * Initializes a USB bus driver + * + * @param driver The driver + * + * \endif + */ +void cdi_usb_driver_init(struct cdi_usb_driver* driver); + +/** + * \if german + * + * Registriert einen USB-Bustreiber beim System + * + * @param driver Der Treiber + * + * \elseif english + * + * Registers a USB bus driver at the OS + * + * @param driver The driver + * + * \endif + */ +void cdi_usb_driver_register(struct cdi_usb_driver* driver); + +/** + * \if german + * + * Registriert ein gefundenes USB-Geraet beim Betriebssystem - diese Funktion + * wird z. B. vom Host-Controller-Treiber nach einem Aufruf von find_devices + * fuer jedes gefundene Geraet aufgerufen + * + * @param device Das zu registrierende Geraet + * + * \elseif english + * + * Registers a detected USB device at the OS - this function is called i. e. by + * the host controller driver after a find_devices call for every device found + * + * @param device The device to be registered + */ +void cdi_usb_device_init(struct cdi_usb_device* device); + +/** + * \if german + * + * Registriert ein Geraetemuster + * + * @param pattern Das Muster + * + * \elseif english + * + * Registers a device pattern + * + * @param pattern The pattern + * + * \endif + */ +void cdi_usb_register_device_pattern(struct cdi_usb_device_pattern* pattern); + +#ifdef __cplusplus +} //extern "C" +#endif + +#endif -- 1.6.0.2 From kevin at tyndur.org Tue Dec 22 16:41:15 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 22 Dec 2009 16:41:15 +0100 Subject: [cdi-devel] [PATCH v3 2/5] OHCI In-Reply-To: <1261496478-20441-1-git-send-email-kevin@tyndur.org> References: <1261496478-20441-1-git-send-email-kevin@tyndur.org> Message-ID: <1261496478-20441-3-git-send-email-kevin@tyndur.org> From: Max Reitz + Added USB open host controller driver Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- ohci/hcd.c | 315 ++++++++++++++++++++++++++++++++++++++++++ ohci/include/ohci-hcd.h | 39 +++++ ohci/include/ohci-init.h | 31 ++++ ohci/include/ohci-transfer.h | 30 ++++ ohci/include/ohci.h | 283 +++++++++++++++++++++++++++++++++++++ ohci/init.c | 241 ++++++++++++++++++++++++++++++++ ohci/main.c | 34 +++++ ohci/transfer.c | 85 +++++++++++ 8 files changed, 1058 insertions(+), 0 deletions(-) create mode 100644 ohci/hcd.c create mode 100644 ohci/include/ohci-hcd.h create mode 100644 ohci/include/ohci-init.h create mode 100644 ohci/include/ohci-transfer.h create mode 100644 ohci/include/ohci.h create mode 100644 ohci/init.c create mode 100644 ohci/main.c create mode 100644 ohci/transfer.c diff --git a/ohci/hcd.c b/ohci/hcd.c new file mode 100644 index 0000000..ca337c2 --- /dev/null +++ b/ohci/hcd.c @@ -0,0 +1,315 @@ +/***************************************************************************** +* 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 +#include +#include + +#include "ohci.h" +#include "ohci-hcd.h" + +static void reset_device(struct cdi_usb_device *device); + +static void find_devices(struct cdi_usb_hc *device) +{ + struct ohci *ohci = (struct ohci *)device; + struct cdi_usb_device *dev; + int i; + + d0printf("Scanning root ports\n"); + for (i = 0; i < ohci->root_ports; i++) { + if (!(ohci->memory->hc_rh_port_status[i] & OHC_RP_CCS)) { + continue; + } + dev = malloc(sizeof(struct cdi_usb_device)); + dev->hc = device; + dev->id = 0; + dev->port = i; + dev->speed = (ohci->memory->hc_rh_port_status[i] & OHC_RP_LSDA) ? + CDI_USB_LOW_SPEED : CDI_USB_FULL_SPEED; + dev->reset = &reset_device; + cdi_usb_device_init(dev); + } +} + +static void activate_device(struct cdi_usb_device *device) +{ + struct ohci *ohci = (struct ohci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Activating device on port %i.\n", device->port); + ohci->memory->hc_rh_port_status[device->port] |= OHC_RP_PES; + cdi_sleep_ms(20); +} + +static void reset_device(struct cdi_usb_device *device) +{ + struct ohci *ohci = (struct ohci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Resetting device on port %i.\n", device->port); + ohci->memory->hc_rh_port_status[device->port] |= OHC_RP_PRS; + cdi_sleep_ms(20); +} + +static void add_pipe(struct cdi_usb_pipe *pipe) +{ + struct ohci *ohci = (struct ohci *)pipe->device->hc; + struct ohci_ed *ved; + uintptr_t ped; + struct ohci_ed_desc *dsc; + //TODO Mehr als Control und Bulk + int iscontrol = 0; + + if (cdi_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->speed == CDI_USB_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) { + cdi_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); + + if (iscontrol) { + ohci->memory->hc_control_head_ed = ped; + } else { + ohci->memory->hc_bulk_head_ed = ped; + } +} + +static inline int _tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} + +static volatile int locked = 0; + +static cdi_usb_status_t do_packets(struct cdi_usb_packet *packets, + size_t num_packets) +{ + struct ohci_ed_desc *edsc; + struct ohci_td_desc *tdsc, *otdsc; + int i, j, cond, toggle, done_packets; + struct ohci *ohci = (struct ohci *)packets[0].pipe->device->hc; + struct ohci_td *vtd[num_packets]; + uintptr_t ptd[num_packets]; + + for (i = 0; (edsc = cdi_list_get(ohci->ed_list, i)) != NULL; i++) { + if ((edsc->function == packets[0].pipe->device->id) && + (edsc->endpoint == packets[0].pipe->endpoint->endpoint_address)) + { + break; + } + } + if (edsc == NULL) { + return CDI_USB_TIMEOUT; //Genau das w??rde passieren + } + + toggle = packets[0].pipe->data_toggle; + for (i = 0; i < (int)num_packets; i++) { + if (cdi_mempool_get(ohci->transfer_pool, (void **)&vtd[i], &ptd[i]) == -1) + { + return CDI_USB_TRIVIAL_ERROR; + } + if ((packets[i].data != NULL) && + (packets[i].type != CDI_USB_PACKET_IN)) + { + memcpy((void *)((uintptr_t)vtd[i] + sizeof(struct ohci_td)), + packets[i].data, packets[i].length); + } + vtd[i]->rounding = 1; //Warum nicht + switch (packets[i].type) { + case CDI_USB_PACKET_SETUP: + vtd[i]->direction = OHC_TD_DIR_SETUP; + break; + case CDI_USB_PACKET_IN: + vtd[i]->direction = OHC_TD_DIR_IN; + break; + case CDI_USB_PACKET_OUT: + vtd[i]->direction = OHC_TD_DIR_OUT; + break; + default: //Hm, passt nicht ganz + for (j = 0; j <= i; j++) { + cdi_mempool_put(ohci->transfer_pool, vtd[j]); + } + return CDI_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++) { + cdi_mempool_put(ohci->transfer_pool, vtd[j]); + } + return CDI_USB_TRIVIAL_ERROR; + } + tdsc->virt = vtd[i]; + tdsc->phys = ptd[i]; + tdsc->endpoint = edsc; + + 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]; + } + + 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; + } + done_packets = 0; + cond = CDI_USB_NO_ERROR; + while (done_packets < (int)num_packets) { + cdi_sleep_ms(1); + for (i = 0; i < (int)num_packets; i++) { + if (vtd[i]->condition != 15) { + done_packets++; + switch (vtd[i]->condition) { + case 0x00: + packets[i].condition = CDI_USB_NO_ERROR; + break; + case 0x02: + packets[i].condition = CDI_USB_BITSTUFF; + break; + case 0x04: + packets[i].condition = CDI_USB_STALLED; + for (j = 0; j < (int)num_packets; j++) { + //Allet, was nicht fertig ist, ist STALLed + vtd[j]->condition = 0x04; + } + break; + case 0x05: + packets[i].condition = CDI_USB_TIMEOUT; + break; + case 0x08: + packets[i].condition = CDI_USB_BABBLE; + break; + case 0x0C: + case 0x0D: + packets[i].condition = CDI_USB_BUFFER_ERROR; + break; + default: + packets[i].condition = CDI_USB_CRC; + break; + } + cond |= packets[i].condition; + vtd[i]->condition = 15; //Nicht nochmal ??berpr??fen + } + } + } + for (i = 0; i < (int)num_packets; i++) { + if ((packets[i].data != NULL) && + (packets[i].type == CDI_USB_PACKET_IN)) + { + memcpy(packets[i].data, (void *)((uintptr_t)vtd[i] + + sizeof(struct ohci_td)), packets[i].length); + } + if (vtd[i]->next_td == 0) { + edsc->virt->td_queue_head = 0; + } + cdi_mempool_put(ohci->transfer_pool, vtd[i]); + } + if (cond & CDI_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 + } + return cond; +} + + +const struct ohci_functions ohci_functions = { + .find_devices = &find_devices, + .activate_device = &activate_device, + .send_packets = &do_packets, + .add_pipe = &add_pipe +}; diff --git a/ohci/include/ohci-hcd.h b/ohci/include/ohci-hcd.h new file mode 100644 index 0000000..ccb6345 --- /dev/null +++ b/ohci/include/ohci-hcd.h @@ -0,0 +1,39 @@ +/***************************************************************************** +* 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 UHCI_HCD_H +#define UHCI_HCD_H + +#include +#include + +struct ohci_functions { + void (*find_devices)(struct cdi_usb_hc *device); + void (*activate_device)(struct cdi_usb_device *device); + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet *packets, + size_t num_packets); + void (*add_pipe)(struct cdi_usb_pipe *pipe); +}; + +cdi_list_t ohci_find_devices(struct cdi_usb_hc *device); + +#endif diff --git a/ohci/include/ohci-init.h b/ohci/include/ohci-init.h new file mode 100644 index 0000000..2f19d2c --- /dev/null +++ b/ohci/include/ohci-init.h @@ -0,0 +1,31 @@ +/***************************************************************************** +* 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 OHCI_INIT_H +#define OHCI_INIT_H + +#include "ohci.h" + +void find_and_init_ohcis(void); +void ohci_init(struct cdi_device *dev); + +#endif diff --git a/ohci/include/ohci-transfer.h b/ohci/include/ohci-transfer.h new file mode 100644 index 0000000..a6dc6be --- /dev/null +++ b/ohci/include/ohci-transfer.h @@ -0,0 +1,30 @@ +/***************************************************************************** +* 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 OHCI_TRANSFER_H +#define OHCI_TRANSFER_H + +#include + +void ohci_handler(struct cdi_device *cdi_hci); + +#endif diff --git a/ohci/include/ohci.h b/ohci/include/ohci.h new file mode 100644 index 0000000..4936960 --- /dev/null +++ b/ohci/include/ohci.h @@ -0,0 +1,283 @@ +/***************************************************************************** +* 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 OHCI_H +#define OHCI_H + +#include +#include + +#include +#include +#include + +#include "ohci.h" + + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[ohci] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[ohci] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + + +#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_legsup +{ + uint32_t hce_control; + uint32_t hce_input; + uint32_t hce_output; + uint32_t hce_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 cdi_usb_hc cdidev; + struct cdi_pci_device *pcidev; + + struct ohci_registers *memory; + struct ohci_legsup *legsup; + int root_ports; + struct ohci_hcca *hcca; + + struct cdi_mempool *ed_pool; + struct cdi_mempool *transfer_pool; + + cdi_list_t ed_list; +}; + +#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 + 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 + +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) + 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); + +#endif diff --git a/ohci/init.c b/ohci/init.c new file mode 100644 index 0000000..338a1ac --- /dev/null +++ b/ohci/init.c @@ -0,0 +1,241 @@ +/***************************************************************************** +* 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 +#include +#include +#include +#include + +#include "ohci.h" +#include "ohci-hcd.h" +#include "ohci-init.h" +#include "ohci-transfer.h" + +extern const struct ohci_functions ohci_functions; +extern int init_ohci(void); + +#define DRIVER_NAME "ohcd" + +static struct cdi_usb_hcd driver = { + .drv = { + .type = CDI_USB_HCD, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = &ohci_init, + .remove_device = NULL, + .init = &init_ohci, + .destroy = NULL + } +}; + +CDI_DRIVER(DRIVER_NAME, driver) + +void find_and_init_ohcis(void) +{ + struct ohci *hci; + char *name; + int ohci_counter = 0; + struct cdi_pci_device *dev; + + cdi_usb_hcd_init(&driver); + + cdi_list_t pci_devices = cdi_list_create(); + cdi_pci_get_all_devices(pci_devices); + for (int i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) { + if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03) + || (dev->interface_id != 0x10)) + { + cdi_pci_device_destroy(dev); + } else { + hci = malloc(sizeof(struct ohci)); + hci->cdidev.dev.type = CDI_USB_HCD; + name = malloc(7); + sprintf(name, "ohci%i", ohci_counter++); + hci->cdidev.dev.name = (const char *)name; + hci->cdidev.speed = CDI_USB_LOW_SPEED | CDI_USB_FULL_SPEED; + hci->pcidev = dev; + cdi_list_push(driver.drv.devices, hci); + } + } + + cdi_list_destroy(pci_devices); + + driver.find_devices = ohci_functions.find_devices; + driver.activate_device = ohci_functions.activate_device; + driver.send_packets = ohci_functions.send_packets; + driver.add_pipe = ohci_functions.add_pipe; +} + +void ohci_init(struct cdi_device *dev) +{ + struct ohci *ohci = (struct ohci *)dev; + struct cdi_pci_resource *res; + int i; + uintptr_t phys_hcca; + + ohci->memory = NULL; + for (i = 0; (res = cdi_list_get(ohci->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) { + d0printf("Wasn't able to allocate memory.\n"); + return; + } + ohci->legsup = (void *)((uintptr_t)ohci->memory + 0x100); + break; + } + } + if (ohci->memory == NULL) { + d1printf("Memory not found!\n"); + return; + } + d1printf("Memory @ 0x%08X\n", (uintptr_t)ohci->memory); + if ((ohci->memory->hc_revision & 0xFF) != 0x10) { + d0printf("This OHCI might be incompatible with this driver" + "(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) + { + d0printf("Wasn't able to allocate HCCA.\n"); + return; + } + memset(ohci->hcca, 0, sizeof(*ohci->hcca)); + + d1printf("IRQ: %i\n", ohci->pcidev->irq); + cdi_register_irq(ohci->pcidev->irq, &ohci_handler, dev); + + ohci->ed_pool = cdi_mempool_create(64 * sizeof(struct ohci_ed), + sizeof(struct ohci_ed)); + if (ohci->ed_pool == NULL) { + d0printf("Wasn't able to create ED memory pool!\n"); + return; + } + + ohci->transfer_pool = cdi_mempool_create(48 * (1024 + sizeof(struct ohci_td)), + 1024 + sizeof(struct ohci_td)); + if (ohci->transfer_pool == NULL) { + d0printf("Wasn't able to create transfer memory pool!\n"); + return; + } + + ohci->memory->hc_interrupt_disable = OHC_INT_MIE; + + if (ohci->memory->hc_control & OHC_CTRL_IR) { + d1printf("Kicking out 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) { + _d1printf("Went fine.\n"); + } else { + _d1printf("Idiot.\n"); + ohci->memory->hc_control &= ~OHC_CTRL_IR; + } + } else if ((ohci->memory->hc_control & OHC_CTRL_CBSR) != OHC_USB_RESET) { + d1printf("BIOS active\n"); + if ((ohci->memory->hc_control & OHC_CTRL_CBSR) != OHC_USB_OPERATIONAL) + { + d1printf("Activating RESUME...\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 + cdi_sleep_ms(10); //Reset abwarten + } + + if (ohci->legsup->hce_control & 1) { + d1printf("USB legacy support activated, but that won't last for" + "long...\n"); + } + //Harhar, that's mine now! + ohci->legsup->hce_control = 0; + + 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... + d1printf("Reset not finished yet...\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; + + d1printf("Activating HC.\n"); + + //DO IT, FAGGOT! + 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); + + d0printf("Found %i root ports.\n", ohci->root_ports); + + for (i = 0; i < ohci->root_ports; i++) { + ohci->memory->hc_rh_port_status[i] |= OHC_RP_CCS; //Deaktivieren + } + + ohci->ed_list = cdi_list_create(); +} diff --git a/ohci/main.c b/ohci/main.c new file mode 100644 index 0000000..4b6cd67 --- /dev/null +++ b/ohci/main.c @@ -0,0 +1,34 @@ +/***************************************************************************** +* 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 "ohci-init.h" + +int init_ohci() +{ + cdi_init(); + + find_and_init_ohcis(); + + return 0; +} diff --git a/ohci/transfer.c b/ohci/transfer.c new file mode 100644 index 0000000..24f7c67 --- /dev/null +++ b/ohci/transfer.c @@ -0,0 +1,85 @@ +/***************************************************************************** +* 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 + +#include "ohci.h" + +void ohci_handler(struct cdi_device *cdi_hci) +{ + struct ohci *ohci = (struct ohci *)cdi_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; + if (!status) { + return; //Wohl nicht f??r uns + } + + if (status & OHC_INT_RHSC) { + done |= OHC_INT_RHSC; + } + if (status & OHC_INT_UE) { + d0printf("Fatal HC error.\n"); + ohci->memory->hc_command_status |= OHC_CMST_RESET; + done |= OHC_INT_UE; + } + if (status & OHC_INT_SF) { + //??hm... Na ja. Manchmal fragt man sich, ob sich der HC so einsam + //f??hlt, dass er IRQs feuert, obwohl sie deaktiviert sind. + 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); + free(tdsc); + break; + } + } + if (tdsc != NULL) { + //Gefunden + break; + } + } + } + + if (status & ~done) { + d1printf("Non-handled interrupt: 0x%08X\n", status & ~done); + } + + ohci->memory->hc_interrupt_status = status; +} -- 1.6.0.2 From kevin at tyndur.org Tue Dec 22 16:41:16 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 22 Dec 2009 16:41:16 +0100 Subject: [cdi-devel] [PATCH v3 3/5] UHCI In-Reply-To: <1261496478-20441-1-git-send-email-kevin@tyndur.org> References: <1261496478-20441-1-git-send-email-kevin@tyndur.org> Message-ID: <1261496478-20441-4-git-send-email-kevin@tyndur.org> From: Max Reitz + Added USB universal host controller driver Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- uhci/hcd.c | 215 ++++++++++++++++++++++++++++++++++++ uhci/include/uhci-hcd.h | 39 +++++++ uhci/include/uhci-init.h | 31 +++++ uhci/include/uhci-transfer.h | 30 +++++ uhci/include/uhci.h | 138 +++++++++++++++++++++++ uhci/init.c | 249 ++++++++++++++++++++++++++++++++++++++++++ uhci/main.c | 34 ++++++ uhci/transfer.c | 91 +++++++++++++++ 8 files changed, 827 insertions(+), 0 deletions(-) create mode 100644 uhci/hcd.c create mode 100644 uhci/include/uhci-hcd.h create mode 100644 uhci/include/uhci-init.h create mode 100644 uhci/include/uhci-transfer.h create mode 100644 uhci/include/uhci.h create mode 100644 uhci/init.c create mode 100644 uhci/main.c create mode 100644 uhci/transfer.c diff --git a/uhci/hcd.c b/uhci/hcd.c new file mode 100644 index 0000000..51b0ec9 --- /dev/null +++ b/uhci/hcd.c @@ -0,0 +1,215 @@ +/***************************************************************************** +* 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 +#include +#include + +#include "uhci.h" +#include "uhci-hcd.h" + +extern cdi_list_t uhci_active_transfers; + +static void reset_device(struct cdi_usb_device *device); + +static void find_devices(struct cdi_usb_hc *device) +{ + struct uhci *uhci = (struct uhci *)device; + struct cdi_usb_device *dev; + int i; + + d0printf("Scanning root ports\n"); + 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)) { + continue; + } + dev = malloc(sizeof(struct cdi_usb_device)); + dev->hc = device; + dev->id = 0; + dev->port = i; + dev->speed = (cdi_inw(uhci->pbase + UHCI_RPORTS + 2 * i) & + RPORT_LOSPD) ? CDI_USB_LOW_SPEED : CDI_USB_FULL_SPEED; + dev->hub = NULL; + dev->reset = &reset_device; + cdi_usb_device_init(dev); + } +} + +static void activate_device(struct cdi_usb_device *device) +{ + struct uhci *uhci = (struct uhci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Activating device on port %i.\n", device->port); + cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, + RPORT_ENABLE | RPORT_CSC); + cdi_sleep_ms(20); +} + +static void reset_device(struct cdi_usb_device *device) +{ + struct uhci *uhci = (struct uhci *)device->hc; + + if (device->hub != NULL) { + return; + } + + d1printf("Resetting device on port %i.\n", device->port); + cdi_outw(uhci->pbase + UHCI_RPORTS + 2 * device->port, + RPORT_ENABLE | RPORT_RESET | RPORT_CSC); + cdi_sleep_ms(20); +} + +static void add_pipe(struct cdi_usb_pipe *pipe) +{ + //Macht nix, weil das bei UHCI nicht n??tig ist + pipe = NULL; //Aber GCC k??nnen wir trotzdem gl??cklich machen +} + +static inline int _tsl(volatile int *variable) +{ + int rval; + rval = *variable; + *variable = 1; + return rval; +} + +static volatile int locked = 0; + +static cdi_usb_status_t do_packet(struct cdi_usb_packet *packet) +{ + struct cdi_usb_device *usbdev = packet->pipe->device; + struct uhci *uhci = (struct uhci *)usbdev->hc; + struct uhci_td td; + struct transfer addr; + int timeout, frame; + + void *buf; + uintptr_t pbuf; + + memset(&td, 0, sizeof(td)); + 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->speed == CDI_USB_LOW_SPEED); + td.errors = 1; + td.pid = packet->type; + td.device = usbdev->id; + td.endpoint = packet->pipe->endpoint->endpoint_address & 0x07; + td.maxlen = packet->length ? packet->length - 1 : 0x7FF; + while (_tsl(&locked)); + frame = (cdi_inw(uhci->pbase + UHCI_FRNUM) + 5) & 0x3FF; + while (!(uhci->queue_heads[frame].transfer & 1)) { + frame++; + frame &= 0x3FF; + } + + if (cdi_mempool_get(uhci->buffers, &buf, &pbuf) < 0) { + return CDI_USB_TRIVIAL_ERROR; + } + + addr.virt = buf; + addr.phys = pbuf; + addr.error = 0xFFFF; + + buf = (void *)((uintptr_t)buf + sizeof(td)); + pbuf += sizeof(td); + + if (!packet->length) { + td.buffer = 0; + } else { + td.buffer = pbuf; + if (packet->type != CDI_USB_PACKET_IN) { + memcpy(buf, packet->data, packet->length); + } + } + + memcpy(addr.virt, &td, sizeof(td)); + + + if (uhci_active_transfers == NULL) { + uhci_active_transfers = cdi_list_create(); + } + uhci->queue_heads[frame].transfer = addr.phys; + cdi_list_push(uhci_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)) { +#ifndef CDI_STANDALONE + __asm__ __volatile__ ("hlt"); +#endif + } + if (packet->type == CDI_USB_PACKET_IN) { + memcpy(packet->data, buf, packet->length); + } + if (addr.error == 0xFFFF) { + addr.error = CDI_USB_TIMEOUT; + } + + cdi_mempool_put(uhci->buffers, addr.virt); + + return addr.error; +} + +static cdi_usb_status_t do_packets(struct cdi_usb_packet *packets, + size_t num_packets) +{ + size_t i; + cdi_usb_status_t cond = CDI_USB_NO_ERROR; + + //TODO + for (i = 0; (i < num_packets) && !cond; i++) { + packets[i].condition = do_packet(&packets[i]); + cond |= packets[i].condition; + } + + return cond; +} + + +const struct uhci_functions uhci_functions = { + .find_devices = &find_devices, + .activate_device = &activate_device, + .send_packets = &do_packets, + .add_pipe = &add_pipe +}; diff --git a/uhci/include/uhci-hcd.h b/uhci/include/uhci-hcd.h new file mode 100644 index 0000000..46e4093 --- /dev/null +++ b/uhci/include/uhci-hcd.h @@ -0,0 +1,39 @@ +/***************************************************************************** +* 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 UHCI_HCD_H +#define UHCI_HCD_H + +#include +#include + +struct uhci_functions { + void (*find_devices)(struct cdi_usb_hc *device); + void (*activate_device)(struct cdi_usb_device *device); + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet *packets, + size_t num_packets); + void (*add_pipe)(struct cdi_usb_pipe *pipe); +}; + +cdi_list_t uhci_find_devices(struct cdi_usb_hc *device); + +#endif diff --git a/uhci/include/uhci-init.h b/uhci/include/uhci-init.h new file mode 100644 index 0000000..7b357bc --- /dev/null +++ b/uhci/include/uhci-init.h @@ -0,0 +1,31 @@ +/***************************************************************************** +* 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 UHCI_INIT_H +#define UHCI_INIT_H + +#include "uhci.h" + +void find_and_init_uhcis(void); +void uhci_init(struct cdi_device *dev); + +#endif diff --git a/uhci/include/uhci-transfer.h b/uhci/include/uhci-transfer.h new file mode 100644 index 0000000..53b46b0 --- /dev/null +++ b/uhci/include/uhci-transfer.h @@ -0,0 +1,30 @@ +/***************************************************************************** +* 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 UHCI_TRANSFER_H +#define UHCI_TRANSFER_H + +#include + +void uhci_handler(struct cdi_device *cdi_hci); + +#endif diff --git a/uhci/include/uhci.h b/uhci/include/uhci.h new file mode 100644 index 0000000..c9b3b05 --- /dev/null +++ b/uhci/include/uhci.h @@ -0,0 +1,138 @@ +/***************************************************************************** +* 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 UHCI_H +#define UHCI_H + +#include +#include + +#include +#include +#include +#include + + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[uhci] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[uhci] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + + +#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 CONFIGURE 0x0040 +#define GLOB_SUSP_MODE 0x0008 +#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 + +//Legacy support +#define UHCI_PCI_LEGSUP 0xC0 //w + +#define UHCI_LEGSUP_STATUS 0x8F00 //Statusbits, die mittels schreiben einer 1 + //gel??scht werden +#define UHCI_LEGSUP_NO_CHG 0x5040 //Bits, die nicht ver??ndert werden k??nnen + //(RO bzw. reserviert) +#define UHCI_LEGSUP_PIRQ 0x2000 //Interrupt wird als PCI-IRQ ausgef??hrt + + +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 { + 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; +}; + +struct uhci { + struct cdi_usb_hc cdidev; + struct cdi_pci_device *pcidev; + int 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 cdi_mempool *buffers; +}; + +#endif diff --git a/uhci/init.c b/uhci/init.c new file mode 100644 index 0000000..9d08cde --- /dev/null +++ b/uhci/init.c @@ -0,0 +1,249 @@ +/***************************************************************************** +* 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 +#include +#include +#include + +#include "uhci.h" +#include "uhci-hcd.h" +#include "uhci-init.h" +#include "uhci-transfer.h" + +extern const struct uhci_functions uhci_functions; +extern int init_uhci(void); + +#define DRIVER_NAME "uhcd" + +static struct cdi_usb_hcd driver = { + .drv = { + .type = CDI_USB_HCD, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = &uhci_init, + .remove_device = NULL, + .init = &init_uhci, + .destroy = NULL + } +}; + +CDI_DRIVER(DRIVER_NAME, driver) + +void find_and_init_uhcis(void) +{ + struct uhci *hci; + char *name; + int uhci_counter = 0; + struct cdi_pci_device *dev; + + cdi_usb_hcd_init(&driver); + + cdi_list_t pci_devices = cdi_list_create(); + cdi_pci_get_all_devices(pci_devices); + for (int i = 0; (dev = cdi_list_get(pci_devices, i)) != NULL; i++) { + if ((dev->class_id != 0x0C) || (dev->subclass_id != 0x03) + || (dev->interface_id != 0x00)) + { + cdi_pci_device_destroy(dev); + } else { + hci = malloc(sizeof(struct uhci)); + hci->cdidev.dev.type = CDI_USB_HCD; + name = malloc(7); + sprintf(name, "uhci%i", uhci_counter++); + hci->cdidev.dev.name = (const char *)name; + hci->cdidev.speed = CDI_USB_LOW_SPEED | CDI_USB_FULL_SPEED; + hci->pcidev = dev; + cdi_list_push(driver.drv.devices, hci); + } + } + + cdi_list_destroy(pci_devices); + + driver.find_devices = uhci_functions.find_devices; + driver.activate_device = uhci_functions.activate_device; + driver.send_packets = uhci_functions.send_packets; + driver.add_pipe = uhci_functions.add_pipe; +} + +void uhci_init(struct cdi_device *dev) +{ + struct uhci *uhci = (struct uhci *)dev; + struct cdi_pci_resource *res; + int size = 0, request_size, cmd, intr, i; +#ifdef CDI_PCI_DIRECT_ACCESS + int legsup; +#endif + + d0printf("Trying to init %s at %02x:%02x.%02x...\n", uhci->cdidev.dev.name, + uhci->pcidev->bus, uhci->pcidev->dev, uhci->pcidev->function); + + uhci->pbase = 0; + for (i = 0; (res = cdi_list_get(uhci->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) { + d0printf("Cannot allocate I/O ports.\n"); + return; + } + uhci->pbase = res->start; + break; + } + } + if (!uhci->pbase) { + d0printf("No I/O space found.\n"); + return; + } + request_size = + // Maximale Paketl??nge (Full Speed) ist 1023, Alignment au??erdem + 1024 + + // Transferdeskriptor + sizeof(struct uhci_td); + + uhci->buffers = cdi_mempool_create(8192, request_size); + if (uhci->buffers == NULL) { + d0printf("Cannot create mempool.\n"); + return; + } + + if (cdi_alloc_phys_mem(sizeof(*uhci->queue_heads) * 1024, + (void **)&uhci->queue_heads, (void **)&uhci->phys_queue_heads) == -1) + { + d0printf("Cannot allocate QH memory.\n"); + return; + } + + if (cdi_alloc_phys_mem(4096, (void **)&uhci->frame_list, + (void **)&uhci->phys_frame_list) == -1) + { + d0printf("Cannot allocate frame list.\n"); + return; + } + +#ifdef CDI_PCI_DIRECT_ACCESS + cdi_pci_config_writew(uhci->pcidev, 4, 0x05); +#endif + + cdi_register_irq(uhci->pcidev->irq, &uhci_handler, dev); + uhci->root_ports = (size - 0x10) >> 1; + //Erstmal testen, wie viele wir genau haben... + for (i = 2; i < uhci->root_ports; i++) { + if (!(cdi_inw(uhci->pbase + UHCI_RPORTS + i * 2) & 0x0080) || + (cdi_inw(uhci->pbase + UHCI_RPORTS + i * 2) == 0xFFFF)) + { + uhci->root_ports = i; + break; + } + } + //Mehr als sieben Rootports, klingt lustig + if (uhci->root_ports > 7) { + uhci->root_ports = 2; + } + + d1printf("I/O 0x%04X (%i ports), IRQ %i\n", uhci->pbase, uhci->root_ports, + uhci->pcidev->irq); + +#ifdef CDI_PCI_DIRECT_ACCESS + legsup = cdi_pci_config_readw(uhci->pcidev, UHCI_PCI_LEGSUP); +#endif + cmd = cdi_inw(uhci->pbase + UHCI_USBCMD); + intr = cdi_inw(uhci->pbase + UHCI_USBINTR); + +#ifdef CDI_PCI_DIRECT_ACCESS + //Wenn der HC l??uft, das Configurebit gesetzt ist, der nicht runtergefahren + //ist oder dieses Interrupt-enable-Bit gesetzt ist, dann war wohl schon + //jemand dran. + //(oder wenn der Legacy Support aktiviert ist, das hei??t, wenn in LEGSUP + //irgendein Bit gesetzt ist, dass nicht RO/reserviert oder ein Statusbit + //ist) + if ((legsup & ~(UHCI_LEGSUP_STATUS | UHCI_LEGSUP_NO_CHG)) || + (cmd & USB_RUN) || !(cmd & CONFIGURE) || !(cmd & GLOB_SUSP_MODE) || + (intr & 0x0002)) + { + //Na, wer hat denn da am HC rumgefummelt. B????????ses BIOS. Oder SMM. Oder + //wer auch immer. + //Alle LEGSUP-Statusbits l??schen + cdi_pci_config_writew(uhci->pcidev, UHCI_PCI_LEGSUP, + UHCI_LEGSUP_STATUS); +#endif + //Resetten + cdi_outw(uhci->pbase + UHCI_USBCMD, HCRESET); + //Warten, bis der Reset beendet ist + for (i = 0; (cdi_inw(uhci->pbase + UHCI_USBCMD) & HCRESET) && + (i < 50); i++) + { + cdi_sleep_ms(10); + } + if (i == 50) { + d1printf("Reset not finished...\n"); + } + + //Interrupts erstmal deaktivieren + cdi_outw(uhci->pbase + UHCI_USBINTR, 0); + //Den gesamten HC ebenso + cdi_outw(uhci->pbase + UHCI_USBCMD, 0); + + //Alle Ports "runterfahren" + for (int port = 0; port < uhci->root_ports; port++) { + cdi_outw(uhci->pbase + UHCI_RPORTS + (port * 2), 0); + } +#ifdef CDI_PCI_DIRECT_ACCESS + } +#endif + + for (i = 0; i < 1024; i++) { + 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; + } + + //Standardwert, jede Millisekunde ein Frame + cdi_outb(uhci->pbase + UHCI_SOFMOD, 0x40); + //Unsere Frameliste eintragen + cdi_outl(uhci->pbase + UHCI_FRBASEADD, uhci->phys_frame_list); + //Frame zur??cksetzen + cdi_outw(uhci->pbase + UHCI_FRNUM, 0); +#ifdef CDI_PCI_DIRECT_ACCESS + //Hier setzen wir das PIRQ-Bit und deaktivieren s??mtlichen Legacy Support. + cdi_pci_config_writew(uhci->pcidev, UHCI_PCI_LEGSUP, UHCI_LEGSUP_PIRQ); +#else + d0printf("Failed to deactivate USB legacy support.\n"); +#endif + + //HC starten + cdi_outw(uhci->pbase + UHCI_USBCMD, USB_RUN | CONFIGURE | MAXP); + //Alle Interrupts aktivieren + cdi_outw(uhci->pbase + UHCI_USBINTR, 0xF); + + //Connect status change l??schen + for (i = 0; i < uhci->root_ports; i++) { + cdi_outw(uhci->pbase + UHCI_RPORTS + i * 2, RPORT_CSC); + } +} diff --git a/uhci/main.c b/uhci/main.c new file mode 100644 index 0000000..8c5fb07 --- /dev/null +++ b/uhci/main.c @@ -0,0 +1,34 @@ +/***************************************************************************** +* 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 "uhci-init.h" + +int init_uhci() +{ + cdi_init(); + + find_and_init_uhcis(); + + return 0; +} diff --git a/uhci/transfer.c b/uhci/transfer.c new file mode 100644 index 0000000..33c60bf --- /dev/null +++ b/uhci/transfer.c @@ -0,0 +1,91 @@ +/***************************************************************************** +* 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 "uhci.h" + +cdi_list_t uhci_active_transfers = NULL; + +void uhci_handler(struct cdi_device *cdi_hci) +{ + struct uhci *uhci = (struct uhci *)cdi_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; + } + //Ist Bit 0 gesetzt, dann ist das ein normaler IOC (Interrupt on + //completion), bei Bit 1 ist ein Fehler aufgetreten, aber das werden wir ja + //unten sehen. + if (status & ~0x0003) { + d1printf("Unerwarteter IRQ 0x%04X von %s\n", status, cdi_hci->name); + } + if (status & 0x10) { + d0printf("SCHWERWIEGENDER FEHLER - HC WIRD ANGEHALTEN\n"); + cdi_outw(uhci->pbase + UHCI_USBCMD, MAXP | HCRESET); //FU! + } else { + for (i = 0; (addr = cdi_list_get(uhci_active_transfers, i)) != NULL; + i++) + { + td = addr->virt; + if (td->active) { + continue; + } + addr->error = CDI_USB_NO_ERROR; + cdi_list_remove(uhci_active_transfers, i--); + if (td->stalled_err) { + d0printf("ENDPOINT STALLED\n"); + addr->error |= CDI_USB_STALLED; + } + if (td->buf_err) { + d1printf("Puffer??berlauf oder Underrun\n"); + addr->error |= CDI_USB_BUFFER_ERROR; + } + if (td->babble) { + d1printf("Da war ein Ger??t wohl sehr gespr??chig: Babble.\n"); + addr->error |= CDI_USB_BABBLE; + } + if (td->nak) { + d1printf("NAK empfangen\n"); + addr->error |= CDI_USB_NAK; + } + if (td->crc_time_err) { + d1printf("CRC-Fehler oder Timeout\n"); + addr->error |= CDI_USB_CRC | CDI_USB_TIMEOUT; + } + if (td->bitstuff_err) { + d1printf("Bitstufffehler\n"); + addr->error |= CDI_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 Tue Dec 22 16:41:17 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 22 Dec 2009 16:41:17 +0100 Subject: [cdi-devel] [PATCH v3 4/5] USB 1.1 In-Reply-To: <1261496478-20441-1-git-send-email-kevin@tyndur.org> References: <1261496478-20441-1-git-send-email-kevin@tyndur.org> Message-ID: <1261496478-20441-5-git-send-email-kevin@tyndur.org> From: Max Reitz + Added USB 1.1 bus driver Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- usb11/control.c | 252 +++++++++++++++++++++++++++++++++++++++++++ usb11/include/control.h | 31 ++++++ usb11/include/transfer.h | 38 +++++++ usb11/include/usb-structs.h | 184 +++++++++++++++++++++++++++++++ usb11/include/usb11.h | 41 +++++++ usb11/main.c | 58 ++++++++++ usb11/transfer.c | 223 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 827 insertions(+), 0 deletions(-) create mode 100644 usb11/control.c create mode 100644 usb11/include/control.h create mode 100644 usb11/include/transfer.h create mode 100644 usb11/include/usb-structs.h create mode 100644 usb11/include/usb11.h create mode 100644 usb11/main.c create mode 100644 usb11/transfer.c diff --git a/usb11/control.c b/usb11/control.c new file mode 100644 index 0000000..170341d --- /dev/null +++ b/usb11/control.c @@ -0,0 +1,252 @@ +/***************************************************************************** +* 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 "usb11.h" +#include "usb-structs.h" +#include "transfer.h" + +static int usb_dev_ids = 1; + +static void enumerate_hub(struct cdi_usb_device *usbdev); + +static int usb11_real_enumerate(struct cdi_usb_device *device) +{ + struct endpoint_desc *ep0; + struct device_desc *dev_desc; + struct cdi_usb_hcd *hcd; + int id; + char *name; + void *position; + struct config_desc *conf_desc; + struct interface_desc *if_desc, *best_if; + + hcd = (struct cdi_usb_hcd *)device->hc->dev.driver; + + //EP0 initialisieren + device->ep0 = malloc(sizeof(*device->ep0)); + device->ep0->endpoint = malloc(sizeof(struct endpoint_desc)); + ep0 = (struct endpoint_desc *)device->ep0->endpoint; + ep0->length = sizeof(struct endpoint_desc); + ep0->descriptor_type = DESC_ENDPOINT; + ep0->endpoint_address = 0; + ep0->attributes = 0; + ep0->max_packet_size = 8; + ep0->interval = 0; + device->ep0->device = device; + device->ep0->data_toggle = 0; + + //Zur??cksetzen + device->reset(device); + + //Pipe erzeugen + hcd->add_pipe(device->ep0); + + //Erste acht Bytes des Device-Descriptors abrufen und die maximale + //Paketgr????e von EP0 feststellen + dev_desc = usb11_do_control(device, DEV_TO_HOST, NULL, 8, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); + if (dev_desc == NULL) { + return 0; + } + ep0->max_packet_size = dev_desc->max_packet_size0; + + //Nochmals zur??cksetzen + device->reset(device); + + //USB-Adresse zuweisen + usb11_do_control(device, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, + REC_DEVICE, SET_ADDRESS, (id = usb_dev_ids++), 0); + device->id = id; + if (usb_dev_ids >= 128) { + d0printf("Warning: USB device ID overflow.\n"); + usb_dev_ids = 1; + } + + //So, jetzt haben wir eine Adresse, da gibts auch eine neue Pipe + hcd->add_pipe(device->ep0); + + //Den ganzen Device-Descriptor einlesen + dev_desc = usb11_do_control(device, DEV_TO_HOST, NULL, sizeof(*dev_desc), + STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, DESC_DEVICE << 8, 0); + device->device_desc = dev_desc; + device->vendor_id = dev_desc->vendor_id; + device->device_id = dev_desc->device_id; + + if (dev_desc->iManufacturer) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iManufacturer, 0); + if (name == NULL) { + return 0; + } + d1printf(" -> Manufacturer: "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf("\n"); + } + if (dev_desc->iProduct) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | dev_desc->iProduct, 0); + if (name == NULL) { + return 0; + } + d1printf(" -> Product: "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf("\n"); + } + //TODO: Man kann doch nicht immer den ersten nehmen... + conf_desc = usb11_do_control(device, DEV_TO_HOST, NULL, + sizeof(struct config_desc), STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return 0; + } + conf_desc = usb11_do_control(device, DEV_TO_HOST, NULL, + conf_desc->total_length, STD_REQUEST, REC_DEVICE, GET_DESCRIPTOR, + DESC_CONFIGURATION << 8, 0); + if (conf_desc == NULL) { + return 0; + } + device->config_desc = conf_desc; + usb11_do_control(device, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_CONFIGURATION, conf_desc->config_value, 0); + if (conf_desc->iConfiguration) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | conf_desc->iConfiguration, 0); + if (name == NULL) { + return 0; + } + d1printf("Using configuration "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf(".\n"); + } + position = conf_desc; + position = (void *)((uintptr_t)position + sizeof(struct config_desc)); + best_if = position; //Standard-IF + for (int i = 0; i < conf_desc->num_interfaces; i++) { + if_desc = position; + //TODO MSD ist zwar h??bsch. Aber andere auch ;-) + if (if_desc->interface_class == 8) { + best_if = if_desc; + break; + } + position = (void *)((uintptr_t)position + + sizeof(struct interface_desc) + + if_desc->num_endpoints * sizeof(struct endpoint_desc)); + } + usb11_do_control(device, HOST_TO_DEV, NULL, 0, STD_REQUEST, REC_DEVICE, + SET_INTERFACE, best_if->interface_number, 0); + device->interface_desc = (void *)best_if; + if (best_if->iInterface) { + name = usb11_do_control(device, DEV_TO_HOST, NULL, 64, STD_REQUEST, + REC_DEVICE, GET_DESCRIPTOR, + (DESC_STRING << 8) | best_if->iInterface, 0); + if (name == NULL) { + return 0; + } + d1printf("Using interface "); + for (int i = 2; i < name[0]; i += 2) { + _d1printf("%c", name[i]); + } + _d1printf(".\n"); + } + d1printf("Configuration: %i:%i (%i)\n", conf_desc->config_value, + best_if->interface_number, best_if->interface_class); + + if (!dev_desc->class_id) { //Composite Device + device->class_id = best_if->interface_class; + device->subclass_id = best_if->interface_subclass; + device->protocol_id = best_if->interface_protocol; + } else { + device->class_id = dev_desc->class_id; + device->subclass_id = dev_desc->subclass_id; + device->protocol_id = dev_desc->protocol_id; + } + + if (best_if->interface_class == USBDC_HUB) { + enumerate_hub(device); + } + + return 1; +} + +void usb11_enumerate(struct cdi_usb_device *device) +{ + if (!usb11_real_enumerate(device)) { + free(device->ep0->endpoint); + free(device->ep0); + } +} + +static void reset_hub_device(struct cdi_usb_device *usbdev) +{ + usb11_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 cdi_usb_device *usbdev) +{ + struct hub_desc *hub_desc; + struct cdi_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 = usb11_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 = usb11_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 + usb11_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); + down = malloc(sizeof(struct cdi_usb_device)); + down->hc = usbdev->hc; + down->hub = usbdev; + down->id = 0; + down->port = i; + down->speed = (port_status[0] & PORT_LOWSPEED) ? + CDI_USB_LOW_SPEED : CDI_USB_FULL_SPEED; + down->reset = &reset_hub_device; + cdi_usb_device_init(down); + } +} diff --git a/usb11/include/control.h b/usb11/include/control.h new file mode 100644 index 0000000..97edefc --- /dev/null +++ b/usb11/include/control.h @@ -0,0 +1,31 @@ +/***************************************************************************** +* 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 CONTROL_H +#define CONTROL_H + +#include +#include + +void usb11_enumerate(struct cdi_usb_device *device); + +#endif diff --git a/usb11/include/transfer.h b/usb11/include/transfer.h new file mode 100644 index 0000000..3b0f086 --- /dev/null +++ b/usb11/include/transfer.h @@ -0,0 +1,38 @@ +/***************************************************************************** +* 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 TRANSFER_H +#define TRANSFER_H + +#include + +#include + +void *usb11_do_control(struct cdi_usb_device *device, int direction, + void *buffer, int length, int rtype, int recipient, int request, int value, + int index); +void *usb11_export_control(struct cdi_usb_device *device, + struct cdi_usb_control_transfer *transfer); +cdi_usb_status_t usb11_send_packets(struct cdi_usb_packet *packets, + size_t num_packets); + +#endif diff --git a/usb11/include/usb-structs.h b/usb11/include/usb-structs.h new file mode 100644 index 0000000..427d0e5 --- /dev/null +++ b/usb11/include/usb-structs.h @@ -0,0 +1,184 @@ +/***************************************************************************** +* 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 USB_STRUCTS_H +#define USB_STRUCTS_H + +#include + + +#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 + + +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; + +#endif diff --git a/usb11/include/usb11.h b/usb11/include/usb11.h new file mode 100644 index 0000000..77634e9 --- /dev/null +++ b/usb11/include/usb11.h @@ -0,0 +1,41 @@ +/***************************************************************************** +* 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 USB11_H +#define USB11_H + +#include + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[usb 1.1] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[usb 1.1] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + +#endif diff --git a/usb11/main.c b/usb11/main.c new file mode 100644 index 0000000..e381062 --- /dev/null +++ b/usb11/main.c @@ -0,0 +1,58 @@ +/***************************************************************************** +* 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 "control.h" +#include "transfer.h" + +#define DRIVER_NAME "USB 1.1" + +int init_usb11(void); + +static struct cdi_usb_driver driver = { + .drv = { + .type = CDI_USB, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = NULL, + .remove_device = NULL, + .init = &init_usb11, + .destroy = NULL + }, + .speeds = CDI_USB_LOW_SPEED | CDI_USB_FULL_SPEED, + .enumerate_device = &usb11_enumerate, + .send_packets = &usb11_send_packets, + .do_control = &usb11_export_control +}; + +CDI_DRIVER(DRIVER_NAME, driver) + +int init_usb11() +{ + cdi_init(); + + cdi_usb_driver_init(&driver); + + return 0; +} diff --git a/usb11/transfer.c b/usb11/transfer.c new file mode 100644 index 0000000..eb4f949 --- /dev/null +++ b/usb11/transfer.c @@ -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. * +*****************************************************************************/ + +#include +#include + +#include + +#include "transfer.h" +#include "usb11.h" +#include "usb-structs.h" + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +/** + * Verarbeitet USB-Pakete. + * + * @param packet Die Paketliste + * @param num_packets Anzahl der Pakete + * + * @return Status + */ + +cdi_usb_status_t usb11_send_packets(struct cdi_usb_packet *packets, + size_t num_packets) +{ + cdi_usb_status_t error = CDI_USB_NAK; + int i = 0, mps; + int num_out_packets; + struct cdi_usb_pipe *pipe; + struct cdi_usb_device *device; + struct cdi_usb_packet *out_packets; + struct endpoint_desc *ep; + struct cdi_usb_hcd *hcd; + + if (num_packets < 1) { + return CDI_USB_TRIVIAL_ERROR; + } + + // Alle Pakete muessen dieselbe Pipe benutzen + pipe = packets[0].pipe; + for (i = 1; i < (int)num_packets; i++) { + if (packets[i].pipe != pipe) { + return CDI_USB_TRIVIAL_ERROR; + } + } + + device = pipe->device; + hcd = (struct cdi_usb_hcd *)device->hc->dev.driver; + ep = (struct endpoint_desc *)pipe->endpoint; + + // Pruefen, wie viele Pakete wir draus machen muessen + mps = ep->max_packet_size; + num_out_packets = 0; + for (i = 0; i < (int)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 == (int)num_packets) { + out_packets = packets; + } else { + int j; + int cur = 0; + + out_packets = calloc(num_out_packets, sizeof(*out_packets)); + for (i = 0; i < (int)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((int)packets[i].length, mps); + out_packets[cur].condition = CDI_USB_NAK; + + packets[i].data = (void *)((uintptr_t)packets[i].data + + out_packets[cur].length); + packets[i].length -= out_packets[cur].length; + cur++; + } + } + } + + error = hcd->send_packets(out_packets, num_out_packets); + if (error & CDI_USB_STALLED) { + d0printf("ENDPOINT 0x%02X OF DEVICE %i STALLED!\n", + ep->endpoint_address, device->id); + usb11_do_control(device, HOST_TO_DEV | NO_DATA, NULL, 0, STD_REQUEST, + REC_ENDPOINT, CLEAR_FEATURE, 0, ep->endpoint_address); + } else if (error & CDI_USB_NAK) { + // Nicht angenommene Pakete (FIXME: einzeln) senden + for (i = 0; i < num_out_packets; i++) { + if (!(out_packets[i].condition & CDI_USB_NAK)) { + continue; + } + error = CDI_USB_NAK; + while (error & CDI_USB_NAK) { + error = hcd->send_packets(&out_packets[i], 1); + if (error & CDI_USB_NAK) { + cdi_sleep_ms(5); + } + } + } + } + + if (out_packets != packets) { + free(out_packets); + } + + return error; +} + +void *usb11_do_control(struct cdi_usb_device *device, int direction, + void *buffer, int length, int rtype, int recipient, int request, int value, + int index) +{ + int no_data, alloced_here = 0; + cdi_usb_status_t rval; + struct setup_packet _setup; + struct setup_packet *setup = &_setup; + + no_data = direction & NO_DATA; + direction &= 0x80; + + setup->request_type = direction | (rtype & 0x60) | (recipient & 0x1F); + setup->request = request; + setup->value = value; + setup->index = index; + setup->length = length; + + struct cdi_usb_packet setup_packet = { + .pipe = device->ep0, + .type = CDI_USB_PACKET_SETUP, + .data = setup, + .length = sizeof(*setup), + .use_toggle = TOGGLE_UNSPECIFIC + }; + + if (usb11_send_packets(&setup_packet, 1)) { + return NULL; + } + + if (no_data) { + rval = CDI_USB_NO_ERROR; + buffer = (void *)0xFFFFFFFF; //Kein Fehler, aber auch keine Daten + } else { + if ((direction != HOST_TO_DEV) || (length && (buffer == NULL))) { + buffer = malloc(length); + alloced_here = 1; + } + + struct cdi_usb_packet data_packet = { + .pipe = device->ep0, + .type = (direction == DEV_TO_HOST ? + CDI_USB_PACKET_IN : CDI_USB_PACKET_OUT), + .data = buffer, + .length = length, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + rval = usb11_send_packets(&data_packet, 1); + } + + if (rval == CDI_USB_NO_ERROR) { + struct cdi_usb_packet ack_packet = { + .pipe = device->ep0, + .data = NULL, + .length = 0, + .use_toggle = TOGGLE_1 + }; + + if (no_data || (direction == HOST_TO_DEV)) { + ack_packet.type = CDI_USB_PACKET_IN; + } else { + ack_packet.type = CDI_USB_PACKET_OUT; + } + + rval = usb11_send_packets(&ack_packet, 1); + } + + if ((rval != CDI_USB_NO_ERROR) && alloced_here) { + free(buffer); + } + + return (rval == CDI_USB_NO_ERROR) ? buffer : NULL; +} + +void *usb11_export_control(struct cdi_usb_device *device, + struct cdi_usb_control_transfer *transfer) +{ + return usb11_do_control(device, transfer->direction, transfer->buffer, + transfer->length, transfer->rtype, transfer->recipient, + transfer->request, transfer->value, transfer->index); +} -- 1.6.0.2 From kevin at tyndur.org Tue Dec 22 16:41:18 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 22 Dec 2009 16:41:18 +0100 Subject: [cdi-devel] [PATCH v3 5/5] USB MSD In-Reply-To: <1261496478-20441-1-git-send-email-kevin@tyndur.org> References: <1261496478-20441-1-git-send-email-kevin@tyndur.org> Message-ID: <1261496478-20441-6-git-send-email-kevin@tyndur.org> From: Max Reitz + Added USB mass storage driver (currently just bulk-only devices supported) Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- usb-msd/include/msd.h | 121 ++++++++++++ usb-msd/main.c | 68 +++++++ usb-msd/msd.c | 486 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 675 insertions(+), 0 deletions(-) create mode 100644 usb-msd/include/msd.h create mode 100644 usb-msd/main.c create mode 100644 usb-msd/msd.c diff --git a/usb-msd/include/msd.h b/usb-msd/include/msd.h new file mode 100644 index 0000000..bb6cf51 --- /dev/null +++ b/usb-msd/include/msd.h @@ -0,0 +1,121 @@ +/***************************************************************************** +* 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 MSD_H +#define MSD_H + +#include +#include + +//#define DEBUG + +#ifdef DEBUG +#define d1printf(...) printf("[usb-msd] " __VA_ARGS__) +#define _d1printf(...) printf(__VA_ARGS__) +#else +#define d1printf(...) +#define _d1printf(...) +#endif + +#define d0printf(...) printf("[usb-msd] " __VA_ARGS__) +#define _d0printf(...) printf(__VA_ARGS__) + +//"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 cdi_usb_device *usb_device; + struct cdi_usb_pipe *bulk_in; + struct cdi_usb_pipe *bulk_out; + 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 + +int check_msd(struct cdi_usb_device *device); +int msd_read_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer); +int msd_write_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer); + +#endif diff --git a/usb-msd/main.c b/usb-msd/main.c new file mode 100644 index 0000000..c5c401f --- /dev/null +++ b/usb-msd/main.c @@ -0,0 +1,68 @@ +/***************************************************************************** +* 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 "msd.h" + +#define DRIVER_NAME "usb-msd" + +static int init_msd(void); + +struct cdi_storage_driver msc_driver = { + .drv = { + .type = CDI_STORAGE, + .name = DRIVER_NAME, + .devices = NULL, + .init_device = NULL, + .remove_device = NULL, + .init = &init_msd, + .destroy = NULL + }, + .read_blocks = &msd_read_blocks, + .write_blocks = &msd_write_blocks +}; + +CDI_DRIVER(DRIVER_NAME, msc_driver) + +static struct cdi_usb_device_pattern msd_pattern = { + .vendor_id = -1, + .device_id = -1, + .class_id = 8, + .subclass_id = -1, + .protocol_id = -1, + + .take_usb_device = &check_msd +}; + +static int init_msd() +{ + cdi_init(); + + cdi_storage_driver_init(&msc_driver); + + cdi_usb_register_device_pattern(&msd_pattern); + + return 0; +} diff --git a/usb-msd/msd.c b/usb-msd/msd.c new file mode 100644 index 0000000..336a250 --- /dev/null +++ b/usb-msd/msd.c @@ -0,0 +1,486 @@ +/***************************************************************************** +* 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 +#include +#include + +#include "msd.h" + +//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 + +extern struct cdi_storage_driver msc_driver; +static uint32_t cbw_tag = 1; + +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); + +int msd_read_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer) +{ + return msd_cdi_read(device, start, count, buffer); +} + +int msd_write_blocks(struct cdi_storage_device *device, uint64_t start, + uint64_t count, void *buffer) +{ + return msd_cdi_write(device, start, count, buffer); +} + +static int msd_get_capacity(struct cdi_usb_device *usbdev, + uint32_t *block_size, uint32_t *block_count); + +int check_msd(struct cdi_usb_device *device) +{ + struct cdi_storage_device *strgdev; + struct cdi_usb_endpoint *ep_desc; + struct cdi_msd *cdimsd; + struct cdi_usb_hcd *hcd; + void *address; + int i; + uint32_t bs, bc; + uint64_t size; + static int msdnum = 0; + + //Nur Bulk-Only + if (device->protocol_id != 0x50) { + return 0; + } + + d0printf("MSD detected.\n"); + + cdimsd = malloc(sizeof(struct cdi_msd)); + if (cdimsd == NULL) { + return 0; + } + cdimsd->usb_device = device; + 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 0; + } + sprintf((char *)strgdev->dev.name, "usb-msd%i", msdnum++); + strgdev->dev.driver = (struct cdi_driver *)&msc_driver; + device->driver_data = cdimsd; + cdimsd->bulk_in = malloc(sizeof(*cdimsd->bulk_in)); + cdimsd->bulk_in->device = device; + cdimsd->bulk_in->endpoint = NULL; + cdimsd->bulk_in->data_toggle = 0; + cdimsd->bulk_out = malloc(sizeof(*cdimsd->bulk_out)); + cdimsd->bulk_out->device = device; + cdimsd->bulk_out->endpoint = NULL; + cdimsd->bulk_out->data_toggle = 0; + address = (void *)((uintptr_t)device->interface_desc + + sizeof(struct cdi_usb_interface)); + for (i = 0; i < device->interface_desc->num_endpoints; i++) { + ep_desc = address; + if ((ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (cdimsd->bulk_in->endpoint == NULL)) + { + //BULK-IN + cdimsd->bulk_in->endpoint = ep_desc; + } + else if (!(ep_desc->endpoint_address & 0x80) && + (ep_desc->attributes == 0x02) && + (cdimsd->bulk_out->endpoint == NULL)) + { + //BULK-OUT + cdimsd->bulk_out->endpoint = ep_desc; + } + address = (void *)((uintptr_t)address + + sizeof(struct cdi_usb_endpoint)); + } + if ((cdimsd->bulk_in->endpoint == NULL) || + (cdimsd->bulk_out->endpoint == NULL)) + { + d0printf("Not enough endpoints found.\n"); + return 0; + } + hcd = (struct cdi_usb_hcd *)device->hc->dev.driver; + hcd->add_pipe(cdimsd->bulk_in); + hcd->add_pipe(cdimsd->bulk_out); + if (!msd_get_capacity(device, &bs, &bc)) { + strgdev->block_size = 0; + strgdev->block_count = 0; + d1printf("Wasn't able to determine size of %s.\n", strgdev->dev.name); + } else { + strgdev->block_size = bs; + strgdev->block_count = bc; + size = bs; + size *= bc; + d1printf("%s: %i * %i B (about %d MB).\n", strgdev->dev.name, bc, bs, + (int)(size >> 20)); + } + cdi_storage_device_init(strgdev); + cdi_list_push(msc_driver.drv.devices, strgdev); + + return 1; +} + +static int write_cmd(struct cdi_usb_device *usbdev, void *src) +{ + struct cdi_msd *cdimsd = usbdev->driver_data; + struct cdi_usb_packet cmd_packet = { + .pipe = cdimsd->bulk_out, + .type = CDI_USB_PACKET_OUT, + .data = src, + .length = 0x1F, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + return usbdev->drv->send_packets(&cmd_packet, 1); +} + +/** + * 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 cdi_usb_device *usbdev, uint32_t expected_tag) +{ + struct cdi_msd *msc = usbdev->driver_data; + struct command_status_wrapper _csw; + struct command_status_wrapper *csw = &_csw; + cdi_usb_status_t error; + + struct cdi_usb_packet status_packet = { + .pipe = msc->bulk_in, + .type = CDI_USB_PACKET_IN, + .data = csw, + .length = 0x0D, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + error = usbdev->drv->send_packets(&status_packet, 1); + if (error != CDI_USB_NO_ERROR) { + return error; + } + if ((csw->csw_signature != CSW_SIGNATURE) || + (csw->csw_tag != expected_tag) || csw->csw_status) + { + d1printf("0x%08X %i==%i 0x%08X\n", csw->csw_signature, csw->csw_tag, + expected_tag, csw->csw_status); + return CDI_USB_TRIVIAL_ERROR; + } + + return CDI_USB_NO_ERROR; +} + +static int msd_get_capacity(struct cdi_usb_device *usbdev, + uint32_t *block_size, uint32_t *block_count) +{ + struct cdi_msd *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper *cbw = &_cbw; + struct msd_capacity _cap; + struct msd_capacity *cap = &_cap; + uint32_t expected_tag; + + if ((usbdev == NULL) || (block_size == NULL) || (block_count == NULL)) { + return 0; + } + + msc = usbdev->driver_data; + 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 (write_cmd(usbdev, cbw) != CDI_USB_NO_ERROR) { + return 0; + } + + struct cdi_usb_packet in_packet = { + .pipe = msc->bulk_in, + .type = CDI_USB_PACKET_IN, + .data = cap, + .length = sizeof(*cap), + .use_toggle = TOGGLE_UNSPECIFIC + }; + + if (usbdev->drv->send_packets(&in_packet, 1) != CDI_USB_NO_ERROR) { + return 0; + } + + if (read_status(usbdev, expected_tag) != CDI_USB_NO_ERROR) { + 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 cdi_usb_device *usbdev) +{ + struct msclass_data *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper *cbw = &_cbw; + uint32_t expected_tag; + + msc = (struct msclass_data *)usbdev->driver_data; + 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, cbw) != USB_NO_ERROR) { + return 0; + } + + if (read_status(usbdev, expected_tag) != USB_NO_ERROR) { + return 0; + } + + return 1; +} +#endif + +#define MAX_ACCESS_BLOCKS 32 + +static uint32_t msd_read(struct cdi_usb_device *usbdev, uint32_t lba, + 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) +{ + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int i; +#endif + struct cdi_usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + int j, bbs; + + d1printf("read(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + + if (!count) { + d1printf("Empty read request.\n"); + return 0; + } + for (j = 0; j < (int)count; j += MAX_ACCESS_BLOCKS) { +#ifdef WAIT_FOR_MSD_READY + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) { + cdi_sleep_ms(20); + } +#endif + bbs = (count - j > MAX_ACCESS_BLOCKS) ? MAX_ACCESS_BLOCKS : count - j; + error = msd_read(usbdev, start + j, bbs, + (void *)((uintptr_t)buffer + j * bs), bbs * bs); + if (error != CDI_USB_NO_ERROR) { + d0printf("Read error 0x%X at block %lld.\n", error, start + j); + return -1; + } + } + return 0; +} + +static uint32_t msd_read(struct cdi_usb_device *usbdev, uint32_t lba, + uint16_t sectors, void *buffer, size_t length) +{ + struct cdi_msd *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper *cbw = &_cbw; + uint32_t expected_tag; + int error; + + if ((buffer == NULL) || !length) { + return CDI_USB_TRIVIAL_ERROR; + } + msc = usbdev->driver_data; + 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 = write_cmd(usbdev, cbw); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + struct cdi_usb_packet in_packet = { + .pipe = msc->bulk_in, + .type = CDI_USB_PACKET_IN, + .data = buffer, + .length = length, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + error = usbdev->drv->send_packets(&in_packet, 1); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + error = read_status(usbdev, expected_tag); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + return CDI_USB_NO_ERROR; +} + +static uint32_t msd_write(struct cdi_usb_device *usbdev, uint32_t lba, + 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) +{ + int bs = strgdev->block_size, error; +#ifdef WAIT_FOR_MSD_READY + int i; +#endif + struct cdi_usb_device *usbdev = ((struct cdi_msd *)strgdev)->usb_device; + int j, bbs; + + d1printf("write(%i, %i)\n", (int)start, (int)count); + start += ((struct cdi_msd *)strgdev)->offset; + + if (!count) { + d1printf("Empty write request.\n"); + return 0; + } + for (j = 0; j < (int)count; j += MAX_ACCESS_BLOCKS) { +#ifdef WAIT_FOR_MSD_READY + for (i = 0; !msd_ready(usbdev) && (i < 10); i++) + cdi_sleep_ms(20); +#endif + bbs = (count - j > MAX_ACCESS_BLOCKS) ? MAX_ACCESS_BLOCKS : count - j; + error = msd_write(usbdev, start + j, bbs, + (void *)((uintptr_t)buffer + j * bs), bbs * bs); + if (error != CDI_USB_NO_ERROR) { + d0printf("Write error 0x%X at block %lld.\n", error, start + j); + return -1; + } + } + return 0; +} + +static uint32_t msd_write(struct cdi_usb_device *usbdev, uint32_t lba, + uint16_t sectors, void *buffer, size_t length) +{ + struct cdi_msd *msc; + struct command_block_wrapper _cbw; + struct command_block_wrapper* cbw = &_cbw; + uint32_t expected_tag; + int error; + + if ((buffer == NULL) || !length) { + return CDI_USB_TRIVIAL_ERROR; + } + msc = usbdev->driver_data; + 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, cbw); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + struct cdi_usb_packet out_packet = { + .pipe = msc->bulk_out, + .type = CDI_USB_PACKET_OUT, + .data = buffer, + .length = length, + .use_toggle = TOGGLE_UNSPECIFIC + }; + + error = usbdev->drv->send_packets(&out_packet, 1); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + error = read_status(usbdev, expected_tag); + if (error != CDI_USB_NO_ERROR) { + return error; + } + + return CDI_USB_NO_ERROR; +} -- 1.6.0.2 From kevin at tyndur.org Tue Dec 22 17:06:53 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 22 Dec 2009 17:06:53 +0100 Subject: [cdi-devel] [PATCH v3 1/5] CDI.usb, CDI.usb-hcd In-Reply-To: <1261496478-20441-2-git-send-email-kevin@tyndur.org> References: <1261496478-20441-1-git-send-email-kevin@tyndur.org> <1261496478-20441-2-git-send-email-kevin@tyndur.org> Message-ID: <20091222160650.GC32651@silvanus> On Tue, Dec 22, 2009 at 04:41:14PM +0100, Kevin Wolf wrote: > From: Max Reitz > > + Added CDI.usb header > + Added CDI.usb-hcd header > > Signed-off-by: Max Reitz > Signed-off-by: Kevin Wolf > --- > include/cdi/usb-hcd.h | 178 ++++++++++ > include/cdi/usb.h | 868 +++++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 1046 insertions(+), 0 deletions(-) > create mode 100644 include/cdi/usb-hcd.h > create mode 100644 include/cdi/usb.h > > diff --git a/include/cdi/usb-hcd.h b/include/cdi/usb-hcd.h > new file mode 100644 > index 0000000..60b0a01 > --- /dev/null > +++ b/include/cdi/usb-hcd.h > @@ -0,0 +1,178 @@ > +/* > + * Copyright (c) 2009 Max Reitz > + * > + * This program is free software. It comes without any warranty, to > + * the extent permitted by applicable law. You can redistribute it > + * and/or modify it under the terms of the Do What The Fuck You Want > + * To Public License, Version 2, as published by Sam Hocevar. See > + * http://sam.zoy.org/projects/COPYING.WTFPL for more details. > + */ > + > +/** > + * \if german > + * USB-Hostcontrollertreiber > + * \elseif english > + * USB host controller drivers > + * \endif > + * \defgroup USBHCD > + */ > +/*\@{*/ > + > +#ifndef _CDI_USB_HCD_H_ > +#define _CDI_USB_HCD_H_ > + > +#include "cdi/misc.h" > +#include "cdi/usb.h" > + > +struct cdi_usb_hc { > + struct cdi_device dev; > + > + /** > + * \if german > + * USB-Geschwindigkeit > + * \elseif english > + * USB speed > + * \endif > + */ > + cdi_usb_speed_t speed; > +}; > + > +struct cdi_usb_hcd { > + struct cdi_driver drv; > + > + /** > + * \if german > + * > + * Listet alle Geraete auf, die an den Rootports haengen und ruft ggf. > + * cdi_usb_device_init auf > + * > + * @param device Der Hostcontroller > + * > + * \elseif english > + * > + * Lists all devices hanging at the root ports and calls (if necessary) > + * cdi_usb_device_init > + * > + * @param device The host controller > + * > + * \endif > + */ > + void (*find_devices)(struct cdi_usb_hc* device); > + > + /** > + * \if german > + * > + * Aktiviert den zum angegebenen Geraet gehoerigen Rootport > + * > + * @param usb_device Das USB-Geraet (muss an einem Rootport haengen) > + * > + * \elseif english > + * > + * Activates the root port corresponding to the device > + * > + * @param usb_device The USB device (must be located at a root port) > + * > + * \endif > + */ > + void (*activate_device)(struct cdi_usb_device* usb_device); > + > + /** > + * \if german > + * > + * Verarbeitet USB-Pakete > + * > + * @param packets Array zu sendender Pakete > + * @param num_packets Anzahl der Pakete > + * > + * @return Gibt den Status an (z. B. USB_NO_ERROR fuer keinen Fehler) > + * > + * \elseif english > + * > + * Sends USB packets > + * > + * @param packets Array of packages to be sent > + * @param num_packages Number of packages > + * > + * \endif > + */ > + cdi_usb_status_t (*send_packets)(struct cdi_usb_packet* packets, > + size_t num_packets); > + > + /** > + * \if german > + * > + * Erstellt eine neue Pipe > + * > + * @param pipe Die Pipe > + * > + * \elseif english > + * > + * Creates a new pipe > + * > + * @param pipe The pipe > + * > + * \endif > + */ > + void (*add_pipe)(struct cdi_usb_pipe* pipe); > +}; > + > +#ifdef __cplusplus > +extern "C" { > +#endif Any reason not to put this to the top of the file? > + > +/** > + * \if german > + * > + * Initialisiert einen HCD > + * > + * @param driver Der HCD > + * > + * \elseif english > + * > + * Initializes an HCD > + * > + * @param driver The HCD > + * > + * \endif > + */ > +void cdi_usb_hcd_init(struct cdi_usb_hcd* driver); > + > +/** > + * \if german > + * > + * Gibt einen HCD frei > + * > + * @param driver Freizugebender HCD > + * > + * \elseif english > + * > + * Removes an HCD > + * > + * @param driver HCD to be removed > + * > + * \endif > + */ > +void cdi_usb_hcd_destroy(struct cdi_usb_hcd* driver); > + > +/** > + * \if german > + * > + * Registriert einen HCD > + * > + * @param driver Zu registrierender HCD > + * > + * \elseif english > + * > + * Registers an HCD > + * > + * @param driver HCD to be registered > + * > + * \endif > + */ > +void cdi_usb_hcd_register(struct cdi_usb_hcd* driver); > + > +#ifdef __cplusplus > +} //extern "C" > +#endif > + > +#endif > diff --git a/include/cdi/usb.h b/include/cdi/usb.h > new file mode 100644 > index 0000000..54ce1a0 > --- /dev/null > +++ b/include/cdi/usb.h > @@ -0,0 +1,868 @@ > +/* > + * Copyright (c) 2009 Max Reitz > + * > + * This program is free software. It comes without any warranty, to > + * the extent permitted by applicable law. You can redistribute it > + * and/or modify it under the terms of the Do What The Fuck You Want > + * To Public License, Version 2, as published by Sam Hocevar. See > + * http://sam.zoy.org/projects/COPYING.WTFPL for more details. > + */ > + > +/** > + * \if german > + * USB-Bustreiber > + * \elseif english > + * USB bus drivers > + * \endif > + * \defgroup USB > + */ > +/*\@{*/ > + > +#ifndef _CDI_USB_H_ > +#define _CDI_USB_H_ > + > +#include "stddef.h" > +#include "stdint.h" These should have angled brackets. > + > +typedef enum { > + /// Low-Speed (1,5 Mbit/s) > + CDI_USB_LOW_SPEED = 0x01, > + /// Full-Speed (12 Mbit/s) > + CDI_USB_FULL_SPEED = 0x02, > + /// High-Speed (480 Mbit/s) > + CDI_USB_HIGH_SPEED = 0x04, > + /// SuperSpeed (5 Gbit/s) > + CDI_USB_SUPER_SPEED = 0x08, > +} cdi_usb_speed_t; > + > +typedef enum { > + /** > + * \if german > + * SETUP-Paket > + * \elseif english > + * SETUP packet > + * \endif > + */ > + CDI_USB_PACKET_SETUP = 0x2D, > + > + /** > + * \if german > + * Daten vom Geraet zum Host > + * \elseif english > + * Data from the device to the host > + * \endif > + */ > + CDI_USB_PACKET_IN = 0x69, > + > + /** > + * \if german > + * Daten vom Host zum Geraet > + * \elseif english > + * Data from the host to the device > + * \endif > + */ > + CDI_USB_PACKET_OUT = 0xE1, > +} cdi_usb_packet_type_t; > + > +typedef enum { > + /** > + * \if german > + * Kein Fehler > + * \elseif english > + * No error > + * \endif > + */ > + CDI_USB_NO_ERROR = 0x0000, > + > + /// Stalled > + CDI_USB_STALLED = 0x0001, > + > + /** > + * \if german > + * Bufferfehler (Overflow, underrun, ...) > + * \elseif english > + * Buffer error (overflow, underrun, ...) > + * \endif > + */ > + CDI_USB_BUFFER_ERROR = 0x0002, > + > + /** > + * \if german > + * Babble (Geraet hat zu viele Daten gesendet) > + * \elseif english > + * Babble (device has sent more data than expected) > + * \endif > + */ > + CDI_USB_BABBLE = 0x0004, > + > + /** > + * \if german > + * NAK (Daten sind erneut zu senden) > + * \elseif english > + * NAK (data has to be sent again) > + * \endif > + */ > + CDI_USB_NAK = 0x0008, > + > + /** > + * \if german > + * CRC-Fehler > + * \elseif english > + * CRC error > + * \endif > + */ > + CDI_USB_CRC = 0x0010, > + > + /// Timeout > + CDI_USB_TIMEOUT = 0x0020, > + > + /** > + * \if german > + * Bitstuff/-destuff-Fehler > + * \elseif english > + * bitstuff/-destuff error > + * \endif > + */ > + CDI_USB_BITSTUFF = 0x0040, > + > + /** > + * \if german > + * Reservieren von Speicher fehlgeschlagen, ... > + * \elseif english > + * Allocating of memory failed, ... > + * \endif > + */ > + CDI_USB_TRIVIAL_ERROR = 0x0080, > +} cdi_usb_status_t; > + > +struct cdi_usb_hc; > +struct cdi_usb_pipe; > +struct cdi_usb_interface; > +struct cdi_usb_driver; > + > +struct cdi_usb_device { > + /** > + * \if german > + * Der USB-Bustreiber > + * \elseif english > + * The USB bus driver > + * \endif > + */ > + struct cdi_usb_driver* drv; > + > + /** > + * \if german > + * Der Hostcontroller, an dem das Geraet im Endeffekt haengt > + * \elseif english > + * The host controller where the device is eventually located > + * \endif > + */ > + struct cdi_usb_hc* hc; Such pointers have a very fundamental problem: The driver for the host controller might run in a different process. Maybe someone has a better suggestion, but the only options I can think of are: * Use a different struct here like struct cdi_usb_hc_link; its definition would be OS specific (assuming the USB driver doesn't need access to the fields but just some kind of ID for its HC) * Use an IDC endpoint here. Obviously depends on someone implementing the IDC thing. Maybe we could go with the current pointer, put a FIXME there and replace it by an IDC endpoint later. I think this definitely needs some discussion. > + /** > + * \if german > + * Haengt das Geraet an einem Hub, dann ist dieses Feld ein Pointer auf > + * das entsprechende USB-Geraet, haengt es an einem Rootport, dann ist > + * dieses Feld NULL. > + * \elseif english > + * If the device is located at a hub, then this field contains a pointer to > + * the corresponding USB device, if it's located at a root port, then this > + * field is set to NULL. > + * \endif > + */ > + struct cdi_usb_device* hub; > + > + /** > + * \if german > + * Geschwindigkeit(-en) dieses Geraets > + * \elseif english > + * Speed(-s) supported by this device > + * \endif > + */ > + cdi_usb_speed_t speed; > + > + /// ID > + int id; > + > + /** > + * \if german > + * Entweder der Rootport oder der Port des Hubs, an dem das Geraet haengt > + * \elseif english > + * Either the root port or the port hub's port where the device is located > + * \endif > + */ > + int port; > + > + /** > + * \if german > + * Hier darf der Geraetetreiber seine Daten unterbringen > + * \elseif english > + * The device driver may store its data here > + * \endif > + */ > + void* driver_data; This is really ugly, but I can see why you put it there. Hope it'll go away with the planned device creation patches. They should include some notion of a bus (be it USB, PCI or whatever) that each device is on. > + /** > + * \if german > + * Felder, die so auch bei PCI existieren: Vendor-, Device-, Class-, > + * Subclass- und Protocol-ID > + * \elseif english > + * Fields existing at PCI, too: vendor, device, class, subclass and > + * protocol ID. > + * \endif > + */ > + int vendor_id; > + int device_id; > + int class_id; > + int subclass_id; > + int protocol_id; I guess these have a fixed size in fact and not only int? > + > + /** > + * \if german > + * > + * Setzt das USB-Geraet zurueck (kann sowohl vom HCD als auch vom Hub > + * driver ausgefuehrt werden) > + * > + * @param usb_device Das Geraet > + * > + * \elseif english > + * > + * Resets the USB device (may be executed either by the HCD or the hub > + * driver) > + * > + * @param usb_device The device > + * > + * \endif > + */ > + void (*reset)(struct cdi_usb_device* usb_device); > + > + /** > + * \if german > + * Ziemlich lowlevelig. Gehoert das hier wirklich rein? > + * \elseif english > + * Very low level. Does it really fit in here? > + * \endif > + */ Probably doesn't fit here without a matching declaration. ;-) > + /** > + * \if german > + * Im einzelnen sind das: Pipe fuer Endpoint 0 > + * \elseif english > + * In particular these things are: Pipe for endpoint 0 > + * \endif > + */ > + struct cdi_usb_pipe* ep0; > + > + /// Device descriptor > + void* device_desc; > + > + /// Configuration descriptor > + void* config_desc; > + > + /// Interface descriptor > + struct cdi_usb_interface* interface_desc; > + > + /// Class descriptor > + void* class_desc; > +}; From kevin at tyndur.org Tue Dec 22 19:08:00 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 22 Dec 2009 19:08:00 +0100 Subject: [cdi-devel] [PATCH] cdi.h: Remove cdi_device.type Message-ID: <1261505280-7094-1-git-send-email-kevin@tyndur.org> - cdi_device.type is redundant, we already have a pointer to the driver which stores the type. Remove it. Signed-off-by: Kevin Wolf --- include/cdi.h | 1 - 1 files changed, 0 insertions(+), 1 deletions(-) diff --git a/include/cdi.h b/include/cdi.h index f45181a..b09bcff 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -34,7 +34,6 @@ typedef enum { struct cdi_driver; struct cdi_device { - cdi_device_type_t type; const char* name; struct cdi_driver* driver; -- 1.6.0.2 From kevin at tyndur.org Tue Dec 29 19:51:40 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 29 Dec 2009 19:51:40 +0100 Subject: [cdi-devel] [PATCH 0/3] Rework device creation mechanism Message-ID: <1262112703-11261-1-git-send-email-kevin@tyndur.org> Currently, device creation is a very static thing. Each driver looks in its initialisation function for devices that it supports. This means that (a) the init_driver code is duplicated in each driver and (b) that hot-plugging is impossible (becomes more important with USB support). Making it library driven should solve these problems and might simplify adaption to OS specific needs as an extra bonus (I'm thinking of Pedigree's pDev here). Kevin Wolf (3): cdi.h: Introduce cdi_bus_data Move complete initialisation to device_init Initialise PCI devices in the CDI library ata/atapi.c | 2 +- ata/device.c | 11 ++++++++--- ata/device.h | 6 +++--- ata/main.c | 2 -- e1000/device.c | 31 +++++++++++++++++++++++-------- e1000/device.h | 4 +--- e1000/main.c | 31 ++----------------------------- floppy/main.c | 2 +- include/cdi.h | 15 ++++++++++++--- include/cdi/pci.h | 2 ++ ne2k/include/ne2k.h | 4 +--- ne2k/main.c | 28 ++-------------------------- ne2k/ne2k.c | 26 ++++++++++++++++++++------ pcnet/include/pcnet.h | 3 +-- pcnet/main.c | 25 ++----------------------- pcnet/pcnet.c | 29 ++++++++++++++++++++++------- rtl8139/include/rtl8139.h | 3 +-- rtl8139/main.c | 28 ++-------------------------- rtl8139/rtl8139.c | 26 ++++++++++++++++++++------ sis900/device.c | 30 ++++++++++++++++++++++-------- sis900/device.h | 3 +-- sis900/main.c | 31 ++----------------------------- 22 files changed, 149 insertions(+), 193 deletions(-) From kevin at tyndur.org Tue Dec 29 19:51:41 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 29 Dec 2009 19:51:41 +0100 Subject: [cdi-devel] [PATCH 1/3] cdi.h: Introduce cdi_bus_data In-Reply-To: <1262112703-11261-1-git-send-email-kevin@tyndur.org> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> Message-ID: <1262112703-11261-2-git-send-email-kevin@tyndur.org> + Each device gets a structure that links it to its bus. For now this means moving the cdi_pci_device out of the specific device struct of each driver into the generic cdi_device. Signed-off-by: Kevin Wolf --- e1000/device.c | 12 +++++++----- e1000/device.h | 2 -- e1000/main.c | 2 +- include/cdi.h | 11 +++++++++-- include/cdi/pci.h | 2 ++ ne2k/include/ne2k.h | 2 -- ne2k/main.c | 2 +- ne2k/ne2k.c | 7 ++++--- pcnet/include/pcnet.h | 1 - pcnet/main.c | 2 +- pcnet/pcnet.c | 12 ++++++++---- rtl8139/include/rtl8139.h | 1 - rtl8139/main.c | 2 +- rtl8139/rtl8139.c | 7 ++++--- sis900/device.c | 11 ++++++----- sis900/device.h | 1 - sis900/main.c | 2 +- 17 files changed, 45 insertions(+), 34 deletions(-) diff --git a/e1000/device.c b/e1000/device.c index 4e5a62e..a92acf3 100644 --- a/e1000/device.c +++ b/e1000/device.c @@ -34,6 +34,7 @@ #include "cdi.h" #include "cdi/misc.h" +#include "cdi/pci.h" #include "device.h" #include "e1000_io.h" @@ -148,14 +149,15 @@ static uint64_t get_mac_address(struct e1000_device* device) void e1000_init_device(struct cdi_device* device) { struct e1000_device* netcard = (struct e1000_device*) device; + struct cdi_pci_device* pci = (struct cdi_pci_device*) device->bus_data; netcard->net.send_packet = e1000_send_packet; // PCI-bezogenes Zeug initialisieren - netcard->revision = netcard->pci->rev_id; - cdi_register_irq(netcard->pci->irq, e1000_handle_interrupt, device); - cdi_pci_alloc_ioports(netcard->pci); + netcard->revision = pci->rev_id; + cdi_register_irq(pci->irq, e1000_handle_interrupt, device); + cdi_pci_alloc_ioports(pci); - cdi_list_t reslist = netcard->pci->resources; + cdi_list_t reslist = pci->resources; struct cdi_pci_resource* res; int i; for (i = 0; (res = cdi_list_get(reslist, i)); i++) { @@ -167,7 +169,7 @@ void e1000_init_device(struct cdi_device* device) // Karte initialisieren printf("e1000: IRQ %d, MMIO an %p Revision:%d\n", - netcard->pci->irq, netcard->mem_base, netcard->revision); + pci->irq, netcard->mem_base, netcard->revision); printf("e1000: Fuehre Reset der Karte durch\n"); reset_nic(netcard); diff --git a/e1000/device.h b/e1000/device.h index 34f9dc1..40f03a7 100644 --- a/e1000/device.h +++ b/e1000/device.h @@ -33,7 +33,6 @@ #include "cdi.h" #include "cdi/net.h" -#include "cdi/pci.h" /* Register */ @@ -143,7 +142,6 @@ struct e1000_rx_descriptor { struct e1000_device { struct cdi_net_device net; - struct cdi_pci_device* pci; void* phys; diff --git a/e1000/main.c b/e1000/main.c index 26ce832..c98e191 100644 --- a/e1000/main.c +++ b/e1000/main.c @@ -65,7 +65,7 @@ static int e1000_driver_init(void) memset(device, 0, sizeof(*device)); device->phys = phys_device; - device->pci = dev; + device->net.dev.bus_data = (struct cdi_bus_data*) dev; cdi_list_push(driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); diff --git a/include/cdi.h b/include/cdi.h index b09bcff..a0e1826 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -33,9 +33,16 @@ typedef enum { } cdi_device_type_t; struct cdi_driver; +struct cdi_device; + +struct cdi_bus_data { + cdi_device_type_t bus_type; +}; + struct cdi_device { - const char* name; - struct cdi_driver* driver; + const char* name; + struct cdi_driver* driver; + struct cdi_bus_data* bus_data; // tyndur-spezifisch void* backdev; diff --git a/include/cdi/pci.h b/include/cdi/pci.h index 31c1878..1192332 100644 --- a/include/cdi/pci.h +++ b/include/cdi/pci.h @@ -18,6 +18,8 @@ #include "cdi/lists.h" struct cdi_pci_device { + struct cdi_bus_data bus_data; + uint16_t bus; uint16_t dev; uint16_t function; diff --git a/ne2k/include/ne2k.h b/ne2k/include/ne2k.h index 6e4125b..2a86897 100644 --- a/ne2k/include/ne2k.h +++ b/ne2k/include/ne2k.h @@ -33,7 +33,6 @@ #include "cdi.h" #include "cdi/net.h" -#include "cdi/pci.h" #define NE_CMD 0 #define NE_PSTART 1 @@ -84,7 +83,6 @@ typedef struct { struct ne2k_device { struct cdi_net_device net; - struct cdi_pci_device* pci; void* phys; uint16_t port_base; diff --git a/ne2k/main.c b/ne2k/main.c index 229bccb..6f2448b 100644 --- a/ne2k/main.c +++ b/ne2k/main.c @@ -60,7 +60,7 @@ static int ne2k_driver_init(void) memset(device, 0, sizeof(*device)); device->phys = phys_device; - device->pci = dev; + device->net.dev.bus_data = (struct cdi_bus_data*) dev; cdi_list_push(ne2k_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); diff --git a/ne2k/ne2k.c b/ne2k/ne2k.c index a77465b..d7f1e06 100644 --- a/ne2k/ne2k.c +++ b/ne2k/ne2k.c @@ -80,14 +80,15 @@ static inline uint32_t read_register_dword(struct ne2k_device* netcard, uint8_t void ne2k_init_device(struct cdi_device* device) { struct ne2k_device* netcard = (struct ne2k_device*) device; + struct cdi_pci_device* pci = (struct cdi_pci_device*) device->bus_data; netcard->net.send_packet = ne2k_send_packet; // PCI-bezogenes Zeug initialisieren DEBUG_MSG("Interrupthandler und Ports registrieren"); - cdi_register_irq(netcard->pci->irq, ne2k_handle_interrupt, device); - cdi_pci_alloc_ioports(netcard->pci); + cdi_register_irq(pci->irq, ne2k_handle_interrupt, device); + cdi_pci_alloc_ioports(pci); - cdi_list_t reslist = netcard->pci->resources; + cdi_list_t reslist = pci->resources; struct cdi_pci_resource* res; int i; for (i = 0; (res = cdi_list_get(reslist, i)); i++) { diff --git a/pcnet/include/pcnet.h b/pcnet/include/pcnet.h index 8c8198e..0b55122 100644 --- a/pcnet/include/pcnet.h +++ b/pcnet/include/pcnet.h @@ -108,7 +108,6 @@ struct transmit_descriptor{ struct pcnet_device { struct cdi_net_device net; - struct cdi_pci_device* pci; uint16_t port_base; struct receive_descriptor *receive_descriptor; diff --git a/pcnet/main.c b/pcnet/main.c index 342ac39..613c162 100644 --- a/pcnet/main.c +++ b/pcnet/main.c @@ -61,7 +61,7 @@ static int pcnet_driver_init(void) memset(device, 0, sizeof(struct pcnet_device)); - device->pci = dev; + device->net.dev.bus_data = (struct cdi_bus_data*) dev; cdi_list_push(pcnet_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); diff --git a/pcnet/pcnet.c b/pcnet/pcnet.c index dc671e7..2e29389 100644 --- a/pcnet/pcnet.c +++ b/pcnet/pcnet.c @@ -55,15 +55,16 @@ static void pcnet_write_bcr(struct pcnet_device *netcard, size_t bcr, uint16_t v void pcnet_init_device(struct cdi_device* device) { struct pcnet_device* netcard = (struct pcnet_device*) device; + struct cdi_pci_device* pci = (struct cdi_pci_device*) device->bus_data; netcard->net.send_packet = pcnet_send_packet; // PCI-bezogenes Zeug initialisieren DEBUG_MSG("Interrupthandler und Ports registrieren"); - cdi_register_irq(netcard->pci->irq, pcnet_handle_interrupt, device); - cdi_pci_alloc_ioports(netcard->pci); + cdi_register_irq(pci->irq, pcnet_handle_interrupt, device); + cdi_pci_alloc_ioports(pci); // I/O Port raussuchen - cdi_list_t reslist = netcard->pci->resources; + cdi_list_t reslist = pci->resources; struct cdi_pci_resource* res; int i; for (i = 0; (res = cdi_list_get(reslist, i)); i++) { @@ -226,6 +227,9 @@ void pcnet_stop(struct pcnet_device *netcard) } void pcnet_dev_init(struct pcnet_device *netcard, int promiscuous) { + struct cdi_pci_device* pci = + (struct cdi_pci_device*) netcard->net.dev.bus_data; + // Allocate the receive & transmit descriptor buffer void *virt_desc_region; void *phys_desc_region; @@ -307,7 +311,7 @@ void pcnet_dev_init(struct pcnet_device *netcard, int promiscuous) pcnet_write_csr(netcard, CSR_STATUS, STATUS_INIT | STATUS_INTERRUPT_ENABLE); // Wait until initialization completed, NOTE: see pcnet_handle_interrupt() - int irq = cdi_wait_irq(netcard->pci->irq, 1000); + int irq = cdi_wait_irq(pci->irq, 1000); if(irq < 0 || netcard->init_wait_for_irq == 1) printf("pcnet: waiting for IRQ failed: %d\n", irq); diff --git a/rtl8139/include/rtl8139.h b/rtl8139/include/rtl8139.h index 859696f..2c9e419 100644 --- a/rtl8139/include/rtl8139.h +++ b/rtl8139/include/rtl8139.h @@ -82,7 +82,6 @@ typedef struct { struct rtl8139_device { struct cdi_net_device net; - struct cdi_pci_device* pci; void* phys; uint16_t port_base; diff --git a/rtl8139/main.c b/rtl8139/main.c index 131c67b..5411238 100644 --- a/rtl8139/main.c +++ b/rtl8139/main.c @@ -63,7 +63,7 @@ static int rtl8139_driver_init(void) memset(device, 0, sizeof(*device)); device->phys = phys_device; - device->pci = dev; + device->net.dev.bus_data = (struct cdi_bus_data*) dev; cdi_list_push(rtl8139_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); diff --git a/rtl8139/rtl8139.c b/rtl8139/rtl8139.c index 7b43154..c89e08f 100644 --- a/rtl8139/rtl8139.c +++ b/rtl8139/rtl8139.c @@ -78,14 +78,15 @@ static inline uint32_t read_register_dword(struct rtl8139_device* netcard, uint8 void rtl8139_init_device(struct cdi_device* device) { struct rtl8139_device* netcard = (struct rtl8139_device*) device; + struct cdi_pci_device* pci = (struct cdi_pci_device*) device->bus_data; netcard->net.send_packet = rtl8139_send_packet; // PCI-bezogenes Zeug initialisieren DEBUG_MSG("Interrupthandler und Ports registrieren"); - cdi_register_irq(netcard->pci->irq, rtl8139_handle_interrupt, device); - cdi_pci_alloc_ioports(netcard->pci); + cdi_register_irq(pci->irq, rtl8139_handle_interrupt, device); + cdi_pci_alloc_ioports(pci); - cdi_list_t reslist = netcard->pci->resources; + cdi_list_t reslist = pci->resources; struct cdi_pci_resource* res; int i; for (i = 0; (res = cdi_list_get(reslist, i)); i++) { diff --git a/sis900/device.c b/sis900/device.c index 0c95055..d6e68d6 100644 --- a/sis900/device.c +++ b/sis900/device.c @@ -184,14 +184,15 @@ static uint64_t get_mac_address(struct sis900_device* device) void sis900_init_device(struct cdi_device* device) { struct sis900_device* netcard = (struct sis900_device*) device; + struct cdi_pci_device* pci = (struct cdi_pci_device*) device->bus_data; netcard->net.send_packet = sis900_send_packet; // PCI-bezogenes Zeug initialisieren - netcard->revision = netcard->pci->rev_id; - cdi_register_irq(netcard->pci->irq, sis900_handle_interrupt, device); - cdi_pci_alloc_ioports(netcard->pci); + netcard->revision = pci->rev_id; + cdi_register_irq(pci->irq, sis900_handle_interrupt, device); + cdi_pci_alloc_ioports(pci); - cdi_list_t reslist = netcard->pci->resources; + cdi_list_t reslist = pci->resources; struct cdi_pci_resource* res; int i; for (i = 0; (res = cdi_list_get(reslist, i)); i++) { @@ -203,7 +204,7 @@ void sis900_init_device(struct cdi_device* device) // Karte initialisieren #ifdef DEBUG printf("sis900: IRQ %d, Ports an %x Revision:%d\n", - netcard->pci->irq, netcard->port_base, netcard->revision); + pci->irq, netcard->port_base, netcard->revision); printf("sis900: Fuehre Reset der Karte durch\n"); #endif diff --git a/sis900/device.h b/sis900/device.h index f96b148..cbb44bf 100644 --- a/sis900/device.h +++ b/sis900/device.h @@ -128,7 +128,6 @@ struct sis900_tx_descriptor { struct sis900_device { struct cdi_net_device net; - struct cdi_pci_device* pci; void* phys; diff --git a/sis900/main.c b/sis900/main.c index b03511c..f813993 100644 --- a/sis900/main.c +++ b/sis900/main.c @@ -65,7 +65,7 @@ static int sis900_driver_init(void) memset(device, 0, sizeof(*device)); device->phys = phys_device; - device->pci = dev; + device->net.dev.bus_data = (struct cdi_bus_data*) dev; cdi_list_push(sis900_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); -- 1.6.0.2 From kevin at tyndur.org Tue Dec 29 19:51:42 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 29 Dec 2009 19:51:42 +0100 Subject: [cdi-devel] [PATCH 2/3] Move complete initialisation to device_init In-Reply-To: <1262112703-11261-1-git-send-email-kevin@tyndur.org> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> Message-ID: <1262112703-11261-3-git-send-email-kevin@tyndur.org> * Up to now, the cdi-device struct was created during the init_driver call of the driver and init_device only completed the initialisation. The new model is that init_device gets bus information (e.g. PCI address, device/vendor ID) and decides if it can handle the device. If so, it creates a cdi_device object and returns it. This way device creation is decoupled from driver initialisation and hot-plugging becomes possible. Signed-off-by: Kevin Wolf --- ata/atapi.c | 2 +- ata/device.c | 11 ++++++++--- ata/device.h | 6 +++--- ata/main.c | 2 -- e1000/device.c | 23 ++++++++++++++++++----- e1000/device.h | 2 +- e1000/main.c | 17 ++++++----------- floppy/main.c | 2 +- include/cdi.h | 4 +++- ne2k/include/ne2k.h | 2 +- ne2k/main.c | 16 +++++----------- ne2k/ne2k.c | 23 ++++++++++++++++++----- pcnet/include/pcnet.h | 2 +- pcnet/main.c | 13 +++++-------- pcnet/pcnet.c | 21 ++++++++++++++++----- rtl8139/include/rtl8139.h | 2 +- rtl8139/main.c | 16 +++++----------- rtl8139/rtl8139.c | 23 ++++++++++++++++++----- sis900/device.c | 23 ++++++++++++++++++----- sis900/device.h | 2 +- sis900/main.c | 16 +++++----------- 21 files changed, 135 insertions(+), 93 deletions(-) diff --git a/ata/atapi.c b/ata/atapi.c index 77b1074..eaac06d 100644 --- a/ata/atapi.c +++ b/ata/atapi.c @@ -77,7 +77,7 @@ int atapi_drv_identify(struct ata_device* dev) return 1; } -void atapi_init_device(struct cdi_device* device) +void atapi_init_device(struct ata_device* device) { struct cdi_scsi_device* scsi = (struct cdi_scsi_device*) device; diff --git a/ata/device.c b/ata/device.c index e564221..7339d65 100644 --- a/ata/device.c +++ b/ata/device.c @@ -199,6 +199,8 @@ void ata_parse_partitions(struct ata_device* dev) partition_list)); // Geraet registrieren + partition->dev.storage.dev.driver = &dev->controller->storage->drv; + ata_init_device((struct ata_device*) partition); cdi_list_push(dev->controller->storage->drv.devices, partition); // An Partitionsliste fuer das aktuelle Geraet anhaengen @@ -298,6 +300,8 @@ void ata_init_controller(struct ata_controller* controller) ata_parse_partitions(dev); // Geraet registrieren + dev->dev.storage.dev.driver = &controller->storage->drv; + ata_init_device(dev); cdi_list_push(controller->storage->drv.devices, dev); #ifdef ATAPI_ENABLE } else { @@ -305,7 +309,9 @@ void ata_init_controller(struct ata_controller* controller) asprintf((char**) &(dev->dev.scsi.dev.name),"atapi%01d%01d", (uint32_t) controller->id, i); - // Geraet registrieren + // Geraet registrieren + dev->dev.scsi.dev.driver = &controller->scsi->drv; + atapi_init_device(dev); cdi_list_push(controller->scsi->drv.devices, dev); } #endif @@ -338,9 +344,8 @@ void ata_remove_controller(struct ata_controller* controller) } -void ata_init_device(struct cdi_device* device) +void ata_init_device(struct ata_device* dev) { - struct ata_device* dev = (struct ata_device*) device; cdi_storage_device_init(&dev->dev.storage); } diff --git a/ata/device.h b/ata/device.h index 152f1ec..efb3f6f 100644 --- a/ata/device.h +++ b/ata/device.h @@ -128,7 +128,7 @@ // Debug #ifdef DEBUG_ENABLE - #define DEBUG(fmt, ...) printf("ata: " fmt, __VA_ARGS__) + #define DEBUG(fmt, ...) printf("ata: " fmt, ## __VA_ARGS__) #else #define DEBUG(...) #endif @@ -445,7 +445,7 @@ struct ata_request { void ata_init_controller(struct ata_controller* controller); void ata_remove_controller(struct ata_controller* controller); -void ata_init_device(struct cdi_device* device); +void ata_init_device(struct ata_device* dev); void ata_remove_device(struct cdi_device* device); int ata_read_blocks(struct cdi_storage_device* device, uint64_t block, uint64_t count, void* buffer); @@ -468,7 +468,7 @@ int ata_drv_write_sectors(struct ata_device* dev, uint64_t start, size_t count, // ATAPI-Funktionen int atapi_drv_identify(struct ata_device* dev); -void atapi_init_device(struct cdi_device* device); +void atapi_init_device(struct ata_device* device); void atapi_remove_device(struct cdi_device* device); int atapi_request(struct cdi_scsi_device* dev,struct cdi_scsi_packet* packet); diff --git a/ata/main.c b/ata/main.c index 3cc502d..814f830 100644 --- a/ata/main.c +++ b/ata/main.c @@ -181,7 +181,6 @@ static struct cdi_storage_driver driver_storage = { .name = DRIVER_STORAGE_NAME, .init = ata_driver_init, .destroy = ata_driver_destroy, - .init_device = ata_init_device, .remove_device = ata_remove_device, }, .read_blocks = ata_read_blocks, @@ -194,7 +193,6 @@ static struct cdi_scsi_driver driver_scsi = { .name = DRIVER_SCSI_NAME, .init = atapi_driver_init, .destroy = atapi_driver_destroy, - .init_device = atapi_init_device, .remove_device = atapi_remove_device, }, .request = atapi_request, diff --git a/e1000/device.c b/e1000/device.c index a92acf3..b4aa4eb 100644 --- a/e1000/device.c +++ b/e1000/device.c @@ -146,15 +146,26 @@ static uint64_t get_mac_address(struct e1000_device* device) return mac; } -void e1000_init_device(struct cdi_device* device) +struct cdi_device* e1000_init_device(struct cdi_bus_data* bus_data) { - struct e1000_device* netcard = (struct e1000_device*) device; - struct cdi_pci_device* pci = (struct cdi_pci_device*) device->bus_data; + struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data; + struct e1000_device* netcard; + void* phys_device; + + if (!((pci->vendor_id == 0x8086) && (pci->device_id == 0x100e))) { + return NULL; + } + + cdi_alloc_phys_mem(sizeof(*netcard), (void**) &netcard, &phys_device); + memset(netcard, 0, sizeof(*netcard)); + netcard->net.send_packet = e1000_send_packet; + netcard->phys = phys_device; + netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; // PCI-bezogenes Zeug initialisieren netcard->revision = pci->rev_id; - cdi_register_irq(pci->irq, e1000_handle_interrupt, device); + cdi_register_irq(pci->irq, e1000_handle_interrupt, &netcard->net.dev); cdi_pci_alloc_ioports(pci); cdi_list_t reslist = pci->resources; @@ -177,7 +188,9 @@ void e1000_init_device(struct cdi_device* device) netcard->net.mac = get_mac_address(netcard); printf("e1000: MAC-Adresse: %012llx\n", (uint64_t) netcard->net.mac); - cdi_net_device_init((struct cdi_net_device*) device); + cdi_net_device_init(&netcard->net); + + return &netcard->net.dev; } void e1000_remove_device(struct cdi_device* device) diff --git a/e1000/device.h b/e1000/device.h index 40f03a7..9266e0f 100644 --- a/e1000/device.h +++ b/e1000/device.h @@ -157,7 +157,7 @@ struct e1000_device { uint8_t revision; }; -void e1000_init_device(struct cdi_device* device); +struct cdi_device* e1000_init_device(struct cdi_bus_data* bus_data); void e1000_remove_device(struct cdi_device* device); void e1000_send_packet diff --git a/e1000/main.c b/e1000/main.c index c98e191..229ec3f 100644 --- a/e1000/main.c +++ b/e1000/main.c @@ -53,19 +53,13 @@ static int e1000_driver_init(void) cdi_list_t pci_devices = cdi_list_create(); cdi_pci_get_all_devices(pci_devices); + struct cdi_device* device; struct cdi_pci_device* dev; int i; for (i = 0; (dev = cdi_list_get(pci_devices, i)); i++) { - if ((dev->vendor_id == 0x8086) && (dev->device_id == 0x100e)) { - void* phys_device; - struct e1000_device* device; - - cdi_alloc_phys_mem(sizeof(*device), - (void**) &device, &phys_device); - memset(device, 0, sizeof(*device)); - - device->phys = phys_device; - device->net.dev.bus_data = (struct cdi_bus_data*) dev; + device = e1000_init_device((struct cdi_bus_data*) dev); + if (device != NULL) { + // TODO cdi_list_push(driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); @@ -95,8 +89,9 @@ static int e1000_driver_destroy(void) static struct cdi_net_driver driver = { .drv = { - .type = CDI_NETWORK, .name = DRIVER_NAME, + .type = CDI_NETWORK, + .bus = CDI_PCI, .init = e1000_driver_init, .destroy = e1000_driver_destroy, .init_device = e1000_init_device, diff --git a/floppy/main.c b/floppy/main.c index e6208a6..c516a23 100644 --- a/floppy/main.c +++ b/floppy/main.c @@ -68,6 +68,7 @@ static int floppy_driver_init(void) // Geraet nur eintragen wenn es existiert if (floppy_device_probe(device) != 0) { + floppy_init_device((struct cdi_device*) device); cdi_list_push(floppy_driver.drv.devices, device); } else { free(device); @@ -107,7 +108,6 @@ static struct cdi_storage_driver floppy_driver = { .name = DRIVER_NAME, .init = floppy_driver_init, .destroy = floppy_driver_destroy, - .init_device = floppy_init_device, .remove_device = floppy_remove_device, }, .read_blocks = floppy_read_blocks, diff --git a/include/cdi.h b/include/cdi.h index a0e1826..ab9f308 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -30,6 +30,7 @@ typedef enum { CDI_USB_HCD = 7, CDI_USB = 8, CDI_FILESYSTEM = 9, + CDI_PCI = 10, } cdi_device_type_t; struct cdi_driver; @@ -50,10 +51,11 @@ struct cdi_device { struct cdi_driver { cdi_device_type_t type; + cdi_device_type_t bus; const char* name; cdi_list_t devices; - void (*init_device)(struct cdi_device* device); + struct cdi_device* (*init_device)(struct cdi_bus_data* bus_data); void (*remove_device)(struct cdi_device* device); int (*init)(void); diff --git a/ne2k/include/ne2k.h b/ne2k/include/ne2k.h index 2a86897..585eab5 100644 --- a/ne2k/include/ne2k.h +++ b/ne2k/include/ne2k.h @@ -94,7 +94,7 @@ struct ne2k_device { cdi_list_t pending_sends; }; -void ne2k_init_device(struct cdi_device* device); +struct cdi_device* ne2k_init_device(struct cdi_bus_data* bus_data); void ne2k_remove_device(struct cdi_device* device); void ne2k_send_packet diff --git a/ne2k/main.c b/ne2k/main.c index 6f2448b..e46e866 100644 --- a/ne2k/main.c +++ b/ne2k/main.c @@ -49,18 +49,11 @@ static int ne2k_driver_init(void) cdi_pci_get_all_devices(pci_devices); struct cdi_pci_device* dev; + struct cdi_device* device; int i; for (i = 0; (dev = cdi_list_get(pci_devices, i)); i++) { - if ((dev->vendor_id == 0x10ec) && (dev->device_id == 0x8029)) { - void* phys_device; - struct ne2k_device* device; - - cdi_alloc_phys_mem(sizeof(*device), - (void**) &device, &phys_device); - memset(device, 0, sizeof(*device)); - - device->phys = phys_device; - device->net.dev.bus_data = (struct cdi_bus_data*) dev; + device = ne2k_init_device((struct cdi_bus_data*) dev); + if (device != NULL) { cdi_list_push(ne2k_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); @@ -87,8 +80,9 @@ static int ne2k_driver_destroy(void) static struct cdi_net_driver ne2k_driver = { .drv = { - .type = CDI_NETWORK, .name = DRIVER_NAME, + .type = CDI_NETWORK, + .bus = CDI_PCI, .init = ne2k_driver_init, .destroy = ne2k_driver_destroy, .init_device = ne2k_init_device, diff --git a/ne2k/ne2k.c b/ne2k/ne2k.c index d7f1e06..b3faf46 100644 --- a/ne2k/ne2k.c +++ b/ne2k/ne2k.c @@ -77,15 +77,26 @@ static inline uint32_t read_register_dword(struct ne2k_device* netcard, uint8_t return cdi_inl(netcard->port_base + reg); } -void ne2k_init_device(struct cdi_device* device) +struct cdi_device* ne2k_init_device(struct cdi_bus_data* bus_data) { - struct ne2k_device* netcard = (struct ne2k_device*) device; - struct cdi_pci_device* pci = (struct cdi_pci_device*) device->bus_data; + struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data; + struct ne2k_device* netcard; + void* phys_device; + + if (!((pci->vendor_id == 0x10ec) && (pci->device_id == 0x8029))) { + return NULL; + } + + cdi_alloc_phys_mem(sizeof(*netcard), (void**) &netcard, &phys_device); + memset(netcard, 0, sizeof(*netcard)); + netcard->net.send_packet = ne2k_send_packet; + netcard->phys = phys_device; + netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; // PCI-bezogenes Zeug initialisieren DEBUG_MSG("Interrupthandler und Ports registrieren"); - cdi_register_irq(pci->irq, ne2k_handle_interrupt, device); + cdi_register_irq(pci->irq, ne2k_handle_interrupt, &netcard->net.dev); cdi_pci_alloc_ioports(pci); cdi_list_t reslist = pci->resources; @@ -154,8 +165,10 @@ void ne2k_init_device(struct cdi_device* device) netcard->pending_sends = cdi_list_create(); - cdi_net_device_init((struct cdi_net_device*) device); + cdi_net_device_init(&netcard->net); DEBUG_MSG("Fertig initialisiert"); + + return &netcard->net.dev; } void ne2k_remove_device(struct cdi_device* device) diff --git a/pcnet/include/pcnet.h b/pcnet/include/pcnet.h index 0b55122..000862c 100644 --- a/pcnet/include/pcnet.h +++ b/pcnet/include/pcnet.h @@ -127,7 +127,7 @@ struct pcnet_device { volatile int init_wait_for_irq; }; -void pcnet_init_device(struct cdi_device* device); +struct cdi_device* pcnet_init_device(struct cdi_bus_data* bus_data); void pcnet_remove_device(struct cdi_device* device); void pcnet_send_packet (struct cdi_net_device* device, void* data, size_t size); diff --git a/pcnet/main.c b/pcnet/main.c index 613c162..de05930 100644 --- a/pcnet/main.c +++ b/pcnet/main.c @@ -53,15 +53,11 @@ static int pcnet_driver_init(void) cdi_pci_get_all_devices(pci_devices); struct cdi_pci_device* dev; + struct cdi_device* device; int i; for (i = 0; (dev = cdi_list_get(pci_devices, i)); i++) { - if ((dev->vendor_id == VENDOR_ID) && (dev->device_id == DEVICE_ID)) { - struct pcnet_device* device; - device = malloc(sizeof(struct pcnet_device)); - - memset(device, 0, sizeof(struct pcnet_device)); - - device->net.dev.bus_data = (struct cdi_bus_data*) dev; + device = pcnet_init_device((struct cdi_bus_data*) dev); + if (device) { cdi_list_push(pcnet_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); @@ -88,8 +84,9 @@ static int pcnet_driver_destroy(void) static struct cdi_net_driver pcnet_driver = { .drv = { - .type = CDI_NETWORK, .name = DRIVER_NAME, + .type = CDI_NETWORK, + .bus = CDI_PCI, .init = pcnet_driver_init, .destroy = pcnet_driver_destroy, .init_device = pcnet_init_device, diff --git a/pcnet/pcnet.c b/pcnet/pcnet.c index 2e29389..9983fd5 100644 --- a/pcnet/pcnet.c +++ b/pcnet/pcnet.c @@ -52,15 +52,24 @@ static void pcnet_write_bcr(struct pcnet_device *netcard, size_t bcr, uint16_t v #define DEBUG_MSG(s) // #endif -void pcnet_init_device(struct cdi_device* device) +struct cdi_device* pcnet_init_device(struct cdi_bus_data* bus_data) { - struct pcnet_device* netcard = (struct pcnet_device*) device; - struct cdi_pci_device* pci = (struct cdi_pci_device*) device->bus_data; + struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data; + struct pcnet_device* netcard; + + if (!((pci->vendor_id == VENDOR_ID) && (pci->device_id == DEVICE_ID))) { + return NULL; + } + + netcard = malloc(sizeof(struct pcnet_device)); + memset(netcard, 0, sizeof(struct pcnet_device)); + + netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; netcard->net.send_packet = pcnet_send_packet; // PCI-bezogenes Zeug initialisieren DEBUG_MSG("Interrupthandler und Ports registrieren"); - cdi_register_irq(pci->irq, pcnet_handle_interrupt, device); + cdi_register_irq(pci->irq, pcnet_handle_interrupt, &netcard->net.dev); cdi_pci_alloc_ioports(pci); // I/O Port raussuchen @@ -99,9 +108,11 @@ void pcnet_init_device(struct cdi_device* device) pcnet_start(netcard); // Netzwerkkarte registrieren - cdi_net_device_init((struct cdi_net_device*) device); + cdi_net_device_init(&netcard->net); DEBUG_MSG("Fertig initialisiert"); + + return &netcard->net.dev; } void pcnet_remove_device(struct cdi_device* device) diff --git a/rtl8139/include/rtl8139.h b/rtl8139/include/rtl8139.h index 2c9e419..3ec81b0 100644 --- a/rtl8139/include/rtl8139.h +++ b/rtl8139/include/rtl8139.h @@ -98,7 +98,7 @@ struct rtl8139_device { cdi_list_t pending_sends; }; -void rtl8139_init_device(struct cdi_device* device); +struct cdi_device* rtl8139_init_device(struct cdi_bus_data* bus_data); void rtl8139_remove_device(struct cdi_device* device); void rtl8139_send_packet diff --git a/rtl8139/main.c b/rtl8139/main.c index 5411238..3459e1f 100644 --- a/rtl8139/main.c +++ b/rtl8139/main.c @@ -52,18 +52,11 @@ static int rtl8139_driver_init(void) cdi_pci_get_all_devices(pci_devices); struct cdi_pci_device* dev; + struct cdi_device* device; int i; for (i = 0; (dev = cdi_list_get(pci_devices, i)); i++) { - if ((dev->vendor_id == 0x10ec) && (dev->device_id == 0x8139)) { - void* phys_device; - struct rtl8139_device* device; - - cdi_alloc_phys_mem(sizeof(*device), - (void**) &device, &phys_device); - memset(device, 0, sizeof(*device)); - - device->phys = phys_device; - device->net.dev.bus_data = (struct cdi_bus_data*) dev; + device = rtl8139_init_device((struct cdi_bus_data*) dev); + if (device != NULL) { cdi_list_push(rtl8139_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); @@ -90,8 +83,9 @@ static int rtl8139_driver_destroy(void) static struct cdi_net_driver rtl8139_driver = { .drv = { - .type = CDI_NETWORK, .name = DRIVER_NAME, + .type = CDI_NETWORK, + .bus = CDI_PCI, .init = rtl8139_driver_init, .destroy = rtl8139_driver_destroy, .init_device = rtl8139_init_device, diff --git a/rtl8139/rtl8139.c b/rtl8139/rtl8139.c index c89e08f..e922889 100644 --- a/rtl8139/rtl8139.c +++ b/rtl8139/rtl8139.c @@ -75,15 +75,26 @@ static inline uint32_t read_register_dword(struct rtl8139_device* netcard, uint8 return cdi_inl(netcard->port_base + reg); } -void rtl8139_init_device(struct cdi_device* device) +struct cdi_device* rtl8139_init_device(struct cdi_bus_data* bus_data) { - struct rtl8139_device* netcard = (struct rtl8139_device*) device; - struct cdi_pci_device* pci = (struct cdi_pci_device*) device->bus_data; + struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data; + struct rtl8139_device* netcard; + void* phys_device; + + if (!((pci->vendor_id == 0x10ec) && (pci->device_id == 0x8139))) { + return NULL; + } + + cdi_alloc_phys_mem(sizeof(*netcard), (void**) &netcard, &phys_device); + memset(netcard, 0, sizeof(*netcard)); + + netcard->phys = phys_device; + netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; netcard->net.send_packet = rtl8139_send_packet; // PCI-bezogenes Zeug initialisieren DEBUG_MSG("Interrupthandler und Ports registrieren"); - cdi_register_irq(pci->irq, rtl8139_handle_interrupt, device); + cdi_register_irq(pci->irq, rtl8139_handle_interrupt, &netcard->net.dev); cdi_pci_alloc_ioports(pci); cdi_list_t reslist = pci->resources; @@ -139,8 +150,10 @@ void rtl8139_init_device(struct cdi_device* device) netcard->rx_buffer_offset = 0; netcard->pending_sends = cdi_list_create(); - cdi_net_device_init((struct cdi_net_device*) device); + cdi_net_device_init(&netcard->net); DEBUG_MSG("Fertig initialisiert"); + + return &netcard->net.dev; } void rtl8139_remove_device(struct cdi_device* device) diff --git a/sis900/device.c b/sis900/device.c index d6e68d6..ade91c1 100644 --- a/sis900/device.c +++ b/sis900/device.c @@ -181,15 +181,26 @@ static uint64_t get_mac_address(struct sis900_device* device) return mac; } -void sis900_init_device(struct cdi_device* device) +struct cdi_device* sis900_init_device(struct cdi_bus_data* bus_data) { - struct sis900_device* netcard = (struct sis900_device*) device; - struct cdi_pci_device* pci = (struct cdi_pci_device*) device->bus_data; + struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data; + struct sis900_device* netcard; + void* phys_device; + + if (!((pci->vendor_id == 0x1039) && (pci->device_id == 0x0900))) { + return NULL; + } + + cdi_alloc_phys_mem(sizeof(*netcard), (void**) &netcard, &phys_device); + memset(netcard, 0, sizeof(*netcard)); + + netcard->phys = phys_device; + netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; netcard->net.send_packet = sis900_send_packet; // PCI-bezogenes Zeug initialisieren netcard->revision = pci->rev_id; - cdi_register_irq(pci->irq, sis900_handle_interrupt, device); + cdi_register_irq(pci->irq, sis900_handle_interrupt, &netcard->net.dev); cdi_pci_alloc_ioports(pci); cdi_list_t reslist = pci->resources; @@ -228,7 +239,9 @@ void sis900_init_device(struct cdi_device* device) sis900_send_packet(device, sendbuf, 16); */ - cdi_net_device_init((struct cdi_net_device*) device); + cdi_net_device_init(&netcard->net); + + return &netcard->net.dev; } void sis900_remove_device(struct cdi_device* device) diff --git a/sis900/device.h b/sis900/device.h index cbb44bf..cd110c0 100644 --- a/sis900/device.h +++ b/sis900/device.h @@ -143,7 +143,7 @@ struct sis900_device { uint8_t revision; }; -void sis900_init_device(struct cdi_device* device); +struct cdi_device* sis900_init_device(struct cdi_bus_data* bus_data); void sis900_remove_device(struct cdi_device* device); void sis900_send_packet diff --git a/sis900/main.c b/sis900/main.c index f813993..f4f6a2c 100644 --- a/sis900/main.c +++ b/sis900/main.c @@ -54,18 +54,11 @@ static int sis900_driver_init(void) cdi_pci_get_all_devices(pci_devices); struct cdi_pci_device* dev; + struct cdi_device* device; int i; for (i = 0; (dev = cdi_list_get(pci_devices, i)); i++) { - if ((dev->vendor_id == 0x1039) && (dev->device_id == 0x0900)) { - void* phys_device; - struct sis900_device* device; - - cdi_alloc_phys_mem(sizeof(*device), - (void**) &device, &phys_device); - memset(device, 0, sizeof(*device)); - - device->phys = phys_device; - device->net.dev.bus_data = (struct cdi_bus_data*) dev; + device = sis900_init_device((struct cdi_bus_data*) dev); + if (device != NULL) { cdi_list_push(sis900_driver.drv.devices, device); } else { cdi_pci_device_destroy(dev); @@ -95,8 +88,9 @@ static int sis900_driver_destroy(void) static struct cdi_net_driver sis900_driver = { .drv = { - .type = CDI_NETWORK, .name = DRIVER_NAME, + .type = CDI_NETWORK, + .bus = CDI_PCI, .init = sis900_driver_init, .destroy = sis900_driver_destroy, .init_device = sis900_init_device, -- 1.6.0.2 From kevin at tyndur.org Tue Dec 29 19:51:43 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Tue, 29 Dec 2009 19:51:43 +0100 Subject: [cdi-devel] [PATCH 3/3] Initialise PCI devices in the CDI library In-Reply-To: <1262112703-11261-1-git-send-email-kevin@tyndur.org> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> Message-ID: <1262112703-11261-4-git-send-email-kevin@tyndur.org> * Instead of implementing the PCI scan in each driver, we can simply move the logic into the CDI library. It enumerates all PCI devices and calls init_device on all known drivers until it finds a driver that can handle the device. Signed-off-by: Kevin Wolf --- e1000/main.c | 22 ---------------------- ne2k/main.c | 18 ------------------ pcnet/main.c | 18 ------------------ rtl8139/main.c | 18 ------------------ sis900/main.c | 21 --------------------- 5 files changed, 0 insertions(+), 97 deletions(-) diff --git a/e1000/main.c b/e1000/main.c index 229ec3f..faf6f2e 100644 --- a/e1000/main.c +++ b/e1000/main.c @@ -49,28 +49,6 @@ static int e1000_driver_init(void) // Konstruktor der Vaterklasse cdi_net_driver_init(&driver); - // Passende PCI-Geraete suchen - cdi_list_t pci_devices = cdi_list_create(); - cdi_pci_get_all_devices(pci_devices); - - struct cdi_device* device; - struct cdi_pci_device* dev; - int i; - for (i = 0; (dev = cdi_list_get(pci_devices, i)); i++) { - device = e1000_init_device((struct cdi_bus_data*) dev); - if (device != NULL) { - // TODO - cdi_list_push(driver.drv.devices, device); - } else { - cdi_pci_device_destroy(dev); - } - } - - printf("e1000: %d Karten gefunden.\n", - cdi_list_size(driver.drv.devices)); - - cdi_list_destroy(pci_devices); - return 0; } diff --git a/ne2k/main.c b/ne2k/main.c index e46e866..35f31b4 100644 --- a/ne2k/main.c +++ b/ne2k/main.c @@ -44,24 +44,6 @@ static int ne2k_driver_init(void) // Konstruktor der Vaterklasse cdi_net_driver_init(&ne2k_driver); - // Passende PCI-Geraete suchen - cdi_list_t pci_devices = cdi_list_create(); - cdi_pci_get_all_devices(pci_devices); - - struct cdi_pci_device* dev; - struct cdi_device* device; - int i; - for (i = 0; (dev = cdi_list_get(pci_devices, i)); i++) { - device = ne2k_init_device((struct cdi_bus_data*) dev); - if (device != NULL) { - cdi_list_push(ne2k_driver.drv.devices, device); - } else { - cdi_pci_device_destroy(dev); - } - } - - cdi_list_destroy(pci_devices); - return 0; } diff --git a/pcnet/main.c b/pcnet/main.c index de05930..685d281 100644 --- a/pcnet/main.c +++ b/pcnet/main.c @@ -48,24 +48,6 @@ static int pcnet_driver_init(void) // Konstruktor der Vaterklasse cdi_net_driver_init(&pcnet_driver); - // Passende PCI-Geraete suchen - cdi_list_t pci_devices = cdi_list_create(); - cdi_pci_get_all_devices(pci_devices); - - struct cdi_pci_device* dev; - struct cdi_device* device; - int i; - for (i = 0; (dev = cdi_list_get(pci_devices, i)); i++) { - device = pcnet_init_device((struct cdi_bus_data*) dev); - if (device) { - cdi_list_push(pcnet_driver.drv.devices, device); - } else { - cdi_pci_device_destroy(dev); - } - } - - cdi_list_destroy(pci_devices); - return 0; } diff --git a/rtl8139/main.c b/rtl8139/main.c index 3459e1f..b3eb095 100644 --- a/rtl8139/main.c +++ b/rtl8139/main.c @@ -47,24 +47,6 @@ static int rtl8139_driver_init(void) // Konstruktor der Vaterklasse cdi_net_driver_init(&rtl8139_driver); - // Passende PCI-Geraete suchen - cdi_list_t pci_devices = cdi_list_create(); - cdi_pci_get_all_devices(pci_devices); - - struct cdi_pci_device* dev; - struct cdi_device* device; - int i; - for (i = 0; (dev = cdi_list_get(pci_devices, i)); i++) { - device = rtl8139_init_device((struct cdi_bus_data*) dev); - if (device != NULL) { - cdi_list_push(rtl8139_driver.drv.devices, device); - } else { - cdi_pci_device_destroy(dev); - } - } - - cdi_list_destroy(pci_devices); - return 0; } diff --git a/sis900/main.c b/sis900/main.c index f4f6a2c..93cb4dd 100644 --- a/sis900/main.c +++ b/sis900/main.c @@ -49,27 +49,6 @@ static int sis900_driver_init(void) // Konstruktor der Vaterklasse cdi_net_driver_init(&sis900_driver); - // Passende PCI-Geraete suchen - cdi_list_t pci_devices = cdi_list_create(); - cdi_pci_get_all_devices(pci_devices); - - struct cdi_pci_device* dev; - struct cdi_device* device; - int i; - for (i = 0; (dev = cdi_list_get(pci_devices, i)); i++) { - device = sis900_init_device((struct cdi_bus_data*) dev); - if (device != NULL) { - cdi_list_push(sis900_driver.drv.devices, device); - } else { - cdi_pci_device_destroy(dev); - } - } - - printf("sis900: %d Karten gefunden.\n", - cdi_list_size(sis900_driver.drv.devices)); - - cdi_list_destroy(pci_devices); - return 0; } -- 1.6.0.2 From max at tyndur.org Tue Dec 29 20:07:45 2009 From: max at tyndur.org (Max Reitz) Date: Tue, 29 Dec 2009 20:07:45 +0100 Subject: [cdi-devel] [PATCH 0/3] Rework device creation mechanism In-Reply-To: <1262112703-11261-1-git-send-email-kevin@tyndur.org> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> Message-ID: <4B3A5381.506@tyndur.org> Kevin Wolf wrote: > Currently, device creation is a very static thing. Each driver looks in > its initialisation function for devices that it supports. This means > that (a) the init_driver code is duplicated in each driver and (b) that > hot-plugging is impossible (becomes more important with USB support). That looks very nice to me - especially because of USB and its hot-plugging. Do you want to know something else or is that enough? ;-) From max at tyndur.org Tue Dec 29 20:10:53 2009 From: max at tyndur.org (Max Reitz) Date: Tue, 29 Dec 2009 20:10:53 +0100 Subject: [cdi-devel] [PATCH 1/3] cdi.h: Introduce cdi_bus_data In-Reply-To: <1262112703-11261-2-git-send-email-kevin@tyndur.org> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> <1262112703-11261-2-git-send-email-kevin@tyndur.org> Message-ID: <4B3A543D.3050600@tyndur.org> Kevin Wolf wrote: > + Each device gets a structure that links it to its bus. For now this > means moving the cdi_pci_device out of the specific device struct of > each driver into the generic cdi_device. > > Signed-off-by: Kevin Wolf > [...] > diff --git a/include/cdi.h b/include/cdi.h > index b09bcff..a0e1826 100644 > --- a/include/cdi.h > +++ b/include/cdi.h > @@ -33,9 +33,16 @@ typedef enum { > } cdi_device_type_t; > > struct cdi_driver; > +struct cdi_device; Not critical at all, but is this necessary? > + > +struct cdi_bus_data { > + cdi_device_type_t bus_type; > +}; > + > struct cdi_device { > - const char* name; > - struct cdi_driver* driver; > + const char* name; > + struct cdi_driver* driver; > + struct cdi_bus_data* bus_data; > > // tyndur-spezifisch > void* backdev; > [...] From matthew at theiselins.net Wed Dec 30 00:13:02 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Wed, 30 Dec 2009 09:13:02 +1000 Subject: [cdi-devel] [PATCH 0/3] Rework device creation mechanism In-Reply-To: <1262112703-11261-1-git-send-email-kevin@tyndur.org> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> Message-ID: On Wed, Dec 30, 2009 at 4:51 AM, Kevin Wolf wrote: > Making it library driven should solve these problems and might simplify > adaption to OS specific needs as an extra bonus (I'm thinking of > Pedigree's pDev here). Sounds great - certainly seems to be a major improvement that will make CDI more dynamic. I'm wondering if this may be an important stepping stone towards a system where the OS can not only load devices on-demand (and in hotplug circumstances), but where the OS can also load _drivers_ when devices are detected. For example, a table linking devices to CDI driver names, which the OS can use to load drivers on the fly rather than have a static set of drivers loaded at any one time. I think this idea may be out of the scope of this specific patchset discussion, however. I will work on a proper proposal and interface while I am unable to access the internet over the next week, and submit it to the list. Consider the patchset ACK'd by me ;-) Cheers, Matt From kevin at tyndur.org Wed Dec 30 14:53:50 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Wed, 30 Dec 2009 14:53:50 +0100 Subject: [cdi-devel] [PATCH] cdi: Move send_packet into cdi_net_driver Message-ID: <1262181230-25071-1-git-send-email-kevin@tyndur.org> * cdi: send_packet is currently in cdi_net_device despite being driver specific and not device specific. Move it to cdi_net_driver. Signed-off-by: Kevin Wolf --- e1000/device.c | 1 - e1000/main.c | 2 ++ include/cdi/net.h | 6 +++--- ne2k/main.c | 2 ++ ne2k/ne2k.c | 1 - pcnet/main.c | 2 ++ pcnet/pcnet.c | 1 - rtl8139/main.c | 2 ++ rtl8139/rtl8139.c | 1 - sis900/device.c | 1 - sis900/main.c | 2 ++ 11 files changed, 13 insertions(+), 8 deletions(-) diff --git a/e1000/device.c b/e1000/device.c index b4aa4eb..b21df2f 100644 --- a/e1000/device.c +++ b/e1000/device.c @@ -159,7 +159,6 @@ struct cdi_device* e1000_init_device(struct cdi_bus_data* bus_data) cdi_alloc_phys_mem(sizeof(*netcard), (void**) &netcard, &phys_device); memset(netcard, 0, sizeof(*netcard)); - netcard->net.send_packet = e1000_send_packet; netcard->phys = phys_device; netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; diff --git a/e1000/main.c b/e1000/main.c index faf6f2e..dbbe80d 100644 --- a/e1000/main.c +++ b/e1000/main.c @@ -75,6 +75,8 @@ static struct cdi_net_driver driver = { .init_device = e1000_init_device, .remove_device = e1000_remove_device, }, + + .send_packet = e1000_send_packet, }; CDI_DRIVER(DRIVER_NAME, driver) diff --git a/include/cdi/net.h b/include/cdi/net.h index 1fddab6..9d4f0f0 100644 --- a/include/cdi/net.h +++ b/include/cdi/net.h @@ -26,13 +26,13 @@ struct cdi_net_device { struct cdi_device dev; uint64_t mac : 48; int number; - - void (*send_packet) - (struct cdi_net_device* device, void* data, size_t size); }; struct cdi_net_driver { struct cdi_driver drv; + + void (*send_packet) + (struct cdi_net_device* device, void* data, size_t size); }; diff --git a/ne2k/main.c b/ne2k/main.c index 35f31b4..636a76d 100644 --- a/ne2k/main.c +++ b/ne2k/main.c @@ -70,6 +70,8 @@ static struct cdi_net_driver ne2k_driver = { .init_device = ne2k_init_device, .remove_device = ne2k_remove_device, }, + + .send_packet = ne2k_send_packet, }; CDI_DRIVER(DRIVER_NAME, ne2k_driver) diff --git a/ne2k/ne2k.c b/ne2k/ne2k.c index b3faf46..7f1262e 100644 --- a/ne2k/ne2k.c +++ b/ne2k/ne2k.c @@ -90,7 +90,6 @@ struct cdi_device* ne2k_init_device(struct cdi_bus_data* bus_data) cdi_alloc_phys_mem(sizeof(*netcard), (void**) &netcard, &phys_device); memset(netcard, 0, sizeof(*netcard)); - netcard->net.send_packet = ne2k_send_packet; netcard->phys = phys_device; netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; diff --git a/pcnet/main.c b/pcnet/main.c index 685d281..ce7152e 100644 --- a/pcnet/main.c +++ b/pcnet/main.c @@ -74,6 +74,8 @@ static struct cdi_net_driver pcnet_driver = { .init_device = pcnet_init_device, .remove_device = pcnet_remove_device, }, + + .send_packet = pcnet_send_packet, }; CDI_DRIVER(DRIVER_NAME, pcnet_driver) diff --git a/pcnet/pcnet.c b/pcnet/pcnet.c index 9983fd5..a157094 100644 --- a/pcnet/pcnet.c +++ b/pcnet/pcnet.c @@ -65,7 +65,6 @@ struct cdi_device* pcnet_init_device(struct cdi_bus_data* bus_data) memset(netcard, 0, sizeof(struct pcnet_device)); netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; - netcard->net.send_packet = pcnet_send_packet; // PCI-bezogenes Zeug initialisieren DEBUG_MSG("Interrupthandler und Ports registrieren"); diff --git a/rtl8139/main.c b/rtl8139/main.c index b3eb095..78b6f94 100644 --- a/rtl8139/main.c +++ b/rtl8139/main.c @@ -73,6 +73,8 @@ static struct cdi_net_driver rtl8139_driver = { .init_device = rtl8139_init_device, .remove_device = rtl8139_remove_device, }, + + .send_packet = rtl8139_send_packet, }; CDI_DRIVER(DRIVER_NAME, rtl8139_driver) diff --git a/rtl8139/rtl8139.c b/rtl8139/rtl8139.c index e922889..600f3e9 100644 --- a/rtl8139/rtl8139.c +++ b/rtl8139/rtl8139.c @@ -90,7 +90,6 @@ struct cdi_device* rtl8139_init_device(struct cdi_bus_data* bus_data) netcard->phys = phys_device; netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; - netcard->net.send_packet = rtl8139_send_packet; // PCI-bezogenes Zeug initialisieren DEBUG_MSG("Interrupthandler und Ports registrieren"); diff --git a/sis900/device.c b/sis900/device.c index ade91c1..30c6d39 100644 --- a/sis900/device.c +++ b/sis900/device.c @@ -196,7 +196,6 @@ struct cdi_device* sis900_init_device(struct cdi_bus_data* bus_data) netcard->phys = phys_device; netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; - netcard->net.send_packet = sis900_send_packet; // PCI-bezogenes Zeug initialisieren netcard->revision = pci->rev_id; diff --git a/sis900/main.c b/sis900/main.c index 93cb4dd..7f3aae7 100644 --- a/sis900/main.c +++ b/sis900/main.c @@ -75,6 +75,8 @@ static struct cdi_net_driver sis900_driver = { .init_device = sis900_init_device, .remove_device = sis900_remove_device, }, + + .send_packet = sis900_send_packet, }; CDI_DRIVER(DRIVER_NAME, sis900_driver) -- 1.6.0.2 From kevin at tyndur.org Wed Dec 30 15:20:10 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Wed, 30 Dec 2009 15:20:10 +0100 Subject: [cdi-devel] [PATCH 0/3] Rework device creation mechanism In-Reply-To: References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> Message-ID: <20091230142006.GA25191@silvanus> On Wed, Dec 30, 2009 at 09:13:02AM +1000, Matthew Iselin wrote: > On Wed, Dec 30, 2009 at 4:51 AM, Kevin Wolf wrote: > > Making it library driven should solve these problems and might simplify > > adaption to OS specific needs as an extra bonus (I'm thinking of > > Pedigree's pDev here). > > Sounds great - certainly seems to be a major improvement that will > make CDI more dynamic. > > I'm wondering if this may be an important stepping stone towards a > system where the OS can not only load devices on-demand (and in > hotplug circumstances), but where the OS can also load _drivers_ when > devices are detected. For example, a table linking devices to CDI > driver names, which the OS can use to load drivers on the fly rather > than have a static set of drivers loaded at any one time. I've never thought about doing this in CDI, but probably it's a good idea. I think linking devices to driver names should actually be linking a cdi_bus_addr to a driver name. For PCI devices this would be based on device and vendor IDs. I think what we could do now very easily is to add some table of valid addresses to each driver. However, there is also a harder part. Let's forget PCI for a while now, it's not a CDI driver (might be a future project, too). USB looks much more like the generic case. And imagine a microkernel approach where each CDI driver runs in its own process. Who is responsible for the drivers (probably the driver providing the bus?), what information does it need (list of all available drivers and their list of supported bus_addrs) and where can it get the information from? The last question is probably the most interesting one: We need the table for all installed drivers, but the drivers are not yet running. So this is something that cannot be hidden in the CDI library. Hm, brain dump completed on this topic, I think. Maybe it helps, maybe it doesn't. ;-) > I think this idea may be out of the scope of this specific patchset > discussion, however. I will work on a proper proposal and interface > while I am unable to access the internet over the next week, and > submit it to the list. Sounds great. I'm sure you'll find a nice answer to my questions. Kevin From kevin at tyndur.org Wed Dec 30 17:00:34 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Wed, 30 Dec 2009 17:00:34 +0100 Subject: [cdi-devel] [PATCH] cdi.h: Remove cdi_run_drivers from the interface Message-ID: <1262188834-676-1-git-send-email-kevin@tyndur.org> - cdi.h: cdi_run_drivers is no longer needed in the public interface. It's now something completely internal to the CDI implementation. Signed-off-by: Kevin Wolf --- include/cdi.h | 13 ------------- 1 files changed, 0 insertions(+), 13 deletions(-) diff --git a/include/cdi.h b/include/cdi.h index 289af7c..d7cd210 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -71,19 +71,6 @@ struct cdi_driver { void cdi_init(void); /** - * Diese Funktion wird von Treibern aufgerufen, nachdem ein neuer Treiber - * hinzugefuegt worden ist. - * - * Sie registriert typischerweise die neu hinzugefuegten Treiber und/oder - * Geraete beim Betriebssystem und startet damit ihre Ausfuehrung. - * - * Nach dem Aufruf dieser Funktion duerfen vom Treiber keine weiteren Befehle - * ausgefuehrt werden, da nicht definiert ist, ob und wann die Funktion - * zurueckkehrt. - */ -void cdi_run_drivers(void); - -/** * Initialisiert die Datenstrukturen fuer einen Treiber * (erzeugt die devices-Liste) */ -- 1.6.0.2 From max at tyndur.org Wed Dec 30 17:28:57 2009 From: max at tyndur.org (Max Reitz) Date: Wed, 30 Dec 2009 17:28:57 +0100 Subject: [cdi-devel] [PATCH 0/3] Rework device creation mechanism In-Reply-To: <20091230142006.GA25191@silvanus> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> <20091230142006.GA25191@silvanus> Message-ID: <4B3B7FC9.7060206@tyndur.org> Kevin Wolf wrote: > [...] > However, there is also a harder part. Let's forget PCI for a while now, > it's not a CDI driver (might be a future project, too). USB looks much > more like the generic case. And imagine a microkernel approach where > each CDI driver runs in its own process. Who is responsible for the > drivers (probably the driver providing the bus?), what information does > it need (list of all available drivers and their list of supported > bus_addrs) and where can it get the information from? If I understand you correctly, that question has already been answered by my CDI.usb proposal. There's a function called cdi_usb_register_device_pattern() which does what its name says: A USB device driver calls it to tell the OS: "Hey, I'm a USB driver and I'm to handle that kind of USB devices." The OS has its cdi_usb_device_init() function which is called whenever a USB device is found (by a USB HC driver or by a USB hub driver), this one calls a function to enumerate that device. The enumerating process delivers the device's class id, subclass id, protocol id, vendor id and device id to the OS (just by filling those fields in the device structure). The OS then looks after a registered USB device driver which is able to handle that device. If there's none available it should store the device in a list which is in turn scanned every time a new USB device driver appears by calling cdi_usb_register_device_pattern(). Just look at the structure called "cdi_usb_device_pattern" if you want to know what information is needed about the USB device drivers. But all that just in case I understood you correctly. If you were "just" talking about sharing those information between the different drivers (aka "processes" in a microkernel ;-)), see my statement below. > The last question > is probably the most interesting one: We need the table for all > installed drivers, but the drivers are not yet running. So this is > something that cannot be hidden in the CDI library. As I said that's no problem with the current proposal. If a driver is not running when its device is found, the device structure is stored in a list until that driver registers itself. > [...] In my opinion the greatest problem is to share that list of drivers and devices between all USB bus drivers and USB device drivers - that's of course no problem in a monolithic kernel, but using a microkernel... You can't even simply do SHM, because the device and driver structures contain pointers to other drivers and devices which are not in that SHM area. From kevin at tyndur.org Wed Dec 30 18:05:58 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Wed, 30 Dec 2009 18:05:58 +0100 Subject: [cdi-devel] [PATCH 0/3] Rework device creation mechanism In-Reply-To: <4B3B7FC9.7060206@tyndur.org> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> <20091230142006.GA25191@silvanus> <4B3B7FC9.7060206@tyndur.org> Message-ID: <20091230170554.GA20917@silvanus> On Wed, Dec 30, 2009 at 05:28:57PM +0100, Max Reitz wrote: > Kevin Wolf wrote: > > >[...] > >However, there is also a harder part. Let's forget PCI for a while now, > >it's not a CDI driver (might be a future project, too). USB looks much > >more like the generic case. And imagine a microkernel approach where > >each CDI driver runs in its own process. Who is responsible for the > >drivers (probably the driver providing the bus?), what information does > >it need (list of all available drivers and their list of supported > >bus_addrs) and where can it get the information from? > > If I understand you correctly, that question has already been answered by > my CDI.usb proposal. There's a function called > cdi_usb_register_device_pattern() which does what its name says: A USB > device driver calls it to tell the OS: "Hey, I'm a USB driver and I'm to > handle that kind of USB devices." The OS has its cdi_usb_device_init() > function which is called whenever a USB device is found (by a USB HC > driver or by a USB hub driver), this one calls a function to enumerate > that device. The enumerating process delivers the device's class id, > subclass id, protocol id, vendor id and device id to the OS (just by > filling those fields in the device structure). Well, yes. This is what I described as the easy part. Your function is obviously not the generic thing that we're looking for (as the usb in its name indicates), but in the end you generate a cdi_bus_addr here. Fine. > The OS then looks after a > registered USB device driver which is able to handle that device. Registered means running here, right? If so, it doesn't use help any further. What Matt is talking about is _launching_ the driver if it's not running. We have a device 8086:100e, no running driver likes the device and we need to find out that we should start e1000. > If > there's none available it should store the device in a list which is in > turn scanned every time a new USB device driver appears by calling > cdi_usb_register_device_pattern(). > Just look at the structure called "cdi_usb_device_pattern" if you want to > know what information is needed about the USB device drivers. I'll probably need to do that. Sounds like it could be made more generic instead of being limited to USB. Or maybe it's not even needed any more with the recent changes? > >The last question > >is probably the most interesting one: We need the table for all > >installed drivers, but the drivers are not yet running. So this is > >something that cannot be hidden in the CDI library. > > As I said that's no problem with the current proposal. If a driver is not > running when its device is found, the device structure is stored in a list > until that driver registers itself. It's a good point that needs to be considered but yet another unrelated discussion. ;-) > > >[...] > > In my opinion the greatest problem is to share that list of drivers and > devices between all USB bus drivers and USB device drivers - that's of > course no problem in a monolithic kernel, but using a microkernel... You > can't even simply do SHM, because the device and driver structures contain > pointers to other drivers and devices which are not in that SHM area. cdi_bus_addr must not contain pointers then. I think this is a reasonable restriction. Doing the actual IPC would be something internal to the CDI implementation anyway. Kevin From max at tyndur.org Wed Dec 30 18:39:47 2009 From: max at tyndur.org (Max Reitz) Date: Wed, 30 Dec 2009 18:39:47 +0100 Subject: [cdi-devel] [PATCH 0/3] Rework device creation mechanism In-Reply-To: <20091230170554.GA20917@silvanus> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> <20091230142006.GA25191@silvanus> <4B3B7FC9.7060206@tyndur.org> <20091230170554.GA20917@silvanus> Message-ID: <4B3B9063.5080404@tyndur.org> Kevin Wolf wrote: >> The OS then looks after a >> registered USB device driver which is able to handle that device. > > Registered means running here, right? If so, it doesn't use help any > further. What Matt is talking about is _launching_ the driver if it's > not running. We have a device 8086:100e, no running driver likes the > device and we need to find out that we should start e1000. Well, what do you mean with not running? Not even loaded? I don't know how you want to start a not loaded driver upon device detection. No problem in a monolithic kernel, of course - all drivers are "loaded" right from loading the kernel because they're all in the kernel. But a driver needs to register itself somehow, be it active or passive. The current model for USB is active registering (call a function), but there's no problem about making it passive (store information in a new section or whatever about the devices to be handled). Anyway the OS must (just imo, of course ;-)) load the driver first to know which devices it's able to handle. The only "escape" would be to name the driver in a way the OS can search a driver by its name - i. e. call that e1000 driver "8086-100e". But that's imo stupid (sorry, I just say it in that way, because nobody except me suggested this model up to now *g*) because the name of a driver _in the filesystem_ is totally unrelated to CDI. So one must _load_ a driver before knowing which devices it is able to handle. And if you use a "passive" registration system, loading means registering. Hence, registered is not forced to mean running (just in the active model) but to mean loaded. With your new driver registration model (the new section called "cdi_drivers") CDI changed from such an active model to a passive model. Every driver is registered just by loading it into memory and before it is actually started (by calling the "init" method). I'm sorry, if all I told you was simply crap and totally unrelated to your opinion. ;-) From redelman at gmail.com Wed Dec 30 19:29:50 2009 From: redelman at gmail.com (Rich Edelman) Date: Wed, 30 Dec 2009 12:29:50 -0600 Subject: [cdi-devel] [PATCH 0/3] Rework device creation mechanism In-Reply-To: <4B3B9063.5080404@tyndur.org> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> <20091230142006.GA25191@silvanus> <4B3B7FC9.7060206@tyndur.org> <20091230170554.GA20917@silvanus> <4B3B9063.5080404@tyndur.org> Message-ID: <61acd8d30912301029t16e98262g1874059d7beb8c7e@mail.gmail.com> On Wed, Dec 30, 2009 at 11:39 AM, Max Reitz wrote: > Kevin Wolf wrote: >>> >>> The OS then looks after a >>> registered USB device driver which is able to handle that device. >> >> Registered means running here, right? If so, it doesn't use help any >> further. What Matt is talking about is _launching_ the driver if it's >> not running. We have a device 8086:100e, no running driver likes the >> device and we need to find out that we should start e1000. > > Well, what do you mean with not running? Not even loaded? I don't know > how you want to start a not loaded driver upon device detection. No I think not even loaded is what Matt had originally intended, and what Kevin was referring to as well. It's certainly what I've been thinking all along so far, but perhaps that's just me. > problem in a monolithic kernel, of course - all drivers are "loaded" > right from loading the kernel because they're all in the kernel. But a I'd love to just have a minimal driver set compiled in to my kernel, or perhaps loaded from an initrd of some sort, and then dynamically load drivers for other devices as they are detected (whether a hotplugged USB device or an add-in PCI SATA card). > driver needs to register itself somehow, be it active or passive. The > current model for USB is active registering (call a function), but > there's no problem about making it passive (store information in a new > section or whatever about the devices to be handled). Anyway the OS > must (just imo, of course ;-)) load the driver first to know which > devices it's able to handle. The only "escape" would be to name the > driver in a way the OS can search a driver by its name - i. e. call > that e1000 driver "8086-100e". But that's imo stupid (sorry, I just > say it in that way, because nobody except me suggested this model up > to now *g*) because the name of a driver _in the filesystem_ is > totally unrelated to CDI. So one must _load_ a driver before knowing > which devices it is able to handle. And if you use a "passive" > registration system, loading means registering. Hence, registered is > not forced to mean running (just in the active model) but to mean > loaded. For quite a while I had planned on just using a native driver architecture in QuokOS, but Matt and Kevin got me interested in CDI. :) I had planned on dynamically loading all drivers as they were needed, except for a couple of base drivers that would always be compiled in to the kernel. Some of those drivers were: IDE driver, some filesystem drivers, cdrom driver, a PCI bus driver. When I initialized the driver subsystem the first thing it did after internal initialization was to call the register functions of the drivers that were compiled in. Those register functions did nothing more than register the drivers with the appropriate driver subsystem. The PCI bus driver would use a register_bus_driver() function, while the a SATA driver would call register_storage_driver(). Once all the compiled-in drivers were registered, the subsystem would iterate through the list of bus drivers. Each bus driver would enumerate the bus and call the init method on each driver in the list until one of them returned true. At that point, the driver's "start" method would be called, and the device would be handled. The init method was passed information from the PCI header.. class code, subclass code, vendor id, device id, so it could test against those and do whatever else the driver needed to do to make sure it did really support that device. If no drivers were found for the device, the device was put on an "unhandled" list. After all the appropriate compiled-in drivers were started, I'd be able to iterate through the unhandled list and call a helper process to locate and load any needed drivers. I had planned on having each driver also provide a mapping file of which devices it supported. All the driver mapping files would be added to an index file (likely a sqlite database) that the helper process would query. This would allow multiple drivers to be returned for a single device, perhaps even with other metainfo about the driver itself. The helper process would then be able to direct the kernel to load each driver in succession until one was able to handle the device. This also allowed drivers that returned false from the init method to be unregistered and unloaded if you wanted. In practice I've never actually implemented any of this, but it's how I was planning on doing things. Also, I should note that each driver was meant to run in it's own thread, whether that be kernel space or userspace. Now I've rambled on enough, especially for a first post to this list, but I hope it at least gives some ideas! Rich aka "quok" From kevin at tyndur.org Wed Dec 30 22:18:26 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Wed, 30 Dec 2009 22:18:26 +0100 Subject: [cdi-devel] [PATCH 0/3] Rework device creation mechanism In-Reply-To: <61acd8d30912301029t16e98262g1874059d7beb8c7e@mail.gmail.com> References: <1262112703-11261-1-git-send-email-kevin@tyndur.org> <20091230142006.GA25191@silvanus> <4B3B7FC9.7060206@tyndur.org> <20091230170554.GA20917@silvanus> <4B3B9063.5080404@tyndur.org> <61acd8d30912301029t16e98262g1874059d7beb8c7e@mail.gmail.com> Message-ID: <20091230211826.GB20917@silvanus> On Wed, Dec 30, 2009 at 12:29:50PM -0600, Rich Edelman wrote: > On Wed, Dec 30, 2009 at 11:39 AM, Max Reitz wrote: > > Kevin Wolf wrote: > >>> > >>> The OS then looks after a > >>> registered USB device driver which is able to handle that device. > >> > >> Registered means running here, right? If so, it doesn't use help any > >> further. What Matt is talking about is _launching_ the driver if it's > >> not running. We have a device 8086:100e, no running driver likes the > >> device and we need to find out that we should start e1000. > > > > Well, what do you mean with not running? Not even loaded? I don't know > > how you want to start a not loaded driver upon device detection. No > > I think not even loaded is what Matt had originally intended, and what > Kevin was referring to as well. > It's certainly what I've been thinking all along so far, but perhaps > that's just me. We're two at least. > > problem in a monolithic kernel, of course - all drivers are "loaded" > > right from loading the kernel because they're all in the kernel. But a > > I'd love to just have a minimal driver set compiled in to my kernel, > or perhaps loaded from an initrd of some sort, > and then dynamically load drivers for other devices as they are > detected (whether a hotplugged USB device or > an add-in PCI SATA card). Sounds more or less like the classic monolithic kernel. > I had planned on dynamically loading all drivers as they were needed, > except for a couple of base drivers that would always be compiled in > to the kernel. > Some of those drivers were: IDE driver, some filesystem drivers, cdrom > driver, a PCI bus driver. When I initialized the driver subsystem the > first thing it did > after internal initialization was to call the register functions of > the drivers that were compiled in. Those register functions did > nothing more than register the drivers with the appropriate driver > subsystem. The PCI bus driver would use a register_bus_driver() > function, while the a SATA driver would call > register_storage_driver(). > Once all the compiled-in drivers were registered, the subsystem would > iterate through the list of bus drivers. Each bus driver would > enumerate the bus and call the init method on each driver in the list > until one of them returned true. At that point, the driver's "start" > method would be called, and the device would be handled. The init > method was passed information from the PCI header.. class code, > subclass code, vendor id, device id, so it could test against those > and do whatever else the driver needed to do to make sure it did > really support that device. If no drivers were found for the device, > the device was put on an "unhandled" list. > > After all the appropriate compiled-in drivers were started, I'd be > able to iterate through the unhandled list and call a helper process > to locate and load any needed drivers. I had planned on having each > driver also provide a mapping file of which devices it supported. All > the driver mapping files would be added to an index file (likely a > sqlite database) that the helper process would query. This would allow > multiple drivers to be returned for a single device, perhaps even with > other metainfo about the driver itself. The helper process would then > be able to direct the kernel to load each driver in succession until > one was able to handle the device. I think we're getting really close to it with CDI. Current implementations lack two things: a) The unhandled list. This is what Max already mentioned in one of his posts. It's probably not CDI's business, you can do it transparently in your implementation of the CDI library without any special interface to the drivers. b) The database of all installed drivers. I think this is what Matt's proposal is going to make possible. For a monolithic kernel, that's it. In tyndur, we'll need some more communication behind the scenes, but at the moment I don't see any additional requirements to the interface. Kevin From kevin at tyndur.org Wed Dec 30 23:21:32 2009 From: kevin at tyndur.org (Kevin Wolf) Date: Wed, 30 Dec 2009 23:21:32 +0100 Subject: [cdi-devel] [PATCH] Documentation: Document and translate cdi.h Message-ID: <1262211692-30529-1-git-send-email-kevin@tyndur.org> + Add module documentation for Core module + Add some more doxygen commits in cdi.h + Add English translations for comments in cdi.h Signed-off-by: Kevin Wolf --- include/cdi.h | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 196 insertions(+), 8 deletions(-) diff --git a/include/cdi.h b/include/cdi.h index d7cd210..629bbd1 100644 --- a/include/cdi.h +++ b/include/cdi.h @@ -2,13 +2,99 @@ * Copyright (c) 2007 Kevin Wolf * * This program is free software. It comes without any warranty, to - * the extent permitted by applicable law. You can redistribute it - * and/or modify it under the terms of the Do What The Fuck You Want + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://sam.zoy.org/projects/COPYING.WTFPL for more details. - */ + */ -/** \addtogroup core */ +/** + * \german + * Core ist das Basismodul von CDI. Es stellt die grundlegenden Datenstrukturen + * und Funktionen bereit, auf die andere Module aufbauen. + * + * \section Datenstrukturen + * CDI basiert auf drei grundlegenden Datenstrukturen. Die einzelnen Module + * erweitern diese Strukturen in einer kompatiblen Weise: cdi_net_driver ist + * beispielsweise eine Struktur, die cdi_driver als erstes Element enth??lt, so + * dass sie auf cdi_driver gecastet werden kann. Bei den Strukturen handelt es + * sich um: + * + * - cdi_device beschreibt ein allgemeines Ger??t. Es hat insbesondere einen + * zugeh??rigen Treiber und eine Adresse, die es in seinem Bus eindeutig + * beschreibt. + * - cdi_driver beschreibt einen allgemeinen Treiber. Jeder Treiber hat einen + * Typ, der beschreibt, zu welchem Modul er geh??rt und damit auch, in + * welchen Datentyp sowohl der Treiber als auch seine Ger??te gecastet werden + * k??nnen. Jeder Treiber hat au??erdem eine Reihe von Funktionspointern, die + * es der CDI-Bibliothek erlauben, Funktionen im Treiber aufzurufen (z.B. ein + * Netzwerkpaket zu versenden) + * - cdi_bus_data enth??lt busspezifische Informationen zu einem Ger??t wie die + * Adresse des Ger??ts auf dem Bus (z.B. bus.dev.func f??r PCI-Ger??te) oder + * andere busbezogene Felder enthalten (z.B. Vendor/Device ID). Diese + * Struktur wird zur Initialisierung von Ger??ten benutzt, d.h. wenn das + * Ger??t noch nicht existiert. + * + * \section core_init Initialisierung + * + * Die Initialisierung der Treiber l??uft in folgenden Schritten ab: + * + * -# Initialisierung der Treiber: Es wird der init-Callback jedes Treibers + * aufgerufen. Anschlie??end wird der Treiber bei CDI und dem Betriebssystem + * registriert, so dass er vor und w??hrend der Ausf??hrung von init keine + * Anfragen entgegennehmen muss, aber direkt anschlie??end daf??r bereit + * sein muss. + * -# Suche und Initialisierung der Ger??te: F??r jedes verf??gbare Ger??t wird + * init_device nacheinander f??r alle Treiber aufgerufen, bis ein Treiber + * das Ger??t akzeptiert (oder alle Treiber erfolglos versucht wurden). + * \n + * @todo F??r jedes Ger??t scan_bus aufrufen und die dabei gefundenen + * Ger??te wiederum initialisieren. + *\endgerman + * + * \english + * The Core module contains the fundamental CDI data structures and functions + * on which the other modules are based. + * + * \section Data structures + * CDI is based on three basic structures. The other modules extend these + * structures in a compatible way: For example, cdi_net_driver is a structure + * that contains a cdi_driver as its first element so that it can be cast to + * cdi_device. The structures are: + * + * - cdi_device describes a device. Amongst others, it contains fields that + * reference the corresponding driver and an address that identifies the + * device uniquely on its bus. + * - cdi_driver describes a driver. It has a type which determines to which + * CDI module the driver belongs (and thus which type the struct may be cast + * to). Moreover, each driver has some function points which can be used by + * the CDI library to call driver functions (e.g. for sending a network + * packet) + * - cdi_bus_data contains bus specific information on a device, like the + * address of the device on the bus (e.g. bus.dev.func for PCI) or other + * bus related information (e.g. device/vendor ID). This structure is used + * for initialisation of devices, i.e. when the device itself doesn't exist + * yet. + * + * \section core_init Initialisation + * + * Drivers are initialised in the following steps: + * + * -# Initialisation of drivers: The init() callback of each driver is called. + * After the driver has returned from there, it is registered with CDI and + * the operating system. The driver needs not to be able to handle requests + * before it has completed its init() call, but it must be prepared for them + * immediately afterwards. + * -# Search for and initialisation of devices: For each device init_device is + * called in the available drivers until a driver accepts the device (or all + * of the drivers reject it). + * \n + * @todo Call scan_bus for each device and initialise possible child devices + * \endenglish + * + * + * \defgroup core + */ /*\@{*/ #ifndef _CDI_H_ @@ -35,25 +121,102 @@ typedef enum { struct cdi_driver; +/** + * \german + * Beschreibt ein Ger??t in Bezug auf den Bus, an dem es h??ngt. Diese + * Informationen enthalten in der Regel eine Busadresse und zus??tzliche Daten + * wie Device/Vendor ID + * \endgerman + * + * \english + * Describes a device in relation to its bus. This information usually contains + * a bus address and some additional data like device/vendor ID. + * \endenglish + * + */ struct cdi_bus_data { cdi_device_type_t bus_type; }; +/** + * \if german + * Beschreibt ein Ger??t + * \elseif english + * Describes a device + * \endif + */ struct cdi_device { + /** + * \if german + * Name des Ger??ts (eindeutig unter den Ger??ten desselben Treibers) + * \elseif english + * Name of the device (must be unique among the devices of the same driver) + * \endif + */ const char* name; + + /** + * \if german + * Treiber, der f??r das Ger??t benutzt wird + * \elseif english + * Driver used for the device + * \endif + */ struct cdi_driver* driver; + + /** + * \if german + * Busspezifische Daten zum Ger??t + * \elseif english + * Bus specific data for the device + * \endif + */ struct cdi_bus_data* bus_data; // tyndur-spezifisch void* backdev; }; +/** + * \if german + * Beschreibt einen CDI-Treiber + * \elseif english + * Describes a CDI driver + * \endif + */ struct cdi_driver { cdi_device_type_t type; cdi_device_type_t bus; const char* name; + + /** + * \if german + * Enth??lt alle Ger??te (cdi_device), die den Treiber benutzen + * \elseif english + * Contains all devices (cdi_device) which use this driver + * \endif + */ cdi_list_t devices; + /** + * \german + * Versucht ein Ger??t zu initialisieren. Die Funktion darf von der + * CDI-Bibliothek nur aufgerufen werden, wenn bus_data->type dem Typ des + * Treibers entspricht + * + * @return Ein per malloc() erzeugtes neues cdi_device, wenn der Treiber + * das Ger??t akzeptiert. Wenn er das Ger??t nicht unterst??tzt, gibt er + * NULL zur??ck. + * \endgerman + * + * \english + * Tries to initialise a device. The function may only be called by the CDI + * library if bus_data->type matches the type of the driver. + * + * @return A new cdi_device created by malloc() if the driver accepts the + * device. NULL if the device is not supported by this driver. + * \endenglish + */ struct cdi_device* (*init_device)(struct cdi_bus_data* bus_data); void (*remove_device)(struct cdi_device* device); @@ -62,30 +225,55 @@ struct cdi_driver { }; /** - * Muss vor dem ersten Aufruf einer anderen CDI-Funktion aufgerufen werden. + * \german + * Treiber, die ihre eigene main-Funktion implemenieren, m??ssen diese Funktoin + * vor dem ersten Aufruf einer anderen CDI-Funktion aufrufen. * Initialisiert interne Datenstruktur der Implementierung fuer das jeweilige * Betriebssystem und startet anschliessend alle Treiber. * - * Ein wiederholter Aufruf bleibt ohne Effekt. + * Ein wiederholter Aufruf bleibt ohne Effekt. Es ist nicht definiert, ob diese + * Funktion zur??ckkehrt. + * \endgerman + * + * \english + * Drivers which implement their own main() function must call this function + * before they call any other CDI function. It initialises internal data + * structures of the CDI implementation and starts all drivers. + * + * This function takes effect only once, any other calls remain without effect. + * It is not defined if this function returns. + * \endenglish */ void cdi_init(void); /** - * Initialisiert die Datenstrukturen fuer einen Treiber + * \if german + * Initialisiert die Datenstrukturen f??r einen Treiber * (erzeugt die devices-Liste) + * \elseif english + * Initialises the data structures for a driver + * \endif */ void cdi_driver_init(struct cdi_driver* driver); /** - * Deinitialisiert die Datenstrukturen fuer einen Treiber + * \if german + * Deinitialisiert die Datenstrukturen f??r einen Treiber * (gibt die devices-Liste frei) + * \elseif english + * Deinitialises the data structures for a driver + * \endif */ void cdi_driver_destroy(struct cdi_driver* driver); /** + * \if german * Registriert den Treiber fuer ein neues Geraet * * @param driver Zu registierender Treiber + * \elseif english + * Registers a new driver with CDI + * \endif */ void cdi_driver_register(struct cdi_driver* driver); -- 1.6.0.2 From matthew at theiselins.net Wed Dec 30 23:35:30 2009 From: matthew at theiselins.net (Matthew Iselin) Date: Thu, 31 Dec 2009 08:35:30 +1000 Subject: [cdi-devel] [PATCH] Documentation: Document and translate cdi.h In-Reply-To: <1262211692-30529-1-git-send-email-kevin@tyndur.org> References: <1262211692-30529-1-git-send-email-kevin@tyndur.org> Message-ID: On 12/31/09, Kevin Wolf wrote: > + Add module documentation for Core module > + Add some more doxygen commits in cdi.h > + Add English translations for comments in cdi.h > > Signed-off-by: Kevin Wolf Looks good. Acked-by: Matthew Iselin From redelman at gmail.com Wed Dec 30 23:54:08 2009 From: redelman at gmail.com (Rich Edelman) Date: Wed, 30 Dec 2009 16:54:08 -0600 Subject: [cdi-devel] [PATCH] Documentation: Document and translate cdi.h In-Reply-To: <1262211692-30529-1-git-send-email-kevin@tyndur.org> References: <1262211692-30529-1-git-send-email-kevin@tyndur.org> Message-ID: <61acd8d30912301454m1e44db66v7f0f3d430d372dc6@mail.gmail.com> On Wed, Dec 30, 2009 at 4:21 PM, Kevin Wolf wrote: > + Add module documentation for Core module > + Add some more doxygen commits in cdi.h > + Add English translations for comments in cdi.h > > Signed-off-by: Kevin Wolf > --- > ?include/cdi.h | ?204 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- > ?1 files changed, 196 insertions(+), 8 deletions(-) > > diff --git a/include/cdi.h b/include/cdi.h > index d7cd210..629bbd1 100644 > --- a/include/cdi.h > +++ b/include/cdi.h > @@ -62,30 +225,55 @@ struct cdi_driver { > ?}; > > ?/** > - * Muss vor dem ersten Aufruf einer anderen CDI-Funktion aufgerufen werden. > + * \german > + * Treiber, die ihre eigene main-Funktion implemenieren, m?ssen diese Funktoin > + * vor dem ersten Aufruf einer anderen CDI-Funktion aufrufen. > ?* Initialisiert interne Datenstruktur der Implementierung fuer das jeweilige > ?* Betriebssystem und startet anschliessend alle Treiber. > ?* > - * Ein wiederholter Aufruf bleibt ohne Effekt. > + * Ein wiederholter Aufruf bleibt ohne Effekt. Es ist nicht definiert, ob diese > + * Funktion zur?ckkehrt. > + * \endgerman > + * > + * \english > + * Drivers which implement their own main() function must call this function > + * before they call any other CDI function. It initialises internal data > + * structures of the CDI implementation and starts all drivers. > + * > + * This function takes effect only once, any other calls remain without effect. > + * It is not defined if this function returns. > + * \endenglish > ?*/ > ?void cdi_init(void); For the english translation, I'd perhaps replace the last part with something like this: This function should only be called once, additional calls will have no effect. Depending on the implementation, this function may or may not return. Just a suggestion, of course. Aside from that, maybe look at using the \ versions instead of \if ? If for no other reason than consistency. All in all, looks good to me. Acked-by: Rich Edelman From max at tyndur.org Thu Dec 31 11:09:25 2009 From: max at tyndur.org (Max Reitz) Date: Thu, 31 Dec 2009 11:09:25 +0100 Subject: [cdi-devel] [PATCH] Documentation: Document and translate cdi.h In-Reply-To: <1262211692-30529-1-git-send-email-kevin@tyndur.org> References: <1262211692-30529-1-git-send-email-kevin@tyndur.org> Message-ID: <4B3C7855.2000203@tyndur.org> Kevin Wolf wrote: > + Add module documentation for Core module > + Add some more doxygen commits in cdi.h > + Add English translations for comments in cdi.h > > Signed-off-by: Kevin Wolf Just for the sake of completeness: Acked-by: Max Reitz