From kevin at tyndur.org Sat Dec 12 23:28:00 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:00 +0100 Subject: [tyndur-devel] [PATCH 00/11] Root-Dateisystem nach Volumenamen bestimmen Message-ID: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Hartkodierte Gerätepfade für das Root-Dateisystem sind doof. Wenn wir stattdessen den Volumenamen des Dateisystems benutzen, kann das gleiche Image sowohl auf AHCI als auch IDE funktionieren, und falls mehrere CD-Laufwerke da sind, sollte er auch das richtige heraussuchen. Kevin Wolf (11): kernel2: BUILD_BUG_ON() kernel2: Neuer Syscall lio_probe_service LIOv2: lio_probe_service für Userspaceservices cdi/fs: Add .fs_probe callback ext2: Implement .fs_probe iso9660: Implement .fs_probe cdi/fs: Implementierung für .fs_probe kernel2: dev:/-Dateisystem cdi/storage: Dateisystem proben servmgr: Auf Root-Dateisystem warten build: Root-Dateisystem nach Volumenamen bestimmen build/config/grub_cdrom.cfg | 4 +- build/config/grub_hd_kernel2.cfg | 6 +- build/scripts/image_cdrom_grub1 | 2 +- build/scripts/image_hd_grub1 | 2 +- build/scripts/image_hd_grub2 | 2 +- build/scripts/image_hd_syslinux | 2 +- src/include/syscall_structs.h | 21 +++++++ src/include/syscallno.h | 1 + src/kernel2/include/kernel.h | 5 ++ src/kernel2/include/lostio/client.h | 4 ++ src/kernel2/include/syscall.h | 4 ++ src/kernel2/src/lostio/include/lostio_int.h | 4 ++ src/kernel2/src/lostio/modules/tmp.c | 35 ++++++++--- src/kernel2/src/lostio/tree.c | 34 +++++++++++ src/kernel2/src/syscall.c | 1 + src/kernel2/src/syscalls/lio_server.c | 61 ++++++++++++++++++ src/kernel2/src/syscalls/lostio.c | 14 +++++ src/modules/cdi/ext2/ext2_cdi.h | 1 + src/modules/cdi/ext2/init.c | 22 +++++++ src/modules/cdi/ext2/main.c | 1 + src/modules/cdi/include/cdi-osdep.h | 1 + src/modules/cdi/include/cdi/fs.h | 28 +++++++++ src/modules/cdi/iso9660/init.c | 25 ++++++++ src/modules/cdi/iso9660/iso9660_cdi.h | 1 + src/modules/cdi/iso9660/main.c | 1 + src/modules/cdi/lib/fs/fs.c | 8 ++- src/modules/cdi/lib/fs/lostio_if.c | 33 ++++++++++ src/modules/cdi/lib/storage.c | 95 +++++++++++++++++++++++++++-- src/modules/include/lostio.h | 6 ++ src/modules/include/syscall.h | 9 +++ src/modules/lib/lostio/lio_server.c | 16 +++++ src/modules/lib/syscalls/lostio.c | 21 +++++++ src/modules/servmgr/lang/lang_en.c | 3 + src/modules/servmgr/lang/resstr.h | 1 + src/modules/servmgr/main.c | 46 ++++++++++++++ 35 files changed, 494 insertions(+), 26 deletions(-) -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:01 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:01 +0100 Subject: [tyndur-devel] [PATCH 01/11] kernel2: BUILD_BUG_ON() In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-2-git-send-email-kevin@tyndur.org> + kernel2: BUILD_BUG_ON()-Makro für statische Zusicherungen Signed-off-by: Kevin Wolf --- src/kernel2/include/kernel.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/kernel2/include/kernel.h b/src/kernel2/include/kernel.h index 7fa8209..cd60247 100644 --- a/src/kernel2/include/kernel.h +++ b/src/kernel2/include/kernel.h @@ -42,6 +42,11 @@ extern uint64_t timer_ticks; __attribute__((noreturn)) void panic(char* message, ...); +#define GLUE(x,y) x ## y +#define BUILD_ASSERT(cnt) GLUE(__cdi_build_assert, cnt) +#define BUILD_BUG_ON(x) \ + struct BUILD_ASSERT(__COUNTER__) { int assertion[(x) ? -1 : 1]; }; + #define BUG_ON(c) \ do { \ if (c) { \ -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:02 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:02 +0100 Subject: [tyndur-devel] [PATCH 02/11] kernel2: Neuer Syscall lio_probe_service In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-3-git-send-email-kevin@tyndur.org> + kernel2: Unterstützung für einen neuen Syscall, der zu einer gegebenen Ressource einen Service verwendet, der sie als Pipequelle benutzen kann. Signed-off-by: Kevin Wolf --- src/include/syscall_structs.h | 6 +++++ src/include/syscallno.h | 1 + src/kernel2/include/lostio/client.h | 4 ++++ src/kernel2/include/syscall.h | 4 ++++ src/kernel2/src/lostio/include/lostio_int.h | 4 ++++ src/kernel2/src/lostio/tree.c | 34 +++++++++++++++++++++++++++++ src/kernel2/src/syscall.c | 1 + src/kernel2/src/syscalls/lostio.c | 14 ++++++++++++ src/modules/include/syscall.h | 9 ++++++++ src/modules/lib/syscalls/lostio.c | 21 ++++++++++++++++++ 10 files changed, 98 insertions(+) diff --git a/src/include/syscall_structs.h b/src/include/syscall_structs.h index 0d5892c..3ba12b9 100644 --- a/src/include/syscall_structs.h +++ b/src/include/syscall_structs.h @@ -86,6 +86,12 @@ typedef int64_t lio_usp_resource_t; /** ID eines LostIO-Verzeichnisbaums */ typedef int64_t lio_usp_tree_t; + +struct lio_probe_service_result { + char service[32]; + char volname[32]; +} __attribute__((packed)); + /** * Beschreibt als Bitmaske den Modus, in dem ein Stream arbeiten soll. */ diff --git a/src/include/syscallno.h b/src/include/syscallno.h index 47ab453..5fa5cf1 100644 --- a/src/include/syscallno.h +++ b/src/include/syscallno.h @@ -108,6 +108,7 @@ #define SYSCALL_LIO_UNLINK 98 #define SYSCALL_LIO_SYNC_ALL 99 #define SYSCALL_LIO_PASS_FD 100 +#define SYSCALL_LIO_PROBE_SERVICE 101 #define SYSCALL_LIO_SRV_SERVICE_REGISTER 110 #define SYSCALL_LIO_SRV_TREE_SET_RING 111 diff --git a/src/kernel2/include/lostio/client.h b/src/kernel2/include/lostio/client.h index c7b3795..8880e62 100644 --- a/src/kernel2/include/lostio/client.h +++ b/src/kernel2/include/lostio/client.h @@ -52,6 +52,10 @@ struct lio_dir_entry { */ struct lio_resource* lio_get_resource(const char* path, int follow_symlinks); +/** Service finden, der die Ressource als Pipequelle akzeptiert */ +int lio_probe_service(struct lio_resource* res, + struct lio_probe_service_result* probe_data); + /** * Oeffnet einen Stream zu einer Ressource * diff --git a/src/kernel2/include/syscall.h b/src/kernel2/include/syscall.h index 9ff28f6..e23656e 100644 --- a/src/kernel2/include/syscall.h +++ b/src/kernel2/include/syscall.h @@ -253,6 +253,10 @@ int syscall_lio_unlink(lio_usp_resource_t* parent, const char* name, /// Alle Blocks rausschreiben, die sich im Cache befinden int syscall_lio_sync_all(int soft); +/// Service finden, der die Ressource als Pipequelle akzeptiert +int syscall_lio_probe_service(lio_usp_resource_t* res, + struct lio_probe_service_result* probe_data); + // LIO-Server /// Neuen lio-Service registrieren int syscall_lio_srv_service_register(const char* name, size_t name_len, diff --git a/src/kernel2/src/lostio/include/lostio_int.h b/src/kernel2/src/lostio/include/lostio_int.h index 33c055c..18518d6 100644 --- a/src/kernel2/src/lostio/include/lostio_int.h +++ b/src/kernel2/src/lostio/include/lostio_int.h @@ -50,6 +50,10 @@ struct lio_service; struct lio_driver { + /** Prüfen, ob die Ressource als Pipequelle benutzt werden kann */ + int (*probe)(struct lio_service* service, struct lio_stream* source, + struct lio_probe_service_result* probe_data); + /** * Root-Ressource eines Dateisystems laden, das root-Feld in tree ist also * noch NULL. diff --git a/src/kernel2/src/lostio/tree.c b/src/kernel2/src/lostio/tree.c index 3fcd2c7..fbc36fd 100644 --- a/src/kernel2/src/lostio/tree.c +++ b/src/kernel2/src/lostio/tree.c @@ -35,6 +35,7 @@ #include #include +#include #include "kernel.h" #include "kprintf.h" @@ -273,6 +274,39 @@ void lio_add_service(struct lio_service* service) } /** + * Service finden, der die Ressource als Pipequelle akzeptiert + */ +int lio_probe_service(struct lio_resource* res, + struct lio_probe_service_result* probe_data) +{ + struct lio_service* service; + struct lio_stream *stream; + int ret; + int i; + + stream = lio_open(res, LIO_READ); + if (stream == NULL) { + return -EIO; + } + + for (i = 0; (service = list_get_element_at(lio_services, i)); i++) { + if (!service->lio_ops.probe) { + continue; + } + + ret = service->lio_ops.probe(service, stream, probe_data); + if (ret == 0) { + goto out; + } + } + + ret = -ENOENT; +out: + lio_close(stream); + return ret; +} + +/** * Neue Ressource initialisieren. * Vom Aufrufer initialisiert werden muessen (werden alle auf 0 initialisiert): * - blocksize diff --git a/src/kernel2/src/syscall.c b/src/kernel2/src/syscall.c index e0bd7bb..8d4da44 100644 --- a/src/kernel2/src/syscall.c +++ b/src/kernel2/src/syscall.c @@ -129,6 +129,7 @@ void syscall_init() syscall_register(SYSCALL_LIO_UNLINK, &syscall_lio_unlink, 3); syscall_register(SYSCALL_LIO_SYNC_ALL, &syscall_lio_sync_all, 0); syscall_register(SYSCALL_LIO_PASS_FD, &syscall_lio_pass_fd, 2); + syscall_register(SYSCALL_LIO_PROBE_SERVICE, &syscall_lio_probe_service, 2); syscall_register(SYSCALL_LIO_SRV_SERVICE_REGISTER, &syscall_lio_srv_service_register, 4); diff --git a/src/kernel2/src/syscalls/lostio.c b/src/kernel2/src/syscalls/lostio.c index 32efd4b..9fde44d 100644 --- a/src/kernel2/src/syscalls/lostio.c +++ b/src/kernel2/src/syscalls/lostio.c @@ -378,3 +378,17 @@ int syscall_lio_sync_all(int soft) { return lio_sync_all(soft); } + +/// Service finden, der die Ressource als Pipequelle akzeptiert +int syscall_lio_probe_service(lio_usp_resource_t* usp_res, + struct lio_probe_service_result* probe_data) +{ + struct lio_resource* res; + + res = lio_usp_get_resource(*usp_res); + if (res == NULL) { + return -EINVAL; + } + + return lio_probe_service(res, probe_data); +} diff --git a/src/modules/include/syscall.h b/src/modules/include/syscall.h index 1528b12..7ab42a4 100644 --- a/src/modules/include/syscall.h +++ b/src/modules/include/syscall.h @@ -357,6 +357,15 @@ int lio_unlink(lio_resource_t parent, const char* name); */ int lio_sync_all(void); +/** + * Versucht einen Service zu finden, der die angegebene Ressource als + * Pipequelle verwenden kann (z.B. passendes Dateisystem für Image) + * + * @return 0 bei Erfolg, negativ im Fehlerfall + */ +int lio_probe_service(lio_resource_t res, + struct lio_probe_service_result* ret); + // LIO2-Server /** diff --git a/src/modules/lib/syscalls/lostio.c b/src/modules/lib/syscalls/lostio.c index c702084..81658b7 100644 --- a/src/modules/lib/syscalls/lostio.c +++ b/src/modules/lib/syscalls/lostio.c @@ -526,3 +526,24 @@ int lio_sync_all(void) return result; } +/** + * Versucht einen Service zu finden, der die angegebene Ressource als + * Pipequelle verwenden kann (z.B. passendes Dateisystem für Image) + * + * @return 0 bei Erfolg, negativ im Fehlerfall + */ +int lio_probe_service(lio_resource_t res, struct lio_probe_service_result* ret) +{ + int result; + + asm( + "pushl %3;" + "pushl %2;" + "mov %1, %%eax;" + "int $0x30;" + "add $0x8, %%esp;" + : "=a" (result) : "i" (SYSCALL_LIO_PROBE_SERVICE), "r" (&res), "r" (ret) + : "memory"); + + return result; +} -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:03 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:03 +0100 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_03/11=5D_LIOv2=3A_lio=5Fprobe?= =?utf-8?q?=5Fservice_f=C3=BCr_Userspaceservices?= In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-4-git-send-email-kevin@tyndur.org> + LIOv2: Userspace-LIO-Services können jetzt auch lio_probe_service unterstützen Signed-off-by: Kevin Wolf --- src/include/syscall_structs.h | 15 +++++++++ src/kernel2/src/syscalls/lio_server.c | 61 +++++++++++++++++++++++++++++++++++ src/modules/include/lostio.h | 6 ++++ src/modules/lib/lostio/lio_server.c | 16 +++++++++ 4 files changed, 98 insertions(+) diff --git a/src/include/syscall_structs.h b/src/include/syscall_structs.h index 3ba12b9..d433779 100644 --- a/src/include/syscall_structs.h +++ b/src/include/syscall_structs.h @@ -303,6 +303,11 @@ enum lio_opcode { * Stream der Ressource wird geschlossen */ LIO_OP_CLOSE = 15, + + /** + * Prüfen, ob die Ressource als Pipequelle benutzt werden kann + */ + LIO_OP_PROBE = 16, }; /// Flags fuer Operationen @@ -547,6 +552,16 @@ struct lio_op_sync { void* opaque; } __attribute__((packed)); +struct lio_op_probe { + struct lio_op op; + + /// Stream fuer die Pipequelle + lio_usp_stream_t source; + + /// Rückgabe: Informationen über Dateisystem + struct lio_probe_service_result* probe_data; +} __attribute__((packed)); + /// Flags fuer Server-Ressourcen enum lio_server_flags { diff --git a/src/kernel2/src/syscalls/lio_server.c b/src/kernel2/src/syscalls/lio_server.c index 5127d7a..4b1b03d 100644 --- a/src/kernel2/src/syscalls/lio_server.c +++ b/src/kernel2/src/syscalls/lio_server.c @@ -68,6 +68,8 @@ static struct lio_resource* make_symlink(struct lio_resource* parent, const char* name, const char* target); static int truncate(struct lio_resource* res, uint64_t size); static int unlink(struct lio_resource* parent, const char* name); +static int probe(struct lio_service* serv, struct lio_stream* source, + struct lio_probe_service_result* probe_data); struct lio_pending_op { struct tree_item tinfo; @@ -136,6 +138,7 @@ int syscall_lio_srv_service_register(const char* name, size_t name_len, // TODO: Ein paar is_userspace service->name = strdup(name); + service->lio_ops.probe= probe; service->lio_ops.load_root = load_root; service->lio_ops.load_children = load_children; service->lio_ops.close = close; @@ -831,3 +834,61 @@ static int unlink(struct lio_resource* parent, const char* name) return result; } + +static int probe(struct lio_service* serv, struct lio_stream* source, + struct lio_probe_service_result* probe_data) +{ + struct lio_userspace_service* s = serv->opaque; + struct lio_probe_service_result* result; + struct lio_usp_stream* usp_fd; + struct lio_op_probe* op; + void* user_buf; + int ret; + + if (s->provider == NULL) { + return -EBADF; + } + + op = (struct lio_op_probe*) alloc_ring_op(&s->ring, sizeof(*op)); + + usp_fd = lio_usp_add_stream(s->provider->process, source); + if (!usp_fd) { + return -EBADF; + } + + BUILD_BUG_ON(sizeof(*result) > PAGE_SIZE); + result = mmc_valloc(&mmc_current_context(), 1, MM_FLAGS_KERNEL_CODE); + if (result == NULL) { + ret = -ENOMEM; + goto fail_alloc_page; + } + memset(result, 0, PAGE_SIZE); + + user_buf = mmc_automap_user(&s->provider->process->context, + &mmc_current_context(), result, 1, + MM_USER_START, MM_USER_END, MM_FLAGS_USER_DATA); + if (user_buf == NULL) { + ret = -ENOMEM; + goto fail_map; + } + + op->op.opcode = LIO_OP_PROBE; + op->source = usp_fd->usp_id; + op->probe_data = user_buf; + + ret = commit_ring_op(&s->ring, s->provider, &op->op, NULL); + if (ret == 0) { + strlcpy(result->service, serv->name, sizeof(result->service)); + memcpy(probe_data, result, sizeof(*probe_data)); + } + + mmc_unmap(&s->provider->process->context, user_buf, 1); +fail_map: + mmc_vfree(&mmc_current_context(), result, 1); +fail_alloc_page: + /* Wir dürfen kein close() machen, der Kernel braucht den Stream noch */ + source->usp_refcount++; + lio_usp_unref_stream(s->provider->process, usp_fd); + BUG_ON(--source->usp_refcount != 0); + return ret; +} diff --git a/src/modules/include/lostio.h b/src/modules/include/lostio.h index 06dafec..9ad8c33 100644 --- a/src/modules/include/lostio.h +++ b/src/modules/include/lostio.h @@ -252,8 +252,14 @@ int64_t lio_compat_tell(io_resource_t* io_res); */ void lio_dispatch(void); +struct lio_service; struct lio_tree; struct lio_driver { + /// Prüfen, ob die Ressource als Pipequelle benutzt werden kann + int (*probe)(struct lio_service* service, + lio_stream_t source, + struct lio_probe_service_result* probe_data); + /// Root-Ressource eines Baums laden struct lio_resource* (*load_root)(struct lio_tree* tree); diff --git a/src/modules/lib/lostio/lio_server.c b/src/modules/lib/lostio/lio_server.c index 64206f5..2f3825d 100644 --- a/src/modules/lib/lostio/lio_server.c +++ b/src/modules/lib/lostio/lio_server.c @@ -50,6 +50,7 @@ static void tree_dispatch(struct lio_tree* tree); static void process_op(struct lio_service* service, struct lio_op* op); // Operationen +static void op_probe(struct lio_service* service, struct lio_op* op); static void op_load_root(struct lio_service* service, struct lio_op* op); static void op_load_children(struct lio_service* service, struct lio_op* op); static void op_close(struct lio_service* service, struct lio_op* op); @@ -94,6 +95,10 @@ void lio_dispatch(void) op_load_root(s, op); break; + case LIO_OP_PROBE: + op_probe(s, op); + break; + default: printf("lio_dispatch: Fehler, unerwarteter Opcode 0x%x\n", op->opcode); @@ -203,6 +208,17 @@ static void process_op(struct lio_service* service, struct lio_op* op) op->flags &= ~LIO_OPFL_USP; } +static void op_probe(struct lio_service* service, struct lio_op* op) +{ + struct lio_op_probe* opr = (struct lio_op_probe*) op; + int ret = -EINVAL; + + if (service->lio_ops.probe) { + ret = service->lio_ops.probe(service, opr->source, opr->probe_data); + } + lio_srv_op_done(op, ret, 0); +} + static void op_load_root(struct lio_service* service, struct lio_op* op) { struct lio_op_load_root* olr = (struct lio_op_load_root*) op; -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:04 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:04 +0100 Subject: [tyndur-devel] [PATCH 04/11] cdi/fs: Add .fs_probe callback In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-5-git-send-email-kevin@tyndur.org> The .fs_probe callback checks whether the file system could be mounted on a given storage device without actually mounting it. On success, it can return the volume name of the file system. Cc: cdi-devel at tyndur.org Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi/fs.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/modules/cdi/include/cdi/fs.h b/src/modules/cdi/include/cdi/fs.h index 31ef4a0..ea64b69 100644 --- a/src/modules/cdi/include/cdi/fs.h +++ b/src/modules/cdi/include/cdi/fs.h @@ -29,6 +29,34 @@ struct cdi_fs_driver { struct cdi_driver drv; /** + * \german + * Prüfen, ob sich ein gültiges Dateisystem auf dem Gerät befindet. Falls + * das der Fall ist und das Dateisystem einen Namen hat, wird der Name + * in einem per malloc() neu allozierten String zurückgegeben. + * + * @fs ist kein vollständig initialisiertes Dateisystem; es kann nur + * verwendet werden, um auf das Gerät zuzugreifen. + * + * Diese Funktion ist für Dateisystemtreiber optional. + * + * @return Wenn das Dateisystem gültig ist 1, sonst 0. + * \endgerman + * \english + * Check whether there is a valid file system on the device. If so, and the + * file system has a name, the name is returned in a string newly allocated + * with malloc(). + * + * @fs isn't a fully initialised file system; it can only be used to access + * the storage device. + * + * This function is optional for file system drivers. + * + * @return If the file system is valid 1, otherwise 0. + * \endenglish + */ + int (*fs_probe)(struct cdi_fs_filesystem* fs, char** volname); + + /** * Neues Dateisystem initialisieren; Diese Funktion muss das root_object in * der Dateisystemstruktur eintragen. * -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:05 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:05 +0100 Subject: [tyndur-devel] [PATCH 05/11] ext2: Implement .fs_probe In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-6-git-send-email-kevin@tyndur.org> Cc: cdi-devel at tyndur.org Signed-off-by: Kevin Wolf --- src/modules/cdi/ext2/ext2_cdi.h | 1 + src/modules/cdi/ext2/init.c | 22 ++++++++++++++++++++++ src/modules/cdi/ext2/main.c | 1 + 3 files changed, 24 insertions(+) diff --git a/src/modules/cdi/ext2/ext2_cdi.h b/src/modules/cdi/ext2/ext2_cdi.h index 00de2a2..7ad9c46 100644 --- a/src/modules/cdi/ext2/ext2_cdi.h +++ b/src/modules/cdi/ext2/ext2_cdi.h @@ -57,6 +57,7 @@ struct ext2_fs_res { // CDI-IF +int ext2_fs_probe(struct cdi_fs_filesystem* cdi_fs, char** volname); int ext2_fs_init(struct cdi_fs_filesystem* fs); int ext2_fs_destroy(struct cdi_fs_filesystem* fs); diff --git a/src/modules/cdi/ext2/init.c b/src/modules/cdi/ext2/init.c index d20f647..2da556f 100644 --- a/src/modules/cdi/ext2/init.c +++ b/src/modules/cdi/ext2/init.c @@ -51,6 +51,28 @@ static int dev_write(uint64_t start, size_t size, const void* data, void* prv) return cdi_fs_data_write(fs, start, size, data); } +int ext2_fs_probe(struct cdi_fs_filesystem* cdi_fs, char** volname) +{ + ext2_superblock_t sb; + int ret; + + ext2_fs_t fs = { + .dev_read = dev_read, + .dev_private = cdi_fs, + }; + + ret = ext2_sb_read(&fs, &sb); + if (ret == 0) { + return 0; + } + + *volname = malloc(sizeof(sb.volume_name) + 1); + memcpy(*volname, sb.volume_name, sizeof(sb.volume_name)); + (*volname)[sizeof(sb.volume_name)] = '\0'; + + return 1; +} + int ext2_fs_init(struct cdi_fs_filesystem* cdi_fs) { struct ext2_fs_res* root_res; diff --git a/src/modules/cdi/ext2/main.c b/src/modules/cdi/ext2/main.c index a1d597f..24f4594 100644 --- a/src/modules/cdi/ext2/main.c +++ b/src/modules/cdi/ext2/main.c @@ -72,6 +72,7 @@ static struct cdi_fs_driver ext2_driver = { .init = ext2_driver_init, .destroy = ext2_driver_destroy, }, + .fs_probe = ext2_fs_probe, .fs_init = ext2_fs_init, .fs_destroy = ext2_fs_destroy, }; -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:06 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:06 +0100 Subject: [tyndur-devel] [PATCH 06/11] iso9660: Implement .fs_probe In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-7-git-send-email-kevin@tyndur.org> Cc: cdi-devel at tyndur.org Signed-off-by: Kevin Wolf --- src/modules/cdi/iso9660/init.c | 25 +++++++++++++++++++++++++ src/modules/cdi/iso9660/iso9660_cdi.h | 1 + src/modules/cdi/iso9660/main.c | 1 + 3 files changed, 27 insertions(+) diff --git a/src/modules/cdi/iso9660/init.c b/src/modules/cdi/iso9660/init.c index 641d4f3..f2e8a0d 100644 --- a/src/modules/cdi/iso9660/init.c +++ b/src/modules/cdi/iso9660/init.c @@ -17,6 +17,7 @@ */ #include +#include #include "cdi/fs.h" #include "cdi/cache.h" @@ -25,6 +26,30 @@ #include "volume_descriptor.h" +int iso9660_fs_probe(struct cdi_fs_filesystem* cdi_fs, char** volname) +{ + struct iso9660_voldesc_prim voldesc; + size_t len; + int ret; + + ret = iso9660_voldesc_load(cdi_fs, ISO9660_VOLDESC_PRIM, &voldesc); + if (ret != 0) { + return 0; + } + + len = sizeof(voldesc.volume_identifier); + *volname = malloc(len + 1); + memcpy(*volname, voldesc.volume_identifier, len); + (*volname)[len] = '\0'; + + /* Remove padding */ + while (--len && (*volname)[len] == ' ') { + (*volname)[len] = '\0'; + } + + return 1; +} + /** * Initializes a ISO9660 filesystem * @param fs Filesystem to initialize diff --git a/src/modules/cdi/iso9660/iso9660_cdi.h b/src/modules/cdi/iso9660/iso9660_cdi.h index 25a4a14..483d506 100644 --- a/src/modules/cdi/iso9660/iso9660_cdi.h +++ b/src/modules/cdi/iso9660/iso9660_cdi.h @@ -59,6 +59,7 @@ struct iso9660_fs_res { }; // init.c +int iso9660_fs_probe(struct cdi_fs_filesystem* cdi_fs, char** volname); int iso9660_fs_init(struct cdi_fs_filesystem *fs); int iso9660_fs_destroy(struct cdi_fs_filesystem *fs); diff --git a/src/modules/cdi/iso9660/main.c b/src/modules/cdi/iso9660/main.c index 1e81779..56930d0 100644 --- a/src/modules/cdi/iso9660/main.c +++ b/src/modules/cdi/iso9660/main.c @@ -76,6 +76,7 @@ static struct cdi_fs_driver iso9660_driver = { }, .fs_init = iso9660_fs_init, .fs_destroy = iso9660_fs_destroy, + .fs_probe = iso9660_fs_probe, }; CDI_DRIVER(DRIVER_NAME, iso9660_driver) -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:07 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:07 +0100 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_07/11=5D_cdi/fs=3A_Implementier?= =?utf-8?b?dW5nIGbDvHIgLmZzX3Byb2Jl?= In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-8-git-send-email-kevin@tyndur.org> + cdi/fs: lio_probe_service() benutzt jetzt den .fs_probe-Callbacks von Dateisystemtreibern, um tatsächlich ein nützliches Ergebnis zu liefern Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi-osdep.h | 1 + src/modules/cdi/lib/fs/fs.c | 8 ++++++-- src/modules/cdi/lib/fs/lostio_if.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/modules/cdi/include/cdi-osdep.h b/src/modules/cdi/include/cdi-osdep.h index 94eba73..96f3e05 100644 --- a/src/modules/cdi/include/cdi-osdep.h +++ b/src/modules/cdi/include/cdi-osdep.h @@ -91,6 +91,7 @@ typedef struct { */ typedef struct { struct lio_tree* tree; + lio_stream_t source; } cdi_fs_osdep; struct storage_drv_osdep { diff --git a/src/modules/cdi/lib/fs/fs.c b/src/modules/cdi/lib/fs/fs.c index aeca3ea..64a668b 100644 --- a/src/modules/cdi/lib/fs/fs.c +++ b/src/modules/cdi/lib/fs/fs.c @@ -40,6 +40,9 @@ #include "cdi/fs.h" +int cdi_fs_lio_probe(struct lio_service* service, + lio_stream_t source, + struct lio_probe_service_result* probe_data); struct lio_resource* cdi_fs_lio_load_root(struct lio_tree* tree); int cdi_fs_lio_load_children(struct lio_resource* res); int cdi_fs_lio_read(struct lio_resource* res, uint64_t offset, @@ -74,6 +77,7 @@ void cdi_fs_driver_register(struct cdi_fs_driver* driver) service->name = driver->drv.name; service->opaque = driver; service->lio_ops = (struct lio_driver) { + .probe = cdi_fs_lio_probe, .load_root = cdi_fs_lio_load_root, .load_children = cdi_fs_lio_load_children, .read = cdi_fs_lio_read, @@ -106,7 +110,7 @@ size_t cdi_fs_data_read(struct cdi_fs_filesystem* fs, uint64_t start, { ssize_t result; - if ((result = lio_pread(fs->osdep.tree->source, start, size, buffer)) < 0) { + if ((result = lio_pread(fs->osdep.source, start, size, buffer)) < 0) { // FIXME: Das ist irgendwie ein bescheiden return 0; } @@ -130,7 +134,7 @@ size_t cdi_fs_data_write(struct cdi_fs_filesystem* fs, uint64_t start, { ssize_t result; - if ((result = lio_pwrite(fs->osdep.tree->source, start, size, buffer)) < 0) { + if ((result = lio_pwrite(fs->osdep.source, start, size, buffer)) < 0) { // FIXME: Das ist irgendwie ein bescheiden return 0; } diff --git a/src/modules/cdi/lib/fs/lostio_if.c b/src/modules/cdi/lib/fs/lostio_if.c index 2823c5f..2adf696 100644 --- a/src/modules/cdi/lib/fs/lostio_if.c +++ b/src/modules/cdi/lib/fs/lostio_if.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "cdi/fs.h" @@ -104,6 +105,37 @@ static struct lio_resource* create_liores(struct cdi_fs_filesystem* fs, return liores; } +int cdi_fs_lio_probe(struct lio_service* service, + lio_stream_t source, + struct lio_probe_service_result* probe_data) +{ + struct cdi_fs_driver* driver = service->opaque; + struct cdi_fs_filesystem* fs; + char* volname = NULL; + int ret; + + if (driver->fs_probe == NULL) { + return -EINVAL; + } + + fs = calloc(1, sizeof(*fs)); + fs->driver = driver; + fs->osdep.source = source; + + ret = driver->fs_probe(fs, &volname); + if (ret == 1) { + strlcpy(probe_data->volname, volname, sizeof(probe_data->volname)); + ret = 0; + } else { + ret = -EINVAL; + } + + free(volname); + free(fs); + + return ret; +} + struct lio_resource* cdi_fs_lio_load_root(struct lio_tree* tree) { struct cdi_fs_driver* driver = tree->service->opaque; @@ -116,6 +148,7 @@ struct lio_resource* cdi_fs_lio_load_root(struct lio_tree* tree) fs = calloc(1, sizeof(*fs)); fs->driver = driver; fs->osdep.tree = tree; + fs->osdep.source = tree->source; tree->opaque = fs; if (!driver->fs_init(fs)) { -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:08 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:08 +0100 Subject: [tyndur-devel] [PATCH 08/11] kernel2: dev:/-Dateisystem In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-9-git-send-email-kevin@tyndur.org> + kernel2: Ein zusätzliches temporäres Dateisystem dev:/ wird angelegt. Es enthält ein Unterverzeichnis fs/, in dem Symlinks auf existierende Dateisysteme abgelegt werden sollen. Signed-off-by: Kevin Wolf --- src/kernel2/src/lostio/modules/tmp.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/kernel2/src/lostio/modules/tmp.c b/src/kernel2/src/lostio/modules/tmp.c index 1f630e7..f7a7c84 100644 --- a/src/kernel2/src/lostio/modules/tmp.c +++ b/src/kernel2/src/lostio/modules/tmp.c @@ -202,22 +202,37 @@ static int unlink(struct lio_resource* parent, const char* name) return 0; } +#define TMP_LIO_OPS \ + { \ + .load_root = load_root, \ + .load_children = load_children, \ + .read = tmp_read, \ + .write = tmp_write, \ + .make_file = make_file, \ + .make_dir = make_dir, \ + .make_symlink = make_symlink, \ + .unlink = unlink, \ + } static struct lio_service service = { .name = "tmp", - .lio_ops = { - .load_root = load_root, - .load_children = load_children, - .read = tmp_read, - .write = tmp_write, - .make_file = make_file, - .make_dir = make_dir, - .make_symlink = make_symlink, - .unlink = unlink, - }, + .lio_ops = TMP_LIO_OPS, +}; + +static struct lio_service dev_service = { + .name = "dev", + .lio_ops = TMP_LIO_OPS, }; void lio_init_tmp(void) { + struct lio_resource* dev_root; + lio_add_service(&service); + + lio_add_service(&dev_service); + dev_root = lio_get_resource("dev:/", false); + if (dev_root != NULL) { + lio_mkdir(dev_root, "fs"); + } } -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:09 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:09 +0100 Subject: [tyndur-devel] [PATCH 09/11] cdi/storage: Dateisystem proben In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-10-git-send-email-kevin@tyndur.org> + cdi/storage: Für jedes neue Storage-Gerät bzw. jede Partition wird jetzt lio_probe_service() aufgerufen. Bei Erfolg wird ein Symlink in dev:/fs/ angelegt. Als Dateiname für den Symlink wird der Volumename des Dateisystems benutzt. Signed-off-by: Kevin Wolf --- src/modules/cdi/lib/storage.c | 95 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 6 deletions(-) diff --git a/src/modules/cdi/lib/storage.c b/src/modules/cdi/lib/storage.c index 3a50e04..028280f 100644 --- a/src/modules/cdi/lib/storage.c +++ b/src/modules/cdi/lib/storage.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "cdi/storage.h" #include "libpartition.h" @@ -28,6 +29,20 @@ static int load_children(struct lio_resource* res); int cdi_storage_read(struct cdi_storage_device* device, uint64_t pos, size_t size, void* dest); +static const char* service_name; + +static const char* get_service_name(struct cdi_storage_driver* drv) +{ + const char* name; + + name = drv->drv.name; + if (!strcmp(name, "ahci-disk")) { + name = "ahci"; + } + + return name; +} + /** * Initialisiert die Datenstrukturen fuer einen Massenspeichertreiber */ @@ -36,7 +51,6 @@ void cdi_storage_driver_init(struct cdi_storage_driver* driver) static struct lio_resource* first_res = NULL; struct lio_resource* res; struct lio_service* service; - const char* service_name; driver->drv.type = CDI_STORAGE; cdi_driver_init((struct cdi_driver*) driver); @@ -47,10 +61,7 @@ void cdi_storage_driver_init(struct cdi_storage_driver* driver) return; } - service_name = driver->drv.name; - if (!strcmp(service_name, "ahci-disk")) { - service_name = "ahci"; - } + service_name = get_service_name(driver); res = lio_resource_create(NULL); res->browsable = 1; @@ -231,6 +242,75 @@ int cdi_storage_write(struct cdi_storage_device* device, uint64_t pos, return 0; } + +struct fs_detection { + char* path; +}; + +static void fs_detection_thread(void* opaque) +{ + struct fs_detection* fsd = opaque; + lio_resource_t fd; + char* link_path; + char* target_path; + struct lio_probe_service_result probe_data; + int ret; + + fd = lio_resource(fsd->path, false); + if (fd < 0) { + goto fail; + } + + ret = lio_probe_service(fd, &probe_data); + if (ret < 0) { + goto fail; + } + + asprintf(&link_path, "dev:/fs/%s", &probe_data.volname); + asprintf(&target_path, "%s|%s:/", fsd->path, &probe_data.service); + + io_create_link(target_path, link_path, false); + + free(link_path); + free(target_path); +fail: + free(fsd->path); + free(fsd); +} + +/** + * Versucht, einen dev:/-Link für das Dateisystem anlegen. Wenn es schiefgeht, + * ist nicht schlimm, dann fehlt der Link halt. Die eigentliche Erkennung des + * Dateisystems muss in einem anderen Thread ablaufen, weil der Kernel Requests + * an den Service schicken wird und wir nicht deadlocken wollen. + */ +static void queue_fs_detection(const char* service, const char* relpath) +{ + struct fs_detection* fsd; + int ret; + + fsd = malloc(sizeof(*fsd)); + if (fsd == NULL) { + return; + } + + ret = asprintf(&fsd->path, "%s:/%s", service, relpath); + if (ret < 0) { + fsd->path = NULL; + goto fail; + } + + ret = create_thread((uint32_t) &fs_detection_thread, fsd); + if (ret < 0) { + goto fail; + } + return; + +fail: + free(fsd->path); + free(fsd); +} + /** * Erstellt den Dateisystemknoten fuer ein Geraet. */ @@ -259,7 +339,6 @@ int lostio_mst_if_newdev(struct partition* part) res->opaque = part; lio_node_create(driver->osdep.root, res, path); - free(path); // Das erste ATAPI-Laufwerk wird ata:/cdrom if (!has_cdrom && !strncmp(device->dev.name, "atapi", strlen("atapi"))) { @@ -272,6 +351,10 @@ int lostio_mst_if_newdev(struct partition* part) lio_node_create(driver->osdep.root, res, "cdrom"); } + // Versuchen, das Dateisystem zu erkennen + queue_fs_detection(service_name, path); + + free(path); return 0; } -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:10 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:10 +0100 Subject: [tyndur-devel] [PATCH 10/11] servmgr: Auf Root-Dateisystem warten In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-11-git-send-email-kevin@tyndur.org> + servmgr: Anstatt davon auszugehen, dass das Root-Dateisystem bereit ist, sobald alle Multiboot-Services geladen sind, lassen wir dem Dateisystem jetzt ein bisschen Zeit, um aufzutauchen. Mit Probing läuft die Erstellung des Symlinks im Hintergrund, während der Storagetreiber sich schon als Service registriert hat. Signed-off-by: Kevin Wolf --- src/modules/servmgr/lang/lang_en.c | 3 +++ src/modules/servmgr/lang/resstr.h | 1 + src/modules/servmgr/main.c | 46 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/src/modules/servmgr/lang/lang_en.c b/src/modules/servmgr/lang/lang_en.c index 390ce0e..82b6997 100644 --- a/src/modules/servmgr/lang/lang_en.c +++ b/src/modules/servmgr/lang/lang_en.c @@ -47,6 +47,9 @@ static const struct tms_strings dict[] = { &__tms_main_not_running, "service '%s' is not running!\n", + &__tms_main_no_root, + "servmgr: timeout while waiting for root filesystem\n", + &__tms_service_not_found, "servmgr: service not found: '%s'\n", diff --git a/src/modules/servmgr/lang/resstr.h b/src/modules/servmgr/lang/resstr.h index 9664fed..91cfc84 100644 --- a/src/modules/servmgr/lang/resstr.h +++ b/src/modules/servmgr/lang/resstr.h @@ -34,6 +34,7 @@ extern void* __tms_config_dir_open; extern void* __tms_main_usage; extern void* __tms_main_not_running; +extern void* __tms_main_no_root; extern void* __tms_service_not_found; extern void* __tms_service_dependency; diff --git a/src/modules/servmgr/main.c b/src/modules/servmgr/main.c index 09d17fd..86c70d4 100644 --- a/src/modules/servmgr/main.c +++ b/src/modules/servmgr/main.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -122,6 +123,44 @@ static bool wait_startup_services(void) } /** + * Auf Root-Dateisystem warten + * + * @return true wenn das Dateisystem da ist, false sonst. + */ +static bool wait_root_dir(void) +{ + uint64_t timeout = get_tick_count() + 5000000; + lio_resource_t res; + lio_stream_t s; + + while (timeout > get_tick_count()) { + res = lio_resource(root_dir, false); + if (res >= 0) { + goto found; + } + + // Das System nicht mit Dauerpolling verstopfen + timer_register(do_nothing, 50000); + wait_for_rpc(); + } + return false; + +found: + s = lio_open(res, LIO_SYMLINK | LIO_READ); + if (s >= 0) { + char buf[1024]; + int ret; + ret = lio_read(s, sizeof(buf), buf); + if (ret > 0) { + root_dir = strdup(buf); + chdir(root_dir); + } + lio_close(s); + } + return true; +} + +/** * Startup-Services anzeigen, die nicht gestartet wurden. */ static void failed_startup_services(void) @@ -163,6 +202,13 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } + // Auf Rootdateisystem warten + if (!wait_root_dir()) { + printf(TMS(no_root, "servmgr: Timeout beim Warten auf das " + "Rootdateisystem\n")); + return EXIT_FAILURE; + } + // Konfiguration einlesen if (config_read()) { config_done = true; -- 2.1.4 From kevin at tyndur.org Sat Dec 12 23:28:11 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 12 Dec 2015 23:28:11 +0100 Subject: [tyndur-devel] [PATCH 11/11] build: Root-Dateisystem nach Volumenamen bestimmen In-Reply-To: <1449959291-24784-1-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> Message-ID: <1449959291-24784-12-git-send-email-kevin@tyndur.org> + build: Volumenamen bei den generierten Dateisystemen setzen + config: AHCI-Treiber laden * config: Das Root-Dateisystem nicht mehr auf einen bestimmten Gerätepfad hartkodieren, sondern nach Volumenamen gehen. Dadurch kann das gleiche Image jetzt sowohl von IDE als auch AHCI booten, und auch bei mehreren CD-Laufwerken sollte das richtige gefunden werden. Signed-off-by: Kevin Wolf --- build/config/grub_cdrom.cfg | 4 ++-- build/config/grub_hd_kernel2.cfg | 6 ++++-- build/scripts/image_cdrom_grub1 | 2 +- build/scripts/image_hd_grub1 | 2 +- build/scripts/image_hd_grub2 | 2 +- build/scripts/image_hd_syslinux | 2 +- 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/build/config/grub_cdrom.cfg b/build/config/grub_cdrom.cfg index 662a57f..6d12638 100644 --- a/build/config/grub_cdrom.cfg +++ b/build/config/grub_cdrom.cfg @@ -7,7 +7,7 @@ module /iso9660 module /ramoverlay module /ext2 module /console servmgr:/term servmgr:/term -module /servmgr ata:/cdrom|iso9660:/hd.img|ramoverlay:/cached|ext2:/ vterm:/vterm8/out ata iso9660 console ramoverlay ext2 pci +module /servmgr dev:/fs/tyndur-cdrom/hd.img|ramoverlay:/cached|ext2:/ vterm:/vterm8/out ata iso9660 console ramoverlay ext2 pci boot title tyndur (English) @@ -19,5 +19,5 @@ module /iso9660 module /ramoverlay module /ext2 module /console servmgr:/term servmgr:/term -module /servmgr ata:/cdrom|iso9660:/hd.img|ramoverlay:/cached|ext2:/ vterm:/vterm8/out ata iso9660 console ramoverlay ext2 pci +module /servmgr dev:/fs/tyndur-cdrom/hd.img|ramoverlay:/cached|ext2:/ vterm:/vterm8/out ata iso9660 console ramoverlay ext2 pci boot diff --git a/build/config/grub_hd_kernel2.cfg b/build/config/grub_hd_kernel2.cfg index 6de064b..6079e9c 100644 --- a/build/config/grub_hd_kernel2.cfg +++ b/build/config/grub_hd_kernel2.cfg @@ -3,9 +3,10 @@ kernel /boot/tyndur2 debug=s module /modules/init boot=file:/ module /modules/pci module /modules/ata +module /modules/ahci module /modules/ext2 module /modules/console servmgr:/term servmgr:/term -module /modules/servmgr ata:/ata00_p0|ext2:/ vterm:/vterm8/out ata ext2 console pci +module /modules/servmgr dev:/fs/tyndur-boot vterm:/vterm8/out ata ahci ext2 console pci boot title tyndur (English) @@ -13,7 +14,8 @@ kernel /boot/tyndur2 debug=s module /modules/init boot=file:/ env.LANG=en env.LC_ALL=en module /modules/pci module /modules/ata +module /modules/ahci module /modules/ext2 module /modules/console servmgr:/term servmgr:/term -module /modules/servmgr ata:/ata00_p0|ext2:/ vterm:/vterm8/out ata ext2 console pci +module /modules/servmgr dev:/fs/tyndur-boot vterm:/vterm8/out ata ahci ext2 console pci boot diff --git a/build/scripts/image_cdrom_grub1 b/build/scripts/image_cdrom_grub1 index 8cd20fd..1d6ea8c 100755 --- a/build/scripts/image_cdrom_grub1 +++ b/build/scripts/image_cdrom_grub1 @@ -134,7 +134,7 @@ fi # CD-Image bauen cp $FLOPPY_IMAGE_PATH $ROOT_CDROM -mkisofs -b $(basename $FLOPPY_IMAGE_PATH) -m .svn -o $IMAGE_PATH $ROOT_CDROM +mkisofs -V tyndur-cdrom -b $(basename $FLOPPY_IMAGE_PATH) -m .svn -o $IMAGE_PATH $ROOT_CDROM rm $ROOT_CDROM/$(basename $FLOPPY_IMAGE_PATH) echo "CD-Image erfolgreich erstellt!" diff --git a/build/scripts/image_hd_grub1 b/build/scripts/image_hd_grub1 index 6f292432..7a793f7 100755 --- a/build/scripts/image_hd_grub1 +++ b/build/scripts/image_hd_grub1 @@ -75,7 +75,7 @@ if [ $NEW -ne 0 ]; then fi # Dateisystem erstellen - $SUDO $LOST_TOOLS_MKE2FS -T ext2 -F -q $LOOP_DEV || die "Dateisystem konnte nicht erstellt werden" + $SUDO $LOST_TOOLS_MKE2FS -T ext2 -F -q $LOOP_DEV -L tyndur-boot || die "Dateisystem konnte nicht erstellt werden" $SUDO losetup -d $LOOP_DEV fi diff --git a/build/scripts/image_hd_grub2 b/build/scripts/image_hd_grub2 index 8527ee1..9171289 100755 --- a/build/scripts/image_hd_grub2 +++ b/build/scripts/image_hd_grub2 @@ -102,7 +102,7 @@ if [ $NEW -ne 0 ]; then fi # Dateisystem erstellen - $SUDO $LOST_TOOLS_MKE2FS -T ext2 -F -q $LOOP_DEV || die "Dateisystem konnte nicht erstellt werden" + $SUDO $LOST_TOOLS_MKE2FS -T ext2 -F -q $LOOP_DEV -L tyndur-boot || die "Dateisystem konnte nicht erstellt werden" $SUDO losetup -d $LOOP_DEV fi diff --git a/build/scripts/image_hd_syslinux b/build/scripts/image_hd_syslinux index a487920..5b4ec68 100755 --- a/build/scripts/image_hd_syslinux +++ b/build/scripts/image_hd_syslinux @@ -75,7 +75,7 @@ if [ $NEW -ne 0 ]; then fi # Dateisystem erstellen - $SUDO $LOST_TOOLS_MKE2FS -T ext2 -F -q $LOOP_DEV || die "Dateisystem konnte nicht erstellt werden" + $SUDO $LOST_TOOLS_MKE2FS -T ext2 -F -q $LOOP_DEV -L tyndur-boot || die "Dateisystem konnte nicht erstellt werden" $SUDO losetup -d $LOOP_DEV fi -- 2.1.4 From max at tyndur.org Sun Dec 13 00:25:50 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:25:50 +0100 Subject: [tyndur-devel] [PATCH 00/22] CDI.usb (und mehr) Message-ID: <1449962772-29705-1-git-send-email-max@tyndur.org> Diese Serie fügt CDI.usb-Unterstützung zu týndur hinzu, fixt ein paar artverwandte Dinge und enthält noch ein paar mehr Patches von cdi.git. Wer das liest, ist doof. Max Reitz (22): cdi/pci: Ausrichtung von BARs an Pageframes cdi/misc: Erlaube IRQ-Sharing cdi/vesa: Entferne "inline" cdi/misc: Add CDI_UPCAST and endianness functions cdi/misc: Add cdi_elapsed_ms() cdi/scsi, cdi.h: Remove týndur-specific fields cdi/scsi: Add support for multiple LUNs cdi.h: Add cdi_handle_bus_device() rtl8168b: Add driver ata: Temporary buffer for ATAPI commands ata: Initialize ata_device to 0 cdi/scsi: Neu geschrieben lock.h: locked_decrement() servmgr: Workaround für zyklische Abhängigkeiten cdi/misc: Implementiere cdi_elapsed_ms() cdi/usb: Add headers cdi/usb: Implementierung der CDI-Bibliothek usb: Entferne týndur-eigene Treiber ehci: Add EHCI driver usb: Add USB 2.0 bus driver usb-storage: Add USB mass storage driver Images: USB-Stick-Image Makefile | 8 +- build/config/grub_usb.cfg | 11 + build/config/grub_usb_kernel2.cfg | 23 + build/config/image_usb.sh | 3 + build/scripts/image_usb_grub1 | 122 ++++ build/scripts/image_usb_grub2 | 193 ++++++ buildmk.sh | 2 +- src/include/lock.h | 10 + src/modules/cdi/ata/atapi.c | 11 +- src/modules/cdi/ata/device.c | 2 +- src/modules/cdi/ehci/ehci.c | 640 +++++++++++++++++++ src/modules/cdi/ehci/ehci.h | 292 +++++++++ src/modules/cdi/ehci/main.c | 69 +++ src/modules/cdi/include/cdi-osdep.h | 21 + src/modules/cdi/include/cdi.h | 117 +++- src/modules/cdi/include/cdi/misc.h | 228 +++++++ src/modules/cdi/include/cdi/scsi.h | 11 +- src/modules/cdi/include/cdi/usb-structures.h | 174 ++++++ src/modules/cdi/include/cdi/usb.h | 211 +++++++ src/modules/cdi/include/cdi/usb_hcd.h | 168 +++++ src/modules/cdi/lib/cdi.c | 107 +++- src/modules/cdi/lib/misc.c | 41 +- src/modules/cdi/lib/pci.c | 6 +- src/modules/cdi/lib/scsi/disk.c | 506 +++++++++++---- src/modules/cdi/lib/usb.c | 403 ++++++++++++ src/modules/cdi/lib/usb_dd.c | 173 ++++++ src/modules/cdi/lib/usb_hcd.c | 413 +++++++++++++ src/modules/cdi/rtl8168b/include/rtl8168b.h | 143 +++++ src/modules/cdi/rtl8168b/main.c | 70 +++ src/modules/cdi/rtl8168b/rtl8168b.c | 409 ++++++++++++ src/modules/cdi/usb-storage/main.c | 76 +++ src/modules/cdi/usb-storage/usb-storage.c | 216 +++++++ src/modules/cdi/usb-storage/usb-storage.h | 60 ++ src/modules/cdi/usb/main.c | 61 ++ src/modules/cdi/usb/usb-hubs.h | 78 +++ src/modules/cdi/usb/usb.c | 893 +++++++++++++++++++++++++++ src/modules/cdi/usb/usb.h | 86 +++ src/modules/cdi/vesa/drawing.c | 12 +- src/modules/cdi/vesa/drawing.h | 12 +- src/modules/include/usb-ipc.h | 103 +++ src/modules/lib/servmgr.c | 11 +- src/modules/servmgr/rpcif.c | 22 +- src/modules/usb/include/usb-ddrv.h | 59 -- src/modules/usb/include/usb-hc.h | 141 ----- src/modules/usb/include/usb-hub.h | 49 -- src/modules/usb/include/usb-server.h | 153 ----- src/modules/usb/include/usb-structs.h | 176 ------ src/modules/usb/include/usb-trans.h | 66 -- src/modules/usb/msd/Makefile.all | 8 - src/modules/usb/msd/init.c | 206 ------ src/modules/usb/msd/lostio.c | 218 ------- src/modules/usb/msd/main.c | 65 -- src/modules/usb/msd/msd-bo.c | 104 ---- src/modules/usb/msd/msd-bo.h | 99 --- src/modules/usb/msd/msd-cbi.c | 86 --- src/modules/usb/msd/msd-cbi.h | 71 --- src/modules/usb/msd/msd.h | 154 ----- src/modules/usb/msd/rpc.c | 89 --- src/modules/usb/msd/scsi.c | 240 ------- src/modules/usb/msd/scsi.h | 219 ------- src/modules/usb/msd/usb-lib.c | 207 ------- src/modules/usb/msd/usb-lib.h | 124 ---- src/modules/usb/uhci/Makefile.all | 8 - src/modules/usb/uhci/ctrl.c | 96 --- src/modules/usb/uhci/init.c | 265 -------- src/modules/usb/uhci/main.c | 73 --- src/modules/usb/uhci/rpc.c | 297 --------- src/modules/usb/uhci/trans.c | 316 ---------- src/modules/usb/uhci/uhci.h | 269 -------- src/modules/usb/usb1/Makefile.all | 8 - src/modules/usb/usb1/detect.c | 444 ------------- src/modules/usb/usb1/hub.c | 180 ------ src/modules/usb/usb1/main.c | 59 -- src/modules/usb/usb1/rpc.c | 350 ----------- src/modules/usb/usb1/transfer.c | 123 ---- src/modules/usb/usb1/usb.h | 209 ------- 76 files changed, 6044 insertions(+), 5404 deletions(-) create mode 100644 build/config/grub_usb.cfg create mode 100644 build/config/grub_usb_kernel2.cfg create mode 100644 build/config/image_usb.sh create mode 100755 build/scripts/image_usb_grub1 create mode 100755 build/scripts/image_usb_grub2 create mode 100644 src/modules/cdi/ehci/ehci.c create mode 100644 src/modules/cdi/ehci/ehci.h create mode 100644 src/modules/cdi/ehci/main.c create mode 100644 src/modules/cdi/include/cdi/usb-structures.h create mode 100644 src/modules/cdi/include/cdi/usb.h create mode 100644 src/modules/cdi/include/cdi/usb_hcd.h create mode 100644 src/modules/cdi/lib/usb.c create mode 100644 src/modules/cdi/lib/usb_dd.c create mode 100644 src/modules/cdi/lib/usb_hcd.c create mode 100644 src/modules/cdi/rtl8168b/include/rtl8168b.h create mode 100644 src/modules/cdi/rtl8168b/main.c create mode 100644 src/modules/cdi/rtl8168b/rtl8168b.c create mode 100644 src/modules/cdi/usb-storage/main.c create mode 100644 src/modules/cdi/usb-storage/usb-storage.c create mode 100644 src/modules/cdi/usb-storage/usb-storage.h create mode 100644 src/modules/cdi/usb/main.c create mode 100644 src/modules/cdi/usb/usb-hubs.h create mode 100644 src/modules/cdi/usb/usb.c create mode 100644 src/modules/cdi/usb/usb.h create mode 100644 src/modules/include/usb-ipc.h delete mode 100644 src/modules/usb/include/usb-ddrv.h delete mode 100644 src/modules/usb/include/usb-hc.h delete mode 100644 src/modules/usb/include/usb-hub.h delete mode 100644 src/modules/usb/include/usb-server.h delete mode 100644 src/modules/usb/include/usb-structs.h delete mode 100644 src/modules/usb/include/usb-trans.h delete mode 100644 src/modules/usb/msd/Makefile.all delete mode 100644 src/modules/usb/msd/init.c delete mode 100644 src/modules/usb/msd/lostio.c delete mode 100644 src/modules/usb/msd/main.c delete mode 100644 src/modules/usb/msd/msd-bo.c delete mode 100644 src/modules/usb/msd/msd-bo.h delete mode 100644 src/modules/usb/msd/msd-cbi.c delete mode 100644 src/modules/usb/msd/msd-cbi.h delete mode 100644 src/modules/usb/msd/msd.h delete mode 100644 src/modules/usb/msd/rpc.c delete mode 100644 src/modules/usb/msd/scsi.c delete mode 100644 src/modules/usb/msd/scsi.h delete mode 100644 src/modules/usb/msd/usb-lib.c delete mode 100644 src/modules/usb/msd/usb-lib.h delete mode 100644 src/modules/usb/uhci/Makefile.all delete mode 100644 src/modules/usb/uhci/ctrl.c delete mode 100644 src/modules/usb/uhci/init.c delete mode 100644 src/modules/usb/uhci/main.c delete mode 100644 src/modules/usb/uhci/rpc.c delete mode 100644 src/modules/usb/uhci/trans.c delete mode 100644 src/modules/usb/uhci/uhci.h delete mode 100644 src/modules/usb/usb1/Makefile.all delete mode 100644 src/modules/usb/usb1/detect.c delete mode 100644 src/modules/usb/usb1/hub.c delete mode 100644 src/modules/usb/usb1/main.c delete mode 100644 src/modules/usb/usb1/rpc.c delete mode 100644 src/modules/usb/usb1/transfer.c delete mode 100644 src/modules/usb/usb1/usb.h -- 2.6.3 From max at tyndur.org Sun Dec 13 00:25:51 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:25:51 +0100 Subject: [tyndur-devel] [PATCH 01/22] cdi/pci: Ausrichtung von BARs an Pageframes In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-2-git-send-email-max@tyndur.org> ! BARs müssen nicht immer an 4k (oder was auch immer die Pageframegröße ist) ausgerichtet sein. Wenn sie das nicht sind, sollte die CDI-Bibliothek entsprechend handeln. Signed-off-by: Max Reitz --- src/modules/cdi/lib/pci.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/modules/cdi/lib/pci.c b/src/modules/cdi/lib/pci.c index 1df92dd..0dd07f8 100644 --- a/src/modules/cdi/lib/pci.c +++ b/src/modules/cdi/lib/pci.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -191,7 +192,10 @@ void cdi_pci_alloc_memory(struct cdi_pci_device* device) for (i = 0; (res = cdi_list_get(device->resources, i)); i++) { if (res->type == CDI_PCI_MEMORY) { - res->address = mem_allocate_physical(res->length, res->start, 0); + uintptr_t offset = res->start % PAGE_SIZE; + void* mapping = mem_allocate_physical(res->length + offset, + res->start - offset, 0); + res->address = (void*) ((uintptr_t)mapping + offset); } } } -- 2.6.3 From max at tyndur.org Sun Dec 13 00:25:52 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:25:52 +0100 Subject: [tyndur-devel] [PATCH 02/22] cdi/misc: Erlaube IRQ-Sharing In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-3-git-send-email-max@tyndur.org> * Dass Treiber mehrere Geräte verwalten, ist nicht selten. Dass mehrere Geräte denselben IRQ verwenden, ebenfalls nicht (zumindest, wenn man die IRQs nicht neu zuweist). Damit sollte die CDI-Bibliothek es auf jeden Fall erlauben, dass mehrere Handler für denselben IRQ angelegt werden. Signed-off-by: Max Reitz --- src/modules/cdi/lib/misc.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/modules/cdi/lib/misc.c b/src/modules/cdi/lib/misc.c index 11661b4..1b965af 100644 --- a/src/modules/cdi/lib/misc.c +++ b/src/modules/cdi/lib/misc.c @@ -9,6 +9,7 @@ */ #include +#include #include #include #include @@ -22,10 +23,16 @@ /** Anzahl der verfuegbaren IRQs */ #define IRQ_COUNT 0x10 +struct irq_handler { + struct irq_handler* next; + + void (*handler)(struct cdi_device* dev); + struct cdi_device* dev; +}; + /** Array mit allen IRQ-Handlern; Als index wird die Nummer benutzt */ -static void (*driver_irq_handler[IRQ_COUNT])(struct cdi_device*) = { NULL }; -/** Array mit den passenden Geraeten zu den registrierten IRQs */ -static struct cdi_device* driver_irq_device[IRQ_COUNT] = { NULL }; +static struct irq_handler* driver_irq_handlers[IRQ_COUNT]; + /** * Array, das die jeweilige Anzahl an aufgerufenen Interrupts seit dem * cdi_reset_wait_irq speichert. @@ -44,8 +51,8 @@ static void irq_handler(uint8_t irq) irq -= TYNDUR_IRQ_OFFSET; driver_irq_count[irq]++; - if (driver_irq_handler[irq]) { - driver_irq_handler[irq](driver_irq_device[irq]); + for (struct irq_handler* h = driver_irq_handlers[irq]; h; h = h->next) { + h->handler(h->dev); } } @@ -65,16 +72,18 @@ void cdi_register_irq(uint8_t irq, void (*handler)(struct cdi_device*), return; } - // Der Interrupt wurde schon mal registriert - if (driver_irq_handler[irq]) { - fprintf(stderr, "cdi: Versuch IRQ %d mehrfach zu registrieren\n", irq); - return; - } + struct irq_handler* h = malloc(sizeof(*h)); + *h = (struct irq_handler){ + .next = driver_irq_handlers[irq], + .handler = handler, + .dev = device + }; - driver_irq_handler[irq] = handler; - driver_irq_device[irq] = device; + driver_irq_handlers[irq] = h; - register_intr_handler(TYNDUR_IRQ_OFFSET + irq, irq_handler); + if (!h->next) { + register_intr_handler(TYNDUR_IRQ_OFFSET + irq, irq_handler); + } } /** @@ -118,7 +127,7 @@ int cdi_wait_irq(uint8_t irq, uint32_t timeout) return -1; } - if (!driver_irq_handler[irq]) { + if (!driver_irq_handlers[irq]) { return -2; } -- 2.6.3 From max at tyndur.org Sun Dec 13 00:25:53 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:25:53 +0100 Subject: [tyndur-devel] [PATCH 03/22] cdi/vesa: Entferne "inline" In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-4-git-send-email-max@tyndur.org> * Die Funktionen sind im Header zwar als inline deklariert, dort ist aber nur der Funktionskopf. Weil das "inline" vermutlich tatsächlich nichts bewirkt und GCC obendrein bei -std=gnu11 drüber meckert, ist es am besten, es einfach zu entfernen. Signed-off-by: Max Reitz --- src/modules/cdi/vesa/drawing.c | 12 ++++++------ src/modules/cdi/vesa/drawing.h | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/modules/cdi/vesa/drawing.c b/src/modules/cdi/vesa/drawing.c index b169ec2..8f53e8b 100644 --- a/src/modules/cdi/vesa/drawing.c +++ b/src/modules/cdi/vesa/drawing.c @@ -422,7 +422,7 @@ void vesa_draw_bitmap_part(struct cdi_video_bitmap* target, } } -inline void vesa_draw_dot(struct cdi_video_bitmap* target, unsigned int x, +void vesa_draw_dot(struct cdi_video_bitmap* target, unsigned int x, unsigned int y, struct cdi_video_color* color) { // Ungueltige Positionen abfangen @@ -454,7 +454,7 @@ inline void vesa_draw_dot(struct cdi_video_bitmap* target, unsigned int x, *start |= newcolor & colormask; } -inline uint32_t do_rop(enum cdi_video_raster_op rop, uint32_t new, uint32_t old) +uint32_t do_rop(enum cdi_video_raster_op rop, uint32_t new, uint32_t old) { switch (rop) { case CDI_VIDEO_ROP_OR: @@ -472,7 +472,7 @@ inline uint32_t do_rop(enum cdi_video_raster_op rop, uint32_t new, uint32_t old) } } -inline uint32_t convert_color(struct cdi_video_pixel_format fmt, +uint32_t convert_color(struct cdi_video_pixel_format fmt, struct cdi_video_color* cdicolor) { uint32_t red, blue, green, alpha, color; @@ -493,7 +493,7 @@ inline uint32_t convert_color(struct cdi_video_pixel_format fmt, return color; } -inline struct cdi_video_color* convert_color_from_pixel(struct cdi_video_pixel_format +struct cdi_video_color* convert_color_from_pixel(struct cdi_video_pixel_format fmt, uint32_t value) { struct cdi_video_color* color = malloc(sizeof(struct cdi_video_color)); @@ -520,7 +520,7 @@ inline struct cdi_video_color* convert_color_from_pixel(struct cdi_video_pixel_f return color; } -inline void* get_read_buffer_addr(struct cdi_video_bitmap* bitmap) +void* get_read_buffer_addr(struct cdi_video_bitmap* bitmap) { int x; char* addr = NULL; @@ -537,7 +537,7 @@ inline void* get_read_buffer_addr(struct cdi_video_bitmap* bitmap) return NULL; } -inline void* get_write_buffer_addr(struct cdi_video_bitmap* bitmap) +void* get_write_buffer_addr(struct cdi_video_bitmap* bitmap) { int x; char* addr = NULL; diff --git a/src/modules/cdi/vesa/drawing.h b/src/modules/cdi/vesa/drawing.h index bed6043..76b1fee 100644 --- a/src/modules/cdi/vesa/drawing.h +++ b/src/modules/cdi/vesa/drawing.h @@ -47,16 +47,16 @@ void vesa_draw_bitmap_part(struct cdi_video_bitmap* target, struct cdi_video_bitmap* bitmap, unsigned int x, unsigned int y, unsigned int srcx, unsigned int srcy, unsigned int width, unsigned int height); -inline void vesa_draw_dot(struct cdi_video_bitmap* target, unsigned int x, +void vesa_draw_dot(struct cdi_video_bitmap* target, unsigned int x, unsigned int y, struct cdi_video_color* color); -inline uint32_t do_rop(enum cdi_video_raster_op rop, uint32_t new, uint32_t old); -inline uint32_t convert_color(struct cdi_video_pixel_format fmt, +uint32_t do_rop(enum cdi_video_raster_op rop, uint32_t new, uint32_t old); +uint32_t convert_color(struct cdi_video_pixel_format fmt, struct cdi_video_color* cdicolor); -inline struct cdi_video_color* convert_color_from_pixel(struct cdi_video_pixel_format +struct cdi_video_color* convert_color_from_pixel(struct cdi_video_pixel_format fmt, uint32_t value); -inline void* get_read_buffer_addr(struct cdi_video_bitmap* bitmap); -inline void* get_write_buffer_addr(struct cdi_video_bitmap* bitmap); +void* get_read_buffer_addr(struct cdi_video_bitmap* bitmap); +void* get_write_buffer_addr(struct cdi_video_bitmap* bitmap); #endif /* __VESA_DRAWING_H__ */ -- 2.6.3 From max at tyndur.org Sun Dec 13 00:25:54 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:25:54 +0100 Subject: [tyndur-devel] [PATCH 04/22] cdi/misc: Add CDI_UPCAST and endianness functions In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-5-git-send-email-max@tyndur.org> + Type inheritance is a common pattern seen in CDI. The new CDI_UPCAST() macro makes conversion to superclasses safer and allows for multiple inheritance if the need arises. + Add some endianness conversion functions. Even though support for big endian platforms is nowhere in sight, endianness conversion is still a common operation which should not be implemented in the driver itself but provided by the CDI implementation. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi/misc.h | 141 +++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/src/modules/cdi/include/cdi/misc.h b/src/modules/cdi/include/cdi/misc.h index 07967ec..b0973e6 100644 --- a/src/modules/cdi/include/cdi/misc.h +++ b/src/modules/cdi/include/cdi/misc.h @@ -85,6 +85,147 @@ int cdi_ioports_free(uint16_t start, uint16_t count); */ void cdi_sleep_ms(uint32_t ms); +/** + * \german + * Castet @object (muss ein Pointer sein) zu einem Pointer auf ein Objekt des + * Typs @to, welcher eine Unterklasse vom Typ von @object sein muss. @field ist + * das Feld von @to, das @object enthält. + * + * Beispiel: + * struct cdi_driver* driver; + * struct cdi_storage_driver* storage_driver = + * CDI_UPCAST(driver, struct cdi_storage_driver, drv); + * \endgerman + * \english + * Casts @object (must be a pointer) to a pointer to an object of type @to which + * must be a subclass of @object's type. @field is the entry in @to which + * contains @object. + * + * Example: + * struct cdi_driver* driver; + * struct cdi_storage_driver* storage_driver = + * CDI_UPCAST(driver, struct cdi_storage_driver, drv); + * \endenglish + * \thuringiansaxonian + * Dud @object (mussä Boindr sein) zuä Boindr uffn Objekt vondn Dühp @to + * umwürschn, der dannoo ähne Undrglasse vondn Dühp von @object sein muss. + * @field is das Feld von @to, dasdawelsches @object enthaldn dud. + * + * Baispiel: + * struct cdi_driver* driver; + * struct cdi_storage_driver* storage_driver = + * CDI_UPCAST(driver, struct cdi_storage_driver, drv); + * \endthuringiansaxonian + * \french + * Convertit @object (qui doit être un pointeur) en un pointeur à un objet du + * type @to, qui doit être une sous-classe du type d'@objet. @field est + * l'inscription de @to qui est @object. + * + * Exemple: + * struct cdi_driver* driver; + * struct cdi_storage_driver* storage_driver = + * CDI_UPCAST(driver, struct cdi_storage_driver, drv); + * \endfrench + * \japanese + * ?????@object????@to??????????????@to?@object? + * ????????????????@field?@to?@object?????????? + * + * ?? + * struct cdi_driver* driver; + * struct cdi_storage_driver* storage_driver = + * CDI_UPCAST(driver, struct cdi_storage_driver, drv); + * \endjapanese + */ +#define CDI_UPCAST(object, to, field) \ + /* Type compatibility check */ \ + ((void)((object) - &((to*)0)->field), \ + /* The cast itself */ \ + (to*)((char*)(object) - (char*)&((to*)0)->field)) + +/** + * \german + * Funktionen zur Endiannesskonvertierung. Es sei darauf hingewiesen, dass die + * meisten (wenn nicht alle) CDI-Treiber nur Little-Endian-Plattformen als Host + * unterstützen. + * \endgerman + * \english + * Functions for endianness conversion. Be aware that most if not all CDI + * drivers only support little endian machines as a host. + * \endenglish + * \thuringiansaxonian + * Fungzschjounen fürde Endschjannessumwandlung. Passe uff, fast wenn ni gar + * wirklisch alle CDI-Draibr gönnn nur uff Liddl-Endschjan-Blattform loofn. + * \endthuringiansaxonian + * \french + * Fonctions pour la conversion boutisme. Au fait, la plupart si pas tous des + * pilotes CDI ne peuvent que fonctionner sur une plateforme petit-boutiste. + * \endfrench + * \japanese + * ??????????????????????????????????? + * CDI??????????????????????????????? + * \endjapanese + */ + +static inline uint16_t cdi_be16_to_cpu(uint16_t x) +{ + return (x >> 8) | (x << 8); +} + +static inline uint32_t cdi_be32_to_cpu(uint32_t x) +{ + return ((uint32_t)cdi_be16_to_cpu(x) << 16) | (uint32_t)cdi_be16_to_cpu(x >> 16); +} + +static inline uint64_t cdi_be64_to_cpu(uint64_t x) +{ + return ((uint64_t)cdi_be32_to_cpu(x) << 32) | (uint64_t)cdi_be32_to_cpu(x >> 32); +} + +static inline uint16_t cdi_cpu_to_be16(uint16_t x) +{ + return cdi_be16_to_cpu(x); +} + +static inline uint32_t cdi_cpu_to_be32(uint32_t x) +{ + return cdi_be32_to_cpu(x); +} + +static inline uint64_t cdi_cpu_to_be64(uint64_t x) +{ + return cdi_be64_to_cpu(x); +} + +static inline uint16_t cdi_le16_to_cpu(uint16_t x) +{ + return x; +} + +static inline uint32_t cdi_le32_to_cpu(uint32_t x) +{ + return x; +} + +static inline uint64_t cdi_le64_to_cpu(uint64_t x) +{ + return x; +} + +static inline uint16_t cdi_cpu_to_le16(uint16_t x) +{ + return cdi_le16_to_cpu(x); +} + +static inline uint32_t cdi_cpu_to_le32(uint32_t x) +{ + return cdi_le32_to_cpu(x); +} + +static inline uint64_t cdi_cpu_to_le64(uint64_t x) +{ + return cdi_le64_to_cpu(x); +} + #ifdef __cplusplus }; // extern "C" #endif -- 2.6.3 From max at tyndur.org Sun Dec 13 00:25:55 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:25:55 +0100 Subject: [tyndur-devel] [PATCH 05/22] cdi/misc: Add cdi_elapsed_ms() In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-6-git-send-email-max@tyndur.org> + Add cdi_elapsed_ms() + Add CDI_CONDITION_WAIT() and CDI_CONDITION_WAIT_SLEEP() macros for waiting until a volatile condition is fulfilled or a timeout occurs Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi/misc.h | 87 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/src/modules/cdi/include/cdi/misc.h b/src/modules/cdi/include/cdi/misc.h index b0973e6..9676b4a 100644 --- a/src/modules/cdi/include/cdi/misc.h +++ b/src/modules/cdi/include/cdi/misc.h @@ -87,6 +87,93 @@ void cdi_sleep_ms(uint32_t ms); /** * \german + * Gibt die Anzahl der vergangenen Millisekunden seit einem + * implementationsspezifischen Zeitpunkt zurück, mindestens aber seit dem ersten + * Aufruf dieser Funktion. Die Auflösung ist ebenfalls + * implementationsspezifisch. Der Rückgabewert muss monoton wachsen (bis er + * wegen der Begrenzung des Rückgabetyps überläuft). + * \endgerman + * \english + * Returns the number of milliseconds elapsed since an implementation specific + * point in time, not later than the first call of this function. The resolution + * too is implementation specific. The return value must grow monotonically + * (until it overflows due to the return type's limit). + * \endenglish + * \thuringiansaxonian + * Dud zurückgehm, wie lange (in Millisegundn) ä imblämändazschjounsspäzifschr + * Zeitbunkt nu schon her is. Diesr Zeitbunkt muss oor vorm erstn Uffruf diesr + * Fungzschjoun lieschn. De Ufflösung is ooch imblämändazschjounsspäzifsch. Dr + * Rückgabewert muss monodohn wachsn (bissr wäschn dä Begrenzung vondn + * Rückgabedühp übrloofn dud). + * \endthuringiansaxonian + * \french + * Rend le nombre des millisecondes qui ont passé depuis un instant défini par + * implémentation, au plus tard depuis le premier appel de cette fonction. La + * résolution aussi est défini par implémentation. La valeur rendue doit grandir + * monotonement (jusqu'elle dépasse par la limitation du type rendu). + * \endfrench + * \japanese + * ??????????????????????????????????? + * ?????????????????????????????????? + * ????????????????????????? + * \endjapanese + */ +uint64_t cdi_elapsed_ms(void); + +/** + * \german + * Wartet, bis die Bedingung @cond erfüllt ist oder mehr als @timeout + * Millisekunden vergangen sind. + * \endgerman + * \english + * Waits until the condition @cond yields true or more than @timeout + * milliseconds have passed. + * \endenglish + * \thuringiansaxonian + * Dud wortn, bisde Bedingung @cond erfüllt is oodr über @timeout Millisekundn + * vorbei sin. + * \endthuringiansaxonian + * \french + * Attend que la condition @cond est satisfaite ou plus que @timeout + * millisecondes sont passées. + * \endgerman + * \japanese + * ??@cond??????@timeout??????? + * \endjapanese + */ +#define CDI_CONDITION_WAIT(cond, timeout) \ + do { \ + uint64_t start = cdi_elapsed_ms(); \ + while (!(cond) && cdi_elapsed_ms() - start < timeout); \ + } while (0) + +/** + * \german + * CDI_CONDITION_WAIT() mit cdi_sleep_ms(@sleep) zwischen jeder Auswertung. + * \endgerman + * \english + * CDI_CONDITION_WAIT() with cdi_sleep_ms(@sleep) between each evaluation. + * \endenglish + * \thuringiansaxonian + * CDI_CONDITION_WAIT() mittä cdi_sleep_ms(@sleep) zwischn jädr Auswärdungn. + * \endthuringiansaxonian + * \french + * CDI_CONDITION_WAIT() avec cdi_sleep_ms(@sleep) entre toutes les évaluations. + * \endfrench + * \japanese + * ????????cdi_sleep_ms(@sleep)?????CDI_CONDITION_WAIT()? + * \endjapanese + */ +#define CDI_CONDITION_WAIT_SLEEP(cond, timeout, sleep) \ + do { \ + uint64_t start = cdi_elapsed_ms(); \ + while (!(cond) && cdi_elapsed_ms() - start < timeout) { \ + cdi_sleep_ms(sleep); \ + } \ + } while (0) + +/** + * \german * Castet @object (muss ein Pointer sein) zu einem Pointer auf ein Objekt des * Typs @to, welcher eine Unterklasse vom Typ von @object sein muss. @field ist * das Feld von @to, das @object enthält. -- 2.6.3 From max at tyndur.org Sun Dec 13 00:25:56 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:25:56 +0100 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_06/22=5D_cdi/scsi=2C_cdi=2Eh=3A?= =?utf-8?q?_Remove_t=C3=BDndur-specific_fields?= In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-7-git-send-email-max@tyndur.org> - Remove týndur-specific fields from non-OS-dependant structures Signed-off-by: Max Reitz Acked-by: Kevin Wolf Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi.h | 3 --- src/modules/cdi/include/cdi/scsi.h | 7 ------- 2 files changed, 10 deletions(-) diff --git a/src/modules/cdi/include/cdi.h b/src/modules/cdi/include/cdi.h index a9207db..998e080 100644 --- a/src/modules/cdi/include/cdi.h +++ b/src/modules/cdi/include/cdi.h @@ -178,9 +178,6 @@ struct cdi_device { * \endenglish */ struct cdi_bus_data* bus_data; - - // tyndur-spezifisch - void* backdev; }; /** diff --git a/src/modules/cdi/include/cdi/scsi.h b/src/modules/cdi/include/cdi/scsi.h index 8edccbf..cef96ed 100644 --- a/src/modules/cdi/include/cdi/scsi.h +++ b/src/modules/cdi/include/cdi/scsi.h @@ -51,13 +51,6 @@ struct cdi_scsi_device { /// Geraetetyp, der ueber SCSI angesteuert wird cdi_device_type_t type; - - /** - * tyndur-spezifisch: Frontend-Geraet (z.B. CDI_STORAGE-Geraet, das ueber - * SCSI angesteuert wird) - */ - void* frontdev; - }; /// SCSI-Treiber -- 2.6.3 From max at tyndur.org Sun Dec 13 00:25:57 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:25:57 +0100 Subject: [tyndur-devel] [PATCH 07/22] cdi/scsi: Add support for multiple LUNs In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-8-git-send-email-max@tyndur.org> + One SCSI device may provide multiple LUNs. Add a field for the LUN count to cdi_scsi_device. + Furthermore, add a field to select the LUN to cdi_scsi_packet. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi/scsi.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/modules/cdi/include/cdi/scsi.h b/src/modules/cdi/include/cdi/scsi.h index cef96ed..5157a23 100644 --- a/src/modules/cdi/include/cdi/scsi.h +++ b/src/modules/cdi/include/cdi/scsi.h @@ -24,6 +24,9 @@ /// SCSI-Paket struct cdi_scsi_packet { + /// LUN to address + int lun; + /// Buffer zum Senden oder Empfangen von Daten void *buffer; @@ -51,6 +54,9 @@ struct cdi_scsi_device { /// Geraetetyp, der ueber SCSI angesteuert wird cdi_device_type_t type; + + /// Number of LUNs + int lun_count; }; /// SCSI-Treiber -- 2.6.3 From max at tyndur.org Sun Dec 13 00:25:58 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:25:58 +0100 Subject: [tyndur-devel] [PATCH 08/22] cdi.h: Add cdi_handle_bus_device() In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-9-git-send-email-max@tyndur.org> + Currently, all devices provided on a bus have to be passed to the init_device() function of all drivers handling any device type on that bus. cdi_handle_bus_device() can be used by the driver to specify a pattern (struct cdi_bus_device_pattern) which allows the CDI implementation to speed up the selection of the correct driver for a new device. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi.h | 114 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/modules/cdi/include/cdi.h b/src/modules/cdi/include/cdi.h index 998e080..8ed0738 100644 --- a/src/modules/cdi/include/cdi.h +++ b/src/modules/cdi/include/cdi.h @@ -142,6 +142,51 @@ struct cdi_bus_data { /** * \german + * Diese von jedem sie unterstützenden Bustyp zu erweiternde Struktur enthält + * Informationen darüber, welche Geräte von einem bestimmten Treiber verwaltet + * werden können. + * Typischerweise enthält sie einige der von der zugehörigen cdi_bus_data- + * Struktur bereitgestellten Felder (jene, die genutzt werden, um ein Gerät zu + * identifizieren, wie Geräte- und Hersteller-ID), wobei negative Werte "egal" + * bedeuten. + * \endgerman + * \english + * This structure, which is extended by any bus type supporting it, contains + * information about which devices can be handled by a certain driver. + * Typically, it will contain some of the fields provided by the appropriate + * cdi_bus_data structure (the ones which are used to identify a device like + * device/vendor ID) where negative values signify a "don't care". + * \endenglish + * \thuringiansaxonian + * Diese Schdruggtur wird von jehm Busdühp, der se unnerstützn gönnn dut, + * erweidert, und hat dann Informatzschjoun darübber, welsche Geräide vonnä + * bestimmdn Draibr unnr de Fiddische genomm wern gönnn. + * Tübbischerweise endhältse ä bår vondn Feldrn, die ooch in dr zugehörschn + * cdi_bus_data-Schdruggtur sin (welsche de nimmst, damitte weest, was für ä + * Geräit das nu eigndlisch is, zum Beispiel Geräide- un Herschdellor- + * Idendifikatzschjoun). Negadive Werde dun dann "egal" bedeudn. + * \endthuringiansaxonian + * \french + * Cette structure, qui est enrichie par tous les types qui le peuvent, décrit + * quels appareils un particulier pilote peut employer. + * Régulièrement, il y a quelques valeurs de la cdi_bus_data structure adéquate + * (qui sont utilisées pour identifier un appareil, comme l'ID de l'appareil et + * du vendeur), où des valeurs négatives signifient "n'importe quoi". + * \endfrench + * \japanese + * ??????????????????????????????????? + * ???????????? + * ???????cdi_bus_data???????????????????????? + * ????????????????????????????????????ID? + * ????????????????????? + * \endjapanese + */ +struct cdi_bus_device_pattern { + cdi_device_type_t bus_type; +}; + +/** + * \german * Beschreibt ein Gerät * \endgerman * \english @@ -406,6 +451,75 @@ int cdi_provide_device(struct cdi_bus_data* device); int cdi_provide_device_internal_drv(struct cdi_bus_data* device, struct cdi_driver* driver); +/** + * \german + * Teilt der CDI-Implementierung mit, dass der Treiber @drv nur Geräte vom + * Muster @pattern akzeptiert (die am Bus @pattern->bus_type hängen). Diese + * Funktion darf mehrfach aufgerufen werden. Jedes neue Gerät am Bus, das nicht + * auf das Muster passt, wird der @drv->init_device()-Funktion nicht übergeben. + * Diese Funktion darf andererseits aber NULL zurückgeben, selbst wenn + * cdi_handle_bus_device() vom Treiber benutzt wurde, das heißt, diese Funktion + * darf mit einem Muster aufgerufen werden, das nicht nur auf Geräte passt, die + * vom Treiber behandelt werden können, sondern auch auf andere (die dann + * abgewiesen werden können, indem @drv->init_device() NULL zurückgibt). + * + * Diese Funktion darf nur in @drv->init_driver() aufgerufen werden. + * \endgerman + * \english + * Tells the CDI implementation that the driver @drv is able to handle devices + * with the pattern @pattern (appearing on the bus @pattern->bus_type). This + * function can be called multiple times. Any device appearing on the bus not + * fitting the pattern will not be passed to the @drv->init_device() function. + * That function may however return NULL even after the driver has called + * cdi_handle_bus_device(), that is, it is valid to call this function with a + * @pattern that does not only fit devices handled by @drv but others as well + * (which can then be rejected by letting @drv->init_device() return NULL). + * + * This function must only be called from @drv->init_driver(). + * \endenglish + * \thuringiansaxonian + * Dailt dr CDI-Imblämändierung mit, dass dr Draibr @drv nur an Gelumbsch mibm + * Musdr @pattern rummehrn kann (die andn Bus @pattern->bus_type sin). Diese + * Fungzschjoun gann oo mehrfach uffgerufn wern. Wenn ä neues Geräit am Bus + * uffdoochn dut und das ni uffs Musdr bassn dut, dann gommt das bei dr + * @drv->init_device()-Fungzschjoun oo ni an. Uff dr annern Saide darf diese + * Fungzschjoun wiederum NULL zurückgehm, sogar wenner Draibr vorher + * cdi_handle_bus_device() uffgerufn hat, das heißt, diese Fungzschjoun gann + * ooch middä Musdr uffgerufn wern, das ni nur uff Geräide bassn dud, die middä + * Draibr was zu dun hamm, sonnern ooch uff annere (eene Möjlischgeit die + * abzulehn isses dann, in @drv->init_device() NULL zurückzugehm). + * + * Diese Fungzschjoun gannsde nirschndwo sonst als in @drv->init_driver() + * uffrufn. + * \endthuringiansaxonian + * \french + * Informe la CDI implémentation que le pilote @drv ne peut qu'employer des + * appareils du type @pattern (qui se présentent au bus @pattern->bus_type). On + * peut appeler cette fonction à plus d'une fois. Tous les appareils qui se + * présentent au bus qui ne correspondent pas au type ne seront pas transmises + * au fonction @drv->init_device(). À l'inverse, cette fonction peut rendre NULL + * même que le pilote a appelé cdi_handle_bus_device(), c'est-à-dire que on peut + * appeler cette fonction avec un type qui ne corresponde pas seulement aux + * appareils qui le pilote peut employer, mais en plus au des autres (qui on + * peut renvoyer par faire @drv->init_device() rendre NULL). + * + * Cette fonction doit être appeler seulement en @drv->init_driver(). + * \endfrench + * \japanese + * ?????@drv?????@pattern?????????????????? + * CDI???????????????????@pattern->bus_type?????? + * ????????????????????????????????????? + * @drv->init_device()???????cdi_handle_bus_device()????????? + * @drv->init_device()??NULL?????????????????????? + * ????????????????????????????????? + * ??????@drv->init_device()??NULL??????????? + * + * @drv->init_driver()??????????????????????? + * \endjapanese + */ +void cdi_handle_bus_device(struct cdi_driver* drv, + struct cdi_bus_device_pattern* pattern); + #endif /*\@}*/ -- 2.6.3 From max at tyndur.org Sun Dec 13 00:25:59 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:25:59 +0100 Subject: [tyndur-devel] [PATCH 09/22] rtl8168b: Add driver In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-10-git-send-email-max@tyndur.org> + I cannot remember how well this driver works, but it is certainly better than having none at all. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- src/modules/cdi/rtl8168b/include/rtl8168b.h | 143 ++++++++++ src/modules/cdi/rtl8168b/main.c | 70 +++++ src/modules/cdi/rtl8168b/rtl8168b.c | 409 ++++++++++++++++++++++++++++ 3 files changed, 622 insertions(+) create mode 100644 src/modules/cdi/rtl8168b/include/rtl8168b.h create mode 100644 src/modules/cdi/rtl8168b/main.c create mode 100644 src/modules/cdi/rtl8168b/rtl8168b.c diff --git a/src/modules/cdi/rtl8168b/include/rtl8168b.h b/src/modules/cdi/rtl8168b/include/rtl8168b.h new file mode 100644 index 0000000..a038f7a --- /dev/null +++ b/src/modules/cdi/rtl8168b/include/rtl8168b.h @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2007 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Patrick Pokatilo. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _RTL8168B_H_ +#define _RTL8168B_H_ + +#include + +#include "cdi.h" +#include "cdi/mem.h" +#include "cdi/net.h" +#include "cdi/pci.h" + +#define REG_ID0 0x00 +#define REG_TRANSMIT_DESCRIPTORS 0x20 +#define REG_COMMAND 0x37 +#define REG_TRANSMIT_POLL 0x38 +#define REG_INTERRUPT_MASK 0x3C +#define REG_INTERRUPT_STATUS 0x3E +#define REG_TRANSMIT_CONFIGURATION 0x40 +#define REG_RECEIVE_CONFIGURATION 0x44 +#define REG_PHY_ACCESS 0x60 +#define REG_PHY_DATA 0x60 +#define REG_PHY_ADDRESS 0x62 +#define REG_PHY_RW 0x63 +#define REG_PHY_STATUS 0x6C +#define REG_RECV_MAX_SIZE 0xDA +#define REG_CP_COMMAND 0xE0 +#define REG_RECEIVE_DESCRIPTORS 0xE4 +#define REG_MAX_TRANS_SIZE 0xEC + +#define CR_RESET (1 << 4) +#define CR_RECEIVER_ENABLE (1 << 3) +#define CR_TRANSMITTER_ENABLE (1 << 2) + +#define CPCR_VLAN_DETAG (1 << 6) +#define CPCR_CHECKSUM_OFFLOAD (1 << 5) + +#define TPR_PACKET_WAITING (1 << 6) + +#define TCR_IFG_STANDARD (3 << 24) +#define TCR_MXDMA_512 (5 << 8) +#define TCR_MXDMA_1024 (6 << 8) +#define TCR_MXDMA_UNLIMITED (7 << 8) + +#define RCR_NO_FIFO_THRESHOLD (7 << 13) +#define RCR_MXDMA_512 (5 << 8) +#define RCR_MXDMA_1024 (6 << 8) +#define RCR_MXDMA_UNLIMITED (7 << 8) +#define RCR_ACCEPT_BROADCAST (1 << 3) +#define RCR_ACCEPT_MULTICAST (1 << 2) +#define RCR_ACCEPT_PHYS_MATCH (1 << 1) + +#define ISR_LINK_CHANGE (1 << 5) +#define ISR_RECEIVE_BUFFER_OVERFLOW (1 << 4) +#define ISR_TRANSMIT_OK (1 << 2) +#define ISR_RECEIVE_OK (1 << 0) + +#define PA_RW (1 << 31) + +#define PHY_REG_BMCR 0x00 + +#define PHY_BMCR_RESET (1 << 15) +#define PHY_BMCR_AUTO_NEGOTIATION (1 << 12) + +#define DC_OWN (1 << 31) +#define DC_EOR (1 << 30) +#define DC_FS (1 << 29) +#define DC_LS (1 << 28) +#define DC_LENGTH 0x3FFF + +#define PS_1000 (1 << 4) +#define PS_100 (1 << 3) +#define PS_10 (1 << 2) +#define PS_LINK (1 << 1) +#define PS_FULLDUP (1 << 0) + +#define PHYS(netcard, field) \ + (netcard->phys + offsetof(struct rtl8168b_device, field)) + +#define TX_BUFFER_SIZE (0x1000 & DC_LENGTH) +#define RX_BUFFER_SIZE (0x2000 & DC_LENGTH) + +#define TX_BUFFER_COUNT 16 +#define RX_BUFFER_COUNT 16 + +struct rtl8168b_descriptor +{ + volatile uint32_t command; + uint32_t vlan; + uint64_t address; +}; + +struct rtl8168b_device { + struct cdi_net_device net; + void* mmio; + + int tx_index; + int rx_index; + + struct cdi_mem_area* tx_buffer_area; + struct cdi_mem_area* rx_buffer_area; + + struct rtl8168b_descriptor* tx_buffer; + uintptr_t tx_buffer_phys; + struct cdi_mem_area* tx_area[TX_BUFFER_COUNT]; + + struct rtl8168b_descriptor* rx_buffer; + uintptr_t rx_buffer_phys; + struct cdi_mem_area* rx_area[RX_BUFFER_COUNT]; +}; + +struct cdi_device* rtl8168b_init_device(struct cdi_bus_data* bus_data); +void rtl8168b_remove_device(struct cdi_device* device); +void rtl8168b_send_packet(struct cdi_net_device* device, + void* data, size_t size); + +#endif diff --git a/src/modules/cdi/rtl8168b/main.c b/src/modules/cdi/rtl8168b/main.c new file mode 100644 index 0000000..1bbf8ff --- /dev/null +++ b/src/modules/cdi/rtl8168b/main.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2007 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Patrick Pokatilo. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include "cdi/net.h" +#include "cdi/pci.h" +#include "cdi/misc.h" + +#include "rtl8168b.h" + +#define DRIVER_NAME "rtl8168b" + +static struct cdi_net_driver rtl8168b_driver; + +static int rtl8168b_driver_init(void) +{ + cdi_net_driver_init(&rtl8168b_driver); + + return 0; +} + +static int rtl8168b_driver_destroy(void) +{ + cdi_net_driver_destroy(&rtl8168b_driver); + + return 0; +} + +static struct cdi_net_driver rtl8168b_driver = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_NETWORK, + .bus = CDI_PCI, + .init = rtl8168b_driver_init, + .destroy = rtl8168b_driver_destroy, + .init_device = rtl8168b_init_device, + .remove_device = rtl8168b_remove_device, + }, + + .send_packet = rtl8168b_send_packet, +}; + +CDI_DRIVER(DRIVER_NAME, rtl8168b_driver) diff --git a/src/modules/cdi/rtl8168b/rtl8168b.c b/src/modules/cdi/rtl8168b/rtl8168b.c new file mode 100644 index 0000000..ac88eb4 --- /dev/null +++ b/src/modules/cdi/rtl8168b/rtl8168b.c @@ -0,0 +1,409 @@ +/* + * Copyright (c) 2007 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Patrick Pokatilo and Max Reitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "rtl8168b.h" + +#define DEBUG_MSG(s) \ + printf("[RTL8111B/8168B] debug: %s() '%s'\n", __FUNCTION__, s) +#define DEVICE_MSG printf + +static void rtl8168b_handle_interrupt(struct cdi_device* device); + +static inline void write_register_8(struct rtl8168b_device* netcard, + uint8_t reg, uint8_t value) +{ + *(volatile uint8_t *)((uintptr_t)netcard->mmio + reg) = value; +} + +static inline void write_register_16(struct rtl8168b_device* netcard, + uint8_t reg, uint16_t value) +{ + *(volatile uint16_t *)((uintptr_t)netcard->mmio + reg) = value; +} + +static inline void write_register_32(struct rtl8168b_device* netcard, + uint8_t reg, uint32_t value) +{ + *(volatile uint32_t *)((uintptr_t)netcard->mmio + reg) = value; +} + +static inline void write_register_64(struct rtl8168b_device* netcard, + uint8_t reg, uint64_t value) +{ + *(volatile uint64_t *)((uintptr_t)netcard->mmio + reg) = value; +} + +static inline uint8_t read_register_8(struct rtl8168b_device* netcard, + uint8_t reg) +{ + return *(volatile uint8_t *)((uintptr_t)netcard->mmio + reg); +} + +static inline uint16_t read_register_16(struct rtl8168b_device* netcard, + uint8_t reg) +{ + return *(volatile uint16_t *)((uintptr_t)netcard->mmio + reg); +} + +static inline uint32_t read_register_32(struct rtl8168b_device* netcard, + uint8_t reg) +{ + return *(volatile uint32_t *)((uintptr_t)netcard->mmio + reg); +} + +static inline uint64_t read_register_64(struct rtl8168b_device* netcard, + uint8_t reg) +{ + return *(volatile uint64_t *)((uintptr_t)netcard->mmio + reg); +} + + +struct cdi_device* rtl8168b_init_device(struct cdi_bus_data* bus_data) +{ + struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data; + struct rtl8168b_device* netcard; + struct cdi_mem_area* buf; + + if ((pci->vendor_id != 0x10ec) || (pci->device_id != 0x8168)) { + return NULL; + } + + + buf = cdi_mem_alloc(sizeof(*netcard), CDI_MEM_VIRT_ONLY | CDI_MEM_NOINIT); + + if (!buf) { + return NULL; + } + + + netcard = buf->vaddr; + + memset(netcard, 0, sizeof(*netcard)); + + + netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; + + DEBUG_MSG("Interrupthandler registrieren"); + cdi_register_irq(pci->irq, rtl8168b_handle_interrupt, &netcard->net.dev); + cdi_pci_alloc_memory(pci); + + + struct cdi_pci_resource* res; + for (int i = 0; (res = cdi_list_get(pci->resources, i)); i++) { + if ((res->type == CDI_PCI_MEMORY) && (res->index == 2)) { + netcard->mmio = res->address; + break; + } + } + + + DEBUG_MSG("Reset der Karte"); + write_register_16(netcard, REG_CP_COMMAND, CPCR_VLAN_DETAG); + + write_register_8(netcard, REG_COMMAND, CR_RESET); + + while ((read_register_8(netcard, REG_COMMAND) & CR_RESET) == CR_RESET); + + write_register_16(netcard, REG_RECV_MAX_SIZE, 1518); + write_register_16(netcard, REG_MAX_TRANS_SIZE, 0x0c); + + DEBUG_MSG("Aktiviere Rx/Tx"); + write_register_8(netcard, REG_COMMAND, + CR_RECEIVER_ENABLE | CR_TRANSMITTER_ENABLE); + + DEBUG_MSG("MAC-Adresse auslesen"); + netcard->net.mac = + read_register_64(netcard, REG_ID0) & 0xffffffffffffLL; + + DEBUG_MSG("Setze Interruptmaske"); + write_register_16(netcard, REG_INTERRUPT_STATUS, 0x4fff); + write_register_16(netcard, REG_INTERRUPT_MASK, 0x0025); + + DEBUG_MSG("Initialisiere PHY"); + write_register_32(netcard, REG_PHY_ACCESS, + PA_RW | (PHY_REG_BMCR << 16) | PHY_BMCR_RESET); + + while (read_register_32(netcard, REG_PHY_ACCESS) & PA_RW); + + do { + write_register_16(netcard, REG_PHY_ADDRESS, PHY_REG_BMCR); + + while (!(read_register_8(netcard, REG_PHY_RW) >> 7)); + } while (read_register_16(netcard, REG_PHY_DATA) & PHY_BMCR_RESET); + + write_register_32(netcard, REG_PHY_ACCESS, + PA_RW | (PHY_REG_BMCR << 16) | PHY_BMCR_AUTO_NEGOTIATION); + + while (read_register_32(netcard, REG_PHY_ACCESS) & PA_RW); + + + DEVICE_MSG("Lege Deskriptoren an"); + netcard->tx_buffer_area = + cdi_mem_alloc(TX_BUFFER_COUNT * sizeof(struct rtl8168b_descriptor), + CDI_MEM_PHYS_CONTIGUOUS | 8); + netcard->tx_buffer = netcard->tx_buffer_area->vaddr; + netcard->tx_buffer_phys = netcard->tx_buffer_area->paddr.items[0].start; + + netcard->rx_buffer_area = + cdi_mem_alloc(RX_BUFFER_COUNT * sizeof(struct rtl8168b_descriptor), + CDI_MEM_PHYS_CONTIGUOUS | 8); + netcard->rx_buffer = netcard->rx_buffer_area->vaddr; + netcard->rx_buffer_phys = netcard->rx_buffer_area->paddr.items[0].start; + + int i; + for (i = 0; i < RX_BUFFER_COUNT; i++) { + memset(&netcard->rx_buffer[i], 0, sizeof(struct rtl8168b_descriptor)); + + netcard->rx_area[i] = + cdi_mem_alloc(RX_BUFFER_SIZE, CDI_MEM_PHYS_CONTIGUOUS | 3); + netcard->rx_buffer[i].address = + netcard->rx_area[i]->paddr.items[0].start; + netcard->rx_buffer[i].command = DC_OWN | RX_BUFFER_SIZE; + + if (i == RX_BUFFER_COUNT - 1) { + netcard->rx_buffer[i].command |= DC_EOR; + } + } + + if (i == 0) { + DEVICE_MSG("[RTL8111B/8168B] Keine Buffer zum Empfangen von Daten!\n"); + write_register_8(netcard, REG_COMMAND, + read_register_8(netcard, REG_COMMAND) & ~CR_RECEIVER_ENABLE); + } + + for (i = 0; i < TX_BUFFER_COUNT; i++) { + memset(&netcard->tx_buffer[i], 0, sizeof(struct rtl8168b_descriptor)); + + netcard->tx_area[i] = + cdi_mem_alloc(TX_BUFFER_SIZE, CDI_MEM_PHYS_CONTIGUOUS | 3); + netcard->tx_buffer[i].address = + netcard->tx_area[i]->paddr.items[0].start; + netcard->tx_buffer[i].command = 0; + + if (i == TX_BUFFER_COUNT - 1) { + netcard->tx_buffer[i].command |= DC_EOR; + } + } + + if (i == 0) { + DEVICE_MSG("[RTL8111B/8168B] Keine Buffer zum Senden von Daten!\n"); + write_register_8(netcard, REG_COMMAND, + read_register_8(netcard, REG_COMMAND) & ~CR_TRANSMITTER_ENABLE); + } + + DEBUG_MSG("Initialisiere Buffer"); + write_register_64(netcard, REG_RECEIVE_DESCRIPTORS, + netcard->rx_buffer_phys); + write_register_64(netcard, REG_TRANSMIT_DESCRIPTORS, + netcard->tx_buffer_phys); + + DEBUG_MSG("Initialisiere RCR/TCR"); + write_register_32(netcard, REG_RECEIVE_CONFIGURATION, + RCR_MXDMA_UNLIMITED | RCR_NO_FIFO_THRESHOLD | RCR_ACCEPT_BROADCAST | + RCR_ACCEPT_PHYS_MATCH); + write_register_32(netcard, REG_TRANSMIT_CONFIGURATION, + TCR_MXDMA_UNLIMITED | TCR_IFG_STANDARD); + + cdi_net_device_init(&netcard->net); + + DEBUG_MSG("Fertig initialisiert"); + return &netcard->net.dev; +} + +void rtl8168b_remove_device(struct cdi_device* device) +{ + struct rtl8168b_device* netcard = (struct rtl8168b_device*) device; + int i; + + cdi_pci_free_memory((struct cdi_pci_device*) netcard->net.dev.bus_data); + + cdi_mem_free(netcard->tx_buffer_area); + cdi_mem_free(netcard->rx_buffer_area); + + for (i = 0; i < RX_BUFFER_COUNT; i++) { + netcard->rx_buffer[i].command &= 0x40000000; + netcard->rx_buffer[i].address = (uintptr_t) NULL; + + cdi_mem_free(netcard->rx_area[i]); + } + + for (i = 0; i < TX_BUFFER_COUNT; i++) { + netcard->tx_buffer[i].command &= 0x40000000; + netcard->tx_buffer[i].address = (uintptr_t) NULL; + + cdi_mem_free(netcard->tx_area[i]); + } +} + +void rtl8168b_send_packet(struct cdi_net_device* device, + void *data, size_t size) +{ + struct rtl8168b_device* netcard = (struct rtl8168b_device*) device; + + if (size > TX_BUFFER_SIZE) { + DEVICE_MSG("[RTL8111B/8168B] Paket zu groß!\n"); + + return; + } + + int i; + + do { + i = netcard->tx_index; + } while (!__sync_bool_compare_and_swap(&netcard->tx_index, i, + (i + 1) % TX_BUFFER_COUNT)); + + if (netcard->tx_buffer[i].command & DC_OWN) { + DEBUG_MSG("Tx-Buffer ist schon besetzt"); + return; + } + + + memcpy(netcard->tx_area[i]->vaddr, data, size); + netcard->tx_buffer[i].vlan = 0; + + DEVICE_MSG("[RTL8111B/8168B] Sende via Deskriptor %i\n", i); + + if (i == TX_BUFFER_COUNT - 1) { + netcard->tx_buffer[i].command = size | DC_FS | DC_LS | DC_OWN | DC_EOR; + } else { + netcard->tx_buffer[i].command = size | DC_FS | DC_LS | DC_OWN; + } + + write_register_8(netcard, REG_TRANSMIT_POLL, TPR_PACKET_WAITING); +} + + +static void rtl8168b_unown_rx_desc(struct rtl8168b_device* netcard, int i) +{ + if (i == RX_BUFFER_COUNT - 1) { + netcard->rx_buffer[i].command = RX_BUFFER_SIZE | DC_OWN | DC_EOR; + } else { + netcard->rx_buffer[i].command = RX_BUFFER_SIZE | DC_OWN; + } +} + +static void rtl8168b_handle_receive_ok(struct rtl8168b_device* netcard) +{ + int i; + + do { + i = netcard->rx_index; + } while (!__sync_bool_compare_and_swap(&netcard->rx_index, i, + (i + 1) % RX_BUFFER_COUNT)); + + if (netcard->rx_buffer[i].command & DC_OWN) { + netcard->rx_index = i; + + if (!(netcard->rx_buffer[0].command & DC_OWN)) { + // FIXME: Make atomic + i = 0; + netcard->rx_index = 1; + } else { + DEVICE_MSG("[RTL8111B/8168B] Empfangsdeskriptor sollte %i sein, " + "dort ist aber nichts.\n", i); + return; + } + } + + + size_t sz = netcard->rx_buffer[i].command & DC_LENGTH; + + DEVICE_MSG("[RTL8111B/8168B] Empfange %i B von Deskriptor %i\n", + (int)sz - 4, i); + + if (sz < 5) + { + rtl8168b_unown_rx_desc(netcard, i); + return; + } + + cdi_net_receive((struct cdi_net_device*) netcard, + netcard->rx_area[i]->vaddr, sz - 4); + + rtl8168b_unown_rx_desc(netcard, i); +} + +static void rtl8168b_handle_transmit_ok(struct rtl8168b_device* netcard) +{ + (void) netcard; +} + +static void rtl8168b_handle_link_change(struct rtl8168b_device* netcard) +{ + uint8_t status = read_register_8(netcard, REG_PHY_STATUS); + + if ((status & PS_LINK) != PS_LINK) { + DEVICE_MSG("[RTL8111B/8168B] Link down\n"); + } else { + uint16_t speed = (status & PS_FULLDUP) << 15; + + if (status & PS_1000) { + speed |= 1000; + } else if (status & PS_100) { + speed |= 100; + } else if (status & PS_10) { + speed |= 10; + } + + DEVICE_MSG("[RTL8111B/8168B] Link up (%uMb/s %s-duplex)\n", + speed & 0x7FFF, (speed & 0x8000) ? "Full" : "Half"); + } +} + +static void rtl8168b_handle_interrupt(struct cdi_device* device) +{ + struct rtl8168b_device* netcard = (struct rtl8168b_device*) device; + uint16_t isr = read_register_16(netcard, REG_INTERRUPT_STATUS); + + write_register_16(netcard, REG_INTERRUPT_STATUS, isr); + + DEVICE_MSG("[RTL8111B/8168B] Interrupt: %x\n", isr); + + if (isr & ISR_RECEIVE_OK) { + rtl8168b_handle_receive_ok(netcard); + } + + if (isr & ISR_TRANSMIT_OK) { + rtl8168b_handle_transmit_ok(netcard); + } + + if (isr & ISR_LINK_CHANGE) { + rtl8168b_handle_link_change(netcard); + } +} -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:00 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:00 +0100 Subject: [tyndur-devel] [PATCH 10/22] ata: Temporary buffer for ATAPI commands In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-11-git-send-email-max@tyndur.org> ! ATAPI commands need to be of size 12. However, general SCSI packets may have any length and thus the SCSI command to be sent has to be copied to a temporary buffer before it can be passed to the ATAPI device. Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/ata/atapi.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/modules/cdi/ata/atapi.c b/src/modules/cdi/ata/atapi.c index 31c7263..78f7f29 100644 --- a/src/modules/cdi/ata/atapi.c +++ b/src/modules/cdi/ata/atapi.c @@ -28,6 +28,7 @@ #include #include +#include #include "cdi.h" #include "cdi/storage.h" @@ -92,6 +93,12 @@ void atapi_remove_device(struct cdi_device* device) int atapi_request(struct cdi_scsi_device* scsi,struct cdi_scsi_packet* packet) { + uint8_t atapi_request[12]; + if (packet->cmdsize > 12) { + return -1; + } + memcpy(atapi_request, packet->command, sizeof(atapi_request)); + struct ata_device *dev = (struct ata_device*)scsi; struct ata_request request = { .dev = dev, @@ -111,9 +118,9 @@ int atapi_request(struct cdi_scsi_device* scsi,struct cdi_scsi_packet* packet) } }, .block_count = 1, - .block_size = packet->cmdsize, + .block_size = sizeof(atapi_request), .blocks_done = 0, - .buffer = packet->command, + .buffer = atapi_request, .error = 0 }; -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:01 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:01 +0100 Subject: [tyndur-devel] [PATCH 11/22] ata: Initialize ata_device to 0 In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-12-git-send-email-max@tyndur.org> * struct ata_device inherits cdi_storage_device or cdi_scsi_device. Since this driver does not know all of the fields contained therein for sure, the object has to be initialized to 0. Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/ata/device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/cdi/ata/device.c b/src/modules/cdi/ata/device.c index baedd72..db47832 100644 --- a/src/modules/cdi/ata/device.c +++ b/src/modules/cdi/ata/device.c @@ -222,7 +222,7 @@ void ata_init_controller(struct ata_controller* controller) // Jetzt werden die einzelnen geraete identifiziert. Hier ist es // anscheinend geschickter mit dem Slave zu beginnen. for (i = 1; i >= 0; i--) { - struct ata_device* dev = malloc(sizeof(*dev)); + struct ata_device* dev = calloc(1, sizeof(*dev)); dev->controller = controller; dev->id = i; dev->partition_list = cdi_list_create(); -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:02 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:02 +0100 Subject: [tyndur-devel] [PATCH 12/22] cdi/scsi: Neu geschrieben In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-13-git-send-email-max@tyndur.org> * Die týndur-Implementierung von cdi/scsi war im Prinzip nur zur Benutzung mit dem CDI-ATAPI-Treiber geeignet. Dieser Patch macht die Implementierung allgemeiner und auch für andere (zukünftige) SCSI-CDI-Treiber einsatzfähig. Signed-off-by: Max Reitz --- src/modules/cdi/lib/cdi.c | 26 ++- src/modules/cdi/lib/scsi/disk.c | 506 ++++++++++++++++++++++++++++++---------- 2 files changed, 404 insertions(+), 128 deletions(-) diff --git a/src/modules/cdi/lib/cdi.c b/src/modules/cdi/lib/cdi.c index 5d519b4..11b3009 100644 --- a/src/modules/cdi/lib/cdi.c +++ b/src/modules/cdi/lib/cdi.c @@ -19,13 +19,17 @@ #include "cdi.h" #include "cdi/audio.h" #include "cdi/fs.h" +#include "cdi/misc.h" #include "cdi/pci.h" +#include "cdi/scsi.h" #include "cdi/storage.h" extern void cdi_storage_driver_register(struct cdi_storage_driver* driver); extern void cdi_audio_driver_register(struct cdi_audio_driver* driver); extern void cdi_tyndur_net_device_init(struct cdi_device* device); +void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev); + static list_t* drivers = NULL; static void cdi_tyndur_run_drivers(void); @@ -149,6 +153,7 @@ static void cdi_tyndur_init_pci_devices(void) } if (device != NULL) { + cdi_osdep_new_device(driver, device); cdi_list_push(driver->devices, device); printf("cdi: %x.%x.%x: Benutze Treiber %s\n", pci->bus, pci->dev, pci->function, driver->name); @@ -214,9 +219,8 @@ static void cdi_tyndur_run_drivers(void) } } - /* Netzwerk ist schon initialisiert und SCSI hat kein RPC-Interface, - * sondern muss mit scsidisk gelinkt werden. */ - if (driver->type != CDI_NETWORK && driver->type != CDI_SCSI) { + /* Netzwerk ist schon initialisiert */ + if (driver->type != CDI_NETWORK) { init_service_register((char*) driver->name); } } @@ -277,6 +281,22 @@ list_t* cdi_tyndur_get_drivers(void) return drivers; } +void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev) +{ + if (!dev) { + return; + } + + dev->driver = drv; + cdi_list_push(drv->devices, dev); + + switch ((int)drv->type) { + case CDI_SCSI: + cdi_scsi_device_init(CDI_UPCAST(dev, struct cdi_scsi_device, dev)); + break; + } +} + /** * Wenn main nicht von einem Treiber ueberschrieben wird, ist hier der * Einsprungspunkt. Die Standardfunktion ruft nur cdi_init() auf. Treiber, die diff --git a/src/modules/cdi/lib/scsi/disk.c b/src/modules/cdi/lib/scsi/disk.c index 49f408e..2f2b7bd 100644 --- a/src/modules/cdi/lib/scsi/disk.c +++ b/src/modules/cdi/lib/scsi/disk.c @@ -1,6 +1,7 @@ /* * Copyright (C) 2008 Mathias Gottschlag * Copyright (C) 2009 Kevin Wolf + * Copyright (C) 2015 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 @@ -26,14 +27,10 @@ #include #include +#include #include #include -#define big_endian_word(x) ((((x) & 0xFF) << 8) | (((x) & 0xFF00) >> 8)) -#define big_endian_dword(x) \ - ((big_endian_word((x) & 0xFFFF) << 16) | \ - (big_endian_word((x) >> 16))) - static int cdi_scsi_disk_read(struct cdi_storage_device* device, uint64_t start, uint64_t count, void* buffer); @@ -44,84 +41,272 @@ struct cdi_storage_driver cdi_scsi_disk_driver = { .drv = { .name = "scsidisk", .type = CDI_STORAGE, + .bus = CDI_SCSI, }, .read_blocks = cdi_scsi_disk_read, .write_blocks = cdi_scsi_disk_write, }; -// TODO: This is kind of incomplete... -.- -// FIXME Das ist alles sehr CD-ROM-spezifisch hartkodiert... -union cdrom_command +typedef struct scsi_storage_device { + struct cdi_storage_device dev; + struct cdi_scsi_device* backend; + int lun; +} scsi_storage_device_t; + + +#define SCSI_REQUEST_SENSE 0x03 +#define SCSI_INQUIRY 0x12 +#define SCSI_START_STOP_UNIT 0x1b +#define SCSI_READ_CAPACITY 0x25 +#define SCSI_READ10 0x28 +#define SCSI_WRITE10 0x2a +#define SCSI_READ16 0x88 +#define SCSI_WRITE16 0x8a + +struct scsi_cmd { + uint8_t operation; +} __attribute__((packed)); + +struct scsi_cmd_request_sense { + struct scsi_cmd cmd; + uint8_t rsvd1; + uint16_t rsvd2; + uint8_t alloc_len; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_inquiry { + struct scsi_cmd cmd; + uint8_t rsvd; + uint8_t page; + uint16_t alloc_len; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_start_stop_unit { + struct scsi_cmd cmd; + uint8_t immed; + uint8_t rsvd; + uint8_t power_condition_modifier; + uint8_t flags_power_condition; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_read_capacity { + struct scsi_cmd cmd; + uint8_t rsvd1; + uint32_t lba; + uint32_t rsvd2; +} __attribute__((packed)); + +struct scsi_cmd_read10 { + struct scsi_cmd cmd; + uint8_t rsvd1; + uint32_t lba; + uint8_t rsvd2; + uint16_t length; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_write10 { + struct scsi_cmd cmd; + uint8_t rsvd1; + uint32_t lba; + uint8_t rsvd2; + uint16_t length; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_read16 { + struct scsi_cmd cmd; + uint8_t flags; + uint64_t lba; + uint32_t length; + uint8_t group; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_write16 { + struct scsi_cmd cmd; + uint8_t flags; + uint64_t lba; + uint32_t length; + uint8_t group; + uint8_t control; +} __attribute__((packed)); + +struct scsi_sense { + uint8_t resp_code; + uint8_t segment; + uint8_t flags; + uint32_t information; + uint8_t add_sense_length; + uint32_t cmd_spec_info; + uint8_t add_sense_code; + uint8_t add_sense_code_qual; + uint8_t field_replacable_unit_code; + uint8_t sense_key_spec1; + uint8_t sense_key_spec2; + uint8_t sense_key_spec3; +} __attribute__((packed)); + +struct scsi_inq_answer { + uint8_t dev_type; + uint8_t type_mod; + uint8_t version; + uint8_t resp_data_format; + uint8_t add_length; + uint8_t flags1, flags2, flags3; + char vendor[8]; + char product[16]; + uint32_t revision; + char serial[8]; + uint8_t vendor_unique[12]; + uint8_t clocking; + uint8_t rsvd1; + uint16_t version_desc[8]; + uint8_t rsvd2[22]; +} __attribute__((packed)); + + +static int scsi_read10(scsi_storage_device_t* dev, uint32_t sector, + uint16_t nb_sectors, char* buffer) { - struct - { - uint8_t opcode; - uint8_t reserved0; - uint32_t address; - uint8_t reserved1; - uint16_t length; - uint8_t reserved2; - } __attribute__ ((packed)) dfl; - struct - { - uint8_t opcode; - uint8_t reserved0; - uint32_t address; - uint32_t length; - uint16_t reserved1; - } __attribute__ ((packed)) ext; -}; - -static int cdrom_read_partial( - struct cdi_scsi_device *device, uint32_t sector, uint64_t nb_sectors, - char *buffer) -{ - struct cdi_scsi_packet packet; - struct cdi_scsi_driver* drv = (struct cdi_scsi_driver*) device->dev.driver; - union cdrom_command* cmd = (union cdrom_command*) &packet.command; + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + struct scsi_cmd_read10 r10 = { + .cmd = { + .operation = SCSI_READ10 + }, + .lba = cdi_cpu_to_be32(sector), + .length = cdi_cpu_to_be16(nb_sectors) + }; + struct cdi_scsi_packet pkt = { + .lun = dev->lun, + .buffer = buffer, + .bufsize = (size_t)nb_sectors * dev->dev.block_size, + .direction = CDI_SCSI_READ, + .cmdsize = sizeof(r10) + }; + memcpy(pkt.command, &r10, sizeof(r10)); + + if (drv->request(dev->backend, &pkt)) { + return -1; + } - nb_sectors = (uint32_t) nb_sectors; + return 0; +} - memset(&packet, 0, sizeof(packet)); - packet.direction = CDI_SCSI_READ; - packet.buffer = buffer; - packet.bufsize = 2048 * nb_sectors; - packet.cmdsize = 12; +static int scsi_write10(scsi_storage_device_t* dev, uint32_t sector, + uint16_t nb_sectors, char* buffer) +{ + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + struct scsi_cmd_write10 w10 = { + .cmd = { + .operation = SCSI_WRITE10 + }, + .lba = cdi_cpu_to_be32(sector), + .length = cdi_cpu_to_be16(nb_sectors) + }; + struct cdi_scsi_packet pkt = { + .lun = dev->lun, + .buffer = buffer, + .bufsize = (size_t)nb_sectors * dev->dev.block_size, + .direction = CDI_SCSI_WRITE, + .cmdsize = sizeof(w10) + }; + memcpy(pkt.command, &w10, sizeof(w10)); + + if (drv->request(dev->backend, &pkt)) { + return -1; + } - cmd->ext.opcode = 0xA8; - cmd->ext.address = big_endian_dword(sector); - cmd->ext.length = big_endian_dword(nb_sectors); + return 0; +} - if (drv->request(device, &packet)) { +static int scsi_read16(scsi_storage_device_t* dev, uint64_t sector, + uint32_t nb_sectors, char* buffer) +{ + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + struct scsi_cmd_read16 r16 = { + .cmd = { + .operation = SCSI_READ16 + }, + .lba = cdi_cpu_to_be64(sector), + .length = cdi_cpu_to_be32(nb_sectors) + }; + struct cdi_scsi_packet pkt = { + .lun = dev->lun, + .buffer = buffer, + .bufsize = (size_t)nb_sectors * dev->dev.block_size, + .direction = CDI_SCSI_READ, + .cmdsize = sizeof(r16) + }; + memcpy(pkt.command, &r16, sizeof(r16)); + + if (drv->request(dev->backend, &pkt)) { return -1; } - return nb_sectors; + return 0; } -static int cdrom_capacity(struct cdi_scsi_device *device, - uint32_t* num_sectors, uint32_t* sector_size) +static int scsi_write16(scsi_storage_device_t* dev, uint64_t sector, + uint32_t nb_sectors, char* buffer) { - uint32_t buffer[32]; - struct cdi_scsi_packet packet; - struct cdi_scsi_driver* drv = (struct cdi_scsi_driver*) device->dev.driver; - union cdrom_command* cmd = (union cdrom_command*) &packet.command; - - memset(&packet, 0, sizeof(packet)); - packet.direction = CDI_SCSI_READ; - packet.buffer = buffer; - packet.bufsize = sizeof(buffer); - packet.cmdsize = 12; + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + struct scsi_cmd_write16 w16 = { + .cmd = { + .operation = SCSI_WRITE16 + }, + .lba = cdi_cpu_to_be64(sector), + .length = cdi_cpu_to_be32(nb_sectors) + }; + struct cdi_scsi_packet pkt = { + .lun = dev->lun, + .buffer = buffer, + .bufsize = (size_t)nb_sectors * dev->dev.block_size, + .direction = CDI_SCSI_WRITE, + .cmdsize = sizeof(w16) + }; + memcpy(pkt.command, &w16, sizeof(w16)); + + if (drv->request(dev->backend, &pkt)) { + return -1; + } - cmd->ext.opcode = 0x25; + return 0; +} - if (drv->request(device, &packet)) { +static int scsi_get_capacity(scsi_storage_device_t* dev) +{ + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + uint32_t capacity[2] = { 0 }; + struct scsi_cmd_read_capacity read_cap = { + .cmd = { + .operation = SCSI_READ_CAPACITY + } + }; + struct cdi_scsi_packet pkt = { + .lun = dev->lun, + .buffer = capacity, + .bufsize = sizeof(capacity), + .direction = CDI_SCSI_READ, + .cmdsize = sizeof(read_cap) + }; + memcpy(pkt.command, &read_cap, sizeof(read_cap)); + + if (drv->request(dev->backend, &pkt)) { return -1; } - *num_sectors = big_endian_dword(buffer[0]); - *sector_size = big_endian_dword(buffer[1]); + dev->dev.block_count = (uint64_t)cdi_be32_to_cpu(capacity[0]) + 1; + dev->dev.block_size = cdi_be32_to_cpu(capacity[1]); return 0; } @@ -160,27 +345,28 @@ static void cdrom_sense(FsSCSIDevice *device, uint32_t index) } #endif -int cdrom_init(struct cdi_scsi_device* device) +static int scsi_init(scsi_storage_device_t* dev) { - // Send inquiry command - struct cdi_scsi_driver* drv = (struct cdi_scsi_driver*) device->dev.driver; - struct cdi_scsi_packet packet; - uint32_t status; - unsigned char inqdata[96]; - - memset(&packet, 0, sizeof(packet)); - memset(inqdata, 0xF0, sizeof(inqdata)); - - packet.direction = CDI_SCSI_READ; - packet.buffer = inqdata; - packet.bufsize = 96; - packet.cmdsize = 12; - - ((union cdrom_command*)packet.command)->dfl.opcode = 0x12; - packet.command[4] = 96; - status = drv->request(device, &packet); - if (status) - { + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + struct scsi_inq_answer inqdata; + + struct scsi_cmd_inquiry inq = { + .cmd = { + .operation = SCSI_INQUIRY, + }, + .alloc_len = cdi_cpu_to_be16(sizeof(inqdata)) + }; + struct cdi_scsi_packet inq_pkt = { + .lun = dev->lun, + .buffer = &inqdata, + .bufsize = sizeof(inqdata), + .direction = CDI_SCSI_READ, + .cmdsize = sizeof(inq) + }; + memcpy(inq_pkt.command, &inq, sizeof(inq)); + + if (drv->request(dev->backend, &inq_pkt)) { return -1; } @@ -189,56 +375,109 @@ int cdrom_init(struct cdi_scsi_device* device) return KE_ERROR_UNKNOWN; #endif - // Start drive - packet.direction = CDI_SCSI_NODATA; - ((union cdrom_command*)packet.command)->dfl.opcode = 0x1B; - packet.buffer = 0; - packet.bufsize = 0; - packet.command[4] = 0; - packet.command[4] = 3; - packet.cmdsize = 12; - - status = drv->request(device, &packet); - if (status) - { - printf("cdrom %d: Start failed, %x\n", index, status); - return -1; + struct scsi_cmd_start_stop_unit ssu = { + .cmd = { + .operation = SCSI_START_STOP_UNIT, + }, + .flags_power_condition = 0x3 // LOEJ+START + }; + struct cdi_scsi_packet ssu_pkt = { + .lun = dev->lun, + .direction = CDI_SCSI_NODATA, + .cmdsize = sizeof(ssu) + }; + memcpy(ssu_pkt.command, &ssu, sizeof(ssu)); + + if ((inqdata.dev_type & 0x1f) == 0x05) { // CD/DVD + if (drv->request(dev->backend, &ssu_pkt)) { + printf("cdrom: Start failed\n"); + return -1; + } + dev->dev.block_size = 2048; + } else { + dev->dev.block_size = 512; } return 0; } -static int cdi_scsi_disk_read(struct cdi_storage_device* device, - uint64_t start, uint64_t count, void* buffer) +static int cdi_scsi_disk_read(struct cdi_storage_device* dev, uint64_t start, + uint64_t count, void* buffer) { - struct cdi_scsi_device* dev = device->dev.backdev; + scsi_storage_device_t* scsi_dev = CDI_UPCAST(dev, scsi_storage_device_t, + dev); + bool read16 = (start >> 32) || ((start + count) >> 32); while (count > 0) { int ret; + uint32_t it_count; + + if (read16) { + it_count = count > 0xffffffffu ? 0xffffffffu : count; + } else { + it_count = count > 0xffff ? 0xffff : count; + } - ret = cdrom_read_partial(dev, start, count, buffer); - if (ret <= 0) { + if (read16) { + ret = scsi_read16(scsi_dev, start, it_count, buffer); + } else { + ret = scsi_read10(scsi_dev, start, it_count, buffer); + } + if (ret < 0) { return -1; } - start += ret; - buffer += ret; - count -= ret; + + start += it_count; + count -= it_count; + + buffer = (void*) ((uintptr_t)buffer + it_count * dev->block_size); } return 0; } -static int cdi_scsi_disk_write(struct cdi_storage_device* device, - uint64_t start, uint64_t count, void* buffer) +static int cdi_scsi_disk_write(struct cdi_storage_device* dev, uint64_t start, + uint64_t count, void* buffer) { - return -1; + scsi_storage_device_t* scsi_dev = CDI_UPCAST(dev, scsi_storage_device_t, + dev); + bool write16 = (start >> 32) || ((start + count) >> 32); + + while (count > 0) { + int ret; + uint32_t it_count; + + if (write16) { + it_count = count > 0xffffffffu ? 0xffffffffu : count; + } else { + it_count = count > 0xffff ? 0xffff : count; + } + + if (write16) { + ret = scsi_write16(scsi_dev, start, it_count, buffer); + } else { + ret = scsi_write10(scsi_dev, start, it_count, buffer); + } + if (ret < 0) { + return -1; + } + + start += it_count; + count -= it_count; + + buffer = (void*) ((uintptr_t)buffer + it_count * dev->block_size); + } + + return 0; } -static void driver_init(void) +static void driver_init(struct cdi_driver* drv) { static int initialised = 0; if (!initialised) { + cdi_scsi_disk_driver.drv.name = strdup(drv->name); + cdi_driver_register(&cdi_scsi_disk_driver.drv); cdi_storage_driver_init(&cdi_scsi_disk_driver); } @@ -246,30 +485,47 @@ static void driver_init(void) int cdi_scsi_disk_init(struct cdi_scsi_device* device) { - struct cdi_storage_device* frontdev; - uint32_t num_sectors = 0, sector_size = 0; + int luns = device->lun_count ?: 1; - driver_init(); + driver_init(device->dev.driver); - if (cdrom_init(device)) { - return -1; - } + for (int lun = 0; lun < luns; lun++) { + scsi_storage_device_t* dev = calloc(1, sizeof(*dev)); + bool inquiry_failed; + + dev->backend = device; + dev->lun = lun; + + inquiry_failed = scsi_init(dev) < 0; - frontdev = calloc(1, sizeof(*frontdev)); - frontdev->dev.driver = (struct cdi_driver*) &cdi_scsi_disk_driver; - frontdev->dev.name = device->dev.name; + if (scsi_get_capacity(dev) < 0) { + if (inquiry_failed) { + /* We're fine as long as either INQUIRY or READ_CAPACITY work - + * but if both fail, there's not a lot we can do. */ + fprintf(stderr, + "[scsi] Failed to initialize LUN %i of device %s\n", + lun, device->dev.name); - // Groesse des Mediums bestimmen. Falls keins eingelegt ist, brauchen wir - // trotzdem zumindest eine Sektorgroesse. - cdrom_capacity(device, &num_sectors, §or_size); - frontdev->block_size = sector_size ? sector_size : 2048; - frontdev->block_count = num_sectors; + free(dev); + continue; + } - frontdev->dev.backdev = device; - device->frontdev = frontdev; + /* Apparently this is common for CD-ROM devices; at least the old + * SCSI implementation didn't care about it. A default block_size + * has been set by scsi_init(). */ + dev->dev.block_count = 0xffffffffu; + } + + dev->dev.dev.driver = (struct cdi_driver*) &cdi_scsi_disk_driver; + if (luns > 1) { + asprintf((char**) &dev->dev.dev.name, "%s_l%i", + device->dev.name, lun); + } else { + dev->dev.dev.name = strdup(device->dev.name); + } - // LostIO-Verzeichnisknoten anlegen - cdi_storage_device_init(frontdev); + cdi_storage_device_init(&dev->dev); + } return 0; } -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:03 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:03 +0100 Subject: [tyndur-devel] [PATCH 13/22] lock.h: locked_decrement() In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-14-git-send-email-max@tyndur.org> + Wenn man schon locked_increment() hat, dann macht sich locked_decrement() auch ganz gut. Signed-off-by: Max Reitz --- src/include/lock.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/include/lock.h b/src/include/lock.h index fc73b3f..89ddb4a 100644 --- a/src/include/lock.h +++ b/src/include/lock.h @@ -63,6 +63,16 @@ static inline uint32_t locked_increment(volatile uint32_t* var) return __sync_fetch_and_add(var, 1); } +/** + * Eine Variable dekrementieren, während der Bus gesperrt ist. + * + * @param var Pointer auf die zu dekrementierende Variable + */ +static inline uint32_t locked_decrement(volatile uint32_t* var) +{ + return __sync_fetch_and_sub(var, 1); +} + /* Mutexe **************************************/ -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:04 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:04 +0100 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_14/22=5D_servmgr=3A_Workaround_?= =?utf-8?q?f=C3=BCr_zyklische_Abh=C3=A4ngigkeiten?= In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-15-git-send-email-max@tyndur.org> ! Es kann passieren, dass Service A auf B wartet, der auf C wartet. Dann melden sich diese beide beim servmgr, der dann für B darauf warten soll, dass B gestartet ist, und für A, dass B fertig ist. Da týndur leider keine Popupthreads für IPC benutzt, existiert aber nur ein wartender Thread. Wenn A später als B angefragt hat, wird also A ewig auf B warten, da B nie weiterlaufen kann, auch wenn C fertig ist. Ein einfacher Workaround ist es, zu einem Zeitpunkt immer nur einem anfragenden Service/Prozess das Privileg zu gestatten, den servmgr selbst zu benutzen, um zu warten. Alle anderen bekommen ein EAGAIN, wenn der Zielservice noch nicht angemeldet ist, und müssen das Warten selber übernehmen (hier als wiederholte Anfragen bei servmgr implementiert). Signed-off-by: Max Reitz --- src/modules/lib/servmgr.c | 11 +++++++++-- src/modules/servmgr/rpcif.c | 22 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/modules/lib/servmgr.c b/src/modules/lib/servmgr.c index 3909350..429be73 100644 --- a/src/modules/lib/servmgr.c +++ b/src/modules/lib/servmgr.c @@ -26,6 +26,7 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -36,8 +37,14 @@ static pid_t servmgr_pid(void); bool servmgr_need(const char* service_name) { - return rpc_get_dword(servmgr_pid(), "NEEDSERV", strlen(service_name) + 1, - (char*) service_name); + int result; + + do { + result = rpc_get_int(servmgr_pid(), "NEEDSERV", + strlen(service_name) + 1, (char*) service_name); + } while (result == -EAGAIN); + + return result == 1; } /** diff --git a/src/modules/servmgr/rpcif.c b/src/modules/servmgr/rpcif.c index 6bbce79..645c8c5 100644 --- a/src/modules/servmgr/rpcif.c +++ b/src/modules/servmgr/rpcif.c @@ -40,6 +40,8 @@ #include #include #include +#include +#include #include "servmgr.h" static void rpc_needserv(pid_t pid, uint32_t cid, size_t data_size, void* data); @@ -64,7 +66,7 @@ static void rpc_needserv(pid_t pid, uint32_t cid, size_t data_size, void* data) // Pruefen ob der Name nullterminiert ist if (!data_size || (name[data_size - 1] != 0)) { - rpc_send_dword_response(pid, cid, 0); + rpc_send_int_response(pid, cid, 0); return; } @@ -73,10 +75,26 @@ static void rpc_needserv(pid_t pid, uint32_t cid, size_t data_size, void* data) } else { // Noch nicht konfiguriert. Jetzt bleibt nur hoffen, dass der Dienst // schon geladen wurde, sich aber noch nicht registriert hat. + + static volatile uint32_t semaphore; + if (locked_increment(&semaphore) > 0) { + locked_decrement(&semaphore); + // Wir können nicht mehrere gleichzeitig warten lassen, weil das sonst + // zyklische Abhängigkeiten geben könnte + if (service_running(name)) { + rpc_send_int_response(pid, cid, true); + } else { + rpc_send_int_response(pid, cid, -EAGAIN); + } + return; + } + result = service_wait(name, 10000); + + locked_decrement(&semaphore); } - rpc_send_dword_response(pid, cid, result); + rpc_send_int_response(pid, cid, result); } /** -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:05 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:05 +0100 Subject: [tyndur-devel] [PATCH 15/22] cdi/misc: Implementiere cdi_elapsed_ms() In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-16-git-send-email-max@tyndur.org> + Implementiere die cdi/misc-Funktion cdi_elapsed_ms(). Signed-off-by: Max Reitz --- src/modules/cdi/lib/misc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/modules/cdi/lib/misc.c b/src/modules/cdi/lib/misc.c index 1b965af..2f34d87 100644 --- a/src/modules/cdi/lib/misc.c +++ b/src/modules/cdi/lib/misc.c @@ -183,3 +183,7 @@ void cdi_sleep_ms(uint32_t ms) } +uint64_t cdi_elapsed_ms(void) +{ + return get_tick_count() / 1000; +} -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:06 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:06 +0100 Subject: [tyndur-devel] [PATCH 16/22] cdi/usb: Add headers In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-17-git-send-email-max@tyndur.org> + Add headers to be used by USB host controller drivers, the USB bus driver, and USB device drivers. These headers are still lacking functions for periodic transactions (interrupt/isochronous), hotplugging, suspend/resume, etc. pp.. Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/include/cdi-osdep.h | 21 +++ src/modules/cdi/include/cdi/usb-structures.h | 174 ++++++++++++++++++++++ src/modules/cdi/include/cdi/usb.h | 211 +++++++++++++++++++++++++++ src/modules/cdi/include/cdi/usb_hcd.h | 168 +++++++++++++++++++++ 4 files changed, 574 insertions(+) create mode 100644 src/modules/cdi/include/cdi/usb-structures.h create mode 100644 src/modules/cdi/include/cdi/usb.h create mode 100644 src/modules/cdi/include/cdi/usb_hcd.h diff --git a/src/modules/cdi/include/cdi-osdep.h b/src/modules/cdi/include/cdi-osdep.h index 94eba73..3ec20a1 100644 --- a/src/modules/cdi/include/cdi-osdep.h +++ b/src/modules/cdi/include/cdi-osdep.h @@ -58,6 +58,27 @@ typedef struct /** * \german + * OS-spezifische Daten zu USB-Geräten. + * \endgerman + * \english + * OS-specific USB device data. + * \endenglish + * \thuringiansaxonian + * Vonnä jäwailschn Bedriebssysdääm abhängsche Dåhdn, nu für so USB-Geräide. + * \endthuringiansaxonian + * \french + * Des données tributaires du système d'exploitation pour des périphériques + * USB. + * \endfrench + * \japanese + * USB??????????OS???????? + * \endjapanese + */ +typedef struct { +} cdi_usb_device_osdep; + +/** + * \german * OS-spezifische Daten fuer einen ISA-DMA-Zugriff * \endgerman * \english diff --git a/src/modules/cdi/include/cdi/usb-structures.h b/src/modules/cdi/include/cdi/usb-structures.h new file mode 100644 index 0000000..fecb609 --- /dev/null +++ b/src/modules/cdi/include/cdi/usb-structures.h @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2015 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. + */ + +/** + * This file contains some standard USB structures such as descriptors. + */ + +#ifndef _CDI_USB_STRUCTURES_H_ +#define _CDI_USB_STRUCTURES_H_ + +#include + + +struct cdi_usb_device_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint16_t bcd_usb; + uint8_t b_device_class; + uint8_t b_device_sub_class; + uint8_t b_device_protocol; + uint8_t b_max_packet_size_0; + uint16_t id_vendor; + uint16_t id_product; + uint16_t bcd_device; + uint8_t i_manufacturer; + uint8_t i_product; + uint8_t i_serial_number; + uint8_t b_num_configurations; +} __attribute__((packed)); + +struct cdi_usb_configuration_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint16_t w_total_length; + uint8_t b_num_interfaces; + uint8_t b_configuration_value; + uint8_t i_configuration; + uint8_t bm_attributes; + uint8_t b_max_power; +} __attribute__((packed)); + +struct cdi_usb_string_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint16_t b_string[127]; +} __attribute__((packed)); + +struct cdi_usb_interface_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint8_t b_interface_number; + uint8_t b_alternate_setting; + uint8_t b_num_endpoints; + uint8_t b_interface_class; + uint8_t b_interface_sub_class; + uint8_t b_interface_protocol; + uint8_t i_interface; +} __attribute__((packed)); + + +struct cdi_usb_endpoint_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint8_t b_endpoint_address; + uint8_t bm_attributes; + uint16_t w_max_packet_size; + uint8_t b_interval; +} __attribute__((packed)); + +/// cdi_usb_endpoint_descriptor.b_endpoint_address +#define CDI_USB_EPDSC_EPADR_ID(x) ((x) & 0xf) +#define CDI_USB_EPDSC_EPADR_DIR (1 << 7) // IN if set + +/// cdi_usb_endpoint_descriptor.bm_attributes +#define CDI_USB_EPDSC_ATTR_XFER_TYPE_MASK 0x03 // Transfer Type mask +#define CDI_USB_EPDSC_ATTR_CONTROL 0x00 +#define CDI_USB_EPDSC_ATTR_ISOCHRONOUS 0x01 +#define CDI_USB_EPDSC_ATTR_BULK 0x02 +#define CDI_USB_EPDSC_ATTR_INTERRUPT 0x03 +#define CDI_USB_EPDSC_ATTR_SYNC_TYPE_MASK 0x0c // Synchronization Type mask +#define CDI_USB_EPDSC_ATTR_NO_SYNC 0x00 +#define CDI_USB_EPDSC_ATTR_ASYNC 0x04 +#define CDI_USB_EPDSC_ATTR_ADAPTIVE 0x08 +#define CDI_USB_EPDSC_ATTR_SYNC 0x0c +#define CDI_USB_EPDSC_ATTR_USAGE_TYPE_MASK 0x30 // Usage Type mask +#define CDI_USB_EPDSC_ATTR_DATA 0x00 +#define CDI_USB_EPDSC_ATTR_FEEDBACK 0x10 +#define CDI_USB_EPDSC_ATTR_IMPL_FB_DATA 0x20 // Implicit Feedback Data + +/// cdi_usb_endpoint_descriptor.w_max_packet_size +#define CDI_USB_EPDSC_MPS_MPS(x) ((x) & 0x7ff) + + +struct cdi_usb_setup_packet { + uint8_t bm_request_type; + uint8_t b_request; + uint16_t w_value; + uint16_t w_index; + uint16_t w_length; +} __attribute__((packed)); + +/// Values constituting cdi_usb_setup_packet.bm_request_type +#define CDI_USB_CREQ_DEVICE 0 +#define CDI_USB_CREQ_INTERFACE 1 +#define CDI_USB_CREQ_ENDPOINT 2 +#define CDI_USB_CREQ_OTHER 3 +#define CDI_USB_CREQ_CLASS (1 << 5) +#define CDI_USB_CREQ_OUT (0 << 7) +#define CDI_USB_CREQ_IN (1 << 7) + +/// Control request numbers (cdi_usb_setup_packet.b_request) +enum { + CDI_USB_CREQ_GET_STATUS = 0, + CDI_USB_CREQ_CLEAR_FEATURE = 1, + CDI_USB_CREQ_SET_FEATURE = 3, + CDI_USB_CREQ_SET_ADDRESS = 5, + CDI_USB_CREQ_GET_DESCRIPTOR = 6, + CDI_USB_CREQ_SET_DESCRIPTOR = 7, + CDI_USB_CREQ_GET_CONFIGURATION = 8, + CDI_USB_CREQ_SET_CONFIGURATION = 9, + CDI_USB_CREQ_GET_INTERFACE = 10, + CDI_USB_CREQ_SET_INTERFACE = 11, + CDI_USB_CREQ_SYNCH_FRAME = 12, +}; + +/// Descriptor IDs +enum { + CDI_USB_DESC_DEVICE = 1, + CDI_USB_DESC_CONFIGURATION = 2, + CDI_USB_DESC_STRING = 3, + CDI_USB_DESC_INTERFACE = 4, + CDI_USB_DESC_ENDPOINT = 5, +}; + +/// USB class codes. Defined here because they are not part of their respective +/// specifications. + +/// USB class codes on the device level +#define CDI_USB_DEVCLS_NONE 0x00 // No device-wide class, use interfaces +#define CDI_USB_DEVCLS_CCDCC 0x02 // Communications and CDC Control +#define CDI_USB_DEVCLS_HUB 0x09 // Hub +#define CDI_USB_DEVCLS_BILLBD 0x11 // Billboard Device +#define CDI_USB_DEVCLS_DIAG 0xdc // Diagnostic Device +#define CDI_USB_DEVCLS_MISC 0xef // Miscellaneous +#define CDI_USB_DEVCLS_VENDOR 0xff // Vendor-specific + +/// USB class codes on the interface level +#define CDI_USB_IFCCLS_AUDIO 0x01 // Audio +#define CDI_USB_IFCCLS_CCDCC CDI_USB_DEVCLS_CCDCC +#define CDI_USB_IFCCLS_HID 0x03 // Human Interface Device +#define CDI_USB_IFCCLS_PHYS 0x05 // Physical +#define CDI_USB_IFCCLS_IMAGE 0x06 // Image +#define CDI_USB_IFCCLS_PRINTER 0x07 // Printer +#define CDI_USB_IFCCLS_STORAGE 0x08 // Mass Storage +#define CDI_USB_IFCCLS_CDCDATA 0x0a // CDC Data +#define CDI_USB_IFCCLS_SMARTCD 0x0b // Smart Card +#define CDI_USB_IFCCLS_CNTTSEC 0x0d // Content Security +#define CDI_USB_IFCCLS_VIDEO 0x0e // Video +#define CDI_USB_IFCCLS_HEALTH 0x0f // Personal Healthcare +#define CDI_USB_IFCCLS_AV 0x10 // Audio/Video +#define CDI_USB_IFCCLS_DIAG CDI_USB_DEVCLS_DIAG +#define CDI_USB_IFCCLS_WIRELESS 0xe0 // Wireless Controller +#define CDI_USB_IFCCLS_MISC CDI_USB_DEVCLS_MISC +#define CDI_USB_IFCCLS_APP 0xfe // Application-specific +#define CDI_USB_IFCCLS_VENDOR CDI_USB_DEVCLS_VENDOR + +#endif diff --git a/src/modules/cdi/include/cdi/usb.h b/src/modules/cdi/include/cdi/usb.h new file mode 100644 index 0000000..d30e7b0 --- /dev/null +++ b/src/modules/cdi/include/cdi/usb.h @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2015 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. + */ + +#ifndef _CDI_USB_H_ +#define _CDI_USB_H_ + +#include + +#include +#include +#include + + +struct cdi_usb_hc; + +typedef enum { + CDI_USB_CONTROL, + CDI_USB_BULK, + CDI_USB_INTERRUPT, + CDI_USB_ISOCHRONOUS, +} cdi_usb_endpoint_type_t; + +typedef enum { + CDI_USB_LOW_SPEED, + CDI_USB_FULL_SPEED, + CDI_USB_HIGH_SPEED, + CDI_USB_SUPERSPEED, +} cdi_usb_speed_t; + +typedef enum { + CDI_USB_OK = 0, + + CDI_USB_DRIVER_ERROR, + CDI_USB_TIMEOUT, + CDI_USB_BABBLE, + CDI_USB_STALL, + CDI_USB_HC_ERROR, + CDI_USB_BAD_RESPONSE, +} cdi_usb_transmission_result_t; + +/* Bitmask of CDI_USB_PORT_* and CDI_USB_*_SPEED values */ +typedef uint32_t cdi_usb_port_status_t; + +#define CDI_USB_PORT_SPEED_MASK (0x7) +#define CDI_USB_PORT_CONNECTED (1 << 3) + +struct cdi_usb_hub; + +/** + * Describes a hub driver. This is not a CDI driver in the common sense. When + * used for a root hub, it is part of the host controller driver; when used for + * a device hub, it is only used internally by the USB driver. + */ +struct cdi_usb_hub_driver { + /** + * Makes a port unreachable by traffic which reaches the hub. @index is a + * 0-based index. + */ + void (*port_disable)(struct cdi_usb_hub* hub, int index); + + /** + * Makes a port reachable by traffic which reaches the hub and drives the + * reset signal for at least 50 ms on the port. @index is a 0-based index. + */ + void (*port_reset_enable)(struct cdi_usb_hub* hub, int index); + + /** + * Retrieves information about the given port. This should only be called + * after the port has been reset using port_reset_enable(). @index is a + * 0-based index. + */ + cdi_usb_port_status_t (*port_status)(struct cdi_usb_hub* hub, int index); +}; + +/** + * Describes a USB hub. This is not a CDI device in the common sense. When used + * for a root hub, it is part of the host controller object; when used for a + * device hub, it is only used internally by the USB driver. + * + * For this reason, there is no struct cdi_usb_hub_driver object reachable from + * this object. If it is a root hub, the hub driver will be part of the host + * controller driver. Otherwise (if it is a device hub), the hub driver will be + * internal to the USB driver. + */ +struct cdi_usb_hub { + /** + * Number of ports provided by this hub. + */ + int ports; +}; + +/** + * Describes a USB device as given to cdi_provide_device() and the init_device() + * function provided by USB device drivers. + */ +struct cdi_usb_device { + struct cdi_bus_data bus_data; + + /** + * Various information which can be used for identifying a USB device. + */ + uint16_t vendor_id, product_id; + uint8_t class_id, subclass_id, protocol_id; + + /** + * If negative, this logical device is defined on the USB device level; + * otherwise, this is the index of the interface which this logical device + * represents. + */ + int interface; + + /** + * Number of endpoints available for this device; this does include the + * default pipe (endpoint 0) which can be used both on the physical device + * level and from any interface. + */ + int endpoint_count; + + cdi_usb_device_osdep meta; +}; + +/** + * Describes the USB bus driver. + */ +struct cdi_usb_driver { + struct cdi_driver drv; + + /** + * Returns the endpoint descriptor for endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev in @desc. + */ + void (*get_endpoint_descriptor)(struct cdi_usb_device* dev, int ep, + struct cdi_usb_endpoint_descriptor* desc); + + /** + * Executes a control transfer to endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev, using @setup for the setup stage + * and @data for the data stage. The length and direction of transfer is + * deduced from @setup. + */ + cdi_usb_transmission_result_t + (*control_transfer)(struct cdi_usb_device* dev, int ep, + const struct cdi_usb_setup_packet* setup, + void* data); + + /** + * Executes a bulk transfer to endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev, transferring @size bytes from/to + * @data. The direction of transfer is deduced from the endpoint given. + */ + cdi_usb_transmission_result_t (*bulk_transfer)(struct cdi_usb_device* dev, + int ep, void* data, + size_t size); +}; + +/** + * Describes patterns for USB devices a device driver can use to specify which + * devices it can handle. + */ +struct cdi_usb_bus_device_pattern { + struct cdi_bus_device_pattern pattern; + + /** + * Any field may be negative to signify "don't care". + */ + int vendor_id, product_id; + int class_id, subclass_id, protocol_id; +}; + + +/** + * Registers a USB bus driver. Should be called by the CDI library, ideally, + * after the USB bus driver's init() function has run. + */ +void cdi_usb_driver_register(struct cdi_usb_driver* drv); + +/** + * Returns the endpoint descriptor for endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev in @desc. + */ +void cdi_usb_get_endpoint_descriptor(struct cdi_usb_device* dev, int ep, + struct cdi_usb_endpoint_descriptor* desc); + +/** + * Executes a control transfer to endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev, using @setup for the setup stage and + * @data for the data stage. The length and direction of transfer is deduced + * from @setup. + */ +cdi_usb_transmission_result_t + cdi_usb_control_transfer(struct cdi_usb_device* dev, int ep, + const struct cdi_usb_setup_packet* setup, + void* data); + +/** + * Executes a bulk transfer to endpoint @ep (0 up until dev->endpoint_count - 1) + * of device @dev, transferring @size bytes from/to @data. The direction of + * transfer is deduced from the endpoint given. + */ +cdi_usb_transmission_result_t cdi_usb_bulk_transfer(struct cdi_usb_device* dev, + int ep, void* data, + size_t size); + +#endif diff --git a/src/modules/cdi/include/cdi/usb_hcd.h b/src/modules/cdi/include/cdi/usb_hcd.h new file mode 100644 index 0000000..842ae73 --- /dev/null +++ b/src/modules/cdi/include/cdi/usb_hcd.h @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2015 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. + */ + +#ifndef _CDI_USB_HCD_H_ +#define _CDI_USB_HCD_H_ + +#include +#include + + +/** + * Opaque value identifying a transaction for the host controller. + */ +typedef void* cdi_usb_hc_transaction_t; + +typedef enum { + CDI_USB_IN, + CDI_USB_OUT, + CDI_USB_SETUP, +} cdi_usb_transfer_token_t; + + +/** + * Describes a USB host controller. + */ +struct cdi_usb_hc { + struct cdi_device dev; + + /** + * Root hub provided by the HC. The hub driver responsible for this root hub + * is part of the cdi_usb_hcd structure. + */ + struct cdi_usb_hub rh; +}; + +/** + * Describes the USB bus provided by a USB host controller and its root hub. All + * buses are handled by the USB bus driver, therefore there are no "identifying" + * fields in this structure. + */ +struct cdi_usb_hc_bus { + struct cdi_bus_data bus_data; + struct cdi_usb_hc* hc; +}; + +/** + * Contains information about a communication pipe to a USB device endpoint used + * for USB transactions. + */ +struct cdi_usb_hc_ep_info { + /// Device ID + uint8_t dev; + + /// Endpoint ID + uint8_t ep; + + /// Endpoint type + cdi_usb_endpoint_type_t ep_type; + + /// Transfer speed + cdi_usb_speed_t speed; + + /// Maximum packet size for this endpoint + size_t mps; + + /** + * For low and full speed devices operated behind a high speed hub: @tt_addr + * is the USB device address of the translating USB hub (the last high speed + * hub), @tt_port is the port (0-based index) of that hub where the first + * non-high-speed device of this device's tree is plugged into. + */ + uint8_t tt_addr, tt_port; +}; + +/** + * Describes a single USB transmission. + */ +struct cdi_usb_hc_transmission { + /// Transaction this transmission is part of + cdi_usb_hc_transaction_t ta; + + /// Type of transfer to be performed + cdi_usb_transfer_token_t token; + + /// Buffer to be used (may be NULL if @size is 0) + void* buffer; + + /// Number of bytes to be transferred (may be 0, and may exceed the MPS) + size_t size; + + /** + * Toggle bit to be used for the first packet issued in this transmission. + * If @size exceeds the MPS and thus multiple packets are transmitted, the + * toggle bit will be toggled automatically. + */ + unsigned toggle; + + /** + * Result of this transmission. Only valid after wait_transaction() has + * returned. + */ + cdi_usb_transmission_result_t* result; +}; + +/** + * Describes a USB host controller driver. + */ +struct cdi_usb_hcd { + struct cdi_driver drv; + + /** + * USB hub driver which is responsible for all root hubs provided by this + * HCD. + */ + struct cdi_usb_hub_driver rh_drv; + + /** + * Creates a USB transaction. A transaction consists of one or more + * transmissions to a single endpoint. + */ + cdi_usb_hc_transaction_t + (*create_transaction)(struct cdi_usb_hc* hc, + const struct cdi_usb_hc_ep_info* target); + + /** + * Enters a transmission into a transaction. The transmission will not be + * executed until start_transaction() is invoked. + * + * This function may not be called after start_transaction() has been used. + */ + void (*enqueue)(struct cdi_usb_hc* hc, + const struct cdi_usb_hc_transmission* trans); + + /** + * Starts asynchronous execution of a transaction. + */ + void (*start_transaction)(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta); + + /** + * Wait until all transmissions enqueued in the given transaction have been + * executed. The variables pointed to by the @result field of the + * transmissions enqueued will only be valid when this function returns. + * + * This function may not be invoked before start_transaction(). + */ + void (*wait_transaction)(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta); + + /** + * Destroys a transaction. Transmissions which have not been waited on may + * or may not have been executed by the time this function returns. The + * values of the variables pointed to by the @result field of unwaited-for + * transmissions are not valid, neither are the buffer contents for + * receiving transmissions. + */ + void (*destroy_transaction)(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta); +}; + +#endif -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:07 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:07 +0100 Subject: [tyndur-devel] [PATCH 17/22] cdi/usb: Implementierung der CDI-Bibliothek In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-18-git-send-email-max@tyndur.org> + Implementierung der CDI-Bibliothek für CDI.usb für týndur Signed-off-by: Max Reitz --- src/modules/cdi/lib/cdi.c | 81 +++++++++ src/modules/cdi/lib/usb.c | 403 +++++++++++++++++++++++++++++++++++++++++ src/modules/cdi/lib/usb_dd.c | 173 ++++++++++++++++++ src/modules/cdi/lib/usb_hcd.c | 413 ++++++++++++++++++++++++++++++++++++++++++ src/modules/include/usb-ipc.h | 103 +++++++++++ 5 files changed, 1173 insertions(+) create mode 100644 src/modules/cdi/lib/usb.c create mode 100644 src/modules/cdi/lib/usb_dd.c create mode 100644 src/modules/cdi/lib/usb_hcd.c create mode 100644 src/modules/include/usb-ipc.h diff --git a/src/modules/cdi/lib/cdi.c b/src/modules/cdi/lib/cdi.c index 11b3009..fa8a217 100644 --- a/src/modules/cdi/lib/cdi.c +++ b/src/modules/cdi/lib/cdi.c @@ -23,11 +23,20 @@ #include "cdi/pci.h" #include "cdi/scsi.h" #include "cdi/storage.h" +#include "cdi/usb.h" +#include "cdi/usb_hcd.h" extern void cdi_storage_driver_register(struct cdi_storage_driver* driver); extern void cdi_audio_driver_register(struct cdi_audio_driver* driver); +extern void cdi_usb_driver_register(struct cdi_usb_driver* driver); extern void cdi_tyndur_net_device_init(struct cdi_device* device); +extern void cdi_osdep_handle_usb_device(struct cdi_usb_bus_device_pattern* p); + +extern void cdi_osdep_provide_hc(struct cdi_usb_hc* hc); + +extern void cdi_osdep_set_up_usb_dd_ipc(struct cdi_driver* drv); + void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev); static list_t* drivers = NULL; @@ -219,6 +228,10 @@ static void cdi_tyndur_run_drivers(void) } } + if (driver->bus == CDI_USB) { + cdi_osdep_set_up_usb_dd_ipc(driver); + } + /* Netzwerk ist schon initialisiert */ if (driver->type != CDI_NETWORK) { init_service_register((char*) driver->name); @@ -270,6 +283,10 @@ void cdi_driver_register(struct cdi_driver* driver) cdi_audio_driver_register((struct cdi_audio_driver*) driver); break; + case CDI_USB: + cdi_usb_driver_register((struct cdi_usb_driver*) driver); + break; + default: break; } @@ -281,6 +298,66 @@ list_t* cdi_tyndur_get_drivers(void) return drivers; } +void cdi_handle_bus_device(struct cdi_driver* drv, + struct cdi_bus_device_pattern* pattern) +{ + (void)drv; + + switch ((int)pattern->bus_type) { + case CDI_USB: + cdi_osdep_handle_usb_device( + CDI_UPCAST(pattern, struct cdi_usb_bus_device_pattern, + pattern)); + break; + } +} + +extern void cdi_osdep_provide_usb_device_to(struct cdi_usb_device* dev, + pid_t pid); + +int cdi_provide_device(struct cdi_bus_data* device) +{ + if (device->bus_type != CDI_USB) { + return -1; + } + + struct cdi_usb_device* usb_dev = CDI_UPCAST(device, struct cdi_usb_device, + bus_data); + pid_t pid; + char name[32]; + + sprintf(name, "usb-%04x-%04x", usb_dev->vendor_id, usb_dev->product_id); + pid = init_service_get(name); + if (pid > 0) { + cdi_osdep_provide_usb_device_to(usb_dev, pid); + return 0; + } + + sprintf(name, "usb-%02x-%02x-%02x", usb_dev->class_id, usb_dev->subclass_id, + usb_dev->protocol_id); + pid = init_service_get(name); + if (pid > 0) { + cdi_osdep_provide_usb_device_to(usb_dev, pid); + return 0; + } + + sprintf(name, "usb-%02x-%02x-?", usb_dev->class_id, usb_dev->subclass_id); + pid = init_service_get(name); + if (pid > 0) { + cdi_osdep_provide_usb_device_to(usb_dev, pid); + return 0; + } + + sprintf(name, "usb-%02x-?-?", usb_dev->class_id); + pid = init_service_get(name); + if (pid > 0) { + cdi_osdep_provide_usb_device_to(usb_dev, pid); + return 0; + } + + return -1; +} + void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev) { if (!dev) { @@ -294,6 +371,10 @@ void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev) case CDI_SCSI: cdi_scsi_device_init(CDI_UPCAST(dev, struct cdi_scsi_device, dev)); break; + + case CDI_USB_HCD: + cdi_osdep_provide_hc(CDI_UPCAST(dev, struct cdi_usb_hc, dev)); + break; } } diff --git a/src/modules/cdi/lib/usb.c b/src/modules/cdi/lib/usb.c new file mode 100644 index 0000000..69da43f --- /dev/null +++ b/src/modules/cdi/lib/usb.c @@ -0,0 +1,403 @@ +/* + * Copyright (c) 2015 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cdi.h" +#include "cdi/lists.h" +#include "cdi/misc.h" +#include "cdi/usb.h" +#include "cdi/usb_hcd.h" + + +static struct cdi_usb_driver* usb_drv; +static struct cdi_usb_device** dev_list; +static size_t dev_list_size; + +extern void cdi_osdep_new_device(struct cdi_driver* drv, + struct cdi_device* dev); + +#define RPC(name) \ + static void name(pid_t src, uint32_t corr_id, size_t length, void* data) + +#define rpc_int_resp(val) rpc_send_int_response(src, corr_id, val) + + +struct transmission { + cdi_usb_hc_transaction_t ta; + + void* buffer; + size_t size; + + cdi_usb_transmission_result_t* result_ptr; + + struct { + cdi_usb_transmission_result_t result; + char data[]; + } *shm; + uint32_t shmid; +}; + +struct ipc_usb_hc { + struct cdi_usb_hc cdi; + pid_t pid; + int id; + + list_t* open_transmissions; +}; + + +RPC(hc_found); +RPC(get_endpoint_descriptor); +RPC(control_transfer); +RPC(bulk_transfer); + + +void cdi_usb_driver_register(struct cdi_usb_driver* drv) +{ + assert(!usb_drv); + usb_drv = drv; + + // Wait for all USB device drivers + servmgr_need("usb-storage"); + + register_message_handler(USB_IPC_HC_FOUND, hc_found); + register_message_handler(USB_IPC_GET_ENDPOINT_DESCRIPTOR, + get_endpoint_descriptor); + register_message_handler(USB_IPC_CONTROL_TRANSFER, control_transfer); + register_message_handler(USB_IPC_BULK_TRANSFER, bulk_transfer); +} + + +static void rh_port_down(struct cdi_usb_hub* hub, int index) +{ + struct cdi_usb_hc* hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_port_param par = { + .hc = ipc_hc->id, + .index = index + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_RH_PORT_DISABLE, sizeof(par), + (char*) &par); +} + + +static void rh_port_up(struct cdi_usb_hub* hub, int index) +{ + struct cdi_usb_hc* hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_port_param par = { + .hc = ipc_hc->id, + .index = index + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_RH_PORT_RESET_ENABLE, sizeof(par), + (char*) &par); +} + + +static cdi_usb_port_status_t rh_port_status(struct cdi_usb_hub* hub, int index) +{ + struct cdi_usb_hc* hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_port_param par = { + .hc = ipc_hc->id, + .index = index + }; + + return rpc_get_dword(ipc_hc->pid, USB_IPC_RH_PORT_STATUS, sizeof(par), + (char*) &par); +} + + +static cdi_usb_hc_transaction_t + hc_create_transaction(struct cdi_usb_hc* hc, + const struct cdi_usb_hc_ep_info* target) +{ + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_create_transaction_param par = { + .hc = ipc_hc->id, + .target = *target + }; + + response_t* resp = rpc_get_response(ipc_hc->pid, + USB_IPC_HC_CREATE_TRANSACTION, + sizeof(par), (char*) &par); + if (resp->data_length != sizeof(cdi_usb_hc_transaction_t)) { + free(resp->data); + free(resp); + return NULL; + } + + cdi_usb_hc_transaction_t ta = *(cdi_usb_hc_transaction_t*) resp->data; + free(resp->data); + free(resp); + + return ta; +} + + +static void hc_enqueue(struct cdi_usb_hc* hc, + const struct cdi_usb_hc_transmission* trans) +{ + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct transmission* int_trans = calloc(1, sizeof(*trans)); + + int_trans->ta = trans->ta; + int_trans->result_ptr = trans->result; + + int_trans->shmid = create_shared_memory(trans->size + + sizeof(*trans->result)); + int_trans->shm = open_shared_memory(int_trans->shmid); + + if (trans->token == CDI_USB_IN) { + int_trans->buffer = trans->buffer; + int_trans->size = trans->size; + } else { + memcpy(int_trans->shm->data, trans->buffer, trans->size); + } + + list_push(ipc_hc->open_transmissions, int_trans); + + struct usb_ipc_hc_enqueue_param par = { + .hc = ipc_hc->id, + .trans = *trans, + .shm = int_trans->shmid + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_HC_ENQUEUE, sizeof(par), (char*) &par); +} + + +static void hc_start_transaction(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta) +{ + struct ipc_usb_hc *ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_start_transaction_param par = { + .hc = ipc_hc->id, + .ta = ta + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_HC_START_TRANSACTION, + sizeof(par), (char*) &par); +} + + +static void hc_wait_transaction(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta) +{ + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_wait_transaction_param par = { + .hc = ipc_hc->id, + .ta = ta + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_HC_WAIT_TRANSACTION, sizeof(par), + (char*) &par); + + int i = 0; + struct transmission* int_trans; + while ((int_trans = list_get_element_at(ipc_hc->open_transmissions, i++))) { + if (int_trans->ta == ta) { + list_remove(ipc_hc->open_transmissions, --i); + + if (int_trans->result_ptr) { + *int_trans->result_ptr = int_trans->shm->result; + } + + if (int_trans->buffer) { + memcpy(int_trans->buffer, int_trans->shm->data, + int_trans->size); + } + + close_shared_memory(int_trans->shmid); + free(int_trans); + } + } +} + + +static void hc_destroy_transaction(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta) +{ + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_wait_transaction_param par = { + .hc = ipc_hc->id, + .ta = ta + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_HC_DESTROY_TRANSACTION, sizeof(par), + (char*) &par); + + int i = 0; + struct transmission* int_trans; + while ((int_trans = list_get_element_at(ipc_hc->open_transmissions, i++))) { + if (int_trans->ta == ta) { + list_remove(ipc_hc->open_transmissions, --i); + close_shared_memory(int_trans->shmid); + free(int_trans); + } + } +} + + +static struct cdi_usb_hcd wrapper_hcd = { + .drv = { + .name = "ipc-hci", + .type = CDI_USB_HCD, + }, + + .rh_drv = { + .port_disable = rh_port_down, + .port_reset_enable = rh_port_up, + .port_status = rh_port_status, + }, + + .create_transaction = hc_create_transaction, + .enqueue = hc_enqueue, + .start_transaction = hc_start_transaction, + .wait_transaction = hc_wait_transaction, + .destroy_transaction = hc_destroy_transaction, +}; + + +RPC(hc_found) +{ + struct usb_ipc_hc_found_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + struct cdi_usb_hcd* hcd = malloc(sizeof(*hcd)); + *hcd = wrapper_hcd; + hcd->drv.name = strdup(par->drv_name); + + struct ipc_usb_hc* hc = calloc(1, sizeof(*hc)); + hc->pid = src; + hc->id = par->id; + hc->open_transmissions = list_create(); + hc->cdi.dev.driver = &hcd->drv; + hc->cdi.dev.name = strdup(par->hc_name); + hc->cdi.rh = par->rh; + + struct cdi_usb_hc_bus* hcb = calloc(1, sizeof(*hcb)); + hcb->bus_data.bus_type = CDI_USB_HCD; + hcb->hc = &hc->cdi; + + cdi_osdep_new_device(&usb_drv->drv, + usb_drv->drv.init_device(&hcb->bus_data)); + + rpc_int_resp(1); +} + + +RPC(get_endpoint_descriptor) +{ + struct usb_ipc_get_endpoint_descriptor_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->dev < 0 || par->dev >= (int)dev_list_size || !dev_list[par->dev]) { + rpc_int_resp(0); + return; + } + + struct cdi_usb_device* dev = dev_list[par->dev]; + struct cdi_usb_endpoint_descriptor desc; + + usb_drv->get_endpoint_descriptor(dev, par->ep, &desc); + rpc_send_response(src, corr_id, sizeof(desc), (char*) &desc); +} + + +RPC(control_transfer) +{ + struct usb_ipc_control_transfer_param* par = data; + if (length != sizeof(*par)) { + rpc_send_dword_response(src, corr_id, CDI_USB_DRIVER_ERROR); + return; + } + + if (par->dev < 0 || par->dev >= (int)dev_list_size || !dev_list[par->dev]) { + rpc_send_dword_response(src, corr_id, CDI_USB_DRIVER_ERROR); + return; + } + + struct cdi_usb_device* dev = dev_list[par->dev]; + void* shm = open_shared_memory(par->shm); + + cdi_usb_transmission_result_t res; + res = usb_drv->control_transfer(dev, par->ep, &par->setup, shm); + + close_shared_memory(par->shm); + + rpc_send_dword_response(src, corr_id, res); +} + + +RPC(bulk_transfer) +{ + struct usb_ipc_bulk_transfer_param* par = data; + if (length != sizeof(*par)) { + rpc_send_dword_response(src, corr_id, CDI_USB_DRIVER_ERROR); + return; + } + + if (par->dev < 0 || par->dev >= (int)dev_list_size || !dev_list[par->dev]) { + rpc_send_dword_response(src, corr_id, CDI_USB_DRIVER_ERROR); + return; + } + + struct cdi_usb_device* dev = dev_list[par->dev]; + void* shm = open_shared_memory(par->shm); + + cdi_usb_transmission_result_t res; + res = usb_drv->bulk_transfer(dev, par->ep, shm, par->size); + + close_shared_memory(par->shm); + + rpc_send_dword_response(src, corr_id, res); +} + + +void cdi_osdep_provide_usb_device_to(struct cdi_usb_device* usb_dev, pid_t pid); + +void cdi_osdep_provide_usb_device_to(struct cdi_usb_device* usb_dev, pid_t pid) +{ + int idx; + for (idx = 0; idx < (int)dev_list_size && !dev_list[idx]; idx++); + + if (idx == (int)dev_list_size) { + dev_list_size = (dev_list_size + 4) * 3 / 2; + dev_list = realloc(dev_list, dev_list_size * sizeof(*dev_list)); + memset(dev_list + idx, 0, (dev_list_size - idx) * sizeof(*dev_list)); + } + + dev_list[idx] = usb_dev; + dev_list[idx]->meta.pid = getpid(); + dev_list[idx]->meta.id = idx; + + rpc_get_int(pid, USB_IPC_DEVICE_FOUND, + sizeof(*dev_list[idx]), (char*) dev_list[idx]); +} diff --git a/src/modules/cdi/lib/usb_dd.c b/src/modules/cdi/lib/usb_dd.c new file mode 100644 index 0000000..e1a379d --- /dev/null +++ b/src/modules/cdi/lib/usb_dd.c @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2015 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cdi.h" +#include "cdi/misc.h" +#include "cdi/usb.h" + + +extern void cdi_osdep_new_device(struct cdi_driver* drv, + struct cdi_device* dev); + +static struct cdi_driver* usb_dd; + +#define RPC(name) \ + static void name(pid_t src, uint32_t corr_id, size_t length, void* data) + +#define rpc_int_resp(val) rpc_send_int_response(src, corr_id, val) + + +RPC(device_found) +{ + struct cdi_usb_device* dev = data; + if (length != sizeof(*dev)) { + rpc_int_resp(0); + return; + } + + cdi_osdep_new_device(usb_dd, usb_dd->init_device(&dev->bus_data)); + + rpc_int_resp(0); +} + + +void cdi_osdep_set_up_usb_dd_ipc(struct cdi_driver* drv); + +void cdi_osdep_set_up_usb_dd_ipc(struct cdi_driver* drv) +{ + assert(!usb_dd); + usb_dd = drv; + + register_message_handler(USB_IPC_DEVICE_FOUND, device_found); +} + + +void cdi_osdep_handle_usb_device(struct cdi_usb_bus_device_pattern* p); + +void cdi_osdep_handle_usb_device(struct cdi_usb_bus_device_pattern* p) +{ + char alias[32]; + + if (p->vendor_id < 0 && p->product_id < 0) { + assert(p->class_id >= 0); + if (p->protocol_id < 0) { + assert(p->subclass_id < 0); + snprintf(alias, 32, "usb-%02x-?-?", p->class_id); + } else if (p->subclass_id < 0) { + snprintf(alias, 32, "usb-%02x-%02x-?", p->class_id, p->subclass_id); + } else { + snprintf(alias, 32, "usb-%02x-%02x-%02x", p->class_id, + p->subclass_id, p->protocol_id); + } + } else if (p->class_id < 0 && p->subclass_id < 0 && p->protocol_id < 0) { + assert(p->vendor_id >= 0 && p->product_id >= 0); + snprintf(alias, 32, "usb-%04x-%04x", p->vendor_id, p->product_id); + } else { + assert(0); + } + + init_service_register(alias); +} + + +void cdi_usb_get_endpoint_descriptor(struct cdi_usb_device* dev, int ep, + struct cdi_usb_endpoint_descriptor* desc) +{ + struct usb_ipc_get_endpoint_descriptor_param par = { + .dev = dev->meta.id, + .ep = ep + }; + + response_t* resp = rpc_get_response(dev->meta.pid, + USB_IPC_GET_ENDPOINT_DESCRIPTOR, + sizeof(par), (char*) &par); + assert(resp->data_length == sizeof(*desc)); + + memcpy(desc, resp->data, sizeof(*desc)); + free(resp->data); + free(resp); +} + + +cdi_usb_transmission_result_t + cdi_usb_control_transfer(struct cdi_usb_device* dev, int ep, + const struct cdi_usb_setup_packet* setup, + void* data) +{ + uint32_t shmid = 0; + void* shm = NULL; + + if (setup->w_length) { + shmid = create_shared_memory(setup->w_length); + shm = open_shared_memory(shmid); + if (setup->bm_request_type & CDI_USB_CREQ_OUT) { + memcpy(shm, data, setup->w_length); + } + } + + struct usb_ipc_control_transfer_param par = { + .dev = dev->meta.id, + .ep = ep, + .setup = *setup, + .shm = shmid + }; + + cdi_usb_transmission_result_t res; + res = rpc_get_dword(dev->meta.pid, USB_IPC_CONTROL_TRANSFER, + sizeof(par), (char*) &par); + + if (setup->w_length) { + if (setup->bm_request_type & CDI_USB_CREQ_IN) { + memcpy(data, shm, setup->w_length); + } + close_shared_memory(shmid); + } + + return res; +} + + +cdi_usb_transmission_result_t cdi_usb_bulk_transfer(struct cdi_usb_device* dev, + int ep, void* data, + size_t size) +{ + uint32_t shmid = create_shared_memory(size); + void* shm = open_shared_memory(shmid); + + // FIXME (We don't know which direction the transfer is going) + memcpy(shm, data, size); + + struct usb_ipc_bulk_transfer_param par = { + .dev = dev->meta.id, + .ep = ep, + .size = size, + .shm = shmid + }; + + cdi_usb_transmission_result_t res; + res = rpc_get_dword(dev->meta.pid, USB_IPC_BULK_TRANSFER, + sizeof(par), (char*) &par); + + // FIXME (Same as above) + memcpy(data, shm, size); + + close_shared_memory(shmid); + + return res; +} diff --git a/src/modules/cdi/lib/usb_hcd.c b/src/modules/cdi/lib/usb_hcd.c new file mode 100644 index 0000000..b06722c --- /dev/null +++ b/src/modules/cdi/lib/usb_hcd.c @@ -0,0 +1,413 @@ +/* + * Copyright (c) 2015 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cdi/misc.h" +#include "cdi/usb_hcd.h" + + +extern void cdi_osdep_device_found(void); + + +struct transmission { + uint32_t shmid; + void* mapping; +}; + +struct transaction { + cdi_usb_hc_transaction_t ta; + list_t* transmissions; + bool started; +}; + +struct usb_hc { + struct cdi_usb_hc* cdi; + + struct transaction** transactions; + size_t transactions_size; + size_t transactions_first_free; +}; + +static struct usb_hc** hc_list; +static size_t hc_list_size, hc_list_elements; + +#define RPC(name) \ + static void name(pid_t src, uint32_t corr_id, size_t length, void* data) + +#define rpc_int_resp(val) rpc_send_int_response(src, corr_id, val) + + +RPC(hc_create_transaction); +RPC(hc_enqueue); +RPC(hc_start_transaction); +RPC(hc_wait_transaction); +RPC(hc_destroy_transaction); + +RPC(rh_port_disable); +RPC(rh_port_reset_enable); +RPC(rh_port_status); + + +static void set_up_hc_ipc(void) +{ + static bool ipc_set_up; + + if (ipc_set_up) { + return; + } + ipc_set_up = true; + + register_message_handler(USB_IPC_HC_CREATE_TRANSACTION, + hc_create_transaction); + register_message_handler(USB_IPC_HC_ENQUEUE, hc_enqueue); + register_message_handler(USB_IPC_HC_START_TRANSACTION, + hc_start_transaction); + register_message_handler(USB_IPC_HC_WAIT_TRANSACTION, hc_wait_transaction); + register_message_handler(USB_IPC_HC_DESTROY_TRANSACTION, + hc_destroy_transaction); + + register_message_handler(USB_IPC_RH_PORT_DISABLE, rh_port_disable); + register_message_handler(USB_IPC_RH_PORT_RESET_ENABLE, + rh_port_reset_enable); + register_message_handler(USB_IPC_RH_PORT_STATUS, rh_port_status); +} + + +RPC(hc_create_transaction) +{ + struct usb_ipc_hc_create_transaction_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct usb_hc* hc = hc_list[par->hc]; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->cdi->dev.driver, + struct cdi_usb_hcd, drv); + struct transaction* ta = malloc(sizeof(*ta)); + + *ta = (struct transaction){ + .ta = hcd->create_transaction(hc->cdi, &par->target), + .transmissions = list_create() + }; + + size_t idx; + for (idx = hc->transactions_first_free; + idx < hc->transactions_size && hc->transactions[idx]; idx++); + + if (idx >= hc->transactions_size) { + size_t old_ts = hc->transactions_size; + hc->transactions_size = (hc->transactions_size + 4) * 2 / 3; + hc->transactions = realloc(hc->transactions, + hc->transactions_size + * sizeof(*hc->transactions)); + memset(hc->transactions + old_ts, 0, + (hc->transactions_size - old_ts) * sizeof(*hc->transactions)); + } + hc->transactions[idx] = ta; + hc->transactions_first_free = idx + 1; + + cdi_usb_hc_transaction_t ipc_ta = (cdi_usb_hc_transaction_t)(uintptr_t)idx; + rpc_send_response(src, corr_id, sizeof(ipc_ta), (char*) &ipc_ta); +} + + +RPC(hc_enqueue) +{ + struct usb_ipc_hc_enqueue_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct usb_hc* hc = hc_list[par->hc]; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->cdi->dev.driver, + struct cdi_usb_hcd, drv); + + if ((uintptr_t)par->trans.ta > hc->transactions_size || + !hc->transactions[(uintptr_t)par->trans.ta]) + { + rpc_int_resp(0); + return; + } + + struct transaction* ta = hc->transactions[(uintptr_t)par->trans.ta]; + + if (ta->started) { + rpc_int_resp(0); + return; + } + + struct transmission* tm = malloc(sizeof(*tm)); + *tm = (struct transmission) { + .shmid = par->shm, + .mapping = open_shared_memory(par->shm) + }; + + par->trans.ta = ta->ta; + par->trans.result = tm->mapping; + par->trans.buffer = par->trans.result + 1; + + hcd->enqueue(hc->cdi, &par->trans); + + list_push(ta->transmissions, tm); + + rpc_int_resp(0); +} + + +RPC(hc_start_transaction) +{ + struct usb_ipc_hc_start_transaction_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct usb_hc* hc = hc_list[par->hc]; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->cdi->dev.driver, + struct cdi_usb_hcd, drv); + + if ((uintptr_t)par->ta > hc->transactions_size || + !hc->transactions[(uintptr_t)par->ta]) + { + rpc_int_resp(0); + return; + } + + struct transaction* ta = hc->transactions[(uintptr_t)par->ta]; + ta->started = true; + + hcd->start_transaction(hc->cdi, ta->ta); + + rpc_int_resp(0); +} + + +RPC(hc_wait_transaction) +{ + struct usb_ipc_hc_wait_transaction_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct usb_hc* hc = hc_list[par->hc]; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->cdi->dev.driver, + struct cdi_usb_hcd, drv); + + if ((uintptr_t)par->ta > hc->transactions_size || + !hc->transactions[(uintptr_t)par->ta]) + { + rpc_int_resp(0); + return; + } + + struct transaction* ta = hc->transactions[(uintptr_t)par->ta]; + + if (!ta->started) { + rpc_int_resp(0); + return; + } + + hcd->wait_transaction(hc->cdi, ta->ta); + + struct transmission* tm; + while ((tm = list_pop(ta->transmissions))) { + close_shared_memory(tm->shmid); + free(tm); + } + + rpc_int_resp(0); +} + + +RPC(hc_destroy_transaction) +{ + struct usb_ipc_hc_wait_transaction_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct usb_hc* hc = hc_list[par->hc]; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->cdi->dev.driver, + struct cdi_usb_hcd, drv); + + if ((uintptr_t)par->ta > hc->transactions_size || + !hc->transactions[(uintptr_t)par->ta]) + { + rpc_int_resp(0); + return; + } + + struct transaction* ta = hc->transactions[(uintptr_t)par->ta]; + + hcd->destroy_transaction(hc->cdi, ta->ta); + + hc->transactions[(uintptr_t)par->ta] = NULL; + if (hc->transactions_first_free > (uintptr_t)par->ta) { + hc->transactions_first_free = (uintptr_t)par->ta; + } + + struct transmission* tm; + while ((tm = list_pop(ta->transmissions))) { + close_shared_memory(tm->shmid); + free(tm); + } + + list_destroy(ta->transmissions); + free(ta); + + rpc_int_resp(0); +} + + +RPC(rh_port_disable) +{ + struct usb_ipc_hc_port_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct cdi_usb_hc* hc = hc_list[par->hc]->cdi; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->dev.driver, struct cdi_usb_hcd, + drv); + + hcd->rh_drv.port_disable(&hc->rh, par->index); + + rpc_int_resp(0); +} + + +RPC(rh_port_reset_enable) +{ + struct usb_ipc_hc_port_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct cdi_usb_hc* hc = hc_list[par->hc]->cdi; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->dev.driver, struct cdi_usb_hcd, + drv); + + hcd->rh_drv.port_reset_enable(&hc->rh, par->index); + + rpc_int_resp(0); +} + + +RPC(rh_port_status) +{ + struct usb_ipc_hc_port_param* par = data; + if (length != sizeof(*par)) { + rpc_send_dword_response(src, corr_id, 0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_send_dword_response(src, corr_id, 0); + return; + } + + struct cdi_usb_hc* hc = hc_list[par->hc]->cdi; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->dev.driver, struct cdi_usb_hcd, + drv); + + rpc_send_dword_response(src, corr_id, + hcd->rh_drv.port_status(&hc->rh, par->index)); +} + + +void cdi_osdep_provide_hc(struct cdi_usb_hc* cdi_hc); + +void cdi_osdep_provide_hc(struct cdi_usb_hc* cdi_hc) +{ + struct usb_hc* hc = calloc(1, sizeof(*hc)); + hc->cdi = cdi_hc; + + set_up_hc_ipc(); + + if (hc_list_size == hc_list_elements) { + size_t old_hls = hc_list_size; + hc_list_size = (hc_list_size + 4) * 2 / 3; + hc_list = realloc(hc_list, hc_list_size * sizeof(*hc_list)); + memset(hc_list + old_hls, 0, + (hc_list_size - old_hls) * sizeof(*hc_list)); + } + + int id = hc_list_elements; + hc_list[hc_list_elements++] = hc; + + static pid_t usb_pid; + if (!usb_pid) { + servmgr_need("usb"); + usb_pid = init_service_get("usb"); + } + + struct usb_ipc_hc_found_param par; + + strncpy(par.drv_name, cdi_hc->dev.driver->name, 31); + par.drv_name[31] = 0; + + strncpy(par.hc_name, cdi_hc->dev.name, 31); + par.hc_name[31] = 0; + + par.id = id; + par.rh = cdi_hc->rh; + + rpc_get_int(usb_pid, USB_IPC_HC_FOUND, sizeof(par), (char*) &par); +} diff --git a/src/modules/include/usb-ipc.h b/src/modules/include/usb-ipc.h new file mode 100644 index 0000000..ae6ccf0 --- /dev/null +++ b/src/modules/include/usb-ipc.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2015 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. + */ + +#ifndef _USB_IPC_H +#define _USB_IPC_H + +#include + +#include +#include + + +// Provided by HCDs +#define USB_IPC_HC_CREATE_TRANSACTION "HCCRTTA" +#define USB_IPC_HC_ENQUEUE "HCNQXFR" +#define USB_IPC_HC_START_TRANSACTION "HCSTTTA" +#define USB_IPC_HC_WAIT_TRANSACTION "HCWAITA" +#define USB_IPC_HC_DESTROY_TRANSACTION "HCDTYTA" + +#define USB_IPC_RH_PORT_DISABLE "RHPRTDN" +#define USB_IPC_RH_PORT_RESET_ENABLE "RHPRTUP" +#define USB_IPC_RH_PORT_STATUS "RHPRTST" + + +struct usb_ipc_hc_create_transaction_param { + int hc; + struct cdi_usb_hc_ep_info target; +}; + +struct usb_ipc_hc_enqueue_param { + int hc; + // @buffer and @result are ignored; @result is put at the start of the SHM + // provided, @buffer has to be placed directly afterwards + struct cdi_usb_hc_transmission trans; + uint32_t shm; +}; + +struct usb_ipc_hc_start_transaction_param { + int hc; + cdi_usb_hc_transaction_t ta; +}; + +struct usb_ipc_hc_wait_transaction_param { + int hc; + cdi_usb_hc_transaction_t ta; +}; + +struct usb_ipc_hc_destroy_transaction_param { + int hc; + cdi_usb_hc_transaction_t ta; +}; + +struct usb_ipc_hc_port_param { + int hc, index; +}; + + +// Provided by the bus driver +#define USB_IPC_HC_FOUND "HCFOUND" + +#define USB_IPC_GET_ENDPOINT_DESCRIPTOR "GEPDESC" + +#define USB_IPC_CONTROL_TRANSFER "CTRLXFR" +#define USB_IPC_BULK_TRANSFER "BULKXFR" + + +struct usb_ipc_hc_found_param { + char drv_name[32], hc_name[32]; + int id; + struct cdi_usb_hub rh; +}; + +struct usb_ipc_get_endpoint_descriptor_param { + int dev, ep; +}; + +struct usb_ipc_control_transfer_param { + int dev, ep; + struct cdi_usb_setup_packet setup; + uint32_t shm; // @buffer +}; + +struct usb_ipc_bulk_transfer_param { + int dev, ep; + size_t size; + uint32_t shm; // @buffer +}; + + +// Provided by device drivers +#define USB_IPC_DEVICE_FOUND "USBPLUG" + + +// The parameter for device_found() is a struct cdi_usb_device. + +#endif -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:08 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:08 +0100 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_18/22=5D_usb=3A_Entferne_t?= =?utf-8?q?=C3=BDndur-eigene_Treiber?= In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-19-git-send-email-max@tyndur.org> - Diese Treiber sollen durch den CDI-Treiber ersetzt werden. Dieser besitzt noch zwar weder UHCI- noch MSD-CBI-Unterstützung (bspw. Diskettenlaufwerke), ist aber tatsächlich besser getestet und EHCI und Bulk-only-MSD reichen für normale USB-Sticks aus bzw. sind sogar besser geeignet. Signed-off-by: Max Reitz --- src/modules/usb/include/usb-ddrv.h | 59 ----- src/modules/usb/include/usb-hc.h | 141 ----------- src/modules/usb/include/usb-hub.h | 49 ---- src/modules/usb/include/usb-server.h | 153 ------------ src/modules/usb/include/usb-structs.h | 176 -------------- src/modules/usb/include/usb-trans.h | 66 ----- src/modules/usb/msd/Makefile.all | 8 - src/modules/usb/msd/init.c | 206 ---------------- src/modules/usb/msd/lostio.c | 218 ----------------- src/modules/usb/msd/main.c | 65 ----- src/modules/usb/msd/msd-bo.c | 104 -------- src/modules/usb/msd/msd-bo.h | 99 -------- src/modules/usb/msd/msd-cbi.c | 86 ------- src/modules/usb/msd/msd-cbi.h | 71 ------ src/modules/usb/msd/msd.h | 154 ------------ src/modules/usb/msd/rpc.c | 89 ------- src/modules/usb/msd/scsi.c | 240 ------------------ src/modules/usb/msd/scsi.h | 219 ----------------- src/modules/usb/msd/usb-lib.c | 207 ---------------- src/modules/usb/msd/usb-lib.h | 124 ---------- src/modules/usb/uhci/Makefile.all | 8 - src/modules/usb/uhci/ctrl.c | 96 -------- src/modules/usb/uhci/init.c | 265 -------------------- src/modules/usb/uhci/main.c | 73 ------ src/modules/usb/uhci/rpc.c | 297 ----------------------- src/modules/usb/uhci/trans.c | 316 ------------------------ src/modules/usb/uhci/uhci.h | 269 -------------------- src/modules/usb/usb1/Makefile.all | 8 - src/modules/usb/usb1/detect.c | 444 ---------------------------------- src/modules/usb/usb1/hub.c | 180 -------------- src/modules/usb/usb1/main.c | 59 ----- src/modules/usb/usb1/rpc.c | 350 --------------------------- src/modules/usb/usb1/transfer.c | 123 ---------- src/modules/usb/usb1/usb.h | 209 ---------------- 34 files changed, 5231 deletions(-) delete mode 100644 src/modules/usb/include/usb-ddrv.h delete mode 100644 src/modules/usb/include/usb-hc.h delete mode 100644 src/modules/usb/include/usb-hub.h delete mode 100644 src/modules/usb/include/usb-server.h delete mode 100644 src/modules/usb/include/usb-structs.h delete mode 100644 src/modules/usb/include/usb-trans.h delete mode 100644 src/modules/usb/msd/Makefile.all delete mode 100644 src/modules/usb/msd/init.c delete mode 100644 src/modules/usb/msd/lostio.c delete mode 100644 src/modules/usb/msd/main.c delete mode 100644 src/modules/usb/msd/msd-bo.c delete mode 100644 src/modules/usb/msd/msd-bo.h delete mode 100644 src/modules/usb/msd/msd-cbi.c delete mode 100644 src/modules/usb/msd/msd-cbi.h delete mode 100644 src/modules/usb/msd/msd.h delete mode 100644 src/modules/usb/msd/rpc.c delete mode 100644 src/modules/usb/msd/scsi.c delete mode 100644 src/modules/usb/msd/scsi.h delete mode 100644 src/modules/usb/msd/usb-lib.c delete mode 100644 src/modules/usb/msd/usb-lib.h delete mode 100644 src/modules/usb/uhci/Makefile.all delete mode 100644 src/modules/usb/uhci/ctrl.c delete mode 100644 src/modules/usb/uhci/init.c delete mode 100644 src/modules/usb/uhci/main.c delete mode 100644 src/modules/usb/uhci/rpc.c delete mode 100644 src/modules/usb/uhci/trans.c delete mode 100644 src/modules/usb/uhci/uhci.h delete mode 100644 src/modules/usb/usb1/Makefile.all delete mode 100644 src/modules/usb/usb1/detect.c delete mode 100644 src/modules/usb/usb1/hub.c delete mode 100644 src/modules/usb/usb1/main.c delete mode 100644 src/modules/usb/usb1/rpc.c delete mode 100644 src/modules/usb/usb1/transfer.c delete mode 100644 src/modules/usb/usb1/usb.h diff --git a/src/modules/usb/include/usb-ddrv.h b/src/modules/usb/include/usb-ddrv.h deleted file mode 100644 index d213cd7..0000000 --- a/src/modules/usb/include/usb-ddrv.h +++ /dev/null @@ -1,59 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_DDRV_H -#define USB_DDRV_H - -#include - - -#define RPC_USB_DDRV_PLUG_FNN "USB_DDRV_PLUG" - -// Ein Wert ist -1, wenn der Treiber alle Geräte annimmt. -struct usb_handle_devs { - int vendor_id, device_id; - int class, subclass, protocol; -}; - -// Wird vom USB-Bustreiber an einen Gerätetreiber weitergegeben, wenn ein Gerät -// gefunden wurde -struct usb_prov_dev { - // Handelt es sich beim Gerät um ein Interface und nicht um ein physisches - // Gerät, so ist dies true - bool is_interface; - // Geräte-ID (vom USB-Bustreiber zu verwenden) - void* dev_id; - // Interface-ID - int interface_id; - // Typ des Geräts - struct usb_handle_devs type; -}; - -#endif diff --git a/src/modules/usb/include/usb-hc.h b/src/modules/usb/include/usb-hc.h deleted file mode 100644 index a70598a..0000000 --- a/src/modules/usb/include/usb-hc.h +++ /dev/null @@ -1,141 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_HC_H -#define USB_HC_H - -#include -#include - -#include "usb-structs.h" -#include "usb-trans.h" - -// Beschreibt einen USB-Hostcontroller -struct usb_hc { - // Anzahl der Rootports - int root_ports; - // Für den HCD frei verfügbar - void* hcd_avail; - // PID des HCD - pid_t pid; - // USB-Adressenzähler - int addr_counter; -}; - -// RPC-Funktion für USB-Bustreiber (vorrangig Roothubfunktionen) -#define RPC_USB_HC_CONTROL_FNN "HC_CONTROL" -// RPC-Funktion für USB-Bustreiber (vorrangig Transferfunktionen) -#define RPC_USB_HC_TRANSFER_FNN "HC_TRANSFER" - -// PIDs für IN, OUT und SETUP -#define USB_OUT 0xE1 -#define USB_IN 0x69 -#define USB_SETUP 0x2D - -// Roothubfunktionen -enum usb_hc_ctrl_rt { - // Portstatus überprüfen - HC_CTRL_SCAN_PORT = 0, - // Port deaktivieren - HC_CTRL_DSBL_PORT, - // Resetsignal treiben - HC_CTRL_RESET, - // Pipe erstellen - HC_CTRL_ADD_PIPE -}; - - -// Allgemeiner HC_CONTROL-Request -struct usb_hc_ctrl_req { - // Funktion - enum usb_hc_ctrl_rt type; - // Hostcontroller - struct usb_hc hc; -}; - -// Rootport überprüfen -struct usb_hc_ctrl_scan { - struct usb_hc_ctrl_req req; - // Portindex - int port; -}; - -// Rootport deaktivieren -struct usb_hc_ctrl_dsbl { - struct usb_hc_ctrl_req req; - // Portindex - int port; -}; - -// Resetsignal an einem Rootport treiben -struct usb_hc_ctrl_rst { - struct usb_hc_ctrl_req req; - // Portindex - int port; -}; - -// Pipe hinzufügen -struct usb_hc_ctrl_appe { - struct usb_hc_ctrl_req req; - // Informationen zur neuen Pipe - struct usb_trans_pipe pipe; -}; - -// Allgemeine Transferanfrage -struct usb_hc_trs { - // Funktion - enum usb_trans_type type; - // Hostcontroller - struct usb_hc hc; - // Pipe, durch die der Transfer laufen soll - struct usb_trans_pipe pipe; -}; - -// Controltransfer -struct usb_hc_trs_ctrl { - struct usb_hc_trs trs; - // SETUP-Paket - struct usb_setup_packet setup; - // SHM-Bereich für die Datenphase - uint32_t shm; -}; - -// Bulktransfer -struct usb_hc_trs_bulk { - struct usb_hc_trs trs; - // true zum Empfangen - bool receive; - // Anzahl der zu übertragenden Bytes - size_t length; - // SHM-Bereich für die übertragenen Daten - uint32_t shm; -}; - -#endif diff --git a/src/modules/usb/include/usb-hub.h b/src/modules/usb/include/usb-hub.h deleted file mode 100644 index 478c2bc..0000000 --- a/src/modules/usb/include/usb-hub.h +++ /dev/null @@ -1,49 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_HUB_H -#define USB_HUB_H - -// In den unteren acht Bits wird die Geschwindigkeit gespeichert (viel Platz -// für zukünftige Neuerungen...) -#define USB_HUB_SPEED 0xFF -// Low Speed -#define USB_HUB_LOW_SPEED (1 << 0) -// Full Speed -#define USB_HUB_FULL_SPEED (1 << 1) -// High Speed -#define USB_HUB_HIGH_SPEED (1 << 2) - -// Gerät angeschlossen -#define USB_HUB_DEVICE (1 << 8) -// Status hat sich seit der letzten Anfrage verändert -#define USB_HUB_CHANGE (1 << 9) - -#endif diff --git a/src/modules/usb/include/usb-server.h b/src/modules/usb/include/usb-server.h deleted file mode 100644 index 6a98707..0000000 --- a/src/modules/usb/include/usb-server.h +++ /dev/null @@ -1,153 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_SERVER_H -#define USB_SERVER_H - -#include -#include - -#include "usb-ddrv.h" -#include "usb-server.h" -#include "usb-structs.h" -#include "usb-trans.h" - -// Funktion zum Registrieren von Hostcontrollern -#define RPC_USB_SV_REGHC_FNN "HCD_REG" -// Funktion zum Registrieren von Gerätetreibern -#define RPC_USB_SV_REGDD_FNN "DDRV_REG" -// Funktion zur Steuerung von Geräten (Daten austauschen, etc.) -#define RPC_USB_SV_DEVCT_FNN "DEV_CTL" - -// Beschreibt eine USB-Pipe -struct usb_pipe { - // Da die von normalen Treibern sowieso nicht dereferenziert werden dürfen, - // können diese Strukturen auch undefiniert bleiben. - // USB-Gerät (nur für den Bustreiber) - struct usb_device* dev; - // Endpunktdeskriptor (nur für den Bustreiber) - struct usb_endpoint_descriptor* ep; - - // Pipe, zur Kommunikation mit dem Hostcontroller - struct usb_trans_pipe trs; -}; - -// Funktionen für DEV_CTRL (mit Rückgabetypen) -enum usb_devctrl_reqtype { - // Gibt Anzahl der Endpunkte zurück - USB_DC_NUM_ENDPOINTS = 0, // int - // Gibt die Interface-ID zurück - USB_DC_GET_IFC_ID, // int - // Gibt den Endpunktdeskriptor zurück - USB_DC_GET_ENDPOINT, // response - // Gibt eine Pipe zurück (und erstellt sie) - USB_DC_GET_PIPE, // response - // Entstallt eine Pipe/den Endpunkt - USB_DC_CLEAR_HALT, // int - // Führt einen Controltransfer durch - USB_DC_TRSF_CONTROL, // int - // Führt einen Bulktransfer durch - USB_DC_TRSF_BULK // int -}; - -// Allgemeine Anfrage für DEV_CTRL -struct usb_devctrl_req { - // Typ der Anrage - enum usb_devctrl_reqtype type; -}; - -// Gibt die Anzahl der Endpunkte zurück -struct usb_devctrl_gne { - struct usb_devctrl_req req; - // Abzufragendes USB-Gerät - struct usb_prov_dev dev; -}; - -// Gibt die Interface-ID zurück -struct usb_devctrl_gii { - struct usb_devctrl_req req; - // Abzufrasgendes USB-Gerät - struct usb_prov_dev dev; -}; - -// Gibt den Endpunktdeskriptor zurück -struct usb_devctrl_gep { - struct usb_devctrl_req req; - // USB-Gerät - struct usb_prov_dev dev; - // Nummer des Endpunkts (nicht die ID) - int ep_number; -}; - -// Erstellt eine USB-Pipe und gibt sie zurück -struct usb_devctrl_gpp { - struct usb_devctrl_req req; - // USB-Gerät - struct usb_prov_dev dev; - // Nummer des Zielendpunkts - int ep_number; -}; - -// Entstallt eine Pipe resp. den Endpunkt -struct usb_devctrl_clh { - struct usb_devctrl_req req; - // USB-Pipe - struct usb_pipe pipe; -}; - -// Führt einen Controltransfer durch -struct usb_devctrl_tct { - struct usb_devctrl_req req; - // Ist EP0 das Ziel, so muss dev_id auf den Wert aus usb_prov_dev gesetzt - // werden, sonst pipe auf die entsprechende USB-Pipe. - union { - struct usb_pipe pipe; - void* dev_id; - } dst; - // true, wenn durch die Default Control Pipe übertragen werden soll - bool ep0; - // SETUP-Paket - struct usb_setup_packet setup; - // SHM-Bereich für die Datenphase - uint32_t shm; -}; - -// Führt einen Bulktransfer durch -struct usb_devctrl_tbk { - struct usb_devctrl_req req; - // USB-Pipe - struct usb_pipe pipe; - // Anzahl der zu übertragenden Bytes - size_t length; - // SHM-Bereich für die Daten - uint32_t shm; -}; - -#endif diff --git a/src/modules/usb/include/usb-structs.h b/src/modules/usb/include/usb-structs.h deleted file mode 100644 index f0b5927..0000000 --- a/src/modules/usb/include/usb-structs.h +++ /dev/null @@ -1,176 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_STRUCTS_H -#define USB_STRUCTS_H - -#include - -#define USB_LOW_SPEED USB_HUB_LOW_SPEED -#define USB_FULL_SPEED USB_HUB_FULL_SPEED -#define USB_HIGH_SPEED USB_HUB_HIGH_SPEED - -enum usb_state { - USB_ATTACHED = 0, - USB_POWERED, - USB_DEFAULT, - USB_ADDRESS, - USB_CONFIGURED, - USB_SUSPENDED -}; - -struct usb_setup_packet { - uint8_t type; - uint8_t request; - uint16_t value; - uint16_t index; - uint16_t length; -} __attribute__((packed)); - -struct usb_descriptor { - uint8_t length; - uint8_t desc_type; -} __attribute__((packed)); - - -#define USB_SETUP_OUT (0 << 7) -#define USB_SETUP_IN (1 << 7) -#define USB_SETUP_REQ_STD (0 << 5) -#define USB_SETUP_REQ_CLS (1 << 5) -#define USB_SETUP_REQ_VDR (2 << 5) -#define USB_SETUP_REC_DEV 0 -#define USB_SETUP_REC_IF 1 -#define USB_SETUP_REC_EP 2 -#define USB_SETUP_REC_OTH 3 - -enum usb_setup_requst { - USB_GET_STATUS = 0, - USB_CLEAR_FEATURE = 1, - USB_SET_FEATURE = 3, - USB_SET_ADDRESS = 5, - USB_GET_DESCRIPTOR = 6, - USB_SET_DESCRIPTOR = 7, - USB_GET_CONFIGURATION = 8, - USB_SET_CONFIGURATION = 9, - USB_GET_INTERFACE = 10, - USB_SET_INTERFACE = 11, - USB_SYNC_FRAME = 12 -}; - -enum usb_features { - USB_ENDPOINT_HALT = 0, - USB_DEVICE_REMOTE_WAKEUP = 1, - USB_TEST_MODE = 2 -}; - -enum usb_setup_descriptor { - USB_DESC_DEVICE = 1 << 8, - USB_DESC_CONFIGURATION = 2 << 8, - USB_DESC_STRING = 3 << 8, - USB_DESC_INTERFACE = 4 << 8, - USB_DESC_ENDPOINT = 5 << 8, - USB_DESC_DEVICE_QUALIFIER = 6 << 8, - USB_DESC_OTHER_SPEED_CONFIGURATION = 7 << 8, - USB_DESC_INTERFACE_POWER = 8 << 8, - - USB_DESC_HUB = 41 << 8 -}; - -struct usb_device_descriptor { - struct usb_descriptor desc; - uint16_t bcd_usb; - uint8_t class, subclass, protocol; - uint8_t mps0; - uint16_t vendor_id, device_id; - uint16_t bcd_device; - uint8_t i_manufacturer, i_product, i_serial; - uint8_t num_configurations; -} __attribute__((packed)); - -struct usb_config_descriptor -{ - struct usb_descriptor desc; - uint16_t total_length; - uint8_t num_interfaces; - uint8_t configuration_value; - uint8_t i_configuration; - unsigned rsvd0 : 5; - unsigned remote_wakeup : 1; - unsigned self_powered : 1; - unsigned rsvd1 : 1; - uint8_t max_power; -} __attribute__((packed)); - -struct usb_interface_descriptor -{ - struct usb_descriptor desc; - uint8_t interface_number; - uint8_t alternate_setting; - uint8_t num_endpoints; - uint8_t class, subclass, protocol; - uint8_t i_interface; -} __attribute__((packed)); - -#define USB_EP_CONTROL 0 -#define USB_EP_ISOCHRONOUS 1 -#define USB_EP_BULK 2 -#define USB_EP_INTERRUPT 3 - -struct usb_endpoint_descriptor { - struct usb_descriptor desc; - unsigned number : 4; - unsigned rsvd1 : 3; - unsigned direction : 1; - unsigned type : 2; - unsigned sync : 2; - unsigned usage : 2; - unsigned rsvd2 : 2; - unsigned mps : 11; - unsigned trans_opp : 2; - unsigned rsvd3 : 3; - uint8_t interval; -} __attribute__((packed)); - -struct usb_hub_descriptor { - struct usb_descriptor desc; - uint8_t nbr_ports; - unsigned power_mode : 2; - unsigned compound : 1; - unsigned oc_protect : 2; - unsigned tt : 2; - unsigned pindicator : 1; - uint8_t rsvd; - uint8_t pwr_on_2_pwr_good; - uint8_t hub_contr_current; - // Die folgenden Felder haben eine variable Größe -} __attribute__((packed)); - -#endif - diff --git a/src/modules/usb/include/usb-trans.h b/src/modules/usb/include/usb-trans.h deleted file mode 100644 index c5d3a1f..0000000 --- a/src/modules/usb/include/usb-trans.h +++ /dev/null @@ -1,66 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_TRANS_H -#define USB_TRANS_H - -#include - -#include "usb-hub.h" - -// Übertragunsart -enum usb_trans_type { - USB_TRST_CONTROL = 0, - USB_TRST_ISOCHRONOUS = 1, - USB_TRST_BULK = 2, - USB_TRST_INTERRUPT = 3 -}; - -// USB-Pipe für die Kommunikation mit dem Hostcontroller -struct usb_trans_pipe { - // USB-Adresse (0 bis 127) - int usb_addr; - // Endpunkt-ID - int ep_id; - // MPS des Endpunkts - int mps; - - // Typ der Pipe - enum usb_trans_type type; - // Geschwindigkeit des Geräts - int speed; - - // Toggle-Bit - int toggle; - // true, wenn der Endpunkt gestallt ist - bool stalled; -}; - -#endif diff --git a/src/modules/usb/msd/Makefile.all b/src/modules/usb/msd/Makefile.all deleted file mode 100644 index 0e16a2a..0000000 --- a/src/modules/usb/msd/Makefile.all +++ /dev/null @@ -1,8 +0,0 @@ -shopt -s extglob -source $LOST_BUILDMK_ROOT/config.sh - -CC="$CC -I../include" - -echo "LD $1/modules/usb-msd" -$LOST_TOOLS_LD $LDSCRIPT -ousb-msd.mod *.o --start-group $2 --end-group -$LOST_TOOLS_STRIP -s usb-msd.mod -o $1/modules/usb-msd diff --git a/src/modules/usb/msd/init.c b/src/modules/usb/msd/init.c deleted file mode 100644 index 7670194..0000000 --- a/src/modules/usb/msd/init.c +++ /dev/null @@ -1,206 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include - -#include "msd.h" -#include "msd-bo.h" -#include "msd-cbi.h" -#include "usb-lib.h" -#include "usb-structs.h" -#include "usb-trans.h" - -void got_bo_msd(struct usb_prov_dev* dev); -void got_cbi_msd(struct usb_prov_dev* dev); - -int is_valid_msd(struct usb_prov_dev* dev) -{ - if (dev->type.protocol == 0x50) { // Bulk-only - return 1; - } else if ((dev->type.subclass == 0x04) && !dev->type.protocol) { // UFI/CBI - return 1; - } - - return 0; -} - -void got_msd(struct usb_prov_dev* dev) -{ - if (dev->type.protocol == 0x50) { // Bulk-only - got_bo_msd(dev); - } else if ((dev->type.subclass == 0x04) && !dev->type.protocol) { // UFI/CBI - got_cbi_msd(dev); - } else { - abort(); - } -} - -void got_bo_msd(struct usb_prov_dev* dev) -{ - int endpoints = usb_get_number_of_endpoints(dev); - int ifc_id = usb_get_interface_id(dev); - struct usb_endpoint_descriptor* epd = calloc(1, sizeof(*epd)); - struct usb_endpoint_descriptor* bulk_in = malloc(sizeof(*bulk_in)); - struct usb_endpoint_descriptor* bulk_out = malloc(sizeof(*bulk_out)); - int bin = -1, bon = -1; - - int i; - for (i = 0; i < endpoints; i++) { - usb_get_endpoint_info(dev, i, epd); - - if (epd->type == USB_EP_BULK) { - if (epd->direction) { - memcpy(bulk_in, epd, sizeof(*epd)); - bin = i; - } else { - memcpy(bulk_out, epd, sizeof(*epd)); - bon = i; - } - } - } - - free(epd); - - if ((bin == -1) || (bon == -1)) { - free(bulk_in); - free(bulk_out); - return; - } - - struct usb_pipe* bip = calloc(1, sizeof(*bip)); - struct usb_pipe* bop = calloc(1, sizeof(*bop)); - - usb_create_pipe(dev, bin, bip); - usb_create_pipe(dev, bon, bop); - - int max_lun = 0; - usb_control_transfer(dev, NULL, &(struct usb_setup_packet){ - .type = USB_SETUP_IN | USB_SETUP_REQ_CLS | USB_SETUP_REC_IF, - .request = 0xFE, // GET MAX LUN - .index = ifc_id, - .length = 1, - }, &max_lun); - - for (i = 0; i <= max_lun; i++) { - struct msc_bo_dev* new = calloc(1, sizeof(*new)); - - new->gen.lun = i; - new->gen.issue_scsi_cmd = &msd_bo_issue_scsi_cmd; - new->gen.send = &msd_bo_send; - new->gen.recv = &msd_bo_recv; - - new->bulk_in = bulk_in; - new->bulk_out = bulk_out; - - new->bip = bip; - new->bop = bop; - - provide_msd(&new->gen); - } -} - -void got_cbi_msd(struct usb_prov_dev* dev) -{ - int endpoints = usb_get_number_of_endpoints(dev); - int ifc_id = usb_get_interface_id(dev); - struct usb_endpoint_descriptor* epd = calloc(1, sizeof(*epd)); - struct usb_endpoint_descriptor* bulk_in = malloc(sizeof(*bulk_in)); - struct usb_endpoint_descriptor* bulk_out = malloc(sizeof(*bulk_out)); - struct usb_endpoint_descriptor* interrupt = malloc(sizeof(*interrupt)); - int bin = -1, bon = -1, ien = -1; - - int i; - for (i = 0; i < endpoints; i++) { - usb_get_endpoint_info(dev, i, epd); - - if (epd->type == USB_EP_BULK) { - if (epd->direction) { - memcpy(bulk_in, epd, sizeof(*epd)); - bin = i; - } else { - memcpy(bulk_out, epd, sizeof(*epd)); - bon = i; - } - } else if (epd->type == USB_EP_INTERRUPT) { - memcpy(interrupt, epd, sizeof(*epd)); - ien = i; - } - } - - free(epd); - - if (dev->type.protocol || (ien == -1)) { - free(interrupt); - ien = -1; - } - - if ((bin == -1) || (bon == -1)) { - free(bulk_in); - free(bulk_out); - return; - } - - struct usb_pipe* bip = calloc(1, sizeof(*bip)); - struct usb_pipe* bop = calloc(1, sizeof(*bop)); - struct usb_pipe* ip; - - usb_create_pipe(dev, bin, bip); - usb_create_pipe(dev, bon, bop); - - if (ien != -1) { - ip = calloc(1, sizeof(*ip)); - usb_create_pipe(dev, ien, ip); - } - - struct msc_cbi_dev* new = calloc(1, sizeof(*new)); - - new->gen.lun = 0; - new->gen.issue_scsi_cmd = &msd_cbi_issue_scsi_cmd; - new->gen.send = &msd_cbi_send; - new->gen.recv = &msd_cbi_recv; - - new->dev = dev; - new->ifc_id = ifc_id; - - new->bulk_in = bulk_in; - new->bulk_out = bulk_out; - - new->bip = bip; - new->bop = bop; - - if (ien != -1) { - new->interrupt = interrupt; - new->ip = ip; - } - - provide_msd(&new->gen); -} diff --git a/src/modules/usb/msd/lostio.c b/src/modules/usb/msd/lostio.c deleted file mode 100644 index 6a2b503..0000000 --- a/src/modules/usb/msd/lostio.c +++ /dev/null @@ -1,218 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include -#include - -#include "msd.h" - -static size_t msd_read(lostio_filehandle_t* liofh, void* dest, - size_t blocksize, size_t blockcount); -static size_t msd_write(lostio_filehandle_t* liofh, size_t blocksize, - size_t blockcount, void* data); -static int msd_seek(lostio_filehandle_t* liofh, uint64_t offset, int origin); - -struct partition { - struct msc_gen_dev* dev; - uint64_t block_offset; - uint64_t blocks; -}; - -struct parttbl_entry { - uint8_t active; - uint8_t chs_start[3]; - uint8_t type; - uint8_t chs_end[3]; - uint32_t start; - uint32_t size; -} __attribute__((packed)); - -void init_lostio_interface(void) -{ - lostio_init(); - lostio_type_ramfile_use(); - lostio_type_directory_use(); - - typehandle_t* msds = calloc(1, sizeof(*msds)); - - msds->id = 42; - msds->read = &msd_read; - msds->write = &msd_write; - msds->seek = &msd_seek; - - lostio_register_typehandle(msds); -} - -static void detect_partitions(int msd_number, struct msc_gen_dev* dev) -{ - uint8_t* bootsec = malloc(512); - - msd_scsi_read(dev, 0, bootsec, 1); - - if (*(uint16_t*)&bootsec[510] != 0xAA55) { - free(bootsec); - return; - } - - struct parttbl_entry* ptbl = (struct parttbl_entry*)&bootsec[446]; - - int p; - for (p = 0; p < 4; p++) { - if (ptbl[p].size) { - if ((ptbl[p].type == 0x0F) || (ptbl[p].type == 0x85)) { - continue; - } - - struct partition* part = calloc(1, sizeof(*part)); - part->dev = dev; - part->block_offset = ptbl[p].start; - part->blocks = ptbl[p].size; - - char name[13]; - sprintf(name, "/msd%i_p%i", msd_number, p); - - vfstree_create_node(name, 42, ptbl[p].size * dev->block_size, part, - 0); - } - } - - free(bootsec); -} - -void provide_msd(struct msc_gen_dev* dev) -{ - static int msds = 0; - - uint64_t bc, bs; - if (!msd_scsi_init(dev, &bc, &bs)) { - return; - } - - printf("[usb-msd] New SCSI block device, %lli %lli-byte blocks: ", bc, bs); - uint64_t fs = bc * bs; - if (fs >= 10 << 30) { - printf("%lli GB\n", fs >> 30); - } else if (fs >= 10 << 20) { - printf("%lli MB\n", fs >> 20); - } else if (fs >= 10 << 10) { - printf("%lli kB\n", fs >> 10); - } else { - printf("%lli B\n", fs); - } - - dev->block_size = bs; - dev->blocks = bc; - - char name[10]; - int num = msds++; - sprintf(name, "/msd%i", num); - - struct partition* part = calloc(1, sizeof(*part)); - part->dev = dev; - part->blocks = bc; - - vfstree_create_node(name, 42, fs, part, 0); - - // Bei 2880 Sektoren ist es vermutlich eine Diskette (ja, es gibt noch - // andere Diskettenarten als 3,5" HD, aber die sind halt selten), und die - // hat keine Partitionen (verhindert das Erkennen scheinbarer Partitionen) - if (bc != 2880) { - detect_partitions(num, dev); - } - - // TODO: Das vielleicht erst irgendwie registrieren, wenn alle Geräte - // angekommen sind - init_service_register("usb-msd"); - - uint16_t* mem = calloc(1, 512); - msd_scsi_read(dev, 0, mem, 1); -} - -static size_t msd_read(lostio_filehandle_t* liofh, void* dest, - size_t blocksize, size_t blockcount) -{ - struct partition* part = liofh->node->data; - struct msc_gen_dev* dev = part->dev; - - uint64_t begin = liofh->pos / dev->block_size + part->block_offset; - - // Diese Debuginformationen mögen nervig erscheinen, sind aber sehr - // wichtig, da man oft nur so erkennen kann, dass überhaupt etwas - // geschieht - printf("[usb-msd] read from %lli (%i blocks)\n", begin, - (blockcount * blocksize + dev->block_size - 1) / dev->block_size); - size_t ret = msd_scsi_read(dev, begin, dest, - (blockcount * blocksize + dev->block_size - 1) / dev->block_size); - msd_seek(liofh, ret, SEEK_CUR); - - return ret; -} - -static size_t msd_write(lostio_filehandle_t* liofh, size_t blocksize, - size_t blockcount, void* data) -{ - return 0; -} - -static int msd_seek(lostio_filehandle_t* liofh, uint64_t offset, int origin) -{ - struct partition* part = liofh->node->data; - struct msc_gen_dev* dev = part->dev; - uint64_t new_pos = liofh->pos; - uint64_t size = part->blocks * dev->block_size; - - switch (origin) { - case SEEK_SET: - new_pos = offset; - break; - case SEEK_CUR: - new_pos += offset; - break; - case SEEK_END: - new_pos = size; - break; - } - - if (new_pos > size) { - return -1; - } else if (new_pos == size) { - liofh->flags |= LOSTIO_FLAG_EOF; - } else { - liofh->flags &= ~LOSTIO_FLAG_EOF; - } - - liofh->pos = new_pos; - return 0; -} diff --git a/src/modules/usb/msd/main.c b/src/modules/usb/msd/main.c deleted file mode 100644 index cebca5c..0000000 --- a/src/modules/usb/msd/main.c +++ /dev/null @@ -1,65 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include - -#include -#include -#include -#include - -#include "msd.h" - -extern pid_t usb_pid; - -int main(void) -{ - init_messaging(); - init_lostio_interface(); - - // Warten, damit, falls alle Treiber relativ gleichzeitig gestartet werden - // (Boot), dieser Prozess nach usb1 (und möglichst vor uhci) weiterläuft - msleep(1000); - - usb_pid = init_service_get("usb1"); - if (!usb_pid) - { - fprintf(stderr, "You need to start the usb1 driver first.\n"); - return 1; - } - - init_rpc_interface(); - register_msddrv(); - - for (;;) { - wait_for_rpc(); - } -} diff --git a/src/modules/usb/msd/msd-bo.c b/src/modules/usb/msd/msd-bo.c deleted file mode 100644 index 0007b80..0000000 --- a/src/modules/usb/msd/msd-bo.c +++ /dev/null @@ -1,104 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include - -#include "msd.h" -#include "msd-bo.h" -#include "usb-lib.h" - -int check_csw(struct msc_bo_dev* dev) -{ - struct msd_bo_csw csw; - - int ret = usb_bulk_transfer(dev->bip, &csw, sizeof(csw), true); - if (ret) { - return ret; - } - - // Man könnte wohl noch ein bisschen was überprüfen... - // Some checks might be nice... - return 0; -} - -int msd_bo_issue_scsi_cmd(struct msc_gen_dev* dev, const void* cmd_buffer, - size_t cmd_length, bool receive, size_t req_length) -{ - struct msc_bo_dev* bo = (struct msc_bo_dev*)dev; - - if (cmd_length > 16) { - cmd_length = 16; - } - - struct msd_bo_cbw cbw = { - .signature = CBW_SIGNATURE, - .data_transfer_length = req_length, - .flags = receive ? CBW_IN : CBW_OUT, - .lun = dev->lun, - .cb_length = cmd_length - }; - - memcpy(cbw.cb, cmd_buffer, cmd_length); - - int ret = usb_bulk_transfer(bo->bop, &cbw, sizeof(cbw), false); - if (ret || req_length) { - return ret; - } - - return check_csw(bo); -} - -int msd_bo_send(struct msc_gen_dev* dev, const void* buffer, size_t size) -{ - struct msc_bo_dev* bo = (struct msc_bo_dev*)dev; - - int ret = usb_bulk_transfer(bo->bop, (void*)buffer, size, false); - if (ret) { - return ret; - } - - return check_csw(bo); -} - -int msd_bo_recv(struct msc_gen_dev* dev, void* buffer, size_t size) -{ - struct msc_bo_dev* bo = (struct msc_bo_dev*)dev; - - int ret = usb_bulk_transfer(bo->bip, buffer, size, true); - if (ret) { - return ret; - } - - return check_csw(bo); -} diff --git a/src/modules/usb/msd/msd-bo.h b/src/modules/usb/msd/msd-bo.h deleted file mode 100644 index 49451cb..0000000 --- a/src/modules/usb/msd/msd-bo.h +++ /dev/null @@ -1,99 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef MSD_BO_H -#define MSD_BO_H - -#include -#include - -#include "msd.h" -#include "usb-ddrv.h" - -#define CBW_IN 0x80 -#define CBW_OUT 0x00 - -#define CBW_SIGNATURE 0x43425355 //'USBC' => USB command -#define CSW_SIGNATURE 0x53425355 //'USBS' => USB status - - -// Beschreibt ein MSC-Bulk-Only-Gerät -struct msc_bo_dev { - // Das MSD an sich - struct msc_gen_dev gen; - - // Bulk-Endpunkte - struct usb_endpoint_descriptor* bulk_in; - struct usb_endpoint_descriptor* bulk_out; - - // Pipes zu diesen Endpunkten - struct usb_pipe* bip; - struct usb_pipe* bop; -}; - -// Command Block Wrapper für BO-MSDs -struct msd_bo_cbw { - // CBW_SIGNATURE - uint32_t signature; - // Ein Wert, der im CSW wiederholt wird - uint32_t tag; - // Länge des Datentransfers - uint32_t data_transfer_length; - // Flags (vorrangig Richtung des Transfers) - uint8_t flags; - // LUN - uint8_t lun; - // Länge des Befehls - uint8_t cb_length; - // SCSI-Befehl - uint8_t cb[16]; -} __attribute__((packed)); - -// Command Status Wrapper für BO-MSDs -struct msd_bo_csw { - // CSW_SIGNATURE - uint32_t signature; - // Tagwert aus dem CBW - uint32_t tag; - // Größe der Daten, die erwartet, aber nicht übertragen wurden - uint32_t data_residue; - // Status - uint8_t status; -} __attribute__((packed)); - -// Sendet einen SCSI-Befehl -int msd_bo_issue_scsi_cmd(struct msc_gen_dev* dev, const void* buffer, - size_t cmd_length, bool receive, size_t req_length); -// Sendet Daten an das Gerät -int msd_bo_send(struct msc_gen_dev* dev, const void* buffer, size_t size); -// Empfängt Daten vom Gerät -int msd_bo_recv(struct msc_gen_dev* dev, void* buffer, size_t size); - -#endif diff --git a/src/modules/usb/msd/msd-cbi.c b/src/modules/usb/msd/msd-cbi.c deleted file mode 100644 index 66590fd..0000000 --- a/src/modules/usb/msd/msd-cbi.c +++ /dev/null @@ -1,86 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include - -#include "msd.h" -#include "msd-cbi.h" -#include "usb-lib.h" - -int msd_cbi_issue_scsi_cmd(struct msc_gen_dev* dev, const void* cmd_buffer, - size_t cmd_length, bool receive, size_t req_length) -{ - struct msc_cbi_dev* cbi = (struct msc_cbi_dev*)dev; - int8_t cmd[12] = { 0 }; - struct usb_setup_packet setup = { - .type = USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_IF, - .request = 0, - .value = 0, - .index = cbi->ifc_id, - .length = sizeof(cmd) - }; - - if (cmd_length > 12) { - cmd_length = 12; - } - - memcpy(cmd, cmd_buffer, cmd_length); - - int ret = usb_control_transfer(cbi->dev, NULL, &setup, cmd); - - /* - if (ret == -EHOSTDOWN) { - // kA, ob das etwas bringt - if (receive == true) { - usb_clear_halt(cbi->bip); - } else { - usb_clear_halt(cbi->bop); - } - } - */ - - return ret; -} - -int msd_cbi_send(struct msc_gen_dev* dev, const void* buffer, size_t size) -{ - struct msc_cbi_dev* cbi = (struct msc_cbi_dev*)dev; - return usb_bulk_transfer(cbi->bop, (void*)buffer, size, false); -} - -int msd_cbi_recv(struct msc_gen_dev* dev, void* buffer, size_t size) -{ - struct msc_cbi_dev* cbi = (struct msc_cbi_dev*)dev; - return usb_bulk_transfer(cbi->bip, buffer, size, true); -} diff --git a/src/modules/usb/msd/msd-cbi.h b/src/modules/usb/msd/msd-cbi.h deleted file mode 100644 index 66ff221..0000000 --- a/src/modules/usb/msd/msd-cbi.h +++ /dev/null @@ -1,71 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef MSD_CBI_H -#define MSD_CBI_H - -#include -#include - -#include "msd.h" -#include "usb-ddrv.h" - -// Beschreibt ein MSC-CBI-Gerät -struct msc_cbi_dev { - // Das MSD an sich - struct msc_gen_dev gen; - - // Das zugrunde liegende USB-Gerät - struct usb_prov_dev* dev; - // Die Interface-ID (MSDs sind immer Interfaces) - int ifc_id; - - // Alle wichtigen Endpunkte (EP0 ist in dev enthalten), interrupt darf NULL - // sein - struct usb_endpoint_descriptor* bulk_in; - struct usb_endpoint_descriptor* bulk_out; - struct usb_endpoint_descriptor* interrupt; - - // Die Pipes zu diesen Endpunkten, ip darf NULL sein - struct usb_pipe* bip; - struct usb_pipe* bop; - struct usb_pipe* ip; -}; - - -// Sendet einen SCSI-Befehl -int msd_cbi_issue_scsi_cmd(struct msc_gen_dev* dev, const void* buffer, - size_t cmd_length, bool receive, size_t req_length); -// Sendet Daten zum Gerät -int msd_cbi_send(struct msc_gen_dev* dev, const void* buffer, size_t size); -// Empfängt Daten vom Gerät -int msd_cbi_recv(struct msc_gen_dev* dev, void* buffer, size_t size); - -#endif diff --git a/src/modules/usb/msd/msd.h b/src/modules/usb/msd/msd.h deleted file mode 100644 index 3545df0..0000000 --- a/src/modules/usb/msd/msd.h +++ /dev/null @@ -1,154 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef MSD_H -#define MSD_H - -#include -#include -#include - -#include "usb-ddrv.h" - -// Beschreibt eine LUN eines MSC-Geräts (MSD) im Allgemeinen -struct msc_gen_dev { - // LUN - int lun; - - // Größe eines Blocks - uint64_t block_size; - // Anzahl Blöcke - uint64_t blocks; - - /** - * Führt einen SCSI-Befehl aus. - * - * @param dev Zielgerät - * @param cmd_buffer Speicherbereich, der den Befehl enthält - * @param cmd_length Länge dieses Befehls in Byte - * @param receive true, wenn empfangen werden soll - * @param req_length Länge der zu übertragenden Daten - * - * @return 0 bei Erfolg, sonst -errno - */ - int (*issue_scsi_cmd)(struct msc_gen_dev* dev, const void* cmd_buffer, - size_t cmd_length, bool receive, size_t req_length); - - /** - * Sendet Daten zu einem Gerät. - * - * @param dev Zielgerät - * @param buffer Speicherbereich, der die Daten enthält - * @param size Anzahl der zu sendenden Bytes - * - * @return 0 bei Erfolg, sonst -errno - */ - int (*send)(struct msc_gen_dev* dev, const void* buffer, size_t size); - - /** - * Empfängt Daten von einem Gerät. - * - * @param dev Quellgerät - * @param buffer Speicherbereich, in den die Daten gelesen werden sollen - * @param size Anzahl der zu empfangenden Bytes - * - * @return 0 bei Erfolg, sonst -errno - */ - int (*recv)(struct msc_gen_dev* dev, void* buffer, size_t size); -}; - -/** - * Wird von einem RPC-Handler aufgerufen, um zu prüfen, ob das neue Gerät ein - * MSD ist, mit dem der Treiber etwas anfangen kann. - * - * @param dev Neues Gerät - * - * @return 1 wenn das Gerät benutzt werden kann, sonst 0 - */ -int is_valid_msd(struct usb_prov_dev* dev); - -/** - * Wird von einem RPC-Handler aufgerufen, sobald ein neues MSD gefunden wurde. - * - * @param dev Neues Gerät - */ -void got_msd(struct usb_prov_dev* dev); - -/** - * Initialisiert das LostIO-Interface. - */ -void init_lostio_interface(void); - -/** - * Initialisiert das RPC-Interface. - */ -void init_rpc_interface(void); - -/** - * Initialisiert ein MSD auf SCSI-Ebene. - * - * @param dev Das MSD - * @param blocks Pointer zu einer Variable, in die die Anzahl der vorhandenen - * Blöcke geschrieben wird - * @param bsize Pointer zu einer Variable, in die die Größe eines Blocks - * geschrieben wird - * - * @return 1 bei Erfolg, sonst 0 - */ -int msd_scsi_init(struct msc_gen_dev* dev, uint64_t* blocks, - uint64_t* bsize); - -/** - * Liest von einem MSD per SCSI. - * - * @param dev Das MSD - * @param start_lba LBA des ersten zu lesenden Blocks - * @param dest Zielspeicherbereich - * @param blocks Anzahl der zu lesenden Blöcke - * - * @return Anzahl der gelesenen Bytes - */ -size_t msd_scsi_read(struct msc_gen_dev* dev, uint64_t start_lba, void* dest, - uint64_t blocks); - -/** - * Wird von einer MSD-spezifischen Initialisierungsroutine aufgerufen, um ein - * für die SCSI-Initialisierung vorbereitetes MSD bereitzustellen. - * - * @param dev Das neue Gerät - */ -void provide_msd(struct msc_gen_dev* dev); - -/** - * Registriert diesen Treiber beim USB-Bustreiber. - */ -void register_msddrv(void); - -#endif diff --git a/src/modules/usb/msd/rpc.c b/src/modules/usb/msd/rpc.c deleted file mode 100644 index d54fa01..0000000 --- a/src/modules/usb/msd/rpc.c +++ /dev/null @@ -1,89 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "msd.h" -#include "usb-ddrv.h" -#include "usb-server.h" - -extern pid_t usb_pid; - -void msd_got_device_handler(pid_t src, uint32_t corr_id, size_t length, - void* data) -{ - struct usb_prov_dev* pd; - int ret; - - if (length < sizeof(*pd)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - pd = malloc(sizeof(*pd)); - memcpy(pd, data, sizeof(*pd)); - - ret = is_valid_msd(pd); - rpc_send_int_response(src, corr_id, ret); - if (ret) { - got_msd(pd); - } - -} - -void init_rpc_interface(void) -{ - register_message_handler(RPC_USB_DDRV_PLUG_FNN, &msd_got_device_handler); -} - -void register_msddrv(void) -{ - struct usb_handle_devs hd = { - .vendor_id = -1, - .device_id = -1, - .class = 8, // MSC - .subclass = -1, - .protocol = -1 - }; - - if (!rpc_get_int(usb_pid, RPC_USB_SV_REGDD_FNN, sizeof(hd), (char*)&hd)) { - fprintf(stderr, "Could not register USB MSD driver.\n"); - } -} diff --git a/src/modules/usb/msd/scsi.c b/src/modules/usb/msd/scsi.c deleted file mode 100644 index 5a4e007..0000000 --- a/src/modules/usb/msd/scsi.c +++ /dev/null @@ -1,240 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include - -#include "msd.h" -#include "scsi.h" - -const char *dev_type[] = -{ - "magnetic disk", - "magnetic tape", - "printer", - "processor", - "write-once device", - "CD-ROM", - "scanner", - "optical medium", - "medium changer", - "communications device", - "?", - "?", - "RAID", - "enclosure services device", - "magnetic disk (simplified)", - "card reader/writer (optical)", - "?", - "object-based storage", - "automation/drive interface", - "?", - "?", - "?", - "?", - "?", - "?", - "?", - "?", - "?", - "?", - "?", - "well known LUN", - "?" -}; - -static int do_pkt(struct msc_gen_dev* dev, int receive, void* buffer1, - size_t length1, void* buffer2, size_t length2) -{ - while (dev->issue_scsi_cmd(dev, buffer1, length1, !!(receive == 1), - length2)) - { - struct scsi_sense sense; - struct scsi_cmd_request_sense req_sense = { - .cmd = { - .operation = SCSI_REQUEST_SENSE - }, - .alloc_len = sizeof(sense), - .lun = dev->lun - }; - - if (dev->issue_scsi_cmd(dev, &req_sense, sizeof(req_sense), true, - sizeof(sense))) - { - fprintf(stderr, "[usb-msd] Could not request sense\n"); - return -1; - } - - if (dev->recv(dev, &sense, sizeof(sense))) { - fprintf(stderr, "[usb-msd] Could not sense\n"); - return -1; - } - } - - if (receive == 1) { - return dev->recv(dev, buffer2, length2); - } else if (!receive) { - return dev->send(dev, buffer2, length2); - } - - return 0; -} - -int msd_scsi_init(struct msc_gen_dev* dev, uint64_t* blocks, uint64_t* bsize) -{ - struct scsi_inq_answer inq_ans; - struct scsi_cmd_inquiry inq = { - .cmd = { - .operation = SCSI_INQUIRY - }, - .alloc_len = htobe16(sizeof(inq_ans)), - .lun = dev->lun - }; - - if (do_pkt(dev, 1, &inq, sizeof(inq), &inq_ans, sizeof(inq_ans))) { - return 0; - } - - printf("[usb-msd] LUN %i: %s, ", dev->lun, dev_type[inq_ans.dev_type]); - - int last; - for (last = (int)sizeof(inq_ans.vendor) - 1; last >= 0; last--) { - if (inq_ans.vendor[last] != ' ') { - break; - } - } - - last++; - - int i; - - if (last) { - for (i = 0; i < last; i++) { - printf("%c", inq_ans.vendor[i]); - } - printf(" "); - } - - - for (last = (int)sizeof(inq_ans.product) - 1; last >= 0; last--) { - if (inq_ans.product[last] != ' ') { - break; - } - } - - last++; - - for (i = 0; i < last; i++) { - printf("%c", inq_ans.product[i]); - } - printf("\n"); - - // Erklär mir mal einer, wieso man das braucht - // I'd appreciate any explanation why this is required - int8_t zero[6] = { 0 }; - if (do_pkt(dev, -1, &zero, sizeof(zero), NULL, 0)) { - return 0; - } - - uint32_t cap_ans[2]; - struct scsi_cmd_read_capacity cap = { - .cmd = { - .operation = SCSI_READ_CAPACITY - } - }; - - if (do_pkt(dev, 1, &cap, sizeof(cap), &cap_ans, sizeof(cap_ans))) { - return 0; - } - - *blocks = (uint64_t)betoh32(cap_ans[0]) + 1LL; - *bsize = betoh32(cap_ans[1]); - - return 1; -} - -#define SCSI_READ 10 - -size_t msd_scsi_read(struct msc_gen_dev* dev, uint64_t start_lba, void* dest, - uint64_t blocks) -{ - #if SCSI_READ == 10 - struct scsi_cmd_read10 r10 = { - .cmd = { - .operation = SCSI_READ10 - }, - .lun = dev->lun, - .lba = htobe32(start_lba), - .length = htobe16(blocks) - }; - #elif SCSI_READ == 6 - struct scsi_cmd_read6 r6 = { - .cmd = { - .operation = SCSI_READ6 - }, - .lun = dev->lun, - .lba_lo = htobe16(start_lba), - .lba_hi = start_lba >> 16, - .length = blocks - }; - #else - struct scsi_cmd_read12 r12 = { - .cmd = { - .operation = SCSI_READ12 - }, - .lun = dev->lun, - .lba = htobe32(start_lba), - .length = htobe32(blocks) - }; - #endif - - #if SCSI_READ == 10 - if ((start_lba >> 32) || (blocks >> 16)) { - #elif SCSI_READ == 6 - if ((start_lba >> 21) || (blocks >> 8)) { - #else - if ((start_lba >> 32) || (blocks >> 32)) { - #endif - return 0; - } - - #if SCSI_READ == 10 - int ret = do_pkt(dev, 1, &r10, sizeof(r10), dest, blocks * dev->block_size); - #elif SCSI_READ == 6 - int ret = do_pkt(dev, 1, &r6, sizeof(r6), dest, blocks * dev->block_size); - #else - int ret = do_pkt(dev, 1, &r12, sizeof(r12), dest, blocks * dev->block_size); - #endif - - if (ret < 0) { - return 0; - } - return blocks * dev->block_size; -} diff --git a/src/modules/usb/msd/scsi.h b/src/modules/usb/msd/scsi.h deleted file mode 100644 index 98b88f9..0000000 --- a/src/modules/usb/msd/scsi.h +++ /dev/null @@ -1,219 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef SCSI_H -#define SCSI_H - -#include -#include - -// Funktionen zum Konvertieren von/zu Big Endian -#define htobe16(val) htons(val) -#define htobe32(val) htonl(val) -#define betoh16(val) htobe16(val) -#define betoh32(val) htobe32(val) - -// SCSI-Befehlscodes -#define SCSI_REQUEST_SENSE 0x03 -#define SCSI_READ6 0x08 -#define SCSI_INQUIRY 0x12 -#define SCSI_READ_CAPACITY 0x25 -#define SCSI_READ10 0x28 -#define SCSI_WRITE10 0x2A -#define SCSI_READ12 0xA8 - -// Minimaler SCSI-Befehl -struct scsi_cmd { - // Befehlscode - uint8_t operation; -} __attribute__((packed)); - -// REQUEST SENSE -// Gibt Informationen im Fehlerfall zurück. -struct scsi_cmd_request_sense { - struct scsi_cmd cmd; - unsigned rsvd1 : 5; - // LUN - unsigned lun : 3; - uint16_t rsvd2; - // Anzahl der Bytes, die der Host zu empfangen bereit ist - uint8_t alloc_len; - uint8_t control; -} __attribute__((packed)); - -// INQUIRY -// Gibt Informationen zum Gerät zurück. -struct scsi_cmd_inquiry { - struct scsi_cmd cmd; - unsigned evpd : 1; - unsigned rsvd : 4; - // LUN - unsigned lun : 3; - uint8_t page; - // Anzahl der Bytes, die der Host zu empfangen bereit ist - uint16_t alloc_len; - uint8_t control; -} __attribute__((packed)); - -// READ CAPACITY -// Gibt die LBA des letzten Blocks zurück sowie die Blockgröße -struct scsi_cmd_read_capacity { - struct scsi_cmd cmd; - unsigned reladdr : 1; - unsigned rsvd1 : 4; - // LUN - unsigned lun : 3; - uint32_t lba; - uint32_t rsvd2; -} __attribute__((packed)); - -// READ6 -// Liest Daten -struct scsi_cmd_read6 { - struct scsi_cmd cmd; - // Höherwertiger Teil der LBA - unsigned lba_hi : 5; - // LUN - unsigned lun : 3; - // Niederwertiger Teil der LBA - uint16_t lba_lo; - // Anzahl zu lesende Blöcke - uint8_t length; - uint8_t control; -} __attribute__((packed)); - -// READ10 -// Liest Daten -struct scsi_cmd_read10 { - struct scsi_cmd cmd; - unsigned reladdr : 1; - unsigned rsvd1 : 2; - unsigned fua : 1; - unsigned dpo : 1; - // LUN - unsigned lun : 3; - // LBA - uint32_t lba; - uint8_t rsvd2; - // Anzahl zu lesende Blöcke - uint16_t length; - uint8_t control; -} __attribute__((packed)); - -// READ12 -// Liest Daten -struct scsi_cmd_read12 { - struct scsi_cmd cmd; - unsigned reladdr : 1; - unsigned rsvd1 : 2; - unsigned fua : 1; - unsigned dpo : 1; - // LUN - unsigned lun : 3; - // LBA - uint32_t lba; - // Anzahl zu lesende Blöcke - uint32_t length; - uint16_t rsvd2; -} __attribute__((packed)); - -// WRITE10 -// Schreibt Daten -struct scsi_cmd_write10 { - struct scsi_cmd cmd; - unsigned reladdr : 1; - unsigned rsvd1 : 1; - unsigned ebp : 1; - unsigned fua : 1; - unsigned dpo : 1; - // LUN - unsigned lun : 3; - // LBA - uint32_t lba; - uint8_t rsvd2; - // Anzahl zu lesende Blöcke - uint16_t length; - uint8_t control; -} __attribute__((packed)); - -// Rückgabestruktur von SCSI SENSE -struct scsi_sense { - unsigned resp_code : 7; - unsigned valid : 1; - uint8_t segment; - unsigned sense_key : 4; - unsigned rsvd1 : 1; - unsigned ili : 1; - unsigned eom : 1; - unsigned filemark : 1; - uint32_t information; - uint8_t add_sense_length; - uint32_t cmd_spec_info; - uint8_t add_sense_code; - uint8_t add_sense_code_qual; - uint8_t field_replacable_unit_code; - unsigned sense_key_spec1 : 7; - unsigned sksv : 1; - uint8_t sense_key_spec2; - uint8_t sense_key_spec3; -} __attribute__((packed)); - -// Rückgabestruktur von SCSI INQUIRY -struct scsi_inq_answer { - // Gerätetype - unsigned dev_type : 5; - unsigned qualifier : 3; - unsigned type_mod : 7; - unsigned rmb : 1; - unsigned ansi_version : 3; - unsigned ecma_version : 3; - unsigned iso_version : 2; - unsigned resp_data_format : 4; - unsigned rsvd1 : 2; - unsigned trm_iop : 1; - unsigned aec : 1; - uint8_t add_length; - uint16_t rsvd2; - unsigned soft_rst : 1; - unsigned cmd_queue : 1; - unsigned rsvd3 : 1; - unsigned linked : 1; - unsigned sync : 1; - unsigned wbus16 : 1; - unsigned wbus32 : 1; - unsigned rel_adr : 1; - // Herstellername - char vendor[8]; - // Produktname - char product[16]; - uint32_t revision; -} __attribute__((packed)); - -#endif diff --git a/src/modules/usb/msd/usb-lib.c b/src/modules/usb/msd/usb-lib.c deleted file mode 100644 index fe4eb69..0000000 --- a/src/modules/usb/msd/usb-lib.c +++ /dev/null @@ -1,207 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include -#include - -#include "usb-ddrv.h" -#include "usb-server.h" -#include "usb-structs.h" -#include "usb-trans.h" - -pid_t usb_pid; - -int usb_get_number_of_endpoints(struct usb_prov_dev* dev) -{ - struct usb_devctrl_gne rq = { - .req = { - .type = USB_DC_NUM_ENDPOINTS - }, - .dev = *dev - }; - - return rpc_get_int(usb_pid, RPC_USB_SV_DEVCT_FNN, sizeof(rq), (char*)&rq); -} - -int usb_get_interface_id(struct usb_prov_dev* dev) -{ - struct usb_devctrl_gii giid = { - .req = { - .type = USB_DC_GET_IFC_ID - }, - .dev = *dev - }; - - return rpc_get_int(usb_pid, RPC_USB_SV_DEVCT_FNN, sizeof(giid), - (char*)&giid); -} - -void usb_get_endpoint_info(struct usb_prov_dev* dev, int ep_number, - struct usb_endpoint_descriptor* ep_desc) -{ - struct usb_devctrl_gep gep = { - .req = { - .type = USB_DC_GET_ENDPOINT - }, - .dev = *dev, - .ep_number = ep_number - }; - - response_t* r = rpc_get_response(usb_pid, RPC_USB_SV_DEVCT_FNN, - sizeof(gep), (char*)&gep); - - memcpy(ep_desc, r->data, sizeof(*ep_desc)); - free(r->data); - free(r); -} - -void usb_create_pipe(struct usb_prov_dev* dev, int ep_number, - struct usb_pipe* pipe) -{ - struct usb_devctrl_gpp gp = { - .req = { - .type = USB_DC_GET_PIPE - }, - .dev = *dev, - .ep_number = ep_number - }; - - response_t* r = rpc_get_response(usb_pid, RPC_USB_SV_DEVCT_FNN, - sizeof(gp), (char*)&gp); - - memcpy(pipe, r->data, sizeof(*pipe)); - free(r->data); - free(r); -} - -int usb_control_transfer(struct usb_prov_dev* dev, struct usb_pipe* pipe, - struct usb_setup_packet* setup, void* buffer) -{ - uint32_t shm = 0; - void* shm_addr = NULL; - - if (setup->length) { - shm = create_shared_memory(setup->length); - shm_addr = open_shared_memory(shm); - - if ((buffer != NULL) && !(setup->type & USB_SETUP_IN)) { - memcpy(shm_addr, buffer, setup->length); - } - } - - struct usb_devctrl_tct tctrq = { - .req = { - .type = USB_DC_TRSF_CONTROL - }, - .setup = *setup, - .shm = shm - }; - - if (pipe != NULL) { - tctrq.dst.pipe = *pipe; - tctrq.ep0 = false; - } else { - tctrq.dst.dev_id = dev->dev_id; - tctrq.ep0 = true; - } - - int ret = rpc_get_int(usb_pid, RPC_USB_SV_DEVCT_FNN, sizeof(tctrq), - (char*)&tctrq); - - if (setup->length && (buffer != NULL) && (setup->type & USB_SETUP_IN)) { - memcpy(buffer, shm_addr, setup->length); - } - - if (shm_addr != NULL) { - close_shared_memory(shm); - } - - return ret; -} - -int usb_bulk_transfer(struct usb_pipe* pipe, void* buffer, size_t length, - bool receive) -{ - uint32_t shm = 0; - void* shm_addr = NULL; - - if (length) { - shm = create_shared_memory(length); - shm_addr = open_shared_memory(shm); - - if ((buffer != NULL) && !receive) { - memcpy(shm_addr, buffer, length); - } - } - - struct usb_devctrl_tbk tbkrq = { - .req = { - .type = USB_DC_TRSF_BULK - }, - .pipe = *pipe, - .length = length, - .shm = shm - }; - - int ret = rpc_get_int(usb_pid, RPC_USB_SV_DEVCT_FNN, sizeof(tbkrq), - (char*)&tbkrq); - - if (length && (buffer != NULL) && receive) { - memcpy(buffer, shm_addr, length); - } - - if (shm_addr != NULL) { - close_shared_memory(shm); - } - - if (ret >= 0) { - pipe->trs.toggle = ret; - ret = 0; - } - - return ret; -} - -void usb_clear_halt(struct usb_pipe* pipe) -{ - struct usb_devctrl_clh clh = { - .req = { - .type = USB_DC_CLEAR_HALT - }, - .pipe = *pipe - }; - - rpc_get_int(usb_pid, RPC_USB_SV_DEVCT_FNN, sizeof(clh), (char*)&clh); -} diff --git a/src/modules/usb/msd/usb-lib.h b/src/modules/usb/msd/usb-lib.h deleted file mode 100644 index 2f7fb9b..0000000 --- a/src/modules/usb/msd/usb-lib.h +++ /dev/null @@ -1,124 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_LIB_H -#define USB_LIB_H - -#include -#include - -#include "usb-ddrv.h" -#include "usb-server.h" -#include "usb-structs.h" -#include "usb-trans.h" - -/* - * Hinweis: Wenn hier die Rede von einem USB-Gerät ist, so ist ein logisches - * USB-Gerät gemeint. Dies kann sowohl ein echtes (physisches) Gerät sein oder - * aber auch nur ein Interface dieses Geräts (meistens letzteres, da so gut - * wie alle USB-Geräteklassen auf Interfaceebene arbeiten und somit die meisten - * physischen USB-Geräte genau ein Interface besitzen). - */ - -/** - * Führt einen USB-Bulktransfer durch. - * - * @param pipe Zu verwendende Pipe - * @param buffer Zu bearbeitender Speicherbereich - * @param length Länge des Transfers - * @param receive true, wenn empfangen werden soll, sonst false. - * - * @return 0 bei Erfolg, sonst -errno. - */ -int usb_bulk_transfer(struct usb_pipe* pipe, void* buffer, size_t length, - bool receive); - -/** - * Entstallt einen Endpunkt. - * - * @param pipe Eine zum Endpunkt gehörige Pipe. - */ -void usb_clear_halt(struct usb_pipe* pipe); - -/** - * Führt einen USB-Controltransfer durch. - * - * @param dev Gerät - * @param pipe Zu verwendende Pipe oder NULL für die Default Control Pipe - * @param setup SETUP-Paket - * @param buffer Speicherbereich für die Datenphase - * - * @return 0 bei Erfolg, sonst -errno. - */ -int usb_control_transfer(struct usb_prov_dev* dev, struct usb_pipe* pipe, - struct usb_setup_packet* setup, void* buffer); - -/** - * Erstellt eine USB-Pipe. - * - * @param dev Zielgerät - * @param ep_number Nummer des Zielendpunkts (Nummer innerhalb des Interfaces, - * nicht ID!) - * @param pipe Speicherbereich, in dem die Daten der Pipe abgelegt werden - * sollen. - */ -void usb_create_pipe(struct usb_prov_dev* dev, int ep_number, - struct usb_pipe* pipe); - -/** - * Empfängt Informationen zu einem Endpunkt. - * - * @param dev Gerät - * @param ep_number Nummer des Endpunkts - * @param ep_desc Pointer zu einem Speicherbereich, der einen Endpoint- - * Descriptor aufnehmen kann. - */ -void usb_get_endpoint_info(struct usb_prov_dev* dev, int ep_number, - struct usb_endpoint_descriptor* ep_desc); - -/** - * Gibt die Interface-ID des logischen USB-Geräts zurück. - * - * @param dev Gerät - * - * @return Interface-ID - */ -int usb_get_interface_id(struct usb_prov_dev* dev); - -/** - * Gibt die Anzahl an Endpunkten des USB-Geräts zurück. - * - * @param dev Logisches Gerät - * - * @return Anzahl Endpunkte - */ -int usb_get_number_of_endpoints(struct usb_prov_dev* dev); - -#endif diff --git a/src/modules/usb/uhci/Makefile.all b/src/modules/usb/uhci/Makefile.all deleted file mode 100644 index cf401d2..0000000 --- a/src/modules/usb/uhci/Makefile.all +++ /dev/null @@ -1,8 +0,0 @@ -shopt -s extglob -source $LOST_BUILDMK_ROOT/config.sh - -CC="$CC -I../include" - -echo "LD $1/modules/uhci" -$LOST_TOOLS_LD $LDSCRIPT -ouhci.mod *.o --start-group $2 --end-group -$LOST_TOOLS_STRIP -s uhci.mod -o $1/modules/uhci diff --git a/src/modules/usb/uhci/ctrl.c b/src/modules/usb/uhci/ctrl.c deleted file mode 100644 index ad1c3a0..0000000 --- a/src/modules/usb/uhci/ctrl.c +++ /dev/null @@ -1,96 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include -#include - -#include "uhci.h" -#include "usb-hub.h" - -void uhci_reset_port(struct uhci* hc, int port) -{ - if (hc == NULL) - return; - - outw(hc->iobase + UHCI_RPORTS + port * 2, RPORT_ENABLE | RPORT_RESET | RPORT_CSC); - msleep(50); - uhci_set_port_status(hc, port, 1); - msleep(10); -} - -int uhci_scan_port(struct uhci* hc, int port) -{ - int ret; - - if (hc == NULL) - return 0; - - uint_fast16_t val = inw(hc->iobase + UHCI_RPORTS + port * 2); - - if (!(val & RPORT_DEVICE)) { - ret = 0; - } else { - ret = USB_HUB_DEVICE; - if (val & RPORT_LOSPD) { - ret |= USB_HUB_LOW_SPEED; - } else { - ret |= USB_HUB_FULL_SPEED; - } - - if (val & RPORT_CSC) { - ret |= USB_HUB_CHANGE; - outw(hc->iobase + UHCI_RPORTS + port * 2, (val & RPORT_ENABLE) | RPORT_CSC); - } - } - - return ret; -} - -void uhci_set_port_status(struct uhci* hc, int port, int status) -{ - if (hc == NULL) - return; - - int previous = inw(hc->iobase + UHCI_RPORTS + port * 2) & RPORT_ENABLE; - outw(hc->iobase + UHCI_RPORTS + port * 2, (status ? RPORT_ENABLE : 0) | RPORT_CSC); - msleep(50); - if (status && !previous) - { - outw(hc->iobase + UHCI_RPORTS + port * 2, RPORT_ENABLE | RPORT_RESUME); - msleep(20); - outw(hc->iobase + UHCI_RPORTS + port * 2, RPORT_ENABLE | RPORT_CSC); - msleep(10); - } -} diff --git a/src/modules/usb/uhci/init.c b/src/modules/usb/uhci/init.c deleted file mode 100644 index 7a4d6f6..0000000 --- a/src/modules/usb/uhci/init.c +++ /dev/null @@ -1,265 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "uhci.h" - -extern pid_t usb_pid, pci_pid; - - -extern struct uhci* uhcis[IRQ_COUNT]; -extern void uhci_irq_handler(uint8_t irq); - -static uint16_t pci_inw(struct pci_device* dev, int reg) -{ - struct pci_config_space_access csa = { - .type = PCI_CSA_READ16, - .bus = dev->bus, - .device = dev->device, - .function = dev->function, - .reg = reg - }; - - return rpc_get_dword(pci_pid, RPC_PCI_CONFIG_SPACE_ACCESS, sizeof(csa), - (char*)&csa); -} - -static void pci_outw(struct pci_device* dev, int reg, uint16_t val) -{ - struct pci_config_space_access csa = { - .type = PCI_CSA_WRITE16, - .bus = dev->bus, - .device = dev->device, - .function = dev->function, - .reg = reg, - .value = val - }; - - rpc_get_dword(pci_pid, RPC_PCI_CONFIG_SPACE_ACCESS, sizeof(csa), - (char*)&csa); -} - -void init_uhc(struct pci_device* dev, size_t devlen) -{ - devlen -= sizeof(*dev); - int8_t* data = (int8_t*)(dev + 1); - - struct pci_resource pres; - uint16_t iobase = 0, iosize = 0; - - while (devlen >= sizeof(pres)) { - memcpy(&pres, data, sizeof(pres)); - - devlen -= sizeof(pres); - data += sizeof(pres); - - if (pres.type == PCI_RESOURCE_PORT) { - iobase = pres.start; - iosize = pres.length; - break; - } - } - - if (!iobase) { - return; - } - - struct uhci* ndev = calloc(1, sizeof(*ndev)); - ndev->pci = dev; - ndev->iobase = iobase; - ndev->rh = calloc(1, sizeof(*ndev->rh)); - - ndev->rh->ports = (iosize - 0x10) >> 1; - - request_ports(iobase, iosize); - uhcis[dev->irq] = ndev; - register_intr_handler(TYNDUR_IRQ_OFFSET + dev->irq, &uhci_irq_handler); - - outw(iobase + UHCI_USBCMD, GRESET); - msleep(50); - outw(iobase + UHCI_USBCMD, 0); - - int i; - for (i = 2; i < ndev->rh->ports; i++) { - if (!(inw(iobase + UHCI_RPORTS + i*2) & 0x0080) || - (inw(iobase + UHCI_RPORTS + i*2) == 0xFFFF)) - { - // Ungültige Werte für einen Rootport, also ist das hier keiner - ndev->rh->ports = i; - break; - } - } - - // Mehr als sieben Rootports sind leicht ungewöhnlich - if (ndev->rh->ports > 7) { - ndev->rh->ports = 2; - } - - int legsup = pci_inw(dev, UHCI_PCI_LEGSUP); - int cmd = inw(iobase + UHCI_USBCMD); - int intr = inw(iobase + UHCI_USBINTR); - - if ((legsup & - ~(UHCI_LEGSUP_STATUS | UHCI_LEGSUP_NO_CHG | UHCI_LEGSUP_PIRQ)) || - (cmd & USB_RUN) || (cmd & CONFIGURE) || !(cmd & GLOB_SUSP_MODE) || - (intr & 0x000F)) - { - // Damit uns nicht die IRQs um die Ohren fliegen - outw(iobase + UHCI_USBSTS, 0x3F); - - // Einen Frame abwarten - msleep(1); - - // Alle LEGSUP-Statusbits löschen - pci_outw(dev, UHCI_PCI_LEGSUP, UHCI_LEGSUP_STATUS); - - // Resetten - outw(iobase + UHCI_USBCMD, HCRESET); - // Ende des Resets abwarten - for (i = 0; (inw(iobase + UHCI_USBCMD) & HCRESET) && (i < 50); i++) { - msleep(10); - } - - // Interrupts erstmal deaktivieren - outw(iobase + UHCI_USBINTR, 0); - // Den gesamten HC ebenso - outw(iobase + UHCI_USBCMD, 0); - - // Alle Ports "runterfahren" - for (i = 0; i < ndev->rh->ports; i++) { - outw(iobase + UHCI_RPORTS + i * 2, 0); - } - } - - ndev->frame_list = mem_allocate(4096, 0); - assert(ndev->frame_list); - ndev->p_frame_list = (uintptr_t)get_phys_addr(ndev->frame_list); - assert(ndev->p_frame_list); - ndev->frame_list_lock = 0; - - ndev->queue_head = mem_allocate(sizeof(*ndev->queue_head), 0); - assert(ndev->queue_head); - ndev->p_queue_head = (uintptr_t)get_phys_addr(ndev->queue_head); - assert(ndev->p_queue_head); - ndev->queue_head_lock = 0; - - // Alle Pointer auf "invalid" setzen - ndev->queue_head->next = 1; - ndev->queue_head->transfer = 1; - ndev->queue_head->q_first = NULL; - ndev->queue_head->q_last = NULL; - - for (i = 0; i < 1024; i++) { - ndev->frame_list[i] = ndev->p_queue_head | 2; - } - - // Alle 1000 µs ein Frame (Standardwert) - outb(iobase + UHCI_SOFMOD, 0x40); - // Unsere Frameliste eintragen - outl(iobase + UHCI_FRBASEADD, ndev->p_frame_list); - // Framenummer zurücksetzen - outw(iobase + UHCI_FRNUM, 0); - - // Hier setzen wir das PIRQ-Bit und deaktivieren sämtlichen Legacy Support - pci_outw(dev, UHCI_PCI_LEGSUP, UHCI_LEGSUP_PIRQ); - - // HC starten - outw(iobase + UHCI_USBCMD, USB_RUN | CONFIGURE | MAXP); - // Alle Interrupts aktivieren - outw(iobase + UHCI_USBINTR, 0xF); - - // Connect-Status-Change-Bit zurücksetzen - for (i = 0; i < ndev->rh->ports; i++) { - outw(iobase + UHCI_RPORTS + i * 2, RPORT_CSC); - } - - // Resumesignal treiben (egal, obs was bringt) - outw(iobase + UHCI_USBCMD, USB_RUN | CONFIGURE | MAXP | FORCE_GRESUME); - msleep(20); - outw(iobase + UHCI_USBCMD, USB_RUN | CONFIGURE | MAXP); - msleep(100); - - register_uhci(ndev); -} - -int check_pci(void) -{ - struct init_dev_desc* devs; - - int ret = init_dev_list(&devs); - if (ret < 0) { - return 0; - } - - int got_something = 0; - - int8_t* data = (int8_t*)devs; - for (;;) { - size_t size; - - if (ret < sizeof(*devs)) { - break; - } - - devs = (struct init_dev_desc*)data; - size = sizeof(*devs) + devs->bus_data_size; - if (ret < size) { - break; - } - - if (devs->type == 10) { - struct pci_device* dev = (struct pci_device*)devs->bus_data; - if ((dev->class_id == 0x0C) && (dev->subclass_id == 0x03) && - (dev->interface_id == 0x00)) - { - got_something = 1; - init_uhc(dev, size); - } - } - - ret -= size; - data += size; - } - - return got_something; -} diff --git a/src/modules/usb/uhci/main.c b/src/modules/usb/uhci/main.c deleted file mode 100644 index 452f2ad..0000000 --- a/src/modules/usb/uhci/main.c +++ /dev/null @@ -1,73 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include - -#include -#include -#include -#include - -#include "uhci.h" - -extern pid_t usb_pid, pci_pid; - -int main(void) -{ - init_messaging(); - - // Warten, damit dieser Prozess nach usb1 (und möglichst nach usb-msd) - // weiterläuft, sollten diese Treiber gleichzeitig gestartet werden - // (z. B. Bootvorgang) - msleep(2000); - - usb_pid = init_service_get("usb1"); - if (!usb_pid) { - fprintf(stderr, "You need to start the usb1 driver first.\n"); - return 1; - } - - pci_pid = init_service_get("pci"); - if (!pci_pid) { - fprintf(stderr, "You need to start the pci driver first.\n"); - return 1; - } - - init_rpc_interface(); - - if (!check_pci()) { - return 0; - } - - for (;;) { - wait_for_rpc(); - } -} diff --git a/src/modules/usb/uhci/rpc.c b/src/modules/usb/uhci/rpc.c deleted file mode 100644 index e3f862e..0000000 --- a/src/modules/usb/uhci/rpc.c +++ /dev/null @@ -1,297 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "uhci.h" -#include "usb-hc.h" -#include "usb-server.h" - -// #define DEBUG -// #define SHOW_ERRORS - -pid_t usb_pid, pci_pid; - -struct uhci* uhcis[IRQ_COUNT] = { NULL }; - -void uhci_irq_handler(uint8_t irq) -{ - if ((irq < TYNDUR_IRQ_OFFSET) || (irq >= TYNDUR_IRQ_OFFSET + IRQ_COUNT)) { - return; - } - - struct uhci* hc = uhcis[irq - TYNDUR_IRQ_OFFSET]; - - int status = inw(hc->iobase + UHCI_USBSTS); - - if (!status) { - return; - } - - if ((status & 0x10) || (status & 0x08)) { - fprintf(stderr, "[uhci] SERIOUS HOST ERROR CONDITION (STS: 0x%02X)\n", status); - return; - } - - if (status & 0x20) { - fprintf(stderr, "[uhci] Erm, wtf. HC halted...\n"); - return; - } - - if (status & ~0x03) { - return; - } - - struct uhci_td *td, *ntd; - - lock(&hc->queue_head_lock); - - td = (struct uhci_td*)hc->queue_head->q_first; - - while (td != NULL) { - /* - #ifdef DEBUG - printf("Active: %i (next: 0x%08X)\n", td->flags.bits.active, td->next); - #endif - */ - if (td->flags.bits.active) { - if (td->active_count--) { - break; - } else { - #ifdef DEBUG - printf("Active timeout on 0x%08X/0x%08X, QH -> 0x%08X/0x%08X\n", - td, td->ptd, hc->queue_head->q_first, - hc->queue_head->transfer); - #endif - td->flags.bits.active = 0; - } - } - - #ifdef DEBUG - printf("Finished td %s %i:%i, flags: 0x%08X\n", - (td->pid == 0x69) ? "from" : "to", td->addr, td->ep, - td->flags.flags); - #endif - - if (td->result != NULL) { - if (td->flags.bits.spd) { - #ifdef SHOW_ERRORS - printf("[uhci] Short packet detected\n"); - #endif - - if (hc->queue_head->transfer == td->ptd) { - hc->queue_head->transfer = td->next; - } - - *td->result = EMSGSIZE; - } else if ((status == 0x01) && !td->flags.bits.e_stall) { - #ifdef DEBUG - printf("[uhci] Fine, actual len: %i; max len: %i\n", - td->flags.bits.act_len, td->maxlen); - #endif - if (((td->flags.bits.act_len + 1) & 0x7FF) < ((td->maxlen + 1) & 0x7FF)) { - *td->result = EMSGSIZE; - } else { - *td->result = 0; - } - } else { - td->pipe->toggle = td->toggle; - // TODO Weitere Transfers aus der Liste entfernen - #ifdef SHOW_ERRORS - printf("[uhci] Something failed, errors: %c%c%c%c%c%c\n", - td->flags.bits.e_bitstuff ? 'b' : '-', - td->flags.bits.e_crc_timeout ? 'c' : '-', - td->flags.bits.e_nak ? 'n' : '-', - td->flags.bits.e_babble ? 'q' : '-', - td->flags.bits.e_buffer ? 'f' : '-', - td->flags.bits.e_stall ? 's' : '-'); - printf("[uhci] Next'll be: 0x%08X/0x%08X\n", td->next, - td->q_next); - #endif - - if (hc->queue_head->transfer == td->ptd) { - hc->queue_head->transfer = td->next; - } - - if (td->flags.bits.e_stall) { - *td->result = EHOSTDOWN; - } else { - *td->result = EAGAIN; - } - } - } - - ntd = td->q_next; - if (td->free_on_completion) { - mem_free(td, 4096); - } - - if (td->next & 1) { - td = ntd; - break; - } - - td = ntd; - } - - hc->queue_head->q_first = td; - if (td == NULL) { - hc->queue_head->q_last = NULL; - hc->queue_head->transfer = 1; - } - - unlock(&hc->queue_head_lock); - - outw(hc->iobase + UHCI_USBSTS, status); -} - -void uhci_hct_handler(pid_t src, uint32_t corr_id, size_t length, void* data) -{ - struct usb_hc_trs* trs = data; - void* shm; - int result; - - if (length < sizeof(*trs)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - - switch (trs->type) - { - case USB_TRST_CONTROL: - if (length < sizeof(struct usb_hc_trs_ctrl)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - struct usb_hc_trs_ctrl* ctrl = data; - - shm = open_shared_memory(ctrl->shm); - result = uhci_do_control(trs->hc.hcd_avail, &trs->pipe, - &ctrl->setup, shm); - close_shared_memory(ctrl->shm); - rpc_send_int_response(src, corr_id, result); - break; - case USB_TRST_BULK: - if (length < sizeof(struct usb_hc_trs_bulk)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - struct usb_hc_trs_bulk* bulk = data; - - shm = open_shared_memory(bulk->shm); - result = uhci_do_bulk(trs->hc.hcd_avail, &trs->pipe, - bulk->receive, shm, bulk->length); - close_shared_memory(bulk->shm); - - if (!result) { - // Irgendwie muss der Status des Bits ja zurück - result = trs->pipe.toggle; - } - rpc_send_int_response(src, corr_id, result); - break; - default: - rpc_send_int_response(src, corr_id, -EINVAL); - break; - } -} - -void uhci_hcc_handler(pid_t src, uint32_t corr_id, size_t length, void* data) -{ - struct usb_hc_ctrl_req* rq = data; - struct usb_hc_ctrl_scan* rqsc = data; - struct usb_hc_ctrl_dsbl* rqdbl = data; - struct usb_hc_ctrl_rst* rqrst = data; - - if (length < sizeof(*rq)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - switch (rq->type) - { - case HC_CTRL_SCAN_PORT: - if (length < sizeof(*rqsc)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - int result = uhci_scan_port(rq->hc.hcd_avail, rqsc->port); - rpc_send_int_response(src, corr_id, result); - break; - case HC_CTRL_DSBL_PORT: - if (length < sizeof(*rqdbl)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - uhci_set_port_status(rq->hc.hcd_avail, rqdbl->port, 0); - rpc_send_int_response(src, corr_id, 1); - break; - case HC_CTRL_RESET: - if (length < sizeof(*rqrst)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - uhci_reset_port(rq->hc.hcd_avail, rqrst->port); - rpc_send_int_response(src, corr_id, 1); - break; - case HC_CTRL_ADD_PIPE: - rpc_send_int_response(src, corr_id, 1); - break; - default: - rpc_send_int_response(src, corr_id, 0); - break; - } -} - -void init_rpc_interface(void) -{ - register_message_handler(RPC_USB_HC_CONTROL_FNN, &uhci_hcc_handler); - register_message_handler(RPC_USB_HC_TRANSFER_FNN, &uhci_hct_handler); -} - -void register_uhci(struct uhci* hc) -{ - struct usb_hc rhc = { - .root_ports = hc->rh->ports, - .hcd_avail = hc - }; - - if (!rpc_get_int(usb_pid, RPC_USB_SV_REGHC_FNN, sizeof(rhc), (char*)&rhc)) { - fprintf(stderr, "Could not register UHCI.\n"); - } -} diff --git a/src/modules/usb/uhci/trans.c b/src/modules/usb/uhci/trans.c deleted file mode 100644 index b9c82c2..0000000 --- a/src/modules/usb/uhci/trans.c +++ /dev/null @@ -1,316 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include - -#include "uhci.h" -#include "usb-hc.h" -#include "usb-trans.h" - -#define USB_SETUP_OUT (0 << 7) -#define USB_SETUP_IN (1 << 7) - -int insert_td(struct uhci* hc, uintptr_t ptd, struct uhci_td* td); - -int uhci_do_control(struct uhci* hc, struct usb_trans_pipe* pipe, - struct usb_setup_packet* setup, void* buffer) -{ - if ((pipe->speed != USB_HUB_LOW_SPEED) && - (pipe->speed != USB_HUB_FULL_SPEED)) - { - return -EPROTOTYPE; - } - - struct uhci_td* td = mem_allocate(sizeof(*td), 0); - assert(td); - uintptr_t ptd = (uintptr_t)get_phys_addr(td); - assert(ptd); - - volatile int status; - - do { - status = EALREADY; - - td->next = 1; - if (pipe->speed == USB_HUB_LOW_SPEED) { - td->flags.flags = 0x1D800000; - } else { - td->flags.flags = 0x19800000; - } - td->pid = USB_SETUP; - td->toggle = 0; - td->addr = pipe->usb_addr; - td->ep = pipe->ep_id; - - td->maxlen = sizeof(*setup) - 1; - td->buffer = (uintptr_t)get_phys_addr(setup); - - td->result = &status; - td->free_on_completion = 0; - - td->pipe = pipe; - - while (insert_td(hc, ptd, td)); - - while (status == EALREADY); - } while (status == EAGAIN); - - if (status) { - if (status == EHOSTDOWN) { - pipe->stalled = 1; - } - - mem_free(td, 4096); - return -status; - } - - pipe->toggle = 1; - - int remaining = setup->length; - int direction, done = 0; - - if (setup->type & USB_SETUP_IN) { - direction = USB_IN; - } else { - direction = USB_OUT; - } - - while (remaining) { - int this_len = (remaining > pipe->mps) ? pipe->mps : remaining; - - remaining -= this_len; - - do { - status = EALREADY; - - td->next = 1; - if (pipe->speed == USB_HUB_LOW_SPEED) { - td->flags.flags = 0x1D800000; - } else { - td->flags.flags = 0x19800000; - } - td->pid = direction; - td->toggle = pipe->toggle; - td->addr = pipe->usb_addr; - td->ep = pipe->ep_id; - - td->maxlen = (unsigned int)(this_len - 1) & 0x7FF; - td->buffer = (uintptr_t)get_phys_addr((void*)((uintptr_t)buffer + done)); - - td->result = &status; - td->free_on_completion = 0; - - while (insert_td(hc, ptd, td)); - - while (status == EALREADY); - } while (status == EAGAIN); - - if (status && (status != EMSGSIZE)) { - if (status == EHOSTDOWN) { - pipe->stalled = 1; - } - - mem_free(td, 4096); - return -status; - } - - pipe->toggle ^= 1; - done += this_len; - - if (status == EMSGSIZE) - break; - } - - if (direction == USB_IN) { - direction = USB_OUT; - } else { - direction = USB_IN; - } - - do { - status = EALREADY; - - td->next = 1; - if (pipe->speed == USB_HUB_LOW_SPEED) { - td->flags.flags = 0x1D800000; - } else { - td->flags.flags = 0x19800000; - } - td->pid = direction; - td->toggle = 1; - td->addr = pipe->usb_addr; - td->ep = pipe->ep_id; - - td->maxlen = 0x7FF; - td->buffer = 0; - - td->result = &status; - td->free_on_completion = 0; - - while (insert_td(hc, ptd, td)); - - while (status == EALREADY); - } while (status == EAGAIN); - - if (status == EHOSTDOWN) { - pipe->stalled = 1; - } - - pipe->toggle = 0; - mem_free(td, sizeof(*td)); - - return -status; -} - -int uhci_do_bulk(struct uhci* hc, struct usb_trans_pipe* pipe, bool receive, - void* buffer, size_t length) -{ - if ((pipe->speed != USB_HUB_LOW_SPEED) && - (pipe->speed != USB_HUB_FULL_SPEED)) - { - return -EPROTOTYPE; - } - - if (!length) { - return -EINVAL; - } - - // printf("Issuing bulk (%i B expected)\n", length); - - struct uhci_td* td = mem_allocate(sizeof(*td), 0); - assert(td); - uintptr_t ptd = (uintptr_t)get_phys_addr(td); - assert(ptd); - - volatile int res; - - int remaining = length; - int done = 0; - - while (remaining) { - // printf("%i B to go\n", remaining); - int this_len = (remaining > pipe->mps) ? pipe->mps : remaining; - - remaining -= this_len; - - td->next = 1; - if (pipe->speed == USB_LOW_SPEED) { - td->flags.flags = 0x1D800000; - } else { - td->flags.flags = 0x19800000; - } - if (receive) { - td->pid = USB_IN; - } else { - td->pid = USB_OUT; - } - td->toggle = pipe->toggle; - td->addr = pipe->usb_addr; - td->ep = pipe->ep_id; - - td->maxlen = (unsigned int)(this_len - 1) & 0x7FF; - td->buffer = (uintptr_t) - get_phys_addr((void *)((uintptr_t)buffer + done)); - - res = EALREADY; - td->result = &res; - td->free_on_completion = 0; - td->pipe = pipe; - - // printf("Inserting TD\n"); - while (insert_td(hc, ptd, td)); - - // printf("Waiting for transmission\n"); - while (res == EALREADY); - // printf("Thru\n"); - - if (res == EAGAIN) { - remaining += this_len; - continue; - } - - if (res) { - return -res; - } - - pipe->toggle ^= 1; - - done += this_len; - } - - if (res == EHOSTDOWN) { - pipe->stalled = 1; - } - - mem_free(td, sizeof(*td)); - - return -res; -} - -int insert_td(struct uhci* hc, uintptr_t ptd, struct uhci_td* td) -{ - struct uhci_td *last; - - lock(&hc->queue_head_lock); - - td->ptd = ptd; - td->q_next = NULL; - td->active_count = 10; - - if (hc->queue_head->transfer & 1) { - // Insert as first element to be done - if (hc->queue_head->q_last != NULL) { - hc->queue_head->q_last->q_next = td; - } else { - hc->queue_head->q_first = td; - } - - hc->queue_head->q_last = td; - hc->queue_head->transfer = ptd; - } else { - last = (struct uhci_td*)hc->queue_head->q_last; - #ifdef DEBUG - printf("[uhci] Will append to 0x%08X (p 0x%08X)\n", last, hc->queue_head->transfer); - #endif - - last->q_next = td; - hc->queue_head->q_last = td; - last->next = ptd | 4; - } - - unlock(&hc->queue_head_lock); - - return 0; -} diff --git a/src/modules/usb/uhci/uhci.h b/src/modules/usb/uhci/uhci.h deleted file mode 100644 index c6b758c..0000000 --- a/src/modules/usb/uhci/uhci.h +++ /dev/null @@ -1,269 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef UHCI_H -#define UHCI_H - -#include -#include - -#include "usb-hc.h" -#include "usb-trans.h" - - -#define TYNDUR_IRQ_OFFSET 0x20 -#define IRQ_COUNT 0x10 - -// Befehlsregister (uint16_t) -#define UHCI_USBCMD 0x00 -// Statusregister (uint16_t) -#define UHCI_USBSTS 0x02 -// Interruptregister (uint16_t) -#define UHCI_USBINTR 0x04 -// Aktuelle Framenummer (uint16_t) -#define UHCI_FRNUM 0x06 -// Pointer zur Frameliste (uint32_t) -#define UHCI_FRBASEADD 0x08 -// Bestimmt die genaue Länge eines Frames (uint8_t) -#define UHCI_SOFMOD 0x0C -// Array von Informationen über je einen Rootport (je uint16_t) -#define UHCI_RPORTS 0x10 - -#define MAXP (1 << 7) -#define CONFIGURE (1 << 6) -// Resumesignal treiben -#define FORCE_GRESUME (1 << 4) -// Alle Rootports befinden sich im Suspendmodus -#define GLOB_SUSP_MODE (1 << 3) -// RESET-Signal wird an allen Rootports getrieben -#define GRESET (1 << 2) -// HC wird zurückgesetzt -#define HCRESET (1 << 1) -// Normaler Ausführungsmodus -#define USB_RUN (1 << 0) - -// Setzt einen Rootport zurück -#define RPORT_RESET (1 << 9) -// Gibt an, ob ein Low-Speed-Gerät angeschlossen ist -#define RPORT_LOSPD (1 << 8) -// Treibt das Resume-Signal auf dem Bus -#define RPORT_RESUME (1 << 6) -// Schaltet den Rootport ein -#define RPORT_ENABLE (1 << 2) -// Wird gesetzt, wenn ein Gerät angeschlossen oder entfernt wird -#define RPORT_CSC (1 << 1) -// Gibt an, ob ein Gerät angeschlossen ist -#define RPORT_DEVICE (1 << 0) - -// Das Legacy-Support-Register im PCI-Konfigurationsraum (uint16_t) -#define UHCI_PCI_LEGSUP 0xC0 - -// Alle Statusbits, die mittels Schreiben einer 1 gelöscht werden -#define UHCI_LEGSUP_STATUS 0x8F00 -// Unveränderliche Bits (RO oder reserviert) -#define UHCI_LEGSUP_NO_CHG 0x5040 -// Interrupt wird als PCI-IRQ ausgeführt -#define UHCI_LEGSUP_PIRQ 0x2000 - - -struct uhci_td { - volatile uint32_t next; - - union { - volatile uint32_t flags; - struct { - volatile unsigned act_len : 11; - unsigned rsvd1 : 6; - volatile unsigned e_bitstuff : 1; - volatile unsigned e_crc_timeout : 1; - volatile unsigned e_nak : 1; - volatile unsigned e_babble : 1; - volatile unsigned e_buffer : 1; - volatile unsigned e_stall : 1; - volatile unsigned active : 1; - unsigned ioc : 1; - unsigned isochronous : 1; - unsigned low_speed : 1; - volatile unsigned errors : 2; - unsigned spd : 1; - unsigned rsvd2 : 2; - } __attribute__((packed)) bits; - } __attribute__((packed)) flags; - - unsigned pid : 8; - unsigned addr : 7; - unsigned ep : 4; - unsigned toggle : 1; - unsigned rsvd : 1; - unsigned maxlen : 11; - - uint32_t buffer; - - // Diese Felder sind für den HCD - - // Nächster TD in der Warteschlange - struct uhci_td* q_next; - - // Pointer zu einer Variable, die einen errno-Wert aufnimmt - volatile int* result; - - // Ist dieses Flag gesetzt, so wird der TD nach Abarbeitung vom IRQ-Handler - // automatisch freigegeben - int free_on_completion; - - // Physische Adresse - uintptr_t ptd; - - // Entsprechende Pipe - struct usb_trans_pipe* pipe; - - int active_count; -} __attribute__((packed)); - -struct uhci_qh { - volatile uint32_t next; - volatile uint32_t transfer; - - // Diese Felder sind für den HCD - - // Erster TD in der Warteschlange (oder NULL, wenn keiner) - volatile struct uhci_td* q_first; - - // Letzter TD in der Warteschlange (oder NULL, wenn keiner) - volatile struct uhci_td* q_last; -} __attribute__((packed)); - -// Beschreibt ein UHCI -struct uhci { - // PCI-Geräteinformationen - struct pci_device* pci; - // Beginn des I/O-Raums - uint16_t iobase; - - // Physische Adresse der Framelist - uintptr_t p_frame_list; - // Virtuelle Adresse der Framelist - uint32_t* frame_list; - // Lock für den Zugriff auf diese Liste - lock_t frame_list_lock; - - // Physische Adresse des (bisher einzigen) QHs - uintptr_t p_queue_head; - // Virtuelle Adresse des QHs - struct uhci_qh* queue_head; - // Lock für den Zugriff auf diesen - lock_t queue_head_lock; - - // Roothub, der durch das UHCI bereitgestellt wird - struct usb_rh* rh; -}; - -// Beschreibt einen Roothub (nur für UHCI) -struct usb_rh { - // Anzahl der Rootports - int ports; -}; - - -/** - * Durchsucht alle verfügbaren PCI-Geräte nach einem UHCI. - * - * @return 1, wenn ein UHCI gefunden wurde, sonst 0. - */ -int check_pci(void); - -/** - * Initialisiert das RPC-Interface (Handler) - */ -void init_rpc_interface(void); - -/** - * Registriert einen UHCI als HC beim USB-Treiber. - * - * @param hc Das zu registrierende UHCI - */ -void register_uhci(struct uhci* hc); - -/** - * Führt einen Bulktransfer durch. - * - * @param hc UHCI - * @param pipe Pipe, durch die die Übertragung stattfinden soll - * @param receive true, wenn empfangen werden soll, sonst false - * @param buffer Zielspeicherbereich - * @param length (Angestrebte) Länge des Transfers - * - * @return 0 bei Erfolg, sonst -errno - */ -int uhci_do_bulk(struct uhci* hc, struct usb_trans_pipe* pipe, bool receive, - void* buffer, size_t length); - -/** - * Führt einen Controltransfer durch. - * - * @param hc UHCI - * @param pipe Pipe, durch die die Übertragung stattfinden soll - * @param setup SETUP-Paket - * @param buffer Speicherbereich, mit dem während der Datenphase gearbeitet - * werden soll. - * - * @return 0 bei Erfolg, sonst -errno - */ -int uhci_do_control(struct uhci* hc, struct usb_trans_pipe* pipe, - struct usb_setup_packet* setup, void* buffer); - -/** - * Treibt das Resetsignal auf einem Port und aktiviert ihn. - * - * @param hc UHCI - * @param port Der Portindex - */ -void uhci_reset_port(struct uhci* hc, int port); - -/** - * Überprüft einen Port. - * - * @param hc UHCI - * @param port Zu überprüfender Port - * - * @return Status (USB_HUB_...) - */ -int uhci_scan_port(struct uhci* hc, int port); - -/** - * Aktiviert oder deaktiviert einen Port. - * - * @param hc UHCI - * @param port Portindex - * @param status 1 zum Aktivieren, 0 zum Deaktivieren - */ -void uhci_set_port_status(struct uhci* hc, int port, int status); - -#endif diff --git a/src/modules/usb/usb1/Makefile.all b/src/modules/usb/usb1/Makefile.all deleted file mode 100644 index 54993c8..0000000 --- a/src/modules/usb/usb1/Makefile.all +++ /dev/null @@ -1,8 +0,0 @@ -shopt -s extglob -source $LOST_BUILDMK_ROOT/config.sh - -CC="$CC -I../include" - -echo "LD $1/modules/usb1" -$LOST_TOOLS_LD $LDSCRIPT -ousb1.mod *.o --start-group $2 --end-group -$LOST_TOOLS_STRIP -s usb1.mod -o $1/modules/usb1 diff --git a/src/modules/usb/usb1/detect.c b/src/modules/usb/usb1/detect.c deleted file mode 100644 index a99fd36..0000000 --- a/src/modules/usb/usb1/detect.c +++ /dev/null @@ -1,444 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include -#include -#include - -#include "usb.h" -#include "usb-ddrv.h" -#include "usb-hc.h" -#include "usb-hub.h" - -extern void usb_found_hub(struct usb_device* dev); - -extern list_t* usb_ddrvs; -extern list_t* unhandled_devs; - -static const char* speed_name(int speed) -{ - switch (speed) { - case USB_LOW_SPEED: - return "low speed"; - case USB_FULL_SPEED: - return "full speed"; - case USB_HIGH_SPEED: - return "high speed"; - default: - return "unknown speed"; - } -} - -static int fits(int is, int want) -{ - if (want == -1) - return -1; - return !!(is == want); -} - -static int provide_to(struct usb_device* dev, int ifc_num, - struct usb_interface_descriptor* ifc, struct usb_ddrv* drv) -{ - struct usb_prov_dev pd = { - .is_interface = (ifc == NULL) ? false : true, - .dev_id = dev, - .interface_id = ifc_num, - .type = { - .vendor_id = dev->dev_desc->vendor_id, - .device_id = dev->dev_desc->device_id, - .class = (ifc == NULL) ? dev->dev_desc->class : ifc->class, - .subclass = (ifc == NULL) ? dev->dev_desc->subclass : ifc->subclass, - .protocol = (ifc == NULL) ? dev->dev_desc->protocol : ifc->protocol - } - }; - - return rpc_get_int(drv->pid, RPC_USB_DDRV_PLUG_FNN, sizeof(pd), (char*)&pd); -} - -static void dev_is_unhandled(struct usb_device* dev, int ifc_num, - struct usb_interface_descriptor* ifc) -{ - struct usb_prov_dev* pd = calloc(1, sizeof(*pd)); - - pd->is_interface = (ifc == NULL) ? false : true; - pd->dev_id = dev; - pd->interface_id = ifc_num; - pd->type.vendor_id = dev->dev_desc->vendor_id; - pd->type.device_id = dev->dev_desc->device_id; - pd->type.class = (ifc == NULL) ? dev->dev_desc->class : ifc->class; - pd->type.subclass = (ifc == NULL) ? dev->dev_desc->subclass : ifc->subclass; - pd->type.protocol = (ifc == NULL) ? dev->dev_desc->protocol : ifc->protocol; - - p(); - list_push(unhandled_devs, pd); - v(); -} - -#define check(svar, lname) int svar##f = fits(svar, drv->handle.lname); \ - if (!svar##f) { continue; } - -#define provto(check) if (check && provide_to(dev, ifc_num, ifc, drv)) \ - { break; } - -static void provide(struct usb_device* dev, int ifc_num, - struct usb_interface_descriptor* ifc) -{ - int vid, did, cls, scl, ptc; - - vid = dev->dev_desc->vendor_id; - did = dev->dev_desc->device_id; - if (ifc == NULL) { - cls = dev->dev_desc->class; - scl = dev->dev_desc->subclass; - ptc = dev->dev_desc->protocol; - } else { - cls = ifc->class; - scl = ifc->subclass; - ptc = ifc->protocol; - } - - int i, s = list_size(usb_ddrvs); - for (i = 0; i < s; i++) { - struct usb_ddrv* drv = list_get_element_at(usb_ddrvs, i); - - check(vid, vendor_id) - check(did, device_id) - check(cls, class) - check(scl, subclass) - check(ptc, protocol) - - // Wenn jemandem noch eine sinnvolle Kombination einfällt, kann er die - // gern hier eintragen. - // If anyone knows another combination making sense, he is encouraged - // to insert it here. - provto(vidf && didf) - provto(vidf && clsf && sclf && ptcf) - provto( clsf && sclf && ptcf) - provto(vidf && clsf && sclf) - provto( clsf && sclf) - provto(vidf && clsf) - provto( clsf) - } - - if (i == s) { - dev_is_unhandled(dev, ifc_num, ifc); - } -} - -void update_ep_trs_info(struct usb_pipe* ep) -{ - ep->trs.usb_addr = ep->dev->addr; - ep->trs.ep_id = ep->ep->number; - ep->trs.mps = ep->ep->mps; - ep->trs.type = ep->ep->type; - ep->trs.speed = ep->dev->speed; -} - -#define TNC(fnc) if ((ret = fnc)) { \ - printf("[usb1] err@%i, ret is %i\n" # fnc "\n", __LINE__, ret); return; } - -void enumerate_device(struct usb_device* dev) -{ - struct usb_hub* hub = dev->parent; - int p = dev->hub_port; - - printf("[usb1] Enumerating %s device on port %i of some hub\n", speed_name(dev->speed), dev->hub_port); - - hub->reset_port(hub, p); - dev->addr = 0; - dev->state = USB_DEFAULT; - - struct usb_pipe* ep0; - dev->ep0 = ep0 = calloc(1, sizeof(*ep0)); - - ep0->dev = dev; - ep0->ep = calloc(1, sizeof(*ep0->ep)); - ep0->ep->number = 0; - ep0->ep->mps = 8; - ep0->ep->type = USB_EP_CONTROL; - - update_ep_trs_info(ep0); - - ep0->trs.toggle = 0; - ep0->trs.stalled = false; - - if (hub->add_pipe != NULL) { - hub->add_pipe(hub, ep0); - } - - dev->dev_desc = calloc(1, sizeof(*dev->dev_desc)); - - int ret = usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_DEVICE | 0, 0, 8, dev->dev_desc); - - printf("[usb1] mps0 is %i B\n", dev->dev_desc->mps0); - - if (ret) { - fprintf(stderr, "[usb1] Warning: first transfer returned %i\n", ret); - } - - if (!dev->dev_desc->mps0) { - return; - } - - hub->reset_port(hub, p); - ep0->ep->mps = dev->dev_desc->mps0; - ep0->trs.stalled = false; - update_ep_trs_info(ep0); - - int address = dev->hc->addr_counter++; - TNC(usb_control_transfer(ep0, - USB_SETUP_OUT | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, USB_SET_ADDRESS, - address, 0, 0, NULL)); - dev->addr = address; - update_ep_trs_info(ep0); - - if (hub->add_pipe != NULL) { - hub->add_pipe(hub, dev->ep0); - } - - dev->state = USB_ADDRESS; - - TNC(usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_DEVICE | 0, 0, sizeof(*dev->dev_desc), - dev->dev_desc)); - - printf("[usb1] 0x%04X:0x%04X (%i configs)\n", dev->dev_desc->vendor_id, - dev->dev_desc->device_id, dev->dev_desc->num_configurations); - - int i; - - if (dev->dev_desc->i_manufacturer) { - uint8_t name[255]; - - TNC(usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_STRING | dev->dev_desc->i_manufacturer, - 0, 255, name)); - - printf("[usb1] -> Manufacturer: "); - for (i = 2; i < name[0]; i += 2) { - printf("%c", name[i]); - } - printf("\n"); - } - if (dev->dev_desc->i_product) { - uint8_t name[255]; - - TNC(usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_STRING | dev->dev_desc->i_product, - 0, 255, name)); - - printf("[usb1] -> Product: "); - for (i = 2; i < name[0]; i += 2) { - printf("%c", name[i]); - } - printf("\n"); - } - - dev->cfg_desc = calloc(dev->dev_desc->num_configurations, sizeof(*dev->cfg_desc)); - for (i = 0; i < dev->dev_desc->num_configurations; i++) { - uint8_t tmp[9]; - - // Erstmal Länge aller Deskriptoren herausfinden - TNC(usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_CONFIGURATION | i, 0, 9, tmp)); - dev->cfg_desc[i] = calloc(1, tmp[2] | (tmp[3] << 8)); - TNC(usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_CONFIGURATION | i, 0, - tmp[2] | (tmp[3] << 8), dev->cfg_desc[i])); - } - - if (dev->dev_desc->class == 9) { - usb_found_hub(dev); - return; - } - - if (dev->dev_desc->class) { - provide(dev, 0, NULL); - } - - struct usb_config_descriptor* best_cfg = dev->cfg_desc[0]; - - int c; - for (c = 0; c < dev->dev_desc->num_configurations; c++) { - struct usb_config_descriptor* cur_cfg = dev->cfg_desc[c]; - - if (cur_cfg->self_powered && !best_cfg->self_powered) { - // Wir wollen möglichst Self-powered - best_cfg = cur_cfg; - } else if (cur_cfg->max_power > best_cfg->max_power) { - // Und möglichst hohen Stromverbrauch (FIXME: Das aber nicht immer) - best_cfg = cur_cfg; - } - } - - dev->cur_cfg = best_cfg; - usb_set_configuration(dev, best_cfg->configuration_value); - - struct usb_interface_descriptor* cur_ifc = (struct usb_interface_descriptor*)(best_cfg + 1); - - for (i = 0; i < best_cfg->num_interfaces; i++) { - if (cur_ifc->class && !cur_ifc->alternate_setting) { - provide(dev, i, cur_ifc); - } - - cur_ifc = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(cur_ifc + 1) + - cur_ifc->num_endpoints); - } -} - -void scan_hub(struct usb_hub* hub) -{ - int p; - - printf("[usb1] Hub with %i ports detected.\n", hub->ports); - - for (p = 0; p < hub->ports; p++) { - hub->disable_port(hub, p); - } - - for (p = 0; p < hub->ports; p++) { - int stat = hub->scan_port(hub, p); - - if (stat & USB_HUB_DEVICE) { - struct usb_device* dev = calloc(1, sizeof(*dev)); - - dev->parent = hub; - dev->hub_port = p; - - if (hub->root_hub) { - dev->hc = hub->dev.hc; - } else { - dev->hc = hub->dev.usb->hc; - } - - dev->addr = 0; - dev->state = USB_ATTACHED; - dev->speed = stat & USB_HUB_SPEED; - - enumerate_device(dev); - } - } -} - -static void rh_addpipe(struct usb_hub* hub, struct usb_pipe* pipe) -{ - struct usb_hc* hc; - - if (hub->root_hub) { - hc = hub->dev.hc; - } else { - hc = hub->dev.usb->hc; - } - - struct usb_hc_ctrl_appe appe = { - .req = { - .type = HC_CTRL_ADD_PIPE, - .hc = *hc - }, - .pipe = pipe->trs - }; - - rpc_get_int(hc->pid, RPC_USB_HC_CONTROL_FNN, sizeof(appe), - (char*)&appe); -} - -static void rh_rst(struct usb_hub* hub, int port) -{ - struct usb_hc_ctrl_rst rqrst = { - .req = { - .type = HC_CTRL_RESET, - .hc = *hub->dev.hc - }, - .port = port - }; - - rpc_get_int(hub->dev.hc->pid, RPC_USB_HC_CONTROL_FNN, sizeof(rqrst), - (char*)&rqrst); -} - -static int rh_scan(struct usb_hub* hub, int port) -{ - struct usb_hc_ctrl_scan rqsc = { - .req = { - .type = HC_CTRL_SCAN_PORT, - .hc = *hub->dev.hc - }, - .port = port - }; - - return rpc_get_int(hub->dev.hc->pid, RPC_USB_HC_CONTROL_FNN, sizeof(rqsc), - (char*)&rqsc); -} - -static void rh_disable(struct usb_hub* hub, int port) -{ - struct usb_hc_ctrl_dsbl rqdbl = { - .req = { - .type = HC_CTRL_DSBL_PORT, - .hc = *hub->dev.hc - }, - .port = port - }; - - rpc_get_int(hub->dev.hc->pid, RPC_USB_HC_CONTROL_FNN, sizeof(rqdbl), - (char*)&rqdbl); -} - -void scan_hc(struct usb_hc* hc) -{ - struct usb_hub* rh = malloc(sizeof(*rh)); - - *rh = (struct usb_hub) { - .dev = { - .hc = hc - }, - .root_hub = true, - .ports = hc->root_ports, - .add_pipe = &rh_addpipe, - .disable_port = &rh_disable, - .reset_port = &rh_rst, - .scan_port = &rh_scan - }; - - scan_hub(rh); -} diff --git a/src/modules/usb/usb1/hub.c b/src/modules/usb/usb1/hub.c deleted file mode 100644 index b1d0597..0000000 --- a/src/modules/usb/usb1/hub.c +++ /dev/null @@ -1,180 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "usb.h" -#include "usb-hc.h" - -static int scan_hub_port(struct usb_hub* hub, int port) -{ - uint32_t status; - - usb_control_transfer(hub->dev.usb->ep0, - USB_SETUP_IN | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, USB_GET_STATUS, - 0, port + 1, 4, &status); - - if (!(status & (1 << 0))) { - return 0; - } - - if (status & (1 << 9)) { - return USB_HUB_DEVICE | USB_HUB_LOW_SPEED; - } else if (status & (1 << 10)) { - return USB_HUB_DEVICE | USB_HUB_HIGH_SPEED; - } else { - return USB_HUB_DEVICE | USB_HUB_FULL_SPEED; - } -} - -static void disable_hub_port(struct usb_hub *hub, int port) -{ - // Deaktivieren - usb_control_transfer(hub->dev.usb->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_CLEAR_FEATURE, 1, port + 1, 0, NULL); -} - -static void reset_hub_port(struct usb_hub* hub, int port) -{ - uint32_t cur; - - port++; - - usb_control_transfer(hub->dev.usb->ep0, - USB_SETUP_IN | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, USB_GET_STATUS, - 0, port, 4, &cur); - - // Zurücksetzen - usb_control_transfer(hub->dev.usb->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_SET_FEATURE, 4, port, 0, NULL); - - msleep(20); - - if (!(cur & (1 << 1)) || (cur & (1 << 2))) { - // Vorher deaktiviert oder im Suspendmodus, Resume treiben - usb_control_transfer(hub->dev.usb->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_CLEAR_FEATURE, 2, port, 0, NULL); - msleep(30); - } -} - -void usb_found_hub(struct usb_device* dev) -{ - struct usb_config_descriptor* ccfg; - struct usb_interface_descriptor* cint; - struct usb_hub* hub; - struct usb_hub_descriptor* chub; - uint8_t chub_buf[256]; - - dev->cur_cfg = NULL; - - int c, i; - - for (c = 0; c < dev->dev_desc->num_configurations; c++) { - ccfg = dev->cfg_desc[c]; - cint = (struct usb_interface_descriptor*)(dev->cfg_desc[c] + 1); - for (i = 0; i < ccfg->num_interfaces; i++) { - if (cint->class == 9) { - dev->cur_cfg = ccfg; - break; - } - - cint = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(cint + 1) + - cint->num_endpoints); - } - } - - if (dev->cur_cfg == NULL) { - return; - } - - usb_set_configuration(dev, dev->cur_cfg->configuration_value); - - hub = calloc(1, sizeof(*hub)); - if (hub == NULL) { - return; - } - - hub->dev.usb = dev; - hub->root_hub = false; - - usb_control_transfer(dev->ep0, - USB_SETUP_IN | USB_SETUP_REQ_CLS | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_HUB, 0, 256, chub_buf); - - chub = (struct usb_hub_descriptor*)chub_buf; - - hub->ports = chub->nbr_ports; - - for (i = 1; i <= hub->ports; i++) { - // Strom einschalten - usb_control_transfer(dev->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_SET_FEATURE, 8, i, 0, NULL); - - msleep(chub->pwr_on_2_pwr_good * 2); - } - - msleep(100); - - for (i = 1; i <= hub->ports; i++) { - uint32_t status; - - usb_control_transfer(dev->ep0, - USB_SETUP_IN | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_GET_STATUS, 0, i, 4, &status); - - if (status & (1 << 1)) { - // Port deaktivieren - usb_control_transfer(dev->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_CLEAR_FEATURE, 1, i, 0, NULL); - } - } - - hub->add_pipe = dev->parent->add_pipe; - hub->disable_port = &disable_hub_port; - hub->reset_port = &reset_hub_port; - hub->scan_port = &scan_hub_port; - - scan_hub(hub); -} diff --git a/src/modules/usb/usb1/main.c b/src/modules/usb/usb1/main.c deleted file mode 100644 index a7611ff..0000000 --- a/src/modules/usb/usb1/main.c +++ /dev/null @@ -1,59 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "usb.h" - -extern list_t* usb_hcs; -extern list_t* usb_ddrvs; -extern list_t* unhandled_devs; - -int main(void) -{ - usb_hcs = list_create(); - usb_ddrvs = list_create(); - unhandled_devs = list_create(); - - init_messaging(); - register_rpc_interface(); - - for (;;) { - wait_for_rpc(); - } -} diff --git a/src/modules/usb/usb1/rpc.c b/src/modules/usb/usb1/rpc.c deleted file mode 100644 index 51bf0ca..0000000 --- a/src/modules/usb/usb1/rpc.c +++ /dev/null @@ -1,350 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "usb.h" -#include "usb-ddrv.h" -#include "usb-hc.h" -#include "usb-server.h" - -list_t* usb_hcs; -list_t* usb_ddrvs; -list_t* unhandled_devs; - -extern void scan_hc(struct usb_hc* hc); - -static void hcd_handler(pid_t src, uint32_t corr_id, size_t length, void* data) -{ - struct usb_hc* hc; - - if (length < sizeof(*hc)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - hc = malloc(sizeof(*hc)); - memcpy(hc, data, sizeof(*hc)); - - hc->pid = src; - hc->addr_counter = 1; - list_push(usb_hcs, hc); - - scan_hc(hc); - - rpc_send_int_response(src, corr_id, 42); -} - -#define check(name) if ((dev->type.name != hd->name) && (hd->name != -1)) \ - { continue; } - -static void ddrv_handler(pid_t src, uint32_t corr_id, size_t length, void* data) -{ - struct usb_handle_devs* hd = data; - - if (length < sizeof(*hd)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - p(); - struct usb_ddrv* drv = calloc(1, sizeof(*drv)); - drv->pid = src; - drv->handle = *hd; - list_push(usb_ddrvs, drv); - - int i, s = list_size(unhandled_devs); - for (i = 0; i < s; i++) { - struct usb_prov_dev* dev = list_get_element_at(unhandled_devs, i); - - check(vendor_id) - check(device_id) - check(class) - check(subclass) - check(protocol) - - if (rpc_get_int(src, RPC_USB_DDRV_PLUG_FNN, sizeof(*dev), (char*)dev)) { - list_remove(unhandled_devs, i--); - s--; - } - } - v(); - - rpc_send_int_response(src, corr_id, 42); -} - -static void devctl_handler(pid_t src, uint32_t corr_id, size_t length, - void* data) -{ - struct usb_devctrl_req* rq = data; - - if (length < sizeof(*rq)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - struct usb_device* dev; - struct usb_interface_descriptor* ifc; - int ifcs, ifc_num, i, ret; - - switch (rq->type) { - case USB_DC_NUM_ENDPOINTS: - { - if (length < sizeof(struct usb_devctrl_gne)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - struct usb_devctrl_gne* ne = data; - dev = ne->dev.dev_id; - ifc = (struct usb_interface_descriptor*)(dev->cur_cfg + 1); - - ifcs = dev->cur_cfg->num_interfaces; - ifc_num = ne->dev.interface_id; - if ((ifc_num < 0) || (ifc_num >= ifcs)) { - ifc_num = 0; - } - - for (i = 0; i < ifcs; i++) { - if (i == ifc_num) { - rpc_send_int_response(src, corr_id, ifc->num_endpoints); - return; - } - - ifc = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(ifc + 1) + - ifc->num_endpoints); - } - // Hierhin sollte der Code nie gelangen - // Execution should never get here - break; - } - - case USB_DC_GET_IFC_ID: - { - if (length < sizeof(struct usb_devctrl_gii)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - struct usb_devctrl_gii* ii = data; - dev = ii->dev.dev_id; - ifc = (struct usb_interface_descriptor*)(dev->cur_cfg + 1); - - ifcs = dev->cur_cfg->num_interfaces; - // Das ist nicht die, die wir suchen - // This is not the one we're looking for - ifc_num = ii->dev.interface_id; - if ((ifc_num < 0) || (ifc_num >= ifcs)) { - ifc_num = 0; - } - - for (i = 0; i < ifcs; i++) { - if (i == ifc_num) { - rpc_send_int_response(src, corr_id, ifc->interface_number); - return; - } - - ifc = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(ifc + 1) + - ifc->num_endpoints); - } - // s. o. - break; - } - - case USB_DC_GET_ENDPOINT: - { - if (length < sizeof(struct usb_devctrl_gep)) { - // Hm, geht wohl nicht anders (und vertretbar, da dieser Fall - // nie auftreten sollte) - rpc_send_response(src, corr_id, 0, NULL); - return; - } - - struct usb_devctrl_gep* ge = data; - dev = ge->dev.dev_id; - ifc = (struct usb_interface_descriptor*)(dev->cur_cfg + 1); - - ifcs = dev->cur_cfg->num_interfaces; - ifc_num = ge->dev.interface_id; - if ((ifc_num < 0) || (ifc_num >= ifcs)) { - ifc_num = 0; - } - - for (i = 0; i < ifcs; i++) { - if (i == ifc_num) { - struct usb_endpoint_descriptor* epd = - (struct usb_endpoint_descriptor*)(ifc + 1); - rpc_send_response(src, corr_id, sizeof(*epd), - (char*)&epd[ge->ep_number]); - return; - } - - ifc = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(ifc + 1) + - ifc->num_endpoints); - } - // s. o. - break; - } - - case USB_DC_GET_PIPE: - { - if (length < sizeof(struct usb_devctrl_gpp)) { - // Auch hier wohl nur so möglich - rpc_send_response(src, corr_id, 0, NULL); - return; - } - - struct usb_devctrl_gpp* gp = data; - dev = gp->dev.dev_id; - ifc = (struct usb_interface_descriptor*)(dev->cur_cfg + 1); - - ifcs = dev->cur_cfg->num_interfaces; - ifc_num = gp->dev.interface_id; - if ((ifc_num < 0) || (ifc_num >= ifcs)) { - ifc_num = 0; - } - - for (i = 0; i < ifcs; i++) { - if (i == ifc_num) { - if ((gp->ep_number < 0) || - (gp->ep_number >= ifc->num_endpoints)) - { - gp->ep_number = 0; - } - - struct usb_endpoint_descriptor* epd = - (struct usb_endpoint_descriptor*)(ifc + 1); - epd += gp->ep_number; - - struct usb_pipe pp = { - .dev = dev, - .ep = epd, - .trs = { - .usb_addr = dev->addr, - .ep_id = epd->number, - .mps = epd->mps, - .type = epd->type, - .speed = dev->speed, - .toggle = 0, - .stalled = false - } - }; - - if (dev->parent->add_pipe != NULL) { - dev->parent->add_pipe(dev->parent, &pp); - } - - rpc_send_response(src, corr_id, sizeof(pp), (char*)&pp); - return; - } - - ifc = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(ifc + 1) + - ifc->num_endpoints); - } - // s. o. - break; - } - - case USB_DC_CLEAR_HALT: - { - if (length < sizeof(struct usb_devctrl_clh)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - - struct usb_devctrl_clh* clrh = data; - usb_clear_halt(&clrh->pipe); - rpc_send_int_response(src, corr_id, 0); - break; - } - - case USB_DC_TRSF_CONTROL: - { - if (length < sizeof(struct usb_devctrl_tct)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - - struct usb_devctrl_tct* tctrq = data; - if (tctrq->ep0) { - dev = tctrq->dst.dev_id; - ret = usb_shm_control_transfer(dev->ep0, tctrq->setup.type, - tctrq->setup.request, tctrq->setup.value, - tctrq->setup.index, tctrq->setup.length, tctrq->shm); - } else { - ret = usb_shm_control_transfer(&tctrq->dst.pipe, - tctrq->setup.type, tctrq->setup.request, tctrq->setup.value, - tctrq->setup.index, tctrq->setup.length, tctrq->shm); - } - - rpc_send_int_response(src, corr_id, ret); - break; - } - - case USB_DC_TRSF_BULK: - { - if (length < sizeof(struct usb_devctrl_tbk)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - - struct usb_devctrl_tbk* tbkrq = data; - ret = usb_shm_bulk_transfer(&tbkrq->pipe, tbkrq->shm, - tbkrq->length); - - // Senden des Togglebitstatus erwünscht - rpc_send_int_response(src, corr_id, ret); - break; - } - } -} - -void register_rpc_interface(void) -{ - register_message_handler(RPC_USB_SV_REGHC_FNN, &hcd_handler); - register_message_handler(RPC_USB_SV_REGDD_FNN, &ddrv_handler); - register_message_handler(RPC_USB_SV_DEVCT_FNN, &devctl_handler); - init_service_register("usb1"); -} diff --git a/src/modules/usb/usb1/transfer.c b/src/modules/usb/usb1/transfer.c deleted file mode 100644 index 8c96c4d..0000000 --- a/src/modules/usb/usb1/transfer.c +++ /dev/null @@ -1,123 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include - -#include -#include - -#include "usb.h" -#include "usb-ddrv.h" -#include "usb-hc.h" - -int usb_control_transfer(struct usb_pipe* pipe, int type, int request, - int value, int index, int length, void* buffer) -{ - uint32_t shm = 0; - void *shm_addr = NULL; - - if (length) { - shm = create_shared_memory(length); - shm_addr = open_shared_memory(shm); - - if ((buffer != NULL) && !(type & USB_SETUP_IN)) { - memcpy(shm_addr, buffer, length); - } - } - - int ret = usb_shm_control_transfer(pipe, type, request, value, index, - length, shm); - - if (length && (buffer != NULL) && (type & USB_SETUP_IN)) { - memcpy(buffer, shm_addr, length); - } - - if (shm_addr != NULL) { - close_shared_memory(shm); - } - - return ret; -} - -int usb_shm_control_transfer(struct usb_pipe* pipe, int type, int request, - int value, int index, int length, uint32_t shm) -{ - struct usb_hc_trs_ctrl trs = { - .trs = { - .type = USB_TRST_CONTROL, - .hc = *pipe->dev->hc, - .pipe = pipe->trs - }, - .setup = { - .type = type, - .request = request, - .value = value, - .index = index, - .length = length - }, - .shm = shm - }; - - return rpc_get_int(pipe->dev->hc->pid, RPC_USB_HC_TRANSFER_FNN, - sizeof(trs), (char*)&trs); -} - -int usb_shm_bulk_transfer(struct usb_pipe* pipe, uint32_t shm, size_t length) -{ - struct usb_hc_trs_bulk trs = { - .trs = { - .type = USB_TRST_BULK, - .hc = *pipe->dev->hc, - .pipe = pipe->trs - }, - .receive = pipe->ep->direction ? true : false, - .length = length, - .shm = shm - }; - - return rpc_get_int(pipe->dev->hc->pid, RPC_USB_HC_TRANSFER_FNN, - sizeof(trs), (char*)&trs); -} - -void usb_set_configuration(struct usb_device* dev, int config) -{ - usb_control_transfer(dev->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_SET_CONFIGURATION, config, 0, 0, NULL); - dev->state = USB_CONFIGURED; -} - -void usb_clear_halt(struct usb_pipe* pipe) -{ - usb_control_transfer(pipe->dev->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_STD | USB_SETUP_REC_EP, - USB_CLEAR_FEATURE, USB_ENDPOINT_HALT, pipe->ep->number, 0, NULL); -} diff --git a/src/modules/usb/usb1/usb.h b/src/modules/usb/usb1/usb.h deleted file mode 100644 index 6efc729..0000000 --- a/src/modules/usb/usb1/usb.h +++ /dev/null @@ -1,209 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_H -#define USB_H - -#include -#include -#include - -#include "usb-ddrv.h" -#include "usb-hc.h" -#include "usb-hub.h" -#include "usb-server.h" -#include "usb-structs.h" -#include "usb-trans.h" - -struct usb_pipe; - -// Beschreibt einen Hub (sowohl Roothub als auch einen normalen). -struct usb_hub { - // Da ein Hub immer nur entweder Root- oder normaler Hub sein kann, macht - // sich eine Union hier ganz gut. - union { - struct usb_device* usb; - struct usb_hc* hc; - } dev; - // true, wenn es sich um einen Roothub handelt. - bool root_hub; - // Anzahl der Ports am Hub. - int ports; - - /** - * Wird aufgerufen, wenn eine neue Pipe benötigt wird. Ist bereits eine - * Pipe zur angegebenen Geräte-/Endpunkt-Kombination vorhanden, so sollte - * diese entfernt werden. Der Pointer darf NULL sein, um anzuzeigen, dass - * die Funktion nicht benötigt wird. Normale Hubs müssen jedoch dafür - * sorgen, dass die vom Hostcontroller (Roothub) bereitgestellte Funktion - * aufgerufen wird (es genügt, einfach den Pointer vom übergeordneten Hub - * zu kopieren, der Roothub muss dies erkennen können). - * - * @param hub Der Hub, an dem das Gerät hängt. - * @param pipe Die neu hinzuzufügende Pipe. - */ - void (*add_pipe)(struct usb_hub* hub, struct usb_pipe* pipe); - - /** - * Deaktiviert einen Port (zum Aktivieren wird reset_port verwendet). - * - * @param hub Hub mit dem zu deaktivierenden Port. - * @param port Zu deaktivierender Port. - */ - void (*disable_port)(struct usb_hub* hub, int port); - - /** - * Treibt das Resetsignal auf einem Port und aktiviert ihn. Wenn nötig, - * muss außerdem das Resume-Signal getrieben werden, damit nach dem Aufruf - * dieser Funktion mit dem Gerät kommuniziert werden kann. - * - * @param hub Hub, an dem das Gerät hängt. - * @param port Port, an dem es hängt. - */ - void (*reset_port)(struct usb_hub* hub, int port); - - /** - * Überprüft einen Port und gibt dessen Status zurück. - * - * @param hub Zu untersuchender Hub. - * @param port Entsprechender Port. - * - * @return Kombination aus den in usb-hub.h mit USB_HUB beginnenden Flags. - */ - int (*scan_port)(struct usb_hub* hub, int port); -}; - -// Beschreibt ein USB-Gerät -struct usb_device { - // Hub, an dem es hängt - struct usb_hub* parent; - // Index des Ports, an dem es sich befindet - int hub_port; - - // Zuständiger Hostcontroller - struct usb_hc* hc; - // Dort eindeutige USB-Adresse - int addr; - - // Aktueller Status - enum usb_state state; - // Aktuelle Geschwindigkeit - int speed; - - // Default Control Pipe - struct usb_pipe* ep0; - // Device-Descriptor - struct usb_device_descriptor* dev_desc; - // Alle Configuration-Descriptors - struct usb_config_descriptor** cfg_desc; - // Aktuelle Konfiguration - struct usb_config_descriptor* cur_cfg; -}; - -// USB-Gerätetreiber -struct usb_ddrv { - // Prozess-ID - pid_t pid; - // Gibt an, welche Geräte er behandeln kann - struct usb_handle_devs handle; -}; - -// Bisher nicht behandeltes USB-Gerät (oder Interface) -struct usb_unhandled { - // Das Gerät - struct usb_device* dev; - // Das Interface - struct usb_interface_descriptor* ifc; -}; - - -/** - * Initialisiert die RPC-Handler und trägt sich als usb1 bei init ein- - */ -void register_rpc_interface(void); - -/** - * Enumeriert alle Geräte an einem Hub - * - * @param hub Der Hub - */ -void scan_hub(struct usb_hub* hub); - -/** - * Entstallt einen Endpunkt. - * - * @param pipe Zum Endpunkt gehörige Pipe - */ -void usb_clear_halt(struct usb_pipe* pipe); - -/** - * Führt einen Controltransfer durch. - * - * @param pipe Die Pipe - * @param type type-Feld des SETUP-Pakets - * @param request request-Feld des SETUP-Pakets - * @param value value-Feld des SETUP-Pakets - * @param index index-Feld des SETUP-Pakets - * @param length length-Feld des SETUP-Pakets - * @param buffer Speicherbereich, mit dem während der Datenphase gearbeitet - * werden soll. - * - * @return 0 bei Erfolg, sonst -errno. - */ -int usb_control_transfer(struct usb_pipe* pipe, int type, int request, - int value, int index, int length, void* buffer); - -/** - * Führt einen Bulktransfer durch. - * - * @param pipe Die Pipe für den Transfer - * @param shm SHM-Bereich, mit dem gearbeitet werden soll - * @param length Länge des Transfers - * - * @return -errno bei Fehler, sonst neuer Wert des Toggle-Bits. - */ -int usb_shm_bulk_transfer(struct usb_pipe* pipe, uint32_t shm, size_t length); - -/** - * Führt einen Controltransfer durch. Entspricht usb_control_transfer, außer - * dass ein SHM-Bereich verwendet wird. - */ -int usb_shm_control_transfer(struct usb_pipe* pipe, int type, int request, - int value, int index, int length, uint32_t shm); - -/** - * Setzt die aktuelle Konfiguration eines Geräts. - * - * @param dev USB-Gerät - * @param config ID der neuen Konfiguration - */ -void usb_set_configuration(struct usb_device* dev, int config); - -#endif -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:09 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:09 +0100 Subject: [tyndur-devel] [PATCH 19/22] ehci: Add EHCI driver In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-20-git-send-email-max@tyndur.org> + This adds an EHCI USB host controller driver. It is missing support for periodic schedule (interrupt and isochronous transfers), hotplugging, suspend/resume, etc. pp.. Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/ehci/ehci.c | 640 ++++++++++++++++++++++++++++++++++++++++++++ src/modules/cdi/ehci/ehci.h | 292 ++++++++++++++++++++ src/modules/cdi/ehci/main.c | 69 +++++ 3 files changed, 1001 insertions(+) create mode 100644 src/modules/cdi/ehci/ehci.c create mode 100644 src/modules/cdi/ehci/ehci.h create mode 100644 src/modules/cdi/ehci/main.c diff --git a/src/modules/cdi/ehci/ehci.c b/src/modules/cdi/ehci/ehci.c new file mode 100644 index 0000000..7f3838c --- /dev/null +++ b/src/modules/cdi/ehci/ehci.c @@ -0,0 +1,640 @@ +/****************************************************************************** + * Copyright (c) 2015 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 +#include + +#include "ehci.h" + + +#define EHCI_TIMEOUT 1000 // ms + + +static void bios_handoff(struct cdi_pci_device *pci_dev, struct ehci *hc); +static void irq_handler(struct cdi_device *dev); + + +struct cdi_device *ehci_init_device(struct cdi_bus_data *bus_data) +{ + struct cdi_pci_device *pci_dev = (struct cdi_pci_device *)bus_data; + static int dev_counter = 0; + + if (pci_dev->class_id != PCI_CLASS_SERIAL_BUS_CONTROLLER || + pci_dev->subclass_id != PCI_SUBCLASS_USB_HOST_CONTROLLER || + pci_dev->interface_id != PCI_INTERFACE_EHCI) + { + return NULL; + } + + struct ehci *hc = calloc(1, sizeof(*hc)); + + hc->hc.dev.name = malloc(8); + sprintf((char *)hc->hc.dev.name, "ehci%i", dev_counter++); + + hc->retired_qhs = cdi_list_create(); + + cdi_pci_alloc_memory(pci_dev); + + struct cdi_pci_resource *res; + for (int i = 0; (res = cdi_list_get(pci_dev->resources, i)); i++) { + if (res->type == CDI_PCI_MEMORY && !res->index) { + hc->caps = res->address; + hc->regs = (struct ehci_registers *)((uintptr_t)res->address + + hc->caps->caplength); + break; + } + } + if (!hc->regs) { + goto fail; + } + + bios_handoff(pci_dev, hc); + + // Stop operation + hc->regs->usbcmd = (hc->regs->usbcmd & EHCI_CMD_RSVD) | EHCI_CMD_ITC(8); + + // Wait until HCHALTED becomes true + CDI_CONDITION_WAIT(hc->regs->usbsts & EHCI_STS_HCH, 50); + if (!(hc->regs->usbsts & EHCI_STS_HCH)) { + goto fail; + } + + hc->regs->usbcmd |= EHCI_CMD_HCRES; // Reset + CDI_CONDITION_WAIT(!(hc->regs->usbcmd & EHCI_CMD_HCRES), 50); + if (hc->regs->usbcmd & EHCI_CMD_HCRES) { + goto fail; + } + + if (hc->caps->hccparams & EHCI_HCC_64) { + // 64-bit addressing capability + hc->regs->ctrldssegment = 0; + } + + hc->regs->usbintr = 0; + hc->regs->usbsts = hc->regs->usbsts; + + hc->periodic_list_mem = cdi_mem_alloc(4096, 12 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + hc->periodic_list = hc->periodic_list_mem->vaddr; + for (int i = 0; i < 1024; i++) { + hc->periodic_list[i] = EHCI_T; + } + hc->regs->periodiclistbase = hc->periodic_list_mem->paddr.items[0].start; + + // Create anchor + struct cdi_mem_area *anchor_qh; + anchor_qh = cdi_mem_alloc(sizeof(ehci_fat_qh_t), 6 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + + hc->async_start = anchor_qh->vaddr; + memset(hc->async_start, 0, sizeof(*hc->async_start)); + + hc->async_start->cdi_mem_area = anchor_qh; + hc->async_start->paddr = anchor_qh->paddr.items[0].start + + offsetof(ehci_fat_qh_t, qh); + + hc->async_start->next = hc->async_start; + hc->async_start->previous = hc->async_start; + + hc->async_start->qh.next_qh = hc->async_start->paddr | EHCI_QH; + hc->async_start->qh.ep_state[0] = EHCI_QHES0_RL(0) | EHCI_QHES0_MPL(8) + | EHCI_QHES0_EP(0) | EHCI_QHES0_DEV(0) + | EHCI_QHES0_H | EHCI_QHES0_DTC + | EHCI_QHES0_EPS_HS; + hc->async_start->qh.ep_state[1] = EHCI_QHES1_MULT(1) | EHCI_QHES1_SCM(0xff); + + hc->async_start->qh.overlay.next_qtd = EHCI_T; + hc->async_start->qh.overlay.alt_next_qtd = EHCI_T; + + hc->regs->asynclistaddr = hc->async_start->paddr; + + // Start operation + hc->regs->usbcmd = (hc->regs->usbcmd & EHCI_CMD_RSVD) + | EHCI_CMD_ITC(8) | EHCI_CMD_ASE | EHCI_CMD_PSE + | EHCI_CMD_FLS_1K | EHCI_CMD_RS; + + // Wait until HCHalted becomes false and the schedules are running + CDI_CONDITION_WAIT((hc->regs->usbsts + & (EHCI_STS_ASS | EHCI_STS_PSS | EHCI_STS_HCH)) + == (EHCI_STS_ASS | EHCI_STS_PSS), 50); + if ((hc->regs->usbsts & (EHCI_STS_ASS | EHCI_STS_PSS | EHCI_STS_HCH)) + != (EHCI_STS_ASS | EHCI_STS_PSS)) + { + goto fail; + } + + cdi_register_irq(pci_dev->irq, &irq_handler, &hc->hc.dev); + + // async advance doorbell + hc->regs->usbintr = EHCI_INT_IAA; + + // Route everything to this HC + hc->regs->configflag |= EHCI_CF_CF; + + cdi_sleep_ms(5); + + hc->hc.rh.ports = EHCI_HCS_N_PORTS(hc->caps->hcsparams); + + // Power up all ports + for (int i = 0; i < hc->hc.rh.ports; i++) { + hc->regs->portsc[i] |= EHCI_PSC_PP; + } + + return &hc->hc.dev; + +fail: + if (hc->async_start) { + cdi_mem_free(hc->async_start->cdi_mem_area); + } + if (hc->periodic_list_mem) { + cdi_mem_free(hc->periodic_list_mem); + } + cdi_list_destroy(hc->retired_qhs); + free((void *)hc->hc.dev.name); + free(hc); + cdi_pci_free_memory(pci_dev); + + return NULL; +} + + +static void bios_handoff(struct cdi_pci_device *pci_dev, struct ehci *hc) +{ + int eecp = EHCI_HCC_EECP(hc->caps->hccparams); + + // Iterate through the list of capabilities + while (eecp >= 0x40) { + uint32_t cap = cdi_pci_config_readl(pci_dev, eecp); + + // Look for USBLEGSUP + if (EHCI_EEC_ID(cap) == EHCI_EECID_LEGSUP) { + // Set OS semaphore + cdi_pci_config_writeb(pci_dev, eecp + 3, 1); + + // Wait for BIOS semaphore to get unset + CDI_CONDITION_WAIT(!(cdi_pci_config_readb(pci_dev, eecp + 2) & 1), + 5000); + if (cdi_pci_config_readb(pci_dev, eecp + 2) & 1) { + // Okay Max, stay calm, don't swear + printf("[ehci] Warning: Clean BIOS handoff failed\n"); + // Disable all SMI sources (0xff000000 is the R/WC mask of this + // register) + cdi_pci_config_writel(pci_dev, eecp + 4, + cdi_pci_config_readl(pci_dev, eecp + 4) & 0xff000000u); + // Unset BIOS semaphore + cdi_pci_config_writeb(pci_dev, eecp + 2, 0); + } + } + + eecp = EHCI_EEC_NEXT(cap); + } +} + + +static void irq_handler(struct cdi_device *dev) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(dev, struct cdi_usb_hc, dev); + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + + uint32_t usbsts = hc->regs->usbsts; + hc->regs->usbsts = usbsts; + + // async advance doorbell + if (!(usbsts & EHCI_STS_IAA)) { + return; + } + + ehci_fat_qh_t *rqh; + while ((rqh = cdi_list_pop(hc->retired_qhs))) { + cdi_mem_free(rqh->cdi_mem_area); + } +} + + +void ehci_rh_port_down(struct cdi_usb_hub *hub, int index) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + + hc->regs->portsc[index] &= ~EHCI_PSC_PED; +} + + +void ehci_rh_port_up(struct cdi_usb_hub *hub, int index) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + uint32_t portsc = hc->regs->portsc[index]; + + if ((portsc & EHCI_PSC_LS_MASK) == EHCI_PSC_LS_K) { + // Low-speed device, release ownership + hc->regs->portsc[index] = portsc | EHCI_PSC_PO; + return; + } + + // Assert reset signal + hc->regs->portsc[index] = ((portsc & ~(EHCI_PSC_PO | EHCI_PSC_SUS | + EHCI_PSC_FPR | EHCI_PSC_PED)) + | EHCI_PSC_RES); + cdi_sleep_ms(50); + + // De-assert reset signal + hc->regs->portsc[index] = portsc & ~EHCI_PSC_RES; + CDI_CONDITION_WAIT(!(hc->regs->portsc[index] & EHCI_PSC_RES), 50); + + portsc = hc->regs->portsc[index]; + if (portsc & EHCI_PSC_RES) { + // Disable port, route on to companion controller + // (maybe it can do at least something with it) + hc->regs->portsc[index] = (portsc & ~EHCI_PSC_PED) | EHCI_PSC_PO; + } else if ((portsc & (EHCI_PSC_PED | EHCI_PSC_CCS)) == EHCI_PSC_CCS) { + // Device connected but port disabled: + // Full-speed device, release ownership + hc->regs->portsc[index] = portsc | EHCI_PSC_PO; + } +} + + +cdi_usb_port_status_t ehci_rh_port_status(struct cdi_usb_hub *hub, int index) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + uint32_t portsc = hc->regs->portsc[index]; + cdi_usb_port_status_t status = 0; + + if ((portsc & (EHCI_PSC_PED | EHCI_PSC_CCS)) == + (EHCI_PSC_PED | EHCI_PSC_CCS)) + { + // Device connected and port enabled + status |= CDI_USB_PORT_CONNECTED | CDI_USB_HIGH_SPEED; + } + + return status; +} + + +static void enter_async_qh(struct ehci *hc, ehci_fat_qh_t *qh) +{ + // Enter the new QH as last + + qh->next = hc->async_start; + qh->previous = hc->async_start->previous; + + qh->previous->next = qh; + hc->async_start->previous = qh; + + qh->qh.next_qh = hc->async_start->paddr | EHCI_QH; + qh->previous->qh.next_qh = qh->paddr | EHCI_QH; +} + + +static void retire_qh(struct ehci *hc, ehci_fat_qh_t *qh) +{ + qh->previous->qh.next_qh = qh->qh.next_qh; + + qh->previous->next = qh->next; + qh->next->previous = qh->previous; + + cdi_list_push(hc->retired_qhs, qh); + // Request async advance doorbell + hc->regs->usbcmd |= EHCI_CMD_IAADB; +} + + +static void retire_first_qtd(ehci_fat_qh_t *qh) +{ + ehci_fat_qtd_t *qtd = qh->first_qtd; + + assert(qtd); + + uint32_t err_mask = (qh->qh.ep_state[0] & EHCI_QHES0_EPS_MASK) + == EHCI_QHES0_EPS_HS + ? EHCI_QTDS_HS_ERR_MASK + : EHCI_QTDS_LFS_ERR_MASK; + + if (qtd->qtd.status & err_mask) { + if (qtd->qtd.status & EHCI_QTDS_BABBLE) { + *qtd->result = CDI_USB_BABBLE; + } else if (qtd->qtd.status & (EHCI_QTDS_XACTERR | EHCI_QTDS_P_ERR)) { + *qtd->result = CDI_USB_BAD_RESPONSE; + } else if (qtd->qtd.status & (EHCI_QTDS_BUFERR | EHCI_QTDS_MuF)) { + *qtd->result = CDI_USB_HC_ERROR; + } else if (qtd->qtd.status & EHCI_QTDS_HALTED) { + *qtd->result = CDI_USB_STALL; + } else { + *qtd->result = CDI_USB_HC_ERROR; + } + } + + if (qtd->write_back) { + size_t rem = qtd->write_back_size; + for (int i = 0; i < 5 && rem; i++) { + size_t sz = rem > 4096 ? 4096 : rem; + memcpy((void *)((uintptr_t)qtd->write_back + i * 4096), + qtd->buffers[i]->vaddr, sz); + rem -= sz; + } + assert(!rem); + } + + qh->first_qtd = qtd->next; + if (qh->last_qtd == qtd) { + qh->last_qtd = NULL; + } + + for (int i = 0; i < 5; i++) { + if (qtd->buffers[i]) { + cdi_mem_free(qtd->buffers[i]); + } + } + + cdi_mem_free(qtd->cdi_mem_area); +} + + +/** + * Enters a qTD into a QH that has not yet been entered into the async schedule. + */ +static void enter_qtd(ehci_fat_qh_t *qh, ehci_fat_qtd_t *qtd) +{ + bool first; + + qtd->qh = qh; + qtd->qtd.next_qtd = EHCI_T; + qtd->qtd.alt_next_qtd = EHCI_T; + + first = !qh->first_qtd; + + if (first) { + qh->first_qtd = qtd; + } else { + qh->last_qtd->next = qtd; + } + qh->last_qtd = qtd; + + if (first) { + qh->qh.overlay.next_qtd = qh->first_qtd->paddr; + } else { + qh->last_qtd->qtd.next_qtd = qtd->paddr; + } +} + + +cdi_usb_hc_transaction_t ehci_create_transaction(struct cdi_usb_hc *cdi_hc, + const struct cdi_usb_hc_ep_info *target) +{ + (void)cdi_hc; + + struct cdi_mem_area *qh_mem; + + qh_mem = cdi_mem_alloc(sizeof(ehci_fat_qh_t), 6 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + ehci_fat_qh_t *qh = qh_mem->vaddr; + + memset(qh, 0, sizeof(*qh)); + + int non_high_speed_control = target->speed != CDI_USB_HIGH_SPEED + && target->ep_type == CDI_USB_CONTROL; + + qh->qh.ep_state[0] = EHCI_QHES0_RL(0) + | (non_high_speed_control ? EHCI_QHES0_C : 0) + | EHCI_QHES0_MPL(target->mps) + | EHCI_QHES0_DTC + | EHCI_QHES0_EP(target->ep) + | EHCI_QHES0_DEV(target->dev); + switch (target->speed) { + case CDI_USB_LOW_SPEED: qh->qh.ep_state[0] |= EHCI_QHES0_EPS_LS; break; + case CDI_USB_FULL_SPEED: qh->qh.ep_state[0] |= EHCI_QHES0_EPS_FS; break; + case CDI_USB_HIGH_SPEED: qh->qh.ep_state[0] |= EHCI_QHES0_EPS_HS; break; + case CDI_USB_SUPERSPEED: + default: + abort(); + } + + qh->qh.ep_state[1] = EHCI_QHES1_MULT(1) + | EHCI_QHES1_PORT(target->tt_port + 1) + | EHCI_QHES1_HUB(target->tt_addr) + | EHCI_QHES1_SCM(0xff); + + qh->qh.overlay.next_qtd = EHCI_T; + qh->qh.overlay.alt_next_qtd = EHCI_T; + + qh->cdi_mem_area = qh_mem; + qh->paddr = qh_mem->paddr.items[0].start + offsetof(ehci_fat_qh_t, qh); + + return qh; +} + + +static void create_qtd(ehci_fat_qh_t *qh, cdi_usb_transfer_token_t token, + int toggle, void *buffer, size_t size, + cdi_usb_transmission_result_t *result) +{ + struct cdi_mem_area *qtd_mem; + + qtd_mem = cdi_mem_alloc(sizeof(ehci_fat_qtd_t), 6 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + ehci_fat_qtd_t *qtd = qtd_mem->vaddr; + + memset(qtd, 0, sizeof(*qtd)); + + qtd->qtd.status = EHCI_QTDS_DT(toggle) + | EHCI_QTDS_XFLEN(size) + | EHCI_QTDS_CP(0) + | EHCI_QTDS_CERR(3) + | EHCI_QTDS_ACTIVE; + + switch (token) { + case CDI_USB_IN: qtd->qtd.status |= EHCI_QTDS_PID_IN; break; + case CDI_USB_OUT: qtd->qtd.status |= EHCI_QTDS_PID_OUT; break; + case CDI_USB_SETUP: qtd->qtd.status |= EHCI_QTDS_PID_SETUP; break; + default: + abort(); + } + + if (token == CDI_USB_IN) { + qtd->write_back = buffer; + qtd->write_back_size = size; + } + + for (int i = 0; i < 5 && size; i++) { + size_t sz = size > 4096 ? 4096 : size; + + qtd->buffers[i] = cdi_mem_alloc(4096, 12 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + qtd->qtd.buffers[i] = qtd->buffers[i]->paddr.items[0].start; + + if (token == CDI_USB_OUT || token == CDI_USB_SETUP) { + memcpy(qtd->buffers[i]->vaddr, + (void *)((uintptr_t)buffer + i * 4096), sz); + } + + size -= sz; + } + + qtd->cdi_mem_area = qtd_mem; + qtd->paddr = qtd_mem->paddr.items[0].start + offsetof(ehci_fat_qtd_t, qtd); + + qtd->result = result; + + enter_qtd(qh, qtd); +} + + +void ehci_enqueue(struct cdi_usb_hc *cdi_hc, + const struct cdi_usb_hc_transmission *trans) +{ + (void)cdi_hc; + + ehci_fat_qh_t *qh = trans->ta; + bool first = true; + size_t sz = trans->size; + char *buf = trans->buffer; + int toggle = trans->toggle; + size_t mps = EHCI_QHES0_READ_MPL(qh->qh.ep_state[0]); + + *trans->result = CDI_USB_OK; + + while (sz || first) { + first = false; + + size_t iter_sz = sz > 4096 * 5 ? 4096 * 5 : sz; + + create_qtd(qh, trans->token, toggle, buf, iter_sz, trans->result); + + buf += iter_sz; + sz -= iter_sz; + + toggle ^= ((iter_sz + mps - 1) / mps) & 1; + } +} + + +void ehci_start_transaction(struct cdi_usb_hc *cdi_hc, + cdi_usb_hc_transaction_t ta) +{ + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + ehci_fat_qh_t *qh = ta; + + enter_async_qh(hc, qh); +} + + +void ehci_wait_transaction(struct cdi_usb_hc *cdi_hc, + cdi_usb_hc_transaction_t ta) +{ + (void)cdi_hc; + + ehci_fat_qh_t *qh = ta; + bool had_timeout = false; + + irq_handler(&cdi_hc->dev); + + while (qh->first_qtd) { + if (!had_timeout) { + if (!(qh->qh.overlay.status & EHCI_QTDS_ACTIVE)) { + qh->qh.overlay.next_qtd = qh->first_qtd->paddr; + } + + uint64_t timestamp = cdi_elapsed_ms(); + + while ((qh->first_qtd->qtd.status & EHCI_QTDS_ACTIVE) && + cdi_elapsed_ms() - timestamp < EHCI_TIMEOUT) + { + // I don't really know who's to blame for this (qemu or + // týndur), and actually, I don't even want to know. + // As a matter of fact, qemu will not work on the async + // schedule if the async advance doorbell is set but the driver + // has not acknowledged this; however, the interrupt handler + // will always clear that bit and the interrupt handler is + // active all the time, so something is apparently sometimes + // blocking the handler from being executed. Force it here to + // prevent qemu's HC from stalling. + irq_handler(&cdi_hc->dev); + } + + had_timeout = qh->first_qtd->qtd.status & EHCI_QTDS_ACTIVE; + } + + if (had_timeout) { + // Stop this queue + qh->qh.overlay.next_qtd = EHCI_T; + qh->qh.overlay.status &= ~(uint32_t)EHCI_QTDS_ACTIVE; + + *qh->first_qtd->result = CDI_USB_TIMEOUT; + } + + retire_first_qtd(qh); + } +} + + +void ehci_destroy_transaction(struct cdi_usb_hc *cdi_hc, + cdi_usb_hc_transaction_t ta) +{ + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + ehci_fat_qh_t *qh = ta; + + qh->qh.overlay.next_qtd = EHCI_T; + + uint64_t timestamp = cdi_elapsed_ms(); + + while ((qh->qh.overlay.status & EHCI_QTDS_ACTIVE) && + cdi_elapsed_ms() - timestamp < EHCI_TIMEOUT) + { + // See above. + irq_handler(&cdi_hc->dev); + } + + if (qh->qh.overlay.status & EHCI_QTDS_ACTIVE) { + qh->qh.overlay.status &= ~(uint32_t)EHCI_QTDS_ACTIVE; + // Could probably be solved more elegantly, but we are in a timeout + // already, so waiting a bit longer will not be that big of a problem + // (the sleep is here to ensure that the HC is no longer trying to work + // on the qTDs belonging to this QH) + cdi_sleep_ms(100); + } + + while (qh->first_qtd) { + retire_first_qtd(qh); + } + retire_qh(hc, qh); +} diff --git a/src/modules/cdi/ehci/ehci.h b/src/modules/cdi/ehci/ehci.h new file mode 100644 index 0000000..d37f141 --- /dev/null +++ b/src/modules/cdi/ehci/ehci.h @@ -0,0 +1,292 @@ +/****************************************************************************** + * Copyright (c) 2015 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 EHCI_H +#define EHCI_H + +#include +#include + +#include +#include +#include +#include + + +struct ehci_registers { + uint32_t usbcmd; + uint32_t usbsts; + uint32_t usbintr; + uint32_t frindex; + uint32_t ctrldssegment; + uint32_t periodiclistbase; + uint32_t asynclistaddr; + uint8_t reserved[0x24]; + uint32_t configflag; + uint32_t portsc[]; +} __attribute__((packed)); + +struct ehci_capabilities { + uint8_t caplength; + uint8_t reserved; + uint16_t hciversion; + uint32_t hcsparams; + uint32_t hccparams; + uint64_t hcsp_portroute; +} __attribute__((packed)); + +struct ehci_qtd { + uint32_t next_qtd; + uint32_t alt_next_qtd; + uint32_t status; + uint32_t buffers[5]; + + // 64-bit structures + uint32_t ext_buffer[5]; +} __attribute__((packed)); + +struct ehci_qh { + uint32_t next_qh; + uint32_t ep_state[2]; + uint32_t current_qtd; + struct ehci_qtd overlay; +} __attribute__((packed)); + +typedef struct ehci_fat_qh ehci_fat_qh_t; +typedef struct ehci_fat_qtd ehci_fat_qtd_t; + +#define DIFF_TO_MULTIPLE(value, divisor) \ + ((divisor) - ((((value) + (divisor) - 1) % (divisor)) + 1)) + +struct ehci_fat_qh { + volatile struct ehci_qh qh; + + // Align the QH to 128 bytes (something with cache lines?). Otherwise, + // there may be conflicts between data we are writing to our very own + // fields provided by ehci_fat_qh_t and writes done by the HC to this + // structure. + uint8_t alignment[DIFF_TO_MULTIPLE(sizeof(struct ehci_qh), 128)]; + + struct cdi_mem_area *cdi_mem_area; + uintptr_t paddr; + + ehci_fat_qh_t *next, *previous; + ehci_fat_qtd_t *first_qtd, *last_qtd; +}; + +struct ehci_fat_qtd { + volatile struct ehci_qtd qtd; + + // Align the qTD to a multiple of 128 bytes. + uint8_t alignment[DIFF_TO_MULTIPLE(sizeof(struct ehci_qtd), 128)]; + + struct cdi_mem_area *cdi_mem_area; + uintptr_t paddr; + + ehci_fat_qh_t *qh; + ehci_fat_qtd_t *next; + + struct cdi_mem_area *buffers[5]; + + void *write_back; + size_t write_back_size; + + cdi_usb_transmission_result_t *result; +}; + +struct ehci { + struct cdi_usb_hc hc; + + volatile struct ehci_capabilities *caps; + volatile struct ehci_registers *regs; + + struct cdi_mem_area *periodic_list_mem; + volatile uint32_t *periodic_list; + + ehci_fat_qh_t *async_start; + cdi_list_t retired_qhs; +}; + +#define PCI_CLASS_SERIAL_BUS_CONTROLLER 0x0c +#define PCI_SUBCLASS_USB_HOST_CONTROLLER 0x03 +#define PCI_INTERFACE_EHCI 0x20 + +#define EHCI_T (1u << 0) +#define EHCI_ITD (0u << 1) +#define EHCI_QH (1u << 1) +#define EHCI_SITD (2u << 1) +#define EHCI_FSTN (3u << 1) + +// Operational Registers: USBCMD +#define EHCI_CMD_RSVD 0xff00ff00u // Consider async sched park mode reserved +#define EHCI_CMD_ITC(x) ((x) << 16) // Interrupt Threshold Control +#define EHCI_CMD_LHCRES (1 << 7) // Light Host Controller Reset +#define EHCI_CMD_IAADB (1 << 6) // Interrupt on Async Advance Doorbell +#define EHCI_CMD_ASE (1 << 5) // Async Schedule Enable +#define EHCI_CMD_PSE (1 << 4) // Periodic Schedule Enable +#define EHCI_CMD_FLS_1K (0 << 2) // Frame List Size: 1024 elements (default) +#define EHCI_CMD_HCRES (1 << 1) // Host Controller Reset +#define EHCI_CMD_RS (1 << 0) // Run/Stop (1 = Run) + +// Operational Registers: USBINTR +#define EHCI_INT_IAA (1 << 5) // Interrupt on Async Advance Enable +#define EHCI_INT_HSE (1 << 4) // Host System Error Enable +#define EHCI_INT_FLR (1 << 3) // Frame List Rollover Enable +#define EHCI_INT_PCD (1 << 2) // Port Change Detect Enable +#define EHCI_INT_USBERR (1 << 1) // USB Error Interrupt +#define EHCI_INT_USBINT (1 << 0) // USB Interrupt + +// Operational Registers: USBSTS +#define EHCI_STS_ASS (1 << 15) // Async Schedule Status +#define EHCI_STS_PSS (1 << 14) // Periodic Schedule Status +#define EHCI_STS_REC (1 << 13) // Reclamation bit +#define EHCI_STS_HCH (1 << 12) // Host Controller Halted +#define EHCI_STS_IAA EHCI_INT_IAA // Interrupt on Async Advance +#define EHCI_STS_HSE EHCI_INT_HSE // Host System Error +#define EHCI_STS_FLR EHCI_INT_FLR // Frame List Rollover +#define EHCI_STS_PCD EHCI_INT_PCD // Port Change Detect +#define EHCI_STS_USBERR EHCI_INT_USBERR // USB Error Interrupt +#define EHCI_STS_USBINT EHCI_INT_USBINT // USB Interrupt + +// Operational Registers: FRINDEX +#define EHCI_FI_FI(x) (((x) >> 3) & 0x3ff) // Frame Index + +// Operational Registers: CONFIGFLAG +#define EHCI_CF_CF (1 << 0) // Configure Flag + +// Operational Registers: PORTSC +#define EHCI_PSC_WKOC_E (1 << 22) // Wake on Over-current Enable +#define EHCI_PSC_WKDSCNNT_E (1 << 21) // Wake on Disconnect Enable +#define EHCI_PSC_WKCNNT_E (1 << 20) // Wake on Connect Enable +#define EHCI_PSC_PTC_DIS (0 << 16) // Port Test Control: disabled +#define EHCI_PSC_PTC_J (1 << 16) // Port Test Control: J-State +#define EHCI_PSC_PTC_K (2 << 16) // Port Test Control: K-State +#define EHCI_PSC_PTC_SE0 (3 << 16) // Port Test Control: SE0 Nak +#define EHCI_PSC_PTC_PKT (4 << 16) // Port Test Control: Packet +#define EHCI_PSC_PTC_FEN (5 << 16) // Port Test Control: Force Enable +#define EHCI_PSC_PI_OFF (0 << 14) // Port Indicators: off +#define EHCI_PSC_PI_AMBER (1 << 14) // Port Indicators: amber +#define EHCI_PSC_PI_GREEN (2 << 14) // Port Indicators: green +#define EHCI_PSC_PO (1 << 13) // Port Owner +#define EHCI_PSC_PP (1 << 12) // Port Power +#define EHCI_PSC_LS_MASK (3 << 10) // Line Status mask +#define EHCI_PSC_LS_SE0 (0 << 10) // Line Status: SE0 +#define EHCI_PSC_LS_K (1 << 10) // Line Status: K-State +#define EHCI_PSC_LS_J (2 << 10) // Line Status: J-State +#define EHCI_PSC_RES (1 << 8) // Port Reset +#define EHCI_PSC_SUS (1 << 7) // Suspend +#define EHCI_PSC_FPR (1 << 6) // Force Port Resume +#define EHCI_PSC_OCC (1 << 5) // Over-current Change +#define EHCI_PSC_OCA (1 << 4) // Over-current Active +#define EHCI_PSC_PEDC (1 << 3) // Port Enable/Disable Change +#define EHCI_PSC_PED (1 << 2) // Port Enabled/Disabled +#define EHCI_PSC_CSC (1 << 1) // Connect Status Change +#define EHCI_PSC_CCS (1 << 0) // Current Connect Status + +// Capability Registers: HCSPARAMS +#define EHCI_HCS_N_CC(x) (((x) >> 12) & 0xf) // Number of Companion + // Controllers +#define EHCI_HCS_N_PCC(x) (((x) >> 8) & 0xf) // Number of Ports per CC +#define EHCI_HCS_PRR (1 << 7) // Port Routing Rules +#define EHCI_HCS_PPC (1 << 4) // Port Power Control +#define EHCI_HCS_N_PORTS(x) (((x) >> 0) & 0xf) // Number of Ports + +// Capability Registers: HCCPARAMS +#define EHCI_HCC_EECP(x) (((x) >> 8) & 0xff) // EHCI Extended Capabilities + // Pointer +#define EHCI_HCC_64 (1 << 0) // 64-bit Addressing Capability + +// EHCI Extended Capability +#define EHCI_EEC_NEXT(x) (((x) >> 8) & 0xff) +#define EHCI_EEC_ID(x) (((x) >> 0) & 0xff) + +#define EHCI_EECID_LEGSUP 0x01 + +// QH.ep_state[0] (Endpoint Characteristics) +#define EHCI_QHES0_RL(x) ((uint32_t)(x) << 28) // Nak Count Reload +#define EHCI_QHES0_C (1 << 27) // Control Endpoint Flag +#define EHCI_QHES0_MPL(x) ((x) << 16) // Maximum Packet Length +#define EHCI_QHES0_READ_MPL(x) (((x) >> 16) & 0x7ff) +#define EHCI_QHES0_H (1 << 15) // Head of Reclamation List flag +#define EHCI_QHES0_DTC (1 << 14) // Data Toggle Control +#define EHCI_QHES0_EPS_MASK (3 << 12) // Endpoint Speed mask +#define EHCI_QHES0_EPS_FS (0 << 12) // Endpoint Speed: Full Speed +#define EHCI_QHES0_EPS_LS (1 << 12) // Endpoint Speed: Low Speed +#define EHCI_QHES0_EPS_HS (2 << 12) // Endpoint Speed: High Speed +#define EHCI_QHES0_EP(x) ((x) << 8) // Endpoint Number +#define EHCI_QHES0_I (1 << 7) // Inactive on Next Transaction +#define EHCI_QHES0_DEV(x) ((x) << 0) // Device Address + +// QH.ep_state[1] (Endpoint Capabilities) +#define EHCI_QHES1_MULT(x) ((uint32_t)(x) << 30) // High-Bandwidth Pipe + // Multiplier +#define EHCI_QHES1_PORT(x) ((x) << 23) // Port Number +#define EHCI_QHES1_HUB(x) ((x) << 16) // Hub Address +#define EHCI_QHES1_SCM(x) ((x) << 8) // Split Completion Mask +#define EHCI_QHES1_ISM(x) ((x) << 0) // Interrupt Schedule Mask + +// qTD.status (Token) +#define EHCI_QTDS_DT(x) ((uint32_t)(x) << 31) // Data Toggle +#define EHCI_QTDS_XFLEN(x) ((x) << 16) // Total Bytes to Transfer +#define EHCI_QTDS_IOC (1 << 15) // Interrupt on Complete +#define EHCI_QTDS_CP(x) ((x) << 12) // Current Page +#define EHCI_QTDS_CERR(x) ((x) << 10) // Error Counter +#define EHCI_QTDS_PID_OUT (0 << 8) // PID: OUT +#define EHCI_QTDS_PID_IN (1 << 8) // PID: IN +#define EHCI_QTDS_PID_SETUP (2 << 8) // PID: SETUP +#define EHCI_QTDS_ACTIVE (1 << 7) // Status: Active +#define EHCI_QTDS_HALTED (1 << 6) // Status: Halted +#define EHCI_QTDS_BUFERR (1 << 5) // Status: Data Buffer Error +#define EHCI_QTDS_BABBLE (1 << 4) // Status: Babble Detected +#define EHCI_QTDS_XACTERR (1 << 3) // Status: Transaction Error +#define EHCI_QTDS_MuF (1 << 2) // Status: Missed Microframe +#define EHCI_QTDS_SPLITXS (1 << 1) // Status: Split Transaction State +#define EHCI_QTDS_P_ERR (1 << 0) // Status: Ping State / Error + +// High Speed Error Mask +#define EHCI_QTDS_HS_ERR_MASK \ + (EHCI_QTDS_HALTED | EHCI_QTDS_BUFERR | EHCI_QTDS_BABBLE | \ + EHCI_QTDS_XACTERR | EHCI_QTDS_MuF) + +// Low/Full Speed Error Mask +#define EHCI_QTDS_LFS_ERR_MASK \ + (EHCI_QTDS_HS_ERR_MASK | EHCI_QTDS_P_ERR) + + +struct cdi_device *ehci_init_device(struct cdi_bus_data *bus_data); + +void ehci_rh_port_down(struct cdi_usb_hub *hub, int index); +void ehci_rh_port_up(struct cdi_usb_hub *hub, int index); +cdi_usb_port_status_t ehci_rh_port_status(struct cdi_usb_hub *hub, int index); + +cdi_usb_hc_transaction_t + ehci_create_transaction(struct cdi_usb_hc *hc, + const struct cdi_usb_hc_ep_info *target); +void ehci_enqueue(struct cdi_usb_hc *hc, + const struct cdi_usb_hc_transmission *trans); +void ehci_start_transaction(struct cdi_usb_hc *cdi_hc, + cdi_usb_hc_transaction_t ta); +void ehci_wait_transaction(struct cdi_usb_hc *hc, cdi_usb_hc_transaction_t ta); +void ehci_destroy_transaction(struct cdi_usb_hc *hc, + cdi_usb_hc_transaction_t ta); + +#endif diff --git a/src/modules/cdi/ehci/main.c b/src/modules/cdi/ehci/main.c new file mode 100644 index 0000000..5623819 --- /dev/null +++ b/src/modules/cdi/ehci/main.c @@ -0,0 +1,69 @@ +/****************************************************************************** + * Copyright (c) 2015 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 "ehci.h" + + +#define DRIVER_NAME "ehci" + + +static struct cdi_usb_hcd ehcd; + +static int ehcd_init(void) +{ + cdi_driver_init(&ehcd.drv); + return 0; +} + +static int ehcd_destroy(void) +{ + cdi_driver_destroy(&ehcd.drv); + return 0; +} + +static struct cdi_usb_hcd ehcd = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_USB_HCD, + .bus = CDI_PCI, + .init = ehcd_init, + .destroy = ehcd_destroy, + .init_device = ehci_init_device, + }, + + .rh_drv = { + .port_disable = ehci_rh_port_down, + .port_reset_enable = ehci_rh_port_up, + .port_status = ehci_rh_port_status, + }, + + .create_transaction = ehci_create_transaction, + .enqueue = ehci_enqueue, + .start_transaction = ehci_start_transaction, + .wait_transaction = ehci_wait_transaction, + .destroy_transaction = ehci_destroy_transaction, +}; + +CDI_DRIVER(DRIVER_NAME, ehcd) -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:10 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:10 +0100 Subject: [tyndur-devel] [PATCH 20/22] usb: Add USB 2.0 bus driver In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-21-git-send-email-max@tyndur.org> + This adds a USB 2.0 bus driver. It is missing support for USB 3.x (obviously), and also interrupt and isochronous transfers (and other things, too). Also, it is probably bad (e.g. there is only a single counter for device IDs instead of keeping track of which IDs are used and which are not). Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/usb/main.c | 61 +++ src/modules/cdi/usb/usb-hubs.h | 78 ++++ src/modules/cdi/usb/usb.c | 893 +++++++++++++++++++++++++++++++++++++++++ src/modules/cdi/usb/usb.h | 86 ++++ 4 files changed, 1118 insertions(+) create mode 100644 src/modules/cdi/usb/main.c create mode 100644 src/modules/cdi/usb/usb-hubs.h create mode 100644 src/modules/cdi/usb/usb.c create mode 100644 src/modules/cdi/usb/usb.h diff --git a/src/modules/cdi/usb/main.c b/src/modules/cdi/usb/main.c new file mode 100644 index 0000000..3add372 --- /dev/null +++ b/src/modules/cdi/usb/main.c @@ -0,0 +1,61 @@ +/****************************************************************************** + * Copyright (c) 2015 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 "usb.h" + + +#define DRIVER_NAME "usb" + + +static struct cdi_usb_driver usbd; + +static int usbd_init(void) +{ + cdi_driver_init(&usbd.drv); + return 0; +} + +static int usbd_destroy(void) +{ + cdi_driver_destroy(&usbd.drv); + return 0; +} + +static struct cdi_usb_driver usbd = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_USB, + .bus = CDI_USB_HCD, + .init = usbd_init, + .destroy = usbd_destroy, + .init_device = usb_init_hc, + }, + + .get_endpoint_descriptor = usb_get_endpoint_descriptor, + .control_transfer = usb_control_transfer, + .bulk_transfer = usb_bulk_transfer, +}; + +CDI_DRIVER(DRIVER_NAME, usbd) diff --git a/src/modules/cdi/usb/usb-hubs.h b/src/modules/cdi/usb/usb-hubs.h new file mode 100644 index 0000000..da8ac3e --- /dev/null +++ b/src/modules/cdi/usb/usb-hubs.h @@ -0,0 +1,78 @@ +/****************************************************************************** + * Copyright (c) 2015 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_HUBS_H +#define USB_HUBS_H + +#include + +#include "usb.h" + + +struct usb_hub_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint8_t b_nbr_ports; + uint16_t w_hub_characteristics; + uint8_t b_pwr_on_2_pwr_good; + uint8_t b_hub_contr_current; + uint8_t device_removable[]; +} __attribute__((packed)); + + +enum { + USB_DESC_HUB = 0x29, +}; + +// hub device features +enum { + C_HUB_LOCAL_POWER = 0, + C_HUB_OVER_CURRENT, +}; + +// hub port features +enum { + PORT_CONNECTION = 0, + PORT_ENABLE, + PORT_SUSPEND, + PORT_OVER_CURRENT, + PORT_RESET, + + PORT_POWER = 8, + PORT_LOW_SPEED, + PORT_HIGH_SPEED, + + C_PORT_CONNECTION = 16, + C_PORT_ENABLE, + C_PORT_SUSPEND, + C_PORT_OVER_CURRENT, + C_PORT_RESET, + PORT_TEST, + PORT_INDICATOR, +}; + +typedef struct usb_hub_device { + struct cdi_usb_hub cdi; + usb_hub_t *hub; +} usb_hub_device_t; + +#endif diff --git a/src/modules/cdi/usb/usb.c b/src/modules/cdi/usb/usb.c new file mode 100644 index 0000000..62ef989 --- /dev/null +++ b/src/modules/cdi/usb/usb.c @@ -0,0 +1,893 @@ +/****************************************************************************** + * Copyright (c) 2015 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 + +#include "usb.h" +#include "usb-hubs.h" + + +// #define DEBUG + +#ifdef DEBUG +#define DPRINTF(...) printf(__VA_ARGS__) +#else +#define DPRINTF(...) +#endif + + +static int current_usb_addr = 1; + + +static cdi_usb_transmission_result_t + do_control_transfer(struct cdi_usb_hc *hc, int dev, cdi_usb_speed_t speed, + int tt_addr, int tt_port, const usb_endpoint_t *ep, + const struct cdi_usb_setup_packet *setup, void *data) +{ + struct cdi_usb_hcd *hcd = CDI_UPCAST(hc->dev.driver, struct cdi_usb_hcd, + drv); + cdi_usb_hc_transaction_t ta; + cdi_usb_transmission_result_t r1, r2, r3; + + ta = hcd->create_transaction(hc, &(struct cdi_usb_hc_ep_info){ + .dev = dev, + .ep = ep->ep, + .ep_type = ep->type, + .speed = speed, + .mps = ep->mps, + .tt_addr = tt_addr, + .tt_port = tt_port + }); + + hcd->enqueue(hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = CDI_USB_SETUP, + .buffer = (void *)setup, + .size = sizeof(*setup), + .toggle = 0, + .result = &r1 + }); + + if (data) { + hcd->enqueue(hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = setup->bm_request_type & CDI_USB_CREQ_IN + ? CDI_USB_IN : CDI_USB_OUT, + .buffer = data, + .size = setup->w_length, + .toggle = 1, + .result = &r2 + }); + } else { + r2 = CDI_USB_OK; + } + + hcd->enqueue(hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = setup->bm_request_type & CDI_USB_CREQ_IN ? CDI_USB_OUT + : CDI_USB_IN, + .toggle = 1, + .result = &r3 + }); + + hcd->start_transaction(hc, ta); + hcd->wait_transaction(hc, ta); + hcd->destroy_transaction(hc, ta); + + return r1 ? r1 : r2 ? r2 : r3; +} + +static cdi_usb_transmission_result_t + control_transfer(const usb_device_t *dev, const usb_endpoint_t *ep, + const struct cdi_usb_setup_packet *setup, void *data) +{ + return do_control_transfer(dev->hc, dev->id, dev->speed, dev->tt_addr, + dev->tt_port, ep, setup, data); +} + + +static int get_mps0(usb_hub_t *hub, int hub_port, cdi_usb_speed_t speed) +{ + cdi_usb_transmission_result_t res; + struct cdi_usb_device_descriptor device_descriptor; + int tt_addr = 0, tt_port = 0; + + if (speed < CDI_USB_HIGH_SPEED && hub->ldev) { + if (hub->ldev->dev->speed < CDI_USB_HIGH_SPEED) { + tt_addr = hub->ldev->dev->tt_addr; + tt_port = hub->ldev->dev->tt_port; + } else { + tt_addr = hub->ldev->dev->id; + tt_port = hub_port; + } + } + + res = do_control_transfer(hub->hc, 0, speed, tt_addr, tt_port, + &(usb_endpoint_t){ + .type = CDI_USB_CONTROL, + .mps = 8 + }, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_DEVICE << 8) | 0, + .w_length = 8 + }, &device_descriptor); + + if (res != CDI_USB_OK) { + return -EIO; + } + + return device_descriptor.b_max_packet_size_0; +} + + +static usb_device_t *enumerate(usb_hub_t *hub, int hub_port, + cdi_usb_speed_t speed, int mps0) +{ + cdi_usb_transmission_result_t res; + usb_device_t *dev = calloc(1, sizeof(*dev)); + + int new_id = current_usb_addr++; + if (new_id > 127) { + // TODO: Per-bus ID, and detection of device disconnects so we can reuse + // old (stale) addresses + printf("[usb] USB address space exhausted, cannot enumerate device\n"); + free(dev); + return NULL; + } + + dev->hc = hub->hc; + dev->speed = speed; + + if (speed < CDI_USB_HIGH_SPEED && hub->ldev) { + if (hub->ldev->dev->speed < CDI_USB_HIGH_SPEED) { + dev->tt_addr = hub->ldev->dev->tt_addr; + dev->tt_port = hub->ldev->dev->tt_port; + } else { + dev->tt_addr = hub->ldev->dev->id; + dev->tt_port = hub_port; + } + } + + dev->eps[0][0].type = CDI_USB_CONTROL; + dev->eps[0][0].mps = mps0; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_ADDRESS, + .w_value = new_id + }, NULL); + if (res != CDI_USB_OK) { + free(dev); + return NULL; + } + + dev->id = new_id; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_DEVICE << 8) | 0, + .w_length = sizeof(dev->dev_desc) + }, &dev->dev_desc); + if (res != CDI_USB_OK) { + free(dev); + return NULL; + } + + return dev; +} + + +#ifdef DEBUG +static void get_string_descriptor(usb_device_t *dev, int index, + struct cdi_usb_string_descriptor *dst) +{ + cdi_usb_transmission_result_t res; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_STRING << 8) | index, + .w_length = 1 + }, dst); + if (res != CDI_USB_OK || !dst->b_length) { + dst->b_length = 0; + return; + } + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_STRING << 8) | index, + .w_length = dst->b_length + }, dst); + if (res != CDI_USB_OK) { + dst->b_length = 0; + return; + } +} +#endif + + +static int get_configurations(usb_device_t *dev) +{ + cdi_usb_transmission_result_t res; + struct cdi_usb_configuration_descriptor config; + + dev->configs = calloc(dev->dev_desc.b_num_configurations, + sizeof(dev->configs[0])); + + for (int i = 0; i < (int)dev->dev_desc.b_num_configurations; i++) { + res = control_transfer(dev, &dev->eps[0][0], + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_CONFIGURATION << 8) | i, + .w_length = sizeof(config) + }, &config); + if (res != CDI_USB_OK) { + goto fail; + } + + dev->configs[i] = malloc(config.w_total_length); + res = control_transfer(dev, &dev->eps[0][0], + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_CONFIGURATION << 8) | i, + .w_length = config.w_total_length + }, dev->configs[i]); + if (res != CDI_USB_OK) { + goto fail; + } + } + + return 0; + +fail: + for (int i = 0; i < (int)dev->dev_desc.b_num_configurations; i++) { + free(dev->configs[i]); + } + free(dev->configs); + return -EIO; +} + + +static bool compare_configurations(usb_device_t *dev, int i, int j) +{ + // TODO: Prefer self-powered + if (dev->configs[i]->b_max_power != dev->configs[j]->b_max_power) { + return dev->configs[i]->b_max_power > dev->configs[j]->b_max_power; + } + + if (dev->configs[i]->b_num_interfaces != dev->configs[j]->b_num_interfaces) + { + return dev->configs[i]->b_num_interfaces + > dev->configs[j]->b_num_interfaces; + } + + return false; +} + + +static int choose_configuration(usb_device_t *dev) +{ + cdi_usb_transmission_result_t res; + int best_config = 0; + + for (int i = 1; i < (int)dev->dev_desc.b_num_configurations; i++) { + if (compare_configurations(dev, i, best_config)) { + best_config = i; + } + } + + dev->config = dev->configs[best_config]; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_CONFIGURATION, + .w_value = dev->config->b_configuration_value, + }, NULL); + if (res != CDI_USB_OK) { + dev->config = NULL; + return -EIO; + } + + return 0; +} + + +#ifdef DEBUG +static void debug_device(usb_device_t *dev) +{ + printf("[usb] new %s device %04x:%04x (%x.%x.%x) ", + dev->speed == CDI_USB_LOW_SPEED ? "low speed" : + dev->speed == CDI_USB_FULL_SPEED ? "full speed" : + dev->speed == CDI_USB_HIGH_SPEED ? "high speed" : + dev->speed == CDI_USB_SUPERSPEED ? "superspeed" : + "futurespeed", + dev->dev_desc.id_vendor, dev->dev_desc.id_product, + dev->dev_desc.b_device_class, + dev->dev_desc.b_device_sub_class, + dev->dev_desc.b_device_protocol); + + struct cdi_usb_string_descriptor str; + + if (dev->dev_desc.i_manufacturer) { + get_string_descriptor(dev, dev->dev_desc.i_manufacturer, &str); + for (int i = 0; i < (str.b_length - 2) / 2; i++) { + putchar(str.b_string[i] > 0x7f ? '?' : str.b_string[i]); + } + putchar(' '); + } + + if (dev->dev_desc.i_product) { + get_string_descriptor(dev, dev->dev_desc.i_product, &str); + for (int i = 0; i < (str.b_length - 2) / 2; i++) { + putchar(str.b_string[i] > 0x7f ? '?' : str.b_string[i]); + } + putchar(' '); + } + + putchar('\n'); +} + +static void debug_configuration(usb_device_t *dev) +{ + printf("[usb] chose configuration %i ", dev->config->b_configuration_value); + + struct cdi_usb_string_descriptor str; + + if (dev->config->i_configuration) { + get_string_descriptor(dev, dev->config->i_configuration, &str); + putchar('('); + for (int i = 0; i < (str.b_length - 2) / 2; i++) { + putchar(str.b_string[i] > 0x7f ? '?' : str.b_string[i]); + } + putchar(')'); + putchar(' '); + } + + printf("with %i interface(s):\n", dev->config->b_num_interfaces); + + struct cdi_usb_interface_descriptor *ifc = (void *)(dev->config + 1); + for (int i = 0; i < (int)dev->config->b_num_interfaces; i++) { + printf("[usb] "); + if (ifc->i_interface) { + get_string_descriptor(dev, ifc->i_interface, &str); + for (int j = 0; j < (int)dev->config->b_num_interfaces; j++) { + putchar(str.b_string[j] > 0x7f ? '?' : str.b_string[j]); + } + putchar(' '); + } + printf("%x.%x.%x\n", ifc->b_interface_class, + ifc->b_interface_sub_class, ifc->b_interface_protocol); + + ifc = (void *)((struct cdi_usb_endpoint_descriptor *)(ifc + 1) + + ifc->b_num_endpoints); + } +} +#endif + + +static void new_hub_device(usb_logical_device_t *ldev); + +static void new_hub(usb_hub_t *hub) +{ + for (int i = 0; i < hub->cdi->ports; i++) { + hub->drv->port_disable(hub->cdi, i); + } + + DPRINTF("[usb] new hub with %i ports detected\n", hub->cdi->ports); + + for (int i = 0; i < hub->cdi->ports; i++) { + hub->drv->port_reset_enable(hub->cdi, i); + cdi_sleep_ms(5); + + cdi_usb_port_status_t status = hub->drv->port_status(hub->cdi, i); + if (!(status & CDI_USB_PORT_CONNECTED)) { + hub->drv->port_disable(hub->cdi, i); + continue; + } + + int mps0 = get_mps0(hub, i, status & CDI_USB_PORT_SPEED_MASK); + if (mps0 < 0) { + printf("[usb] failed to inquire MPS0\n"); + hub->drv->port_disable(hub->cdi, i); + continue; + } + + // Reset the device a second time. Maybe the device is broken and still + // wants to send more data even though we only requested the first eight + // bytes of the device descriptor (which we cannot receive without + // knowing the MPS0). A reset will solve the problem and will not do any + // harm otherwise. + hub->drv->port_reset_enable(hub->cdi, i); + cdi_sleep_ms(5); + + status = hub->drv->port_status(hub->cdi, i); + if (!(status & CDI_USB_PORT_CONNECTED)) { + hub->drv->port_disable(hub->cdi, i); + continue; + } + + usb_device_t *dev = enumerate(hub, i, status & CDI_USB_PORT_SPEED_MASK, + mps0); + if (!dev) { + printf("[usb] failed to enumerate device\n"); + hub->drv->port_disable(hub->cdi, i); + continue; + } + +#ifdef DEBUG + debug_device(dev); +#endif + + usb_logical_device_t *ldev = NULL; + int ldev_ep_idx = 0; + bool interface_level; + + interface_level = dev->dev_desc.b_device_class == CDI_USB_DEVCLS_NONE; + if (!interface_level) { + ldev = calloc(1, sizeof(*ldev)); + ldev->cdi.bus_data.bus_type = CDI_USB; + ldev->cdi.interface = -1; + ldev->cdi.endpoint_count = 1; // EP0 + ldev->cdi.vendor_id = dev->dev_desc.id_vendor; + ldev->cdi.product_id = dev->dev_desc.id_product; + ldev->cdi.class_id = dev->dev_desc.b_device_class; + ldev->cdi.subclass_id = dev->dev_desc.b_device_sub_class; + ldev->cdi.protocol_id = dev->dev_desc.b_device_protocol; + + ldev->dev = dev; + + ldev->endpoints = malloc(1 * sizeof(ldev->endpoints[0])); + ldev->endpoints[0] = 0; // EP0 + ldev_ep_idx = 1; + } + + if (get_configurations(dev) < 0) { + printf("[usb] failed to get device configurations\n"); + free(ldev->endpoints); + free(ldev); + free(dev); + hub->drv->port_disable(hub->cdi, i); + continue; + } + if (choose_configuration(dev) < 0) { + printf("[usb] failed to select device configuration\n"); + free(ldev->endpoints); + free(ldev); + for (int j = 0; j < (int)dev->dev_desc.b_num_configurations; j++) { + free(dev->configs[j]); + } + free(dev->config); + free(dev); + hub->drv->port_disable(hub->cdi, i); + continue; + } + +#ifdef DEBUG + debug_configuration(dev); +#endif + + struct cdi_usb_interface_descriptor *ifc = (void *)(dev->config + 1); + for (int j = 0; j < (int)dev->config->b_num_interfaces; j++) { + if (interface_level) { + ldev = calloc(1, sizeof(*ldev)); + ldev->cdi.bus_data.bus_type = CDI_USB; + ldev->cdi.interface = j; + // Include EP0 + ldev->cdi.endpoint_count = ifc->b_num_endpoints + 1; + ldev->cdi.vendor_id = dev->dev_desc.id_vendor; + ldev->cdi.product_id = dev->dev_desc.id_product; + ldev->cdi.class_id = ifc->b_interface_class; + ldev->cdi.subclass_id = ifc->b_interface_sub_class; + ldev->cdi.protocol_id = ifc->b_interface_protocol; + + ldev->dev = dev; + + ldev->endpoints = malloc(ldev->cdi.endpoint_count + * sizeof(ldev->endpoints[0])); + ldev->endpoints[0] = 0; // EP0 + ldev_ep_idx = 1; + } else { + ldev->cdi.endpoint_count += ifc->b_num_endpoints; + ldev->endpoints = realloc(ldev->endpoints, + ldev->cdi.endpoint_count + * sizeof(ldev->endpoints[0])); + } + + struct cdi_usb_endpoint_descriptor *ep = (void *)(ifc + 1); + for (int k = 0; k < (int)ifc->b_num_endpoints; k++, ep++) { + bool in = ep->b_endpoint_address & CDI_USB_EPDSC_EPADR_DIR; + int id = CDI_USB_EPDSC_EPADR_ID(ep->b_endpoint_address); + + dev->eps[in][id].ep = id; + dev->eps[in][id].mps + = CDI_USB_EPDSC_MPS_MPS(ep->w_max_packet_size); + dev->eps[in][id].desc = ep; + + switch (ep->bm_attributes & CDI_USB_EPDSC_ATTR_XFER_TYPE_MASK) { + case CDI_USB_EPDSC_ATTR_CONTROL: + dev->eps[in][id].type = CDI_USB_CONTROL; + break; + case CDI_USB_EPDSC_ATTR_ISOCHRONOUS: + dev->eps[in][id].type = CDI_USB_ISOCHRONOUS; + break; + case CDI_USB_EPDSC_ATTR_BULK: + dev->eps[in][id].type = CDI_USB_BULK; + break; + case CDI_USB_EPDSC_ATTR_INTERRUPT: + dev->eps[in][id].type = CDI_USB_INTERRUPT; + break; + } + + ldev->endpoints[ldev_ep_idx++] = ((int)in << 4) | id; + } + + if (interface_level) { + if (ldev->cdi.class_id == CDI_USB_DEVCLS_HUB) { + new_hub_device(ldev); + } else { + cdi_provide_device(&ldev->cdi.bus_data); + } + } + + ifc = (void *)ep; + } + + if (!interface_level) { + if (ldev->cdi.class_id == CDI_USB_DEVCLS_HUB) { + new_hub_device(ldev); + } else { + cdi_provide_device(&ldev->cdi.bus_data); + } + } + } +} + +struct cdi_device *usb_init_hc(struct cdi_bus_data *bus_data) +{ + struct cdi_usb_hc_bus *hc_bus = CDI_UPCAST(bus_data, struct cdi_usb_hc_bus, + bus_data); + struct cdi_usb_hc *hc = hc_bus->hc; + struct cdi_usb_hcd *hcd = CDI_UPCAST(hc->dev.driver, struct cdi_usb_hcd, + drv); + + usb_hub_t *hub = calloc(1, sizeof(*hub)); + hub->cdi = &hc->rh; + hub->hc = hc; + hub->drv = &hcd->rh_drv; + + new_hub(hub); + + return NULL; +} + + +void usb_get_endpoint_descriptor(struct cdi_usb_device *dev, int ep, + struct cdi_usb_endpoint_descriptor *desc) +{ + usb_logical_device_t *ldev = CDI_UPCAST(dev, usb_logical_device_t, cdi); + + if (ep < 0 || ep >= dev->endpoint_count) { + return; + } + + int ep_idx = ldev->endpoints[ep]; + usb_endpoint_t *ep_info = &ldev->dev->eps[ep_idx >> 4][ep_idx & 0xf]; + + if (ep_idx) { + assert(ep_info->desc); + memcpy(desc, ep_info->desc, sizeof(*desc)); + } else { + *desc = (struct cdi_usb_endpoint_descriptor){ + .b_length = sizeof(*desc), + .b_descriptor_type = CDI_USB_DESC_ENDPOINT, + .b_endpoint_address = 0, + .bm_attributes = 0x00, + .w_max_packet_size = ep_info->mps, + .b_interval = 0 + }; + } +} + + +cdi_usb_transmission_result_t + usb_control_transfer(struct cdi_usb_device *dev, int ep, + const struct cdi_usb_setup_packet *setup, void *data) +{ + usb_logical_device_t *ldev = CDI_UPCAST(dev, usb_logical_device_t, cdi); + + if (ep < 0 || ep >= dev->endpoint_count) { + return CDI_USB_DRIVER_ERROR; + } + + int ep_idx = ldev->endpoints[ep]; + usb_endpoint_t *ep_info = &ldev->dev->eps[ep_idx >> 4][ep_idx & 0xf]; + + if (ep_info->type != CDI_USB_CONTROL) { + return CDI_USB_DRIVER_ERROR; + } + + return control_transfer(ldev->dev, ep_info, setup, data); +} + + +cdi_usb_transmission_result_t usb_bulk_transfer(struct cdi_usb_device *dev, + int ep, void *data, size_t size) +{ + usb_logical_device_t *ldev = CDI_UPCAST(dev, usb_logical_device_t, cdi); + + if (ep < 0 || ep >= dev->endpoint_count) { + return CDI_USB_DRIVER_ERROR; + } + + int ep_idx = ldev->endpoints[ep]; + usb_endpoint_t *ep_info = &ldev->dev->eps[ep_idx >> 4][ep_idx & 0xf]; + + if (ep_info->type != CDI_USB_BULK) { + return CDI_USB_DRIVER_ERROR; + } + + struct cdi_usb_hcd *hcd = CDI_UPCAST(ldev->dev->hc->dev.driver, + struct cdi_usb_hcd, drv); + cdi_usb_hc_transaction_t ta; + cdi_usb_transmission_result_t res; + + ta = hcd->create_transaction(ldev->dev->hc, &(struct cdi_usb_hc_ep_info){ + .dev = ldev->dev->id, + .ep = ep_info->ep, + .ep_type = ep_info->type, + .speed = ldev->dev->speed, + .mps = ep_info->mps, + .tt_addr = ldev->dev->tt_addr, + .tt_port = ldev->dev->tt_port + }); + + hcd->enqueue(ldev->dev->hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = ep_idx >> 4 ? CDI_USB_IN : CDI_USB_OUT, + .buffer = data, + .size = size, + .toggle = ep_info->toggle, + .result = &res + }); + ep_info->toggle ^= (((size + ep_info->mps - 1) / ep_info->mps) & 1); + + hcd->start_transaction(ldev->dev->hc, ta); + hcd->wait_transaction(ldev->dev->hc, ta); + hcd->destroy_transaction(ldev->dev->hc, ta); + + return res; +} + + +static struct cdi_usb_hub_driver hub_drv; + + +static cdi_usb_transmission_result_t + hub_set_hub_feature(usb_hub_device_t *hub, uint16_t feature) +{ + return usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE + | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_FEATURE, + .w_value = feature + }, NULL); +} + + +static cdi_usb_transmission_result_t + hub_clear_port_feature(usb_hub_device_t *hub, + uint16_t feature, uint16_t port) +{ + return usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER + | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_CLEAR_FEATURE, + .w_value = feature, + .w_index = port + 1 + }, NULL); +} + + +static uint32_t hub_get_port_status(usb_hub_device_t *hub, uint16_t port) +{ + cdi_usb_transmission_result_t res; + uint32_t portsc; + + res = usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER + | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_STATUS, + .w_index = port + 1, + .w_length = sizeof(portsc) + }, &portsc); + if (res != CDI_USB_OK) { + return 0; + } + + return portsc; +} + + +static cdi_usb_transmission_result_t + hub_set_port_feature(usb_hub_device_t *hub, + uint16_t feature, uint16_t port) +{ + return usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER + | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_FEATURE, + .w_value = feature, + .w_index = port + 1 + }, NULL); +} + + +static void new_hub_device(usb_logical_device_t *ldev) +{ + cdi_usb_transmission_result_t res; + usb_hub_device_t *hub; + struct usb_hub_descriptor *hub_desc = NULL; + + assert(ldev->cdi.interface < 0); + + hub = calloc(1, sizeof(*hub)); + + hub->hub = calloc(1, sizeof(*hub->hub)); + hub->hub->cdi = &hub->cdi; + hub->hub->drv = &hub_drv; + hub->hub->hc = ldev->dev->hc; + hub->hub->ldev = ldev; + + uint8_t hub_desc_length; + res = usb_control_transfer(&ldev->cdi, 0, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (USB_DESC_HUB << 8) | 0, + .w_length = 1 + }, &hub_desc_length); + if (res != CDI_USB_OK || + hub_desc_length < sizeof(struct usb_hub_descriptor)) + { + goto fail; + } + + hub_desc = malloc(hub_desc_length); + res = usb_control_transfer(&ldev->cdi, 0, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (USB_DESC_HUB << 8) | 0, + .w_length = hub_desc_length + }, hub_desc); + if (res != CDI_USB_OK) { + goto fail; + } + + int ports = hub_desc->b_nbr_ports; + hub->cdi.ports = ports; + + // Enable port power + if (!(hub_desc->w_hub_characteristics & (1 << 1))) { + // Hub supports power switching, enable global power + if (hub_set_hub_feature(hub, C_HUB_LOCAL_POWER) != CDI_USB_OK) { + goto fail; + } + } + + if ((hub_desc->w_hub_characteristics & 0x3) == 1) { + // Hub supports per-port power switching, enable power for each port + for (int port = 0; port < ports; port++) { + if (hub_set_port_feature(hub, PORT_POWER, port) != CDI_USB_OK) { + goto fail; + } + } + } + + cdi_sleep_ms(hub_desc->b_pwr_on_2_pwr_good * 2); + + free(hub_desc); + + new_hub(hub->hub); + + return; + +fail: + free(hub_desc); + free(hub->hub); + free(hub); + return; +} + + +static void hub_port_down(struct cdi_usb_hub *cdi_hub, int index) +{ + usb_hub_device_t *hub = CDI_UPCAST(cdi_hub, usb_hub_device_t, cdi); + hub_clear_port_feature(hub, PORT_ENABLE, index); +} + + +static void hub_port_up(struct cdi_usb_hub *cdi_hub, int index) +{ + usb_hub_device_t *hub = CDI_UPCAST(cdi_hub, usb_hub_device_t, cdi); + + hub_clear_port_feature(hub, C_PORT_RESET, index); + hub_set_port_feature(hub, PORT_RESET, index); + + CDI_CONDITION_WAIT(hub_get_port_status(hub, index) & (1 << C_PORT_RESET), + 100); +} + + +static cdi_usb_port_status_t hub_port_status(struct cdi_usb_hub *cdi_hub, + int index) +{ + usb_hub_device_t *hub = CDI_UPCAST(cdi_hub, usb_hub_device_t, cdi); + + uint32_t portsc = hub_get_port_status(hub, index); + + cdi_usb_port_status_t status = 0; + + if (portsc & ((1 << PORT_ENABLE) | (1 << PORT_CONNECTION))) { + status |= CDI_USB_PORT_CONNECTED; + if (portsc & (1 << PORT_LOW_SPEED)) { + status |= CDI_USB_LOW_SPEED; + } else if (portsc & (1 << PORT_HIGH_SPEED)) { + status |= CDI_USB_HIGH_SPEED; + } else { + status |= CDI_USB_FULL_SPEED; + } + } + + return status; +} + + +static struct cdi_usb_hub_driver hub_drv = { + .port_disable = hub_port_down, + .port_reset_enable = hub_port_up, + .port_status = hub_port_status, +}; diff --git a/src/modules/cdi/usb/usb.h b/src/modules/cdi/usb/usb.h new file mode 100644 index 0000000..27dc061 --- /dev/null +++ b/src/modules/cdi/usb/usb.h @@ -0,0 +1,86 @@ +/****************************************************************************** + * Copyright (c) 2015 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_H +#define USB_H + +#include +#include + +#include +#include +#include + + +typedef struct usb_logical_device usb_logical_device_t; + +typedef struct usb_hub { + struct cdi_usb_hub *cdi; + + const struct cdi_usb_hub_driver *drv; + struct cdi_usb_hc *hc; + usb_logical_device_t *ldev; +} usb_hub_t; + +typedef struct usb_endpoint { + int ep; + cdi_usb_endpoint_type_t type; + int mps; + int toggle; + struct cdi_usb_endpoint_descriptor *desc; +} usb_endpoint_t; + +typedef struct usb_device { + struct cdi_usb_hc *hc; + int id; + cdi_usb_speed_t speed; + + int tt_addr, tt_port; + + // First index: OUT (0) / IN (1) + usb_endpoint_t eps[2][16]; + + struct cdi_usb_device_descriptor dev_desc; + struct cdi_usb_configuration_descriptor **configs, *config; +} usb_device_t; + +struct usb_logical_device { + struct cdi_usb_device cdi; + usb_device_t *dev; + int *endpoints; +}; + + +struct cdi_device *usb_init_hc(struct cdi_bus_data *bus_data); + +void usb_get_endpoint_descriptor(struct cdi_usb_device *dev, int ep, + struct cdi_usb_endpoint_descriptor *desc); + +cdi_usb_transmission_result_t + usb_control_transfer(struct cdi_usb_device *dev, int ep, + const struct cdi_usb_setup_packet *setup, void *data); + +cdi_usb_transmission_result_t usb_bulk_transfer(struct cdi_usb_device *dev, + int ep, void *data, + size_t size); + +#endif -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:11 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:11 +0100 Subject: [tyndur-devel] [PATCH 21/22] usb-storage: Add USB mass storage driver In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-22-git-send-email-max@tyndur.org> + This adds a USB mass storage device driver. It is missing proper error handling (e.g. for STALLed endpoints) and support for any USB MSD class other than bulk-only (e.g. control-bulk-interrupt for USB floppy drives). Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/usb-storage/main.c | 76 +++++++++++ src/modules/cdi/usb-storage/usb-storage.c | 216 ++++++++++++++++++++++++++++++ src/modules/cdi/usb-storage/usb-storage.h | 60 +++++++++ 3 files changed, 352 insertions(+) create mode 100644 src/modules/cdi/usb-storage/main.c create mode 100644 src/modules/cdi/usb-storage/usb-storage.c create mode 100644 src/modules/cdi/usb-storage/usb-storage.h diff --git a/src/modules/cdi/usb-storage/main.c b/src/modules/cdi/usb-storage/main.c new file mode 100644 index 0000000..aa896ca --- /dev/null +++ b/src/modules/cdi/usb-storage/main.c @@ -0,0 +1,76 @@ +/****************************************************************************** + * Copyright (c) 2015 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 "usb-storage.h" + + +#define DRIVER_NAME "usb-storage" + + +static struct cdi_scsi_driver drv; +static struct cdi_usb_bus_device_pattern bus_pattern; + +static int drv_init(void) +{ + cdi_scsi_driver_init(&drv); + cdi_handle_bus_device(&drv.drv, &bus_pattern.pattern); + return 0; +} + +static int drv_destroy(void) +{ + cdi_driver_destroy(&drv.drv); + return 0; +} + + +static struct cdi_scsi_driver drv = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_SCSI, + .bus = CDI_USB, + .init = drv_init, + .destroy = drv_destroy, + .init_device = init_usb_device, + }, + + .request = usb_storage_request, +}; + +static struct cdi_usb_bus_device_pattern bus_pattern = { + .pattern = { + .bus_type = CDI_USB, + }, + + .vendor_id = -1, + .product_id = -1, + + .class_id = CDI_USB_IFCCLS_STORAGE, + .subclass_id = -1, + .protocol_id = -1, +}; + +CDI_DRIVER(DRIVER_NAME, drv) diff --git a/src/modules/cdi/usb-storage/usb-storage.c b/src/modules/cdi/usb-storage/usb-storage.c new file mode 100644 index 0000000..544c779 --- /dev/null +++ b/src/modules/cdi/usb-storage/usb-storage.c @@ -0,0 +1,216 @@ +/****************************************************************************** + * Copyright (c) 2015 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 + +#include "usb-storage.h" + + +#define CBW_IN 0x80 +#define CBW_OUT 0x00 + +#define CBW_SIGNATURE 0x43425355 // 'USBC' => USB command +#define CSW_SIGNATURE 0x53425355 // 'USBS' => USB status + +struct msd_bo_cbw { + uint32_t signature; + uint32_t tag; + uint32_t data_length; + uint8_t flags; + uint8_t lun; + uint8_t cb_length; + uint8_t cb[16]; +} __attribute__((packed)); + +struct msd_bo_csw { + uint32_t signature; + uint32_t tag; + uint32_t data_residue; + uint8_t status; +} __attribute__((packed)); + + +typedef enum usb_mass_storage_type { + USBMS_BULK_ONLY, +} usb_mass_storage_type_t; + + +typedef struct usb_mass_storage_device { + struct cdi_scsi_device dev; + struct cdi_usb_device usb; + + usb_mass_storage_type_t type; + int luns; + + // FIXME: put into own class + int bulk_in, bulk_out; +} usb_mass_storage_device_t; + + +static int bulk_only_reset(usb_mass_storage_device_t *dev) +{ + cdi_usb_transmission_result_t res; + + res = cdi_usb_control_transfer(&dev->usb, 0, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_CLASS | CDI_USB_CREQ_INTERFACE + | CDI_USB_CREQ_OUT, + .b_request = USBMS_REQ_BOMSR, + .w_index = dev->usb.interface + }, NULL); + if (res != CDI_USB_OK) { + return -EIO; + } + + if (dev->luns > 0) { + return 0; + } + + uint8_t lun_count; + res = cdi_usb_control_transfer(&dev->usb, 0, &(struct cdi_usb_setup_packet) { + .bm_request_type = CDI_USB_CREQ_CLASS | CDI_USB_CREQ_INTERFACE + | CDI_USB_CREQ_IN, + .b_request = USBMS_REQ_GML, + .w_index = dev->usb.interface, + .w_length = 1 + }, &lun_count); + if (res == CDI_USB_OK) { + dev->luns = lun_count + 1; + } else { + dev->luns = 1; + } + + return 0; +} + + +struct cdi_device *init_usb_device(struct cdi_bus_data *bus_data) +{ + static int dev_counter = 0; + usb_mass_storage_device_t *dev = calloc(1, sizeof(*dev)); + dev->usb = *CDI_UPCAST(bus_data, struct cdi_usb_device, bus_data); + + if ((dev->usb.subclass_id == USBMS_SUBCLS_SCSILEG || + dev->usb.subclass_id == USBMS_SUBCLS_SCSI) && + dev->usb.protocol_id == USBMS_PROTO_BBB) + { + dev->type = USBMS_BULK_ONLY; + } else { + free(dev); + return NULL; + } + + if (dev->type == USBMS_BULK_ONLY) { + if (bulk_only_reset(dev) < 0) { + free(dev); + return NULL; + } + + struct cdi_usb_endpoint_descriptor ep_desc; + for (int ep = 1; ep < dev->usb.endpoint_count; ep++) { + cdi_usb_get_endpoint_descriptor(&dev->usb, ep, &ep_desc); + + if ((ep_desc.bm_attributes & CDI_USB_EPDSC_ATTR_XFER_TYPE_MASK) + == CDI_USB_EPDSC_ATTR_BULK) + { + bool is_in; + is_in = ep_desc.b_endpoint_address & CDI_USB_EPDSC_EPADR_DIR; + if (!dev->bulk_in && is_in) { + dev->bulk_in = ep; + } + if (!dev->bulk_out && !is_in) { + dev->bulk_out = ep; + } + } + } + + if (!dev->bulk_in || !dev->bulk_out) { + free(dev); + return NULL; + } + } + + asprintf((char **)&dev->dev.dev.name, "msd%i", dev_counter++); + + dev->dev.type = CDI_STORAGE; + dev->dev.lun_count = dev->luns; + + return &dev->dev.dev; +} + + +int usb_storage_request(struct cdi_scsi_device *device, + struct cdi_scsi_packet *packet) +{ + cdi_usb_transmission_result_t res; + usb_mass_storage_device_t *dev = CDI_UPCAST(device, + usb_mass_storage_device_t, dev); + + struct msd_bo_cbw cbw = { + .signature = CBW_SIGNATURE, + .tag = 42, + .data_length = packet->bufsize, + .flags = packet->direction == CDI_SCSI_READ ? CBW_IN : CBW_OUT, + .lun = packet->lun, + .cb_length = packet->cmdsize + }; + memcpy(cbw.cb, packet->command, sizeof(cbw.cb)); + + res = cdi_usb_bulk_transfer(&dev->usb, dev->bulk_out, &cbw, sizeof(cbw)); + if (res != CDI_USB_OK) { + bulk_only_reset(dev); + return -1; + } + + if (packet->direction == CDI_SCSI_READ) { + res = cdi_usb_bulk_transfer(&dev->usb, dev->bulk_in, + packet->buffer, packet->bufsize); + } else if (packet->direction == CDI_SCSI_WRITE) { + res = cdi_usb_bulk_transfer(&dev->usb, dev->bulk_out, + packet->buffer, packet->bufsize); + } + + if (res != CDI_USB_OK) { + bulk_only_reset(dev); + return -1; + } + + struct msd_bo_csw csw; + res = cdi_usb_bulk_transfer(&dev->usb, dev->bulk_in, &csw, sizeof(csw)); + + if (res != CDI_USB_OK || csw.signature != CSW_SIGNATURE || csw.tag != 42 || + csw.data_residue || csw.status) + { + bulk_only_reset(dev); + return -1; + } + + return 0; +} diff --git a/src/modules/cdi/usb-storage/usb-storage.h b/src/modules/cdi/usb-storage/usb-storage.h new file mode 100644 index 0000000..eed3462 --- /dev/null +++ b/src/modules/cdi/usb-storage/usb-storage.h @@ -0,0 +1,60 @@ +/****************************************************************************** + * Copyright (c) 2015 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_STORAGE_H +#define USB_STORAGE_H + +#include +#include + + +// Mass storage subclass codes +#define USBMS_SUBCLS_SCSILEG 0x00 // SCSI legacy ("command set not reported") +#define USBMS_SUBCLS_RBC 0x01 // RBC +#define USBMS_SUBCLS_MMC5 0x02 // MMC-5 (ATAPI) +#define USBMS_SUBCLS_UFI 0x04 // UFI for Floppy Disk Drives +#define USBMS_SUBCLS_SCSI 0x06 // SCSI transparent command set +#define USBMS_SUBCLS_LSDFS 0x07 // LSD FS for negotiating SCSI access +#define USBMS_SUBCLS_IEEE1667 0x08 // IEEE 1667 +#define USBMS_SUBCLS_VENDOR 0xff // Vendor-specific + +// Mass storage protocol codes +#define USBMS_PROTO_CBI_CCI 0x00 // Control/Bulk/Interrupt with + // Command Completion Interrupt +#define USBMS_PROTO_CBI 0x01 // CBI without CCI +#define USBMS_PROTO_BBB 0x50 // Bulk-only +#define USBMS_PROTO_UAS 0x62 // UAS +#define USBMS_PROTO_VENDOR 0xff // Vendor-specific + +// Mass storage request codes +#define USBMS_REQ_ADSC 0x00 // Accept Device-Specific Command (CBI) +#define USBMS_REQ_GETRQ 0xfc // Get Requests (LSD FS) +#define USBMS_REQ_PUTRQ 0xfd // Put Requests (LSD FS) +#define USBMS_REQ_GML 0xfe // Get Max LUN (Bulk-only) +#define USBMS_REQ_BOMSR 0xff // Bulk-Only Mass Storage Reset (Bulk-only) + + +struct cdi_device *init_usb_device(struct cdi_bus_data *bus_data); +int usb_storage_request(struct cdi_scsi_device *device, + struct cdi_scsi_packet *packet); + +#endif -- 2.6.3 From max at tyndur.org Sun Dec 13 00:26:12 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:26:12 +0100 Subject: [tyndur-devel] [PATCH 22/22] Images: USB-Stick-Image In-Reply-To: <1449962772-29705-1-git-send-email-max@tyndur.org> References: <1449962772-29705-1-git-send-email-max@tyndur.org> Message-ID: <1449962772-29705-23-git-send-email-max@tyndur.org> + Die Makefileziele image-usb und test-qemu-usb erlauben es, ein Image für einen USB-Stick zu bauen. Signed-off-by: Max Reitz --- Makefile | 8 +- build/config/grub_usb.cfg | 11 +++ build/config/grub_usb_kernel2.cfg | 23 +++++ build/config/image_usb.sh | 3 + build/scripts/image_usb_grub1 | 122 ++++++++++++++++++++++++ build/scripts/image_usb_grub2 | 193 ++++++++++++++++++++++++++++++++++++++ buildmk.sh | 2 +- 7 files changed, 360 insertions(+), 2 deletions(-) create mode 100644 build/config/grub_usb.cfg create mode 100644 build/config/grub_usb_kernel2.cfg create mode 100644 build/config/image_usb.sh create mode 100755 build/scripts/image_usb_grub1 create mode 100755 build/scripts/image_usb_grub2 diff --git a/Makefile b/Makefile index 96a6b24..9a6e8a4 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ clean: clean_objs for file in *; do if [ -f "$$file/Makefile" -a \( ! -f "$$file/.nobuild" -o -f "$$file/.ignorenobuild" \) ]; then $(MAKE) -sC "$$file" clean; rm "$$file/Makefile"; fi done rm -f Makefile.local -.SILENT: all makefiles subdirs obj lbuilds-env enable-pascal disable-pascal clean softclean clean_objs clean_root updateroot image-floppy image-hd test-qemu test-qemu-hd +.SILENT: all makefiles subdirs obj lbuilds-env enable-pascal disable-pascal clean softclean clean_objs clean_root updateroot image-floppy image-hd image-usb test-qemu test-qemu-hd test-qemu-usb .PHONY: lbuilds-env # Code für lbuilds @@ -73,6 +73,9 @@ image-hd: updateroot image-cdrom: updateroot build/scripts/image_cdrom_$(BOOTLOADER) +image-usb: updateroot + build/scripts/image_usb_$(BOOTLOADER) + test-qemu-floppy: image-floppy $(QEMU) -serial stdio -fda build/images/floppy.img -boot a -net user -net nic,model=rtl8139 | tee qemu-serial.log @@ -82,6 +85,9 @@ test-qemu-hd: image-hd test-qemu-cdrom: image-cdrom $(QEMU) -serial stdio -cdrom build/images/cdrom.img -boot d -net user -net nic,model=rtl8139 | tee qemu-serial.log +test-qemu-usb: image-usb + $(QEMU) -serial stdio -device usb-ehci -drive if=none,id=usb-thumb,file=build/images/usb.img,format=raw -device usb-storage,drive=usb-thumb -net user -net nic,model=rtl8139 | tee qemu-serial.log + test-bochs: image-floppy bochs -f bochs.config diff --git a/build/config/grub_usb.cfg b/build/config/grub_usb.cfg new file mode 100644 index 0000000..bbd2d8e --- /dev/null +++ b/build/config/grub_usb.cfg @@ -0,0 +1,11 @@ +title tyndur (kernel1) +kernel /boot/tyndur debug=s +module /modules/init boot=file:/ +module /modules/pci +module /modules/usb-storage +module /modules/usb +module /modules/ehci +module /modules/ext2 +module /modules/console servmgr:/term servmgr:/term +module /modules/servmgr usb-storage:/msd0_p0|ext2:/ vterm:/vterm8/out ehci usb-storage usb ext2 console pci +boot diff --git a/build/config/grub_usb_kernel2.cfg b/build/config/grub_usb_kernel2.cfg new file mode 100644 index 0000000..73cbf6a --- /dev/null +++ b/build/config/grub_usb_kernel2.cfg @@ -0,0 +1,23 @@ +title tyndur (Deutsch) +kernel /boot/tyndur2 debug=s +module /modules/init boot=file:/ +module /modules/pci +module /modules/usb-storage +module /modules/usb +module /modules/ehci +module /modules/ext2 +module /modules/console servmgr:/term servmgr:/term +module /modules/servmgr usb-storage:/msd0_p0|ext2:/ vterm:/vterm8/out ehci ext2 console pci +boot + +title tyndur (English) +kernel /boot/tyndur2 debug=s +module /modules/init boot=file:/ env.LANG=en +module /modules/pci +module /modules/usb-storage +module /modules/usb +module /modules/ehci +module /modules/ext2 +module /modules/console servmgr:/term servmgr:/term +module /modules/servmgr usb-storage:/msd0_p0|ext2:/ vterm:/vterm8/out ehci ext2 console pci +boot diff --git a/build/config/image_usb.sh b/build/config/image_usb.sh new file mode 100644 index 0000000..73b07ef --- /dev/null +++ b/build/config/image_usb.sh @@ -0,0 +1,3 @@ +IMAGE_PATH="build/images/usb.img" +# Imagegroesse in Megabytes +IMAGE_SIZE=64 diff --git a/build/scripts/image_usb_grub1 b/build/scripts/image_usb_grub1 new file mode 100755 index 0000000..e04fbd1 --- /dev/null +++ b/build/scripts/image_usb_grub1 @@ -0,0 +1,122 @@ +#!/bin/bash + +# Copyright (c) 2008 The tyndur Project. All rights reserved. +# +# This code is derived from software contributed to the tyndur Project +# by Antoine Kaufmann. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. All advertising materials mentioning features or use of this software +# must display the following acknowledgement: +# This product includes software developed by the tyndur Project +# and its contributors. +# 4. Neither the name of the tyndur Project nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +ROOT=build/root +ROOT_COMMON=build/root-common +ROOT_HD=build/root-hd +ROOT_LOCAL=build/root-local +MOUNT=build/mnt +SUDO=sudo + +export PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin + +export LOST_BUILDMK_ROOT="`pwd`" +source build/config/image_usb.sh +source config.sh + +die() +{ + echo $1 + exit -1 +} + +# Wenn das Image neu erstellt werden muss, kommen noch ein paar schritte mehr +# dazu. +NEW=0 +if ! [ -f $IMAGE_PATH ]; then + # Leeres Image erstellen und formatieren + dd of=$IMAGE_PATH bs=$((1024 * 1024)) seek=$IMAGE_SIZE count=0 + build/scripts/create_partition $IMAGE_PATH + NEW=1 +fi + + +# Start und Ende der Partition einlesen +PARTITIONS="`build/scripts/get_partition $IMAGE_PATH`" +PART_START=`echo $PARTITIONS | awk '{print $1}'` +PART_SIZE=`echo $PARTITIONS | awk '{print $2}'` + +if [ $NEW -ne 0 ]; then + LOOP_DEV=`$SUDO losetup -f $IMAGE_PATH --sizelimit $(($PART_SIZE * 512)) -o $(($PART_START * 512)) --show` + if [ $? -ne 0 ]; then + die "Loop Device konnte nicht angelegt werden" + fi + + # Dateisystem erstellen + $SUDO $LOST_TOOLS_MKE2FS -T ext2 -F -q $LOOP_DEV || die "Dateisystem konnte nicht erstellt werden" + + $SUDO losetup -d $LOOP_DEV +fi + +# Partition mounten +mkdir -p $MOUNT +$SUDO mount $IMAGE_PATH $MOUNT -t ext2 -o loop,offset=$((${PART_START} * 512)) +$SUDO chmod 777 -R $MOUNT + +# Daten Kopieren +cp -rP --preserve=links,timestamps $ROOT/* $ROOT_COMMON/* $ROOT_HD/* $MOUNT/ +[ -d $ROOT_LOCAL ] && cp -rP --preserve=links,timestamps $ROOT_LOCAL/* $MOUNT/ + +# GRUB-Konfiguration kopieren +mkdir -p $MOUNT/boot/grub +rm -f $MOUNT/boot/grub/menu.lst +if [ -e $MOUNT/boot/tyndur2 ]; then + cat build/config/grub_usb_kernel2.cfg >> $MOUNT/boot/grub/menu.lst +fi +if [ -e $MOUNT/boot/tyndur ]; then + cat build/config/grub_usb.cfg >> $MOUNT/boot/grub/menu.lst +fi + + +# Grub kopieren +if [ $NEW -ne 0 ]; then + cp $LOST_GRUB_STAGESDIR/stage? $MOUNT/boot/grub/ +fi + +# Partition unmounten und loopback-Device freigeben +while fuser -m $MOUNT; do true; done +$SUDO umount $MOUNT +rmdir $MOUNT + + +# Grub installieren +if [ $NEW -ne 0 ]; then + $LOST_TOOLS_GRUB --no-floppy --batch > /dev/null < /dev/null 2>&1); then + GRUB2_BIN_PREFIX=grub +elif (which grub2-mkimage > /dev/null 2>&1); then + GRUB2_BIN_PREFIX=grub2 +else + die "Konnte grub-mkimage nicht finden" +fi +GRUB2_MKIMAGE=$GRUB2_BIN_PREFIX-mkimage + +if (which $GRUB2_BIN_PREFIX-setup > /dev/null 2>&1); then + GRUB2_SETUP=$GRUB2_BIN_PREFIX-setup +elif (which $GRUB2_BIN_PREFIX-bios-setup > /dev/null 2>&1); then + GRUB2_SETUP=$GRUB2_BIN_PREFIX-bios-setup +else + die "Konnte GRUB-setup-Tool nicht finden" +fi + + +convert_menu_lst() +{ + sed -ie 's/^title \(.*\)$/menuentry "\1" {/' "$1" + sed -ie 's/^kernel \([^ ]\+\)\(.*\)$/ multiboot \1 \1 \2/' "$1" + sed -ie 's/^module \([^ ]\+\)\(.*\)$/ module \1 \1 \2/' "$1" + sed -ie 's/^boot$/}/' "$1" + sed -ie 's/|/\\|/' "$1" +} + +# Wenn das Image neu erstellt werden muss, kommen noch ein paar schritte mehr +# dazu. +NEW=0 +if ! [ -f $IMAGE_PATH ]; then + # Leeres Image erstellen und formatieren + dd of=$IMAGE_PATH bs=$((1024 * 1024)) seek=$IMAGE_SIZE count=0 + build/scripts/create_partition $IMAGE_PATH + NEW=1 +fi + + +# Start und Ende der Partition einlesen +PARTITIONS="`build/scripts/get_partition $IMAGE_PATH`" +PART_START=`echo $PARTITIONS | awk '{print $1}'` +PART_SIZE=`echo $PARTITIONS | awk '{print $2}'` + +if [ $NEW -ne 0 ]; then + LOOP_DEV=`$SUDO losetup -f $IMAGE_PATH --sizelimit $(($PART_SIZE * 512)) -o $(($PART_START * 512)) --show` + if [ $? -ne 0 ]; then + die "Loop Device konnte nicht angelegt werden" + fi + + # Dateisystem erstellen + $SUDO $LOST_TOOLS_MKE2FS -T ext2 -F -q $LOOP_DEV || die "Dateisystem konnte nicht erstellt werden" + + $SUDO losetup -d $LOOP_DEV +fi + +LOOP_DEV=`$SUDO losetup -f $IMAGE_PATH --offset=$((${PART_START} * 512)) --show` +if [ $? -ne 0 ]; then + die "Loop Device konnte nicht angelegt werden" +fi + +# Partition mounten +mkdir -p $MOUNT +$SUDO mount $LOOP_DEV $MOUNT -t ext2 +$SUDO chmod 777 -R $MOUNT + +# Daten Kopieren +cp -rP --preserve=links,timestamps $ROOT/* $ROOT_COMMON/* $ROOT_HD/* $MOUNT/ +[ -d $ROOT_LOCAL ] && cp -rP --preserve=links,timestamps $ROOT_LOCAL/* $MOUNT/ + +# GRUB-Konfiguration kopieren +mkdir -p $MOUNT/boot/grub +rm -f $MOUNT/boot/grub/grub.cfg +echo "insmod multiboot" >> $MOUNT/boot/grub/grub.cfg +if [ -e $MOUNT/boot/tyndur2 ]; then + cat build/config/grub_usb_kernel2.cfg >> $MOUNT/boot/grub/grub.cfg +fi +if [ -e $MOUNT/boot/tyndur ]; then + cat build/config/grub_usb.cfg >> $MOUNT/boot/grub/grub.cfg +fi + +convert_menu_lst $MOUNT/boot/grub/grub.cfg + + +# Erstmal überlegen, welche Module wir dieses Mal brauchen könnten. +# Die Abhänngigkeiten ändern sich leider zu häufig als dass man sich auf +# eine feste Liste verlassen könnte. +dependencies="" + +function list_dependencies() +{ + local dep + local i + + for i in $1; do + if (echo $dependencies | grep " $i " > /dev/null); then + continue; + fi + dependencies="$dependencies $i " + + dep=$(grep "$i:" $LOST_GRUB2_MODULESDIR/moddep.lst | \ + sed -e 's/^.*: *//') + list_dependencies "$dep" + done +} + +list_dependencies "normal multiboot gzio" + +# Grub kopieren +if [ $NEW -ne 0 ]; then + ln -fs . $MOUNT/boot/grub/i386-pc + echo "$dependencies" | sed -e 's/ /\n/g' | xargs -isrc cp $LOST_GRUB2_MODULESDIR/src.mod $MOUNT/boot/grub +fi + +# Grub installieren +if [ $NEW -ne 0 ]; then + DISK_LOOP_DEV=`$SUDO losetup -f $IMAGE_PATH --show` + if [ $? -ne 0 ]; then + die "Loop Device konnte nicht angelegt werden" + fi + + $SUDO chmod 666 $LOOP_DEV + $SUDO chmod 666 $DISK_LOOP_DEV + + mkdir -p build/grub2 + echo "(hd0) $DISK_LOOP_DEV" > build/grub2/device.map + + cp $LOST_GRUB2_MODULESDIR/boot.img $MOUNT/boot/grub + $GRUB2_MKIMAGE --directory=$LOST_GRUB2_MODULESDIR --prefix="(hd0,1)/boot/grub/" -O i386-pc -o $MOUNT/boot/grub/core.img ext2 biosdisk part_msdos minicmd + $GRUB2_SETUP -m build/grub2/device.map -d $MOUNT/boot/grub "(hd0)" + + $SUDO losetup -d $DISK_LOOP_DEV +fi + +# Partition unmounten und loopback-Device freigeben +while fuser -m $MOUNT; do true; done +$SUDO umount $MOUNT +rmdir $MOUNT + +$SUDO losetup -d $LOOP_DEV diff --git a/buildmk.sh b/buildmk.sh index abbd359..c900ef4 100644 --- a/buildmk.sh +++ b/buildmk.sh @@ -264,7 +264,7 @@ clean: clean_objs for file in *; do if [ -f "\$\$file/Makefile" -a \( ! -f "\$\$file/.nobuild" -o -f "\$\$file/.ignorenobuild" \) ]; then \$(MAKE) -sC "\$\$file" clean; rm "\$\$file/Makefile"; fi done rm -f Makefile.local -.SILENT: all makefiles subdirs obj lbuilds-env enable-pascal disable-pascal clean softclean clean_objs clean_root updateroot image-floppy image-hd test-qemu test-qemu-hd +.SILENT: all makefiles subdirs obj lbuilds-env enable-pascal disable-pascal clean softclean clean_objs clean_root updateroot image-floppy image-hd image-usb test-qemu test-qemu-hd EOF # Auf alle Unterprozesse warten -- 2.6.3 From max at tyndur.org Sun Dec 13 00:39:54 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 00:39:54 +0100 Subject: [tyndur-devel] [PATCH 02/11] kernel2: Neuer Syscall lio_probe_service In-Reply-To: <1449959291-24784-3-git-send-email-kevin@tyndur.org> References: <1449959291-24784-1-git-send-email-kevin@tyndur.org> <1449959291-24784-3-git-send-email-kevin@tyndur.org> Message-ID: <566CB04A.9080204@tyndur.org> On 12.12.2015 at 23:28, Kevin Wolf wrote: > + kernel2: Unterstützung für einen neuen Syscall, der zu einer gegebenen > Ressource einen Service verwendet, der sie als Pipequelle benutzen s/verwendet/findet/? Sonst siehts gut aus. Ohne LIOv2 zu k[eö]nnen. Aber das ist natürlich: Kritikel! Max > kann. > > Signed-off-by: Kevin Wolf > --- > src/include/syscall_structs.h | 6 +++++ > src/include/syscallno.h | 1 + > src/kernel2/include/lostio/client.h | 4 ++++ > src/kernel2/include/syscall.h | 4 ++++ > src/kernel2/src/lostio/include/lostio_int.h | 4 ++++ > src/kernel2/src/lostio/tree.c | 34 +++++++++++++++++++++++++++++ > src/kernel2/src/syscall.c | 1 + > src/kernel2/src/syscalls/lostio.c | 14 ++++++++++++ > src/modules/include/syscall.h | 9 ++++++++ > src/modules/lib/syscalls/lostio.c | 21 ++++++++++++++++++ > 10 files changed, 98 insertions(+) From max at tyndur.org Sun Dec 13 01:10:45 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:45 +0100 Subject: [tyndur-devel] [PATCH v2 00/24] CDI.usb (und mehr) Message-ID: <1449965469-16714-1-git-send-email-max@tyndur.org> Leider habe ich den letzten Cover Letter gelesen, also wurde ich doof. Hier ist v2 mit den folgenden Fixes: v2: - Patch 10: Hinzugefügt, fügt die in v1 fehlende Makefile zu rtl8168b hinzu. Ohne ist doof. - Patch 23: Selbiges für ehci, usb und usb-storage. - Patch 18: cdi_usb_device_osdep braucht noch zwei Felder, die in v1 unterschlagen wurden. Jetzt sind sie da. Wer das liest, wird gleich hoffentlich eine funktionierende und nicht ganz so sehr kaputte v1 abschicken. git-backport-diff im Vergleich zu v1: Key: [----] : patches are identical [####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively 001/24:[----] [--] 'cdi/pci: Ausrichtung von BARs an Pageframes' 002/24:[----] [--] 'cdi/misc: Erlaube IRQ-Sharing' 003/24:[----] [--] 'cdi/vesa: Entferne "inline"' 004/24:[----] [--] 'cdi/misc: Add CDI_UPCAST and endianness functions' 005/24:[----] [--] 'cdi/misc: Add cdi_elapsed_ms()' 006/24:[----] [--] 'cdi/scsi, cdi.h: Remove týndur-specific fields' 007/24:[----] [--] 'cdi/scsi: Add support for multiple LUNs' 008/24:[----] [--] 'cdi.h: Add cdi_handle_bus_device()' 009/24:[----] [--] 'rtl8168b: Add driver' 010/24:[down] 'rtl8168b: Makefile.all' 011/24:[----] [--] 'ata: Temporary buffer for ATAPI commands' 012/24:[----] [--] 'ata: Initialize ata_device to 0' 013/24:[----] [--] 'cdi/scsi: Neu geschrieben' 014/24:[----] [--] 'lock.h: locked_decrement()' 015/24:[----] [--] 'servmgr: Workaround für zyklische Abhängigkeiten' 016/24:[----] [--] 'cdi/misc: Implementiere cdi_elapsed_ms()' 017/24:[----] [--] 'cdi/usb: Add headers' 018/24:[0003] [FC] 'cdi/usb: Implementierung der CDI-Bibliothek' 019/24:[----] [--] 'usb: Entferne týndur-eigene Treiber' 020/24:[----] [--] 'ehci: Add EHCI driver' 021/24:[----] [--] 'usb: Add USB 2.0 bus driver' 022/24:[----] [--] 'usb-storage: Add USB mass storage driver' 023/24:[down] 'usb: Makefile.all' 024/24:[----] [--] 'Images: USB-Stick-Image' Max Reitz (24): cdi/pci: Ausrichtung von BARs an Pageframes cdi/misc: Erlaube IRQ-Sharing cdi/vesa: Entferne "inline" cdi/misc: Add CDI_UPCAST and endianness functions cdi/misc: Add cdi_elapsed_ms() cdi/scsi, cdi.h: Remove týndur-specific fields cdi/scsi: Add support for multiple LUNs cdi.h: Add cdi_handle_bus_device() rtl8168b: Add driver rtl8168b: Makefile.all ata: Temporary buffer for ATAPI commands ata: Initialize ata_device to 0 cdi/scsi: Neu geschrieben lock.h: locked_decrement() servmgr: Workaround für zyklische Abhängigkeiten cdi/misc: Implementiere cdi_elapsed_ms() cdi/usb: Add headers cdi/usb: Implementierung der CDI-Bibliothek usb: Entferne týndur-eigene Treiber ehci: Add EHCI driver usb: Add USB 2.0 bus driver usb-storage: Add USB mass storage driver usb: Makefile.all Images: USB-Stick-Image Makefile | 8 +- build/config/grub_usb.cfg | 11 + build/config/grub_usb_kernel2.cfg | 23 + build/config/image_usb.sh | 3 + build/scripts/image_usb_grub1 | 122 ++++ build/scripts/image_usb_grub2 | 193 ++++++ buildmk.sh | 2 +- src/include/lock.h | 10 + src/modules/cdi/ata/atapi.c | 11 +- src/modules/cdi/ata/device.c | 2 +- src/modules/cdi/ehci/Makefile.all | 6 + src/modules/cdi/ehci/ehci.c | 640 +++++++++++++++++++ src/modules/cdi/ehci/ehci.h | 292 +++++++++ src/modules/cdi/ehci/main.c | 69 +++ src/modules/cdi/include/cdi-osdep.h | 24 + src/modules/cdi/include/cdi.h | 117 +++- src/modules/cdi/include/cdi/misc.h | 228 +++++++ src/modules/cdi/include/cdi/scsi.h | 11 +- src/modules/cdi/include/cdi/usb-structures.h | 174 ++++++ src/modules/cdi/include/cdi/usb.h | 211 +++++++ src/modules/cdi/include/cdi/usb_hcd.h | 168 +++++ src/modules/cdi/lib/cdi.c | 107 +++- src/modules/cdi/lib/misc.c | 41 +- src/modules/cdi/lib/pci.c | 6 +- src/modules/cdi/lib/scsi/disk.c | 506 +++++++++++---- src/modules/cdi/lib/usb.c | 403 ++++++++++++ src/modules/cdi/lib/usb_dd.c | 173 ++++++ src/modules/cdi/lib/usb_hcd.c | 413 +++++++++++++ src/modules/cdi/rtl8168b/Makefile.all | 6 + src/modules/cdi/rtl8168b/include/rtl8168b.h | 143 +++++ src/modules/cdi/rtl8168b/main.c | 70 +++ src/modules/cdi/rtl8168b/rtl8168b.c | 409 ++++++++++++ src/modules/cdi/usb-storage/Makefile.all | 6 + src/modules/cdi/usb-storage/main.c | 76 +++ src/modules/cdi/usb-storage/usb-storage.c | 216 +++++++ src/modules/cdi/usb-storage/usb-storage.h | 60 ++ src/modules/cdi/usb/Makefile.all | 6 + src/modules/cdi/usb/main.c | 61 ++ src/modules/cdi/usb/usb-hubs.h | 78 +++ src/modules/cdi/usb/usb.c | 893 +++++++++++++++++++++++++++ src/modules/cdi/usb/usb.h | 86 +++ src/modules/cdi/vesa/drawing.c | 12 +- src/modules/cdi/vesa/drawing.h | 12 +- src/modules/include/usb-ipc.h | 103 +++ src/modules/lib/servmgr.c | 11 +- src/modules/servmgr/rpcif.c | 22 +- src/modules/usb/include/usb-ddrv.h | 59 -- src/modules/usb/include/usb-hc.h | 141 ----- src/modules/usb/include/usb-hub.h | 49 -- src/modules/usb/include/usb-server.h | 153 ----- src/modules/usb/include/usb-structs.h | 176 ------ src/modules/usb/include/usb-trans.h | 66 -- src/modules/usb/msd/Makefile.all | 8 - src/modules/usb/msd/init.c | 206 ------ src/modules/usb/msd/lostio.c | 218 ------- src/modules/usb/msd/main.c | 65 -- src/modules/usb/msd/msd-bo.c | 104 ---- src/modules/usb/msd/msd-bo.h | 99 --- src/modules/usb/msd/msd-cbi.c | 86 --- src/modules/usb/msd/msd-cbi.h | 71 --- src/modules/usb/msd/msd.h | 154 ----- src/modules/usb/msd/rpc.c | 89 --- src/modules/usb/msd/scsi.c | 240 ------- src/modules/usb/msd/scsi.h | 219 ------- src/modules/usb/msd/usb-lib.c | 207 ------- src/modules/usb/msd/usb-lib.h | 124 ---- src/modules/usb/uhci/Makefile.all | 8 - src/modules/usb/uhci/ctrl.c | 96 --- src/modules/usb/uhci/init.c | 265 -------- src/modules/usb/uhci/main.c | 73 --- src/modules/usb/uhci/rpc.c | 297 --------- src/modules/usb/uhci/trans.c | 316 ---------- src/modules/usb/uhci/uhci.h | 269 -------- src/modules/usb/usb1/Makefile.all | 8 - src/modules/usb/usb1/detect.c | 444 ------------- src/modules/usb/usb1/hub.c | 180 ------ src/modules/usb/usb1/main.c | 59 -- src/modules/usb/usb1/rpc.c | 350 ----------- src/modules/usb/usb1/transfer.c | 123 ---- src/modules/usb/usb1/usb.h | 209 ------- 80 files changed, 6071 insertions(+), 5404 deletions(-) create mode 100644 build/config/grub_usb.cfg create mode 100644 build/config/grub_usb_kernel2.cfg create mode 100644 build/config/image_usb.sh create mode 100755 build/scripts/image_usb_grub1 create mode 100755 build/scripts/image_usb_grub2 create mode 100644 src/modules/cdi/ehci/Makefile.all create mode 100644 src/modules/cdi/ehci/ehci.c create mode 100644 src/modules/cdi/ehci/ehci.h create mode 100644 src/modules/cdi/ehci/main.c create mode 100644 src/modules/cdi/include/cdi/usb-structures.h create mode 100644 src/modules/cdi/include/cdi/usb.h create mode 100644 src/modules/cdi/include/cdi/usb_hcd.h create mode 100644 src/modules/cdi/lib/usb.c create mode 100644 src/modules/cdi/lib/usb_dd.c create mode 100644 src/modules/cdi/lib/usb_hcd.c create mode 100644 src/modules/cdi/rtl8168b/Makefile.all create mode 100644 src/modules/cdi/rtl8168b/include/rtl8168b.h create mode 100644 src/modules/cdi/rtl8168b/main.c create mode 100644 src/modules/cdi/rtl8168b/rtl8168b.c create mode 100644 src/modules/cdi/usb-storage/Makefile.all create mode 100644 src/modules/cdi/usb-storage/main.c create mode 100644 src/modules/cdi/usb-storage/usb-storage.c create mode 100644 src/modules/cdi/usb-storage/usb-storage.h create mode 100644 src/modules/cdi/usb/Makefile.all create mode 100644 src/modules/cdi/usb/main.c create mode 100644 src/modules/cdi/usb/usb-hubs.h create mode 100644 src/modules/cdi/usb/usb.c create mode 100644 src/modules/cdi/usb/usb.h create mode 100644 src/modules/include/usb-ipc.h delete mode 100644 src/modules/usb/include/usb-ddrv.h delete mode 100644 src/modules/usb/include/usb-hc.h delete mode 100644 src/modules/usb/include/usb-hub.h delete mode 100644 src/modules/usb/include/usb-server.h delete mode 100644 src/modules/usb/include/usb-structs.h delete mode 100644 src/modules/usb/include/usb-trans.h delete mode 100644 src/modules/usb/msd/Makefile.all delete mode 100644 src/modules/usb/msd/init.c delete mode 100644 src/modules/usb/msd/lostio.c delete mode 100644 src/modules/usb/msd/main.c delete mode 100644 src/modules/usb/msd/msd-bo.c delete mode 100644 src/modules/usb/msd/msd-bo.h delete mode 100644 src/modules/usb/msd/msd-cbi.c delete mode 100644 src/modules/usb/msd/msd-cbi.h delete mode 100644 src/modules/usb/msd/msd.h delete mode 100644 src/modules/usb/msd/rpc.c delete mode 100644 src/modules/usb/msd/scsi.c delete mode 100644 src/modules/usb/msd/scsi.h delete mode 100644 src/modules/usb/msd/usb-lib.c delete mode 100644 src/modules/usb/msd/usb-lib.h delete mode 100644 src/modules/usb/uhci/Makefile.all delete mode 100644 src/modules/usb/uhci/ctrl.c delete mode 100644 src/modules/usb/uhci/init.c delete mode 100644 src/modules/usb/uhci/main.c delete mode 100644 src/modules/usb/uhci/rpc.c delete mode 100644 src/modules/usb/uhci/trans.c delete mode 100644 src/modules/usb/uhci/uhci.h delete mode 100644 src/modules/usb/usb1/Makefile.all delete mode 100644 src/modules/usb/usb1/detect.c delete mode 100644 src/modules/usb/usb1/hub.c delete mode 100644 src/modules/usb/usb1/main.c delete mode 100644 src/modules/usb/usb1/rpc.c delete mode 100644 src/modules/usb/usb1/transfer.c delete mode 100644 src/modules/usb/usb1/usb.h -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:46 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:46 +0100 Subject: [tyndur-devel] [PATCH v2 01/24] cdi/pci: Ausrichtung von BARs an Pageframes In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-2-git-send-email-max@tyndur.org> ! BARs müssen nicht immer an 4k (oder was auch immer die Pageframegröße ist) ausgerichtet sein. Wenn sie das nicht sind, sollte die CDI-Bibliothek entsprechend handeln. Signed-off-by: Max Reitz --- src/modules/cdi/lib/pci.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/modules/cdi/lib/pci.c b/src/modules/cdi/lib/pci.c index 1df92dd..0dd07f8 100644 --- a/src/modules/cdi/lib/pci.c +++ b/src/modules/cdi/lib/pci.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -191,7 +192,10 @@ void cdi_pci_alloc_memory(struct cdi_pci_device* device) for (i = 0; (res = cdi_list_get(device->resources, i)); i++) { if (res->type == CDI_PCI_MEMORY) { - res->address = mem_allocate_physical(res->length, res->start, 0); + uintptr_t offset = res->start % PAGE_SIZE; + void* mapping = mem_allocate_physical(res->length + offset, + res->start - offset, 0); + res->address = (void*) ((uintptr_t)mapping + offset); } } } -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:47 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:47 +0100 Subject: [tyndur-devel] [PATCH v2 02/24] cdi/misc: Erlaube IRQ-Sharing In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-3-git-send-email-max@tyndur.org> * Dass Treiber mehrere Geräte verwalten, ist nicht selten. Dass mehrere Geräte denselben IRQ verwenden, ebenfalls nicht (zumindest, wenn man die IRQs nicht neu zuweist). Damit sollte die CDI-Bibliothek es auf jeden Fall erlauben, dass mehrere Handler für denselben IRQ angelegt werden. Signed-off-by: Max Reitz --- src/modules/cdi/lib/misc.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/modules/cdi/lib/misc.c b/src/modules/cdi/lib/misc.c index 11661b4..1b965af 100644 --- a/src/modules/cdi/lib/misc.c +++ b/src/modules/cdi/lib/misc.c @@ -9,6 +9,7 @@ */ #include +#include #include #include #include @@ -22,10 +23,16 @@ /** Anzahl der verfuegbaren IRQs */ #define IRQ_COUNT 0x10 +struct irq_handler { + struct irq_handler* next; + + void (*handler)(struct cdi_device* dev); + struct cdi_device* dev; +}; + /** Array mit allen IRQ-Handlern; Als index wird die Nummer benutzt */ -static void (*driver_irq_handler[IRQ_COUNT])(struct cdi_device*) = { NULL }; -/** Array mit den passenden Geraeten zu den registrierten IRQs */ -static struct cdi_device* driver_irq_device[IRQ_COUNT] = { NULL }; +static struct irq_handler* driver_irq_handlers[IRQ_COUNT]; + /** * Array, das die jeweilige Anzahl an aufgerufenen Interrupts seit dem * cdi_reset_wait_irq speichert. @@ -44,8 +51,8 @@ static void irq_handler(uint8_t irq) irq -= TYNDUR_IRQ_OFFSET; driver_irq_count[irq]++; - if (driver_irq_handler[irq]) { - driver_irq_handler[irq](driver_irq_device[irq]); + for (struct irq_handler* h = driver_irq_handlers[irq]; h; h = h->next) { + h->handler(h->dev); } } @@ -65,16 +72,18 @@ void cdi_register_irq(uint8_t irq, void (*handler)(struct cdi_device*), return; } - // Der Interrupt wurde schon mal registriert - if (driver_irq_handler[irq]) { - fprintf(stderr, "cdi: Versuch IRQ %d mehrfach zu registrieren\n", irq); - return; - } + struct irq_handler* h = malloc(sizeof(*h)); + *h = (struct irq_handler){ + .next = driver_irq_handlers[irq], + .handler = handler, + .dev = device + }; - driver_irq_handler[irq] = handler; - driver_irq_device[irq] = device; + driver_irq_handlers[irq] = h; - register_intr_handler(TYNDUR_IRQ_OFFSET + irq, irq_handler); + if (!h->next) { + register_intr_handler(TYNDUR_IRQ_OFFSET + irq, irq_handler); + } } /** @@ -118,7 +127,7 @@ int cdi_wait_irq(uint8_t irq, uint32_t timeout) return -1; } - if (!driver_irq_handler[irq]) { + if (!driver_irq_handlers[irq]) { return -2; } -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:48 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:48 +0100 Subject: [tyndur-devel] [PATCH v2 03/24] cdi/vesa: Entferne "inline" In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-4-git-send-email-max@tyndur.org> * Die Funktionen sind im Header zwar als inline deklariert, dort ist aber nur der Funktionskopf. Weil das "inline" vermutlich tatsächlich nichts bewirkt und GCC obendrein bei -std=gnu11 drüber meckert, ist es am besten, es einfach zu entfernen. Signed-off-by: Max Reitz --- src/modules/cdi/vesa/drawing.c | 12 ++++++------ src/modules/cdi/vesa/drawing.h | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/modules/cdi/vesa/drawing.c b/src/modules/cdi/vesa/drawing.c index b169ec2..8f53e8b 100644 --- a/src/modules/cdi/vesa/drawing.c +++ b/src/modules/cdi/vesa/drawing.c @@ -422,7 +422,7 @@ void vesa_draw_bitmap_part(struct cdi_video_bitmap* target, } } -inline void vesa_draw_dot(struct cdi_video_bitmap* target, unsigned int x, +void vesa_draw_dot(struct cdi_video_bitmap* target, unsigned int x, unsigned int y, struct cdi_video_color* color) { // Ungueltige Positionen abfangen @@ -454,7 +454,7 @@ inline void vesa_draw_dot(struct cdi_video_bitmap* target, unsigned int x, *start |= newcolor & colormask; } -inline uint32_t do_rop(enum cdi_video_raster_op rop, uint32_t new, uint32_t old) +uint32_t do_rop(enum cdi_video_raster_op rop, uint32_t new, uint32_t old) { switch (rop) { case CDI_VIDEO_ROP_OR: @@ -472,7 +472,7 @@ inline uint32_t do_rop(enum cdi_video_raster_op rop, uint32_t new, uint32_t old) } } -inline uint32_t convert_color(struct cdi_video_pixel_format fmt, +uint32_t convert_color(struct cdi_video_pixel_format fmt, struct cdi_video_color* cdicolor) { uint32_t red, blue, green, alpha, color; @@ -493,7 +493,7 @@ inline uint32_t convert_color(struct cdi_video_pixel_format fmt, return color; } -inline struct cdi_video_color* convert_color_from_pixel(struct cdi_video_pixel_format +struct cdi_video_color* convert_color_from_pixel(struct cdi_video_pixel_format fmt, uint32_t value) { struct cdi_video_color* color = malloc(sizeof(struct cdi_video_color)); @@ -520,7 +520,7 @@ inline struct cdi_video_color* convert_color_from_pixel(struct cdi_video_pixel_f return color; } -inline void* get_read_buffer_addr(struct cdi_video_bitmap* bitmap) +void* get_read_buffer_addr(struct cdi_video_bitmap* bitmap) { int x; char* addr = NULL; @@ -537,7 +537,7 @@ inline void* get_read_buffer_addr(struct cdi_video_bitmap* bitmap) return NULL; } -inline void* get_write_buffer_addr(struct cdi_video_bitmap* bitmap) +void* get_write_buffer_addr(struct cdi_video_bitmap* bitmap) { int x; char* addr = NULL; diff --git a/src/modules/cdi/vesa/drawing.h b/src/modules/cdi/vesa/drawing.h index bed6043..76b1fee 100644 --- a/src/modules/cdi/vesa/drawing.h +++ b/src/modules/cdi/vesa/drawing.h @@ -47,16 +47,16 @@ void vesa_draw_bitmap_part(struct cdi_video_bitmap* target, struct cdi_video_bitmap* bitmap, unsigned int x, unsigned int y, unsigned int srcx, unsigned int srcy, unsigned int width, unsigned int height); -inline void vesa_draw_dot(struct cdi_video_bitmap* target, unsigned int x, +void vesa_draw_dot(struct cdi_video_bitmap* target, unsigned int x, unsigned int y, struct cdi_video_color* color); -inline uint32_t do_rop(enum cdi_video_raster_op rop, uint32_t new, uint32_t old); -inline uint32_t convert_color(struct cdi_video_pixel_format fmt, +uint32_t do_rop(enum cdi_video_raster_op rop, uint32_t new, uint32_t old); +uint32_t convert_color(struct cdi_video_pixel_format fmt, struct cdi_video_color* cdicolor); -inline struct cdi_video_color* convert_color_from_pixel(struct cdi_video_pixel_format +struct cdi_video_color* convert_color_from_pixel(struct cdi_video_pixel_format fmt, uint32_t value); -inline void* get_read_buffer_addr(struct cdi_video_bitmap* bitmap); -inline void* get_write_buffer_addr(struct cdi_video_bitmap* bitmap); +void* get_read_buffer_addr(struct cdi_video_bitmap* bitmap); +void* get_write_buffer_addr(struct cdi_video_bitmap* bitmap); #endif /* __VESA_DRAWING_H__ */ -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:49 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:49 +0100 Subject: [tyndur-devel] [PATCH v2 04/24] cdi/misc: Add CDI_UPCAST and endianness functions In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-5-git-send-email-max@tyndur.org> + Type inheritance is a common pattern seen in CDI. The new CDI_UPCAST() macro makes conversion to superclasses safer and allows for multiple inheritance if the need arises. + Add some endianness conversion functions. Even though support for big endian platforms is nowhere in sight, endianness conversion is still a common operation which should not be implemented in the driver itself but provided by the CDI implementation. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi/misc.h | 141 +++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/src/modules/cdi/include/cdi/misc.h b/src/modules/cdi/include/cdi/misc.h index 07967ec..b0973e6 100644 --- a/src/modules/cdi/include/cdi/misc.h +++ b/src/modules/cdi/include/cdi/misc.h @@ -85,6 +85,147 @@ int cdi_ioports_free(uint16_t start, uint16_t count); */ void cdi_sleep_ms(uint32_t ms); +/** + * \german + * Castet @object (muss ein Pointer sein) zu einem Pointer auf ein Objekt des + * Typs @to, welcher eine Unterklasse vom Typ von @object sein muss. @field ist + * das Feld von @to, das @object enthält. + * + * Beispiel: + * struct cdi_driver* driver; + * struct cdi_storage_driver* storage_driver = + * CDI_UPCAST(driver, struct cdi_storage_driver, drv); + * \endgerman + * \english + * Casts @object (must be a pointer) to a pointer to an object of type @to which + * must be a subclass of @object's type. @field is the entry in @to which + * contains @object. + * + * Example: + * struct cdi_driver* driver; + * struct cdi_storage_driver* storage_driver = + * CDI_UPCAST(driver, struct cdi_storage_driver, drv); + * \endenglish + * \thuringiansaxonian + * Dud @object (mussä Boindr sein) zuä Boindr uffn Objekt vondn Dühp @to + * umwürschn, der dannoo ähne Undrglasse vondn Dühp von @object sein muss. + * @field is das Feld von @to, dasdawelsches @object enthaldn dud. + * + * Baispiel: + * struct cdi_driver* driver; + * struct cdi_storage_driver* storage_driver = + * CDI_UPCAST(driver, struct cdi_storage_driver, drv); + * \endthuringiansaxonian + * \french + * Convertit @object (qui doit être un pointeur) en un pointeur à un objet du + * type @to, qui doit être une sous-classe du type d'@objet. @field est + * l'inscription de @to qui est @object. + * + * Exemple: + * struct cdi_driver* driver; + * struct cdi_storage_driver* storage_driver = + * CDI_UPCAST(driver, struct cdi_storage_driver, drv); + * \endfrench + * \japanese + * ?????@object????@to??????????????@to?@object? + * ????????????????@field?@to?@object?????????? + * + * ?? + * struct cdi_driver* driver; + * struct cdi_storage_driver* storage_driver = + * CDI_UPCAST(driver, struct cdi_storage_driver, drv); + * \endjapanese + */ +#define CDI_UPCAST(object, to, field) \ + /* Type compatibility check */ \ + ((void)((object) - &((to*)0)->field), \ + /* The cast itself */ \ + (to*)((char*)(object) - (char*)&((to*)0)->field)) + +/** + * \german + * Funktionen zur Endiannesskonvertierung. Es sei darauf hingewiesen, dass die + * meisten (wenn nicht alle) CDI-Treiber nur Little-Endian-Plattformen als Host + * unterstützen. + * \endgerman + * \english + * Functions for endianness conversion. Be aware that most if not all CDI + * drivers only support little endian machines as a host. + * \endenglish + * \thuringiansaxonian + * Fungzschjounen fürde Endschjannessumwandlung. Passe uff, fast wenn ni gar + * wirklisch alle CDI-Draibr gönnn nur uff Liddl-Endschjan-Blattform loofn. + * \endthuringiansaxonian + * \french + * Fonctions pour la conversion boutisme. Au fait, la plupart si pas tous des + * pilotes CDI ne peuvent que fonctionner sur une plateforme petit-boutiste. + * \endfrench + * \japanese + * ??????????????????????????????????? + * CDI??????????????????????????????? + * \endjapanese + */ + +static inline uint16_t cdi_be16_to_cpu(uint16_t x) +{ + return (x >> 8) | (x << 8); +} + +static inline uint32_t cdi_be32_to_cpu(uint32_t x) +{ + return ((uint32_t)cdi_be16_to_cpu(x) << 16) | (uint32_t)cdi_be16_to_cpu(x >> 16); +} + +static inline uint64_t cdi_be64_to_cpu(uint64_t x) +{ + return ((uint64_t)cdi_be32_to_cpu(x) << 32) | (uint64_t)cdi_be32_to_cpu(x >> 32); +} + +static inline uint16_t cdi_cpu_to_be16(uint16_t x) +{ + return cdi_be16_to_cpu(x); +} + +static inline uint32_t cdi_cpu_to_be32(uint32_t x) +{ + return cdi_be32_to_cpu(x); +} + +static inline uint64_t cdi_cpu_to_be64(uint64_t x) +{ + return cdi_be64_to_cpu(x); +} + +static inline uint16_t cdi_le16_to_cpu(uint16_t x) +{ + return x; +} + +static inline uint32_t cdi_le32_to_cpu(uint32_t x) +{ + return x; +} + +static inline uint64_t cdi_le64_to_cpu(uint64_t x) +{ + return x; +} + +static inline uint16_t cdi_cpu_to_le16(uint16_t x) +{ + return cdi_le16_to_cpu(x); +} + +static inline uint32_t cdi_cpu_to_le32(uint32_t x) +{ + return cdi_le32_to_cpu(x); +} + +static inline uint64_t cdi_cpu_to_le64(uint64_t x) +{ + return cdi_le64_to_cpu(x); +} + #ifdef __cplusplus }; // extern "C" #endif -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:50 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:50 +0100 Subject: [tyndur-devel] [PATCH v2 05/24] cdi/misc: Add cdi_elapsed_ms() In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-6-git-send-email-max@tyndur.org> + Add cdi_elapsed_ms() + Add CDI_CONDITION_WAIT() and CDI_CONDITION_WAIT_SLEEP() macros for waiting until a volatile condition is fulfilled or a timeout occurs Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi/misc.h | 87 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/src/modules/cdi/include/cdi/misc.h b/src/modules/cdi/include/cdi/misc.h index b0973e6..9676b4a 100644 --- a/src/modules/cdi/include/cdi/misc.h +++ b/src/modules/cdi/include/cdi/misc.h @@ -87,6 +87,93 @@ void cdi_sleep_ms(uint32_t ms); /** * \german + * Gibt die Anzahl der vergangenen Millisekunden seit einem + * implementationsspezifischen Zeitpunkt zurück, mindestens aber seit dem ersten + * Aufruf dieser Funktion. Die Auflösung ist ebenfalls + * implementationsspezifisch. Der Rückgabewert muss monoton wachsen (bis er + * wegen der Begrenzung des Rückgabetyps überläuft). + * \endgerman + * \english + * Returns the number of milliseconds elapsed since an implementation specific + * point in time, not later than the first call of this function. The resolution + * too is implementation specific. The return value must grow monotonically + * (until it overflows due to the return type's limit). + * \endenglish + * \thuringiansaxonian + * Dud zurückgehm, wie lange (in Millisegundn) ä imblämändazschjounsspäzifschr + * Zeitbunkt nu schon her is. Diesr Zeitbunkt muss oor vorm erstn Uffruf diesr + * Fungzschjoun lieschn. De Ufflösung is ooch imblämändazschjounsspäzifsch. Dr + * Rückgabewert muss monodohn wachsn (bissr wäschn dä Begrenzung vondn + * Rückgabedühp übrloofn dud). + * \endthuringiansaxonian + * \french + * Rend le nombre des millisecondes qui ont passé depuis un instant défini par + * implémentation, au plus tard depuis le premier appel de cette fonction. La + * résolution aussi est défini par implémentation. La valeur rendue doit grandir + * monotonement (jusqu'elle dépasse par la limitation du type rendu). + * \endfrench + * \japanese + * ??????????????????????????????????? + * ?????????????????????????????????? + * ????????????????????????? + * \endjapanese + */ +uint64_t cdi_elapsed_ms(void); + +/** + * \german + * Wartet, bis die Bedingung @cond erfüllt ist oder mehr als @timeout + * Millisekunden vergangen sind. + * \endgerman + * \english + * Waits until the condition @cond yields true or more than @timeout + * milliseconds have passed. + * \endenglish + * \thuringiansaxonian + * Dud wortn, bisde Bedingung @cond erfüllt is oodr über @timeout Millisekundn + * vorbei sin. + * \endthuringiansaxonian + * \french + * Attend que la condition @cond est satisfaite ou plus que @timeout + * millisecondes sont passées. + * \endgerman + * \japanese + * ??@cond??????@timeout??????? + * \endjapanese + */ +#define CDI_CONDITION_WAIT(cond, timeout) \ + do { \ + uint64_t start = cdi_elapsed_ms(); \ + while (!(cond) && cdi_elapsed_ms() - start < timeout); \ + } while (0) + +/** + * \german + * CDI_CONDITION_WAIT() mit cdi_sleep_ms(@sleep) zwischen jeder Auswertung. + * \endgerman + * \english + * CDI_CONDITION_WAIT() with cdi_sleep_ms(@sleep) between each evaluation. + * \endenglish + * \thuringiansaxonian + * CDI_CONDITION_WAIT() mittä cdi_sleep_ms(@sleep) zwischn jädr Auswärdungn. + * \endthuringiansaxonian + * \french + * CDI_CONDITION_WAIT() avec cdi_sleep_ms(@sleep) entre toutes les évaluations. + * \endfrench + * \japanese + * ????????cdi_sleep_ms(@sleep)?????CDI_CONDITION_WAIT()? + * \endjapanese + */ +#define CDI_CONDITION_WAIT_SLEEP(cond, timeout, sleep) \ + do { \ + uint64_t start = cdi_elapsed_ms(); \ + while (!(cond) && cdi_elapsed_ms() - start < timeout) { \ + cdi_sleep_ms(sleep); \ + } \ + } while (0) + +/** + * \german * Castet @object (muss ein Pointer sein) zu einem Pointer auf ein Objekt des * Typs @to, welcher eine Unterklasse vom Typ von @object sein muss. @field ist * das Feld von @to, das @object enthält. -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:51 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:51 +0100 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_v2_06/24=5D_cdi/scsi=2C_cdi=2Eh?= =?utf-8?q?=3A_Remove_t=C3=BDndur-specific_fields?= In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-7-git-send-email-max@tyndur.org> - Remove týndur-specific fields from non-OS-dependant structures Signed-off-by: Max Reitz Acked-by: Kevin Wolf Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi.h | 3 --- src/modules/cdi/include/cdi/scsi.h | 7 ------- 2 files changed, 10 deletions(-) diff --git a/src/modules/cdi/include/cdi.h b/src/modules/cdi/include/cdi.h index a9207db..998e080 100644 --- a/src/modules/cdi/include/cdi.h +++ b/src/modules/cdi/include/cdi.h @@ -178,9 +178,6 @@ struct cdi_device { * \endenglish */ struct cdi_bus_data* bus_data; - - // tyndur-spezifisch - void* backdev; }; /** diff --git a/src/modules/cdi/include/cdi/scsi.h b/src/modules/cdi/include/cdi/scsi.h index 8edccbf..cef96ed 100644 --- a/src/modules/cdi/include/cdi/scsi.h +++ b/src/modules/cdi/include/cdi/scsi.h @@ -51,13 +51,6 @@ struct cdi_scsi_device { /// Geraetetyp, der ueber SCSI angesteuert wird cdi_device_type_t type; - - /** - * tyndur-spezifisch: Frontend-Geraet (z.B. CDI_STORAGE-Geraet, das ueber - * SCSI angesteuert wird) - */ - void* frontdev; - }; /// SCSI-Treiber -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:52 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:52 +0100 Subject: [tyndur-devel] [PATCH v2 07/24] cdi/scsi: Add support for multiple LUNs In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-8-git-send-email-max@tyndur.org> + One SCSI device may provide multiple LUNs. Add a field for the LUN count to cdi_scsi_device. + Furthermore, add a field to select the LUN to cdi_scsi_packet. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi/scsi.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/modules/cdi/include/cdi/scsi.h b/src/modules/cdi/include/cdi/scsi.h index cef96ed..5157a23 100644 --- a/src/modules/cdi/include/cdi/scsi.h +++ b/src/modules/cdi/include/cdi/scsi.h @@ -24,6 +24,9 @@ /// SCSI-Paket struct cdi_scsi_packet { + /// LUN to address + int lun; + /// Buffer zum Senden oder Empfangen von Daten void *buffer; @@ -51,6 +54,9 @@ struct cdi_scsi_device { /// Geraetetyp, der ueber SCSI angesteuert wird cdi_device_type_t type; + + /// Number of LUNs + int lun_count; }; /// SCSI-Treiber -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:53 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:53 +0100 Subject: [tyndur-devel] [PATCH v2 08/24] cdi.h: Add cdi_handle_bus_device() In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-9-git-send-email-max@tyndur.org> + Currently, all devices provided on a bus have to be passed to the init_device() function of all drivers handling any device type on that bus. cdi_handle_bus_device() can be used by the driver to specify a pattern (struct cdi_bus_device_pattern) which allows the CDI implementation to speed up the selection of the correct driver for a new device. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- src/modules/cdi/include/cdi.h | 114 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/src/modules/cdi/include/cdi.h b/src/modules/cdi/include/cdi.h index 998e080..8ed0738 100644 --- a/src/modules/cdi/include/cdi.h +++ b/src/modules/cdi/include/cdi.h @@ -142,6 +142,51 @@ struct cdi_bus_data { /** * \german + * Diese von jedem sie unterstützenden Bustyp zu erweiternde Struktur enthält + * Informationen darüber, welche Geräte von einem bestimmten Treiber verwaltet + * werden können. + * Typischerweise enthält sie einige der von der zugehörigen cdi_bus_data- + * Struktur bereitgestellten Felder (jene, die genutzt werden, um ein Gerät zu + * identifizieren, wie Geräte- und Hersteller-ID), wobei negative Werte "egal" + * bedeuten. + * \endgerman + * \english + * This structure, which is extended by any bus type supporting it, contains + * information about which devices can be handled by a certain driver. + * Typically, it will contain some of the fields provided by the appropriate + * cdi_bus_data structure (the ones which are used to identify a device like + * device/vendor ID) where negative values signify a "don't care". + * \endenglish + * \thuringiansaxonian + * Diese Schdruggtur wird von jehm Busdühp, der se unnerstützn gönnn dut, + * erweidert, und hat dann Informatzschjoun darübber, welsche Geräide vonnä + * bestimmdn Draibr unnr de Fiddische genomm wern gönnn. + * Tübbischerweise endhältse ä bår vondn Feldrn, die ooch in dr zugehörschn + * cdi_bus_data-Schdruggtur sin (welsche de nimmst, damitte weest, was für ä + * Geräit das nu eigndlisch is, zum Beispiel Geräide- un Herschdellor- + * Idendifikatzschjoun). Negadive Werde dun dann "egal" bedeudn. + * \endthuringiansaxonian + * \french + * Cette structure, qui est enrichie par tous les types qui le peuvent, décrit + * quels appareils un particulier pilote peut employer. + * Régulièrement, il y a quelques valeurs de la cdi_bus_data structure adéquate + * (qui sont utilisées pour identifier un appareil, comme l'ID de l'appareil et + * du vendeur), où des valeurs négatives signifient "n'importe quoi". + * \endfrench + * \japanese + * ??????????????????????????????????? + * ???????????? + * ???????cdi_bus_data???????????????????????? + * ????????????????????????????????????ID? + * ????????????????????? + * \endjapanese + */ +struct cdi_bus_device_pattern { + cdi_device_type_t bus_type; +}; + +/** + * \german * Beschreibt ein Gerät * \endgerman * \english @@ -406,6 +451,75 @@ int cdi_provide_device(struct cdi_bus_data* device); int cdi_provide_device_internal_drv(struct cdi_bus_data* device, struct cdi_driver* driver); +/** + * \german + * Teilt der CDI-Implementierung mit, dass der Treiber @drv nur Geräte vom + * Muster @pattern akzeptiert (die am Bus @pattern->bus_type hängen). Diese + * Funktion darf mehrfach aufgerufen werden. Jedes neue Gerät am Bus, das nicht + * auf das Muster passt, wird der @drv->init_device()-Funktion nicht übergeben. + * Diese Funktion darf andererseits aber NULL zurückgeben, selbst wenn + * cdi_handle_bus_device() vom Treiber benutzt wurde, das heißt, diese Funktion + * darf mit einem Muster aufgerufen werden, das nicht nur auf Geräte passt, die + * vom Treiber behandelt werden können, sondern auch auf andere (die dann + * abgewiesen werden können, indem @drv->init_device() NULL zurückgibt). + * + * Diese Funktion darf nur in @drv->init_driver() aufgerufen werden. + * \endgerman + * \english + * Tells the CDI implementation that the driver @drv is able to handle devices + * with the pattern @pattern (appearing on the bus @pattern->bus_type). This + * function can be called multiple times. Any device appearing on the bus not + * fitting the pattern will not be passed to the @drv->init_device() function. + * That function may however return NULL even after the driver has called + * cdi_handle_bus_device(), that is, it is valid to call this function with a + * @pattern that does not only fit devices handled by @drv but others as well + * (which can then be rejected by letting @drv->init_device() return NULL). + * + * This function must only be called from @drv->init_driver(). + * \endenglish + * \thuringiansaxonian + * Dailt dr CDI-Imblämändierung mit, dass dr Draibr @drv nur an Gelumbsch mibm + * Musdr @pattern rummehrn kann (die andn Bus @pattern->bus_type sin). Diese + * Fungzschjoun gann oo mehrfach uffgerufn wern. Wenn ä neues Geräit am Bus + * uffdoochn dut und das ni uffs Musdr bassn dut, dann gommt das bei dr + * @drv->init_device()-Fungzschjoun oo ni an. Uff dr annern Saide darf diese + * Fungzschjoun wiederum NULL zurückgehm, sogar wenner Draibr vorher + * cdi_handle_bus_device() uffgerufn hat, das heißt, diese Fungzschjoun gann + * ooch middä Musdr uffgerufn wern, das ni nur uff Geräide bassn dud, die middä + * Draibr was zu dun hamm, sonnern ooch uff annere (eene Möjlischgeit die + * abzulehn isses dann, in @drv->init_device() NULL zurückzugehm). + * + * Diese Fungzschjoun gannsde nirschndwo sonst als in @drv->init_driver() + * uffrufn. + * \endthuringiansaxonian + * \french + * Informe la CDI implémentation que le pilote @drv ne peut qu'employer des + * appareils du type @pattern (qui se présentent au bus @pattern->bus_type). On + * peut appeler cette fonction à plus d'une fois. Tous les appareils qui se + * présentent au bus qui ne correspondent pas au type ne seront pas transmises + * au fonction @drv->init_device(). À l'inverse, cette fonction peut rendre NULL + * même que le pilote a appelé cdi_handle_bus_device(), c'est-à-dire que on peut + * appeler cette fonction avec un type qui ne corresponde pas seulement aux + * appareils qui le pilote peut employer, mais en plus au des autres (qui on + * peut renvoyer par faire @drv->init_device() rendre NULL). + * + * Cette fonction doit être appeler seulement en @drv->init_driver(). + * \endfrench + * \japanese + * ?????@drv?????@pattern?????????????????? + * CDI???????????????????@pattern->bus_type?????? + * ????????????????????????????????????? + * @drv->init_device()???????cdi_handle_bus_device()????????? + * @drv->init_device()??NULL?????????????????????? + * ????????????????????????????????? + * ??????@drv->init_device()??NULL??????????? + * + * @drv->init_driver()??????????????????????? + * \endjapanese + */ +void cdi_handle_bus_device(struct cdi_driver* drv, + struct cdi_bus_device_pattern* pattern); + #endif /*\@}*/ -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:54 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:54 +0100 Subject: [tyndur-devel] [PATCH v2 09/24] rtl8168b: Add driver In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-10-git-send-email-max@tyndur.org> + I cannot remember how well this driver works, but it is certainly better than having none at all. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- src/modules/cdi/rtl8168b/include/rtl8168b.h | 143 ++++++++++ src/modules/cdi/rtl8168b/main.c | 70 +++++ src/modules/cdi/rtl8168b/rtl8168b.c | 409 ++++++++++++++++++++++++++++ 3 files changed, 622 insertions(+) create mode 100644 src/modules/cdi/rtl8168b/include/rtl8168b.h create mode 100644 src/modules/cdi/rtl8168b/main.c create mode 100644 src/modules/cdi/rtl8168b/rtl8168b.c diff --git a/src/modules/cdi/rtl8168b/include/rtl8168b.h b/src/modules/cdi/rtl8168b/include/rtl8168b.h new file mode 100644 index 0000000..a038f7a --- /dev/null +++ b/src/modules/cdi/rtl8168b/include/rtl8168b.h @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2007 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Patrick Pokatilo. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _RTL8168B_H_ +#define _RTL8168B_H_ + +#include + +#include "cdi.h" +#include "cdi/mem.h" +#include "cdi/net.h" +#include "cdi/pci.h" + +#define REG_ID0 0x00 +#define REG_TRANSMIT_DESCRIPTORS 0x20 +#define REG_COMMAND 0x37 +#define REG_TRANSMIT_POLL 0x38 +#define REG_INTERRUPT_MASK 0x3C +#define REG_INTERRUPT_STATUS 0x3E +#define REG_TRANSMIT_CONFIGURATION 0x40 +#define REG_RECEIVE_CONFIGURATION 0x44 +#define REG_PHY_ACCESS 0x60 +#define REG_PHY_DATA 0x60 +#define REG_PHY_ADDRESS 0x62 +#define REG_PHY_RW 0x63 +#define REG_PHY_STATUS 0x6C +#define REG_RECV_MAX_SIZE 0xDA +#define REG_CP_COMMAND 0xE0 +#define REG_RECEIVE_DESCRIPTORS 0xE4 +#define REG_MAX_TRANS_SIZE 0xEC + +#define CR_RESET (1 << 4) +#define CR_RECEIVER_ENABLE (1 << 3) +#define CR_TRANSMITTER_ENABLE (1 << 2) + +#define CPCR_VLAN_DETAG (1 << 6) +#define CPCR_CHECKSUM_OFFLOAD (1 << 5) + +#define TPR_PACKET_WAITING (1 << 6) + +#define TCR_IFG_STANDARD (3 << 24) +#define TCR_MXDMA_512 (5 << 8) +#define TCR_MXDMA_1024 (6 << 8) +#define TCR_MXDMA_UNLIMITED (7 << 8) + +#define RCR_NO_FIFO_THRESHOLD (7 << 13) +#define RCR_MXDMA_512 (5 << 8) +#define RCR_MXDMA_1024 (6 << 8) +#define RCR_MXDMA_UNLIMITED (7 << 8) +#define RCR_ACCEPT_BROADCAST (1 << 3) +#define RCR_ACCEPT_MULTICAST (1 << 2) +#define RCR_ACCEPT_PHYS_MATCH (1 << 1) + +#define ISR_LINK_CHANGE (1 << 5) +#define ISR_RECEIVE_BUFFER_OVERFLOW (1 << 4) +#define ISR_TRANSMIT_OK (1 << 2) +#define ISR_RECEIVE_OK (1 << 0) + +#define PA_RW (1 << 31) + +#define PHY_REG_BMCR 0x00 + +#define PHY_BMCR_RESET (1 << 15) +#define PHY_BMCR_AUTO_NEGOTIATION (1 << 12) + +#define DC_OWN (1 << 31) +#define DC_EOR (1 << 30) +#define DC_FS (1 << 29) +#define DC_LS (1 << 28) +#define DC_LENGTH 0x3FFF + +#define PS_1000 (1 << 4) +#define PS_100 (1 << 3) +#define PS_10 (1 << 2) +#define PS_LINK (1 << 1) +#define PS_FULLDUP (1 << 0) + +#define PHYS(netcard, field) \ + (netcard->phys + offsetof(struct rtl8168b_device, field)) + +#define TX_BUFFER_SIZE (0x1000 & DC_LENGTH) +#define RX_BUFFER_SIZE (0x2000 & DC_LENGTH) + +#define TX_BUFFER_COUNT 16 +#define RX_BUFFER_COUNT 16 + +struct rtl8168b_descriptor +{ + volatile uint32_t command; + uint32_t vlan; + uint64_t address; +}; + +struct rtl8168b_device { + struct cdi_net_device net; + void* mmio; + + int tx_index; + int rx_index; + + struct cdi_mem_area* tx_buffer_area; + struct cdi_mem_area* rx_buffer_area; + + struct rtl8168b_descriptor* tx_buffer; + uintptr_t tx_buffer_phys; + struct cdi_mem_area* tx_area[TX_BUFFER_COUNT]; + + struct rtl8168b_descriptor* rx_buffer; + uintptr_t rx_buffer_phys; + struct cdi_mem_area* rx_area[RX_BUFFER_COUNT]; +}; + +struct cdi_device* rtl8168b_init_device(struct cdi_bus_data* bus_data); +void rtl8168b_remove_device(struct cdi_device* device); +void rtl8168b_send_packet(struct cdi_net_device* device, + void* data, size_t size); + +#endif diff --git a/src/modules/cdi/rtl8168b/main.c b/src/modules/cdi/rtl8168b/main.c new file mode 100644 index 0000000..1bbf8ff --- /dev/null +++ b/src/modules/cdi/rtl8168b/main.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2007 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Patrick Pokatilo. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include "cdi/net.h" +#include "cdi/pci.h" +#include "cdi/misc.h" + +#include "rtl8168b.h" + +#define DRIVER_NAME "rtl8168b" + +static struct cdi_net_driver rtl8168b_driver; + +static int rtl8168b_driver_init(void) +{ + cdi_net_driver_init(&rtl8168b_driver); + + return 0; +} + +static int rtl8168b_driver_destroy(void) +{ + cdi_net_driver_destroy(&rtl8168b_driver); + + return 0; +} + +static struct cdi_net_driver rtl8168b_driver = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_NETWORK, + .bus = CDI_PCI, + .init = rtl8168b_driver_init, + .destroy = rtl8168b_driver_destroy, + .init_device = rtl8168b_init_device, + .remove_device = rtl8168b_remove_device, + }, + + .send_packet = rtl8168b_send_packet, +}; + +CDI_DRIVER(DRIVER_NAME, rtl8168b_driver) diff --git a/src/modules/cdi/rtl8168b/rtl8168b.c b/src/modules/cdi/rtl8168b/rtl8168b.c new file mode 100644 index 0000000..ac88eb4 --- /dev/null +++ b/src/modules/cdi/rtl8168b/rtl8168b.c @@ -0,0 +1,409 @@ +/* + * Copyright (c) 2007 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Patrick Pokatilo and Max Reitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "rtl8168b.h" + +#define DEBUG_MSG(s) \ + printf("[RTL8111B/8168B] debug: %s() '%s'\n", __FUNCTION__, s) +#define DEVICE_MSG printf + +static void rtl8168b_handle_interrupt(struct cdi_device* device); + +static inline void write_register_8(struct rtl8168b_device* netcard, + uint8_t reg, uint8_t value) +{ + *(volatile uint8_t *)((uintptr_t)netcard->mmio + reg) = value; +} + +static inline void write_register_16(struct rtl8168b_device* netcard, + uint8_t reg, uint16_t value) +{ + *(volatile uint16_t *)((uintptr_t)netcard->mmio + reg) = value; +} + +static inline void write_register_32(struct rtl8168b_device* netcard, + uint8_t reg, uint32_t value) +{ + *(volatile uint32_t *)((uintptr_t)netcard->mmio + reg) = value; +} + +static inline void write_register_64(struct rtl8168b_device* netcard, + uint8_t reg, uint64_t value) +{ + *(volatile uint64_t *)((uintptr_t)netcard->mmio + reg) = value; +} + +static inline uint8_t read_register_8(struct rtl8168b_device* netcard, + uint8_t reg) +{ + return *(volatile uint8_t *)((uintptr_t)netcard->mmio + reg); +} + +static inline uint16_t read_register_16(struct rtl8168b_device* netcard, + uint8_t reg) +{ + return *(volatile uint16_t *)((uintptr_t)netcard->mmio + reg); +} + +static inline uint32_t read_register_32(struct rtl8168b_device* netcard, + uint8_t reg) +{ + return *(volatile uint32_t *)((uintptr_t)netcard->mmio + reg); +} + +static inline uint64_t read_register_64(struct rtl8168b_device* netcard, + uint8_t reg) +{ + return *(volatile uint64_t *)((uintptr_t)netcard->mmio + reg); +} + + +struct cdi_device* rtl8168b_init_device(struct cdi_bus_data* bus_data) +{ + struct cdi_pci_device* pci = (struct cdi_pci_device*) bus_data; + struct rtl8168b_device* netcard; + struct cdi_mem_area* buf; + + if ((pci->vendor_id != 0x10ec) || (pci->device_id != 0x8168)) { + return NULL; + } + + + buf = cdi_mem_alloc(sizeof(*netcard), CDI_MEM_VIRT_ONLY | CDI_MEM_NOINIT); + + if (!buf) { + return NULL; + } + + + netcard = buf->vaddr; + + memset(netcard, 0, sizeof(*netcard)); + + + netcard->net.dev.bus_data = (struct cdi_bus_data*) pci; + + DEBUG_MSG("Interrupthandler registrieren"); + cdi_register_irq(pci->irq, rtl8168b_handle_interrupt, &netcard->net.dev); + cdi_pci_alloc_memory(pci); + + + struct cdi_pci_resource* res; + for (int i = 0; (res = cdi_list_get(pci->resources, i)); i++) { + if ((res->type == CDI_PCI_MEMORY) && (res->index == 2)) { + netcard->mmio = res->address; + break; + } + } + + + DEBUG_MSG("Reset der Karte"); + write_register_16(netcard, REG_CP_COMMAND, CPCR_VLAN_DETAG); + + write_register_8(netcard, REG_COMMAND, CR_RESET); + + while ((read_register_8(netcard, REG_COMMAND) & CR_RESET) == CR_RESET); + + write_register_16(netcard, REG_RECV_MAX_SIZE, 1518); + write_register_16(netcard, REG_MAX_TRANS_SIZE, 0x0c); + + DEBUG_MSG("Aktiviere Rx/Tx"); + write_register_8(netcard, REG_COMMAND, + CR_RECEIVER_ENABLE | CR_TRANSMITTER_ENABLE); + + DEBUG_MSG("MAC-Adresse auslesen"); + netcard->net.mac = + read_register_64(netcard, REG_ID0) & 0xffffffffffffLL; + + DEBUG_MSG("Setze Interruptmaske"); + write_register_16(netcard, REG_INTERRUPT_STATUS, 0x4fff); + write_register_16(netcard, REG_INTERRUPT_MASK, 0x0025); + + DEBUG_MSG("Initialisiere PHY"); + write_register_32(netcard, REG_PHY_ACCESS, + PA_RW | (PHY_REG_BMCR << 16) | PHY_BMCR_RESET); + + while (read_register_32(netcard, REG_PHY_ACCESS) & PA_RW); + + do { + write_register_16(netcard, REG_PHY_ADDRESS, PHY_REG_BMCR); + + while (!(read_register_8(netcard, REG_PHY_RW) >> 7)); + } while (read_register_16(netcard, REG_PHY_DATA) & PHY_BMCR_RESET); + + write_register_32(netcard, REG_PHY_ACCESS, + PA_RW | (PHY_REG_BMCR << 16) | PHY_BMCR_AUTO_NEGOTIATION); + + while (read_register_32(netcard, REG_PHY_ACCESS) & PA_RW); + + + DEVICE_MSG("Lege Deskriptoren an"); + netcard->tx_buffer_area = + cdi_mem_alloc(TX_BUFFER_COUNT * sizeof(struct rtl8168b_descriptor), + CDI_MEM_PHYS_CONTIGUOUS | 8); + netcard->tx_buffer = netcard->tx_buffer_area->vaddr; + netcard->tx_buffer_phys = netcard->tx_buffer_area->paddr.items[0].start; + + netcard->rx_buffer_area = + cdi_mem_alloc(RX_BUFFER_COUNT * sizeof(struct rtl8168b_descriptor), + CDI_MEM_PHYS_CONTIGUOUS | 8); + netcard->rx_buffer = netcard->rx_buffer_area->vaddr; + netcard->rx_buffer_phys = netcard->rx_buffer_area->paddr.items[0].start; + + int i; + for (i = 0; i < RX_BUFFER_COUNT; i++) { + memset(&netcard->rx_buffer[i], 0, sizeof(struct rtl8168b_descriptor)); + + netcard->rx_area[i] = + cdi_mem_alloc(RX_BUFFER_SIZE, CDI_MEM_PHYS_CONTIGUOUS | 3); + netcard->rx_buffer[i].address = + netcard->rx_area[i]->paddr.items[0].start; + netcard->rx_buffer[i].command = DC_OWN | RX_BUFFER_SIZE; + + if (i == RX_BUFFER_COUNT - 1) { + netcard->rx_buffer[i].command |= DC_EOR; + } + } + + if (i == 0) { + DEVICE_MSG("[RTL8111B/8168B] Keine Buffer zum Empfangen von Daten!\n"); + write_register_8(netcard, REG_COMMAND, + read_register_8(netcard, REG_COMMAND) & ~CR_RECEIVER_ENABLE); + } + + for (i = 0; i < TX_BUFFER_COUNT; i++) { + memset(&netcard->tx_buffer[i], 0, sizeof(struct rtl8168b_descriptor)); + + netcard->tx_area[i] = + cdi_mem_alloc(TX_BUFFER_SIZE, CDI_MEM_PHYS_CONTIGUOUS | 3); + netcard->tx_buffer[i].address = + netcard->tx_area[i]->paddr.items[0].start; + netcard->tx_buffer[i].command = 0; + + if (i == TX_BUFFER_COUNT - 1) { + netcard->tx_buffer[i].command |= DC_EOR; + } + } + + if (i == 0) { + DEVICE_MSG("[RTL8111B/8168B] Keine Buffer zum Senden von Daten!\n"); + write_register_8(netcard, REG_COMMAND, + read_register_8(netcard, REG_COMMAND) & ~CR_TRANSMITTER_ENABLE); + } + + DEBUG_MSG("Initialisiere Buffer"); + write_register_64(netcard, REG_RECEIVE_DESCRIPTORS, + netcard->rx_buffer_phys); + write_register_64(netcard, REG_TRANSMIT_DESCRIPTORS, + netcard->tx_buffer_phys); + + DEBUG_MSG("Initialisiere RCR/TCR"); + write_register_32(netcard, REG_RECEIVE_CONFIGURATION, + RCR_MXDMA_UNLIMITED | RCR_NO_FIFO_THRESHOLD | RCR_ACCEPT_BROADCAST | + RCR_ACCEPT_PHYS_MATCH); + write_register_32(netcard, REG_TRANSMIT_CONFIGURATION, + TCR_MXDMA_UNLIMITED | TCR_IFG_STANDARD); + + cdi_net_device_init(&netcard->net); + + DEBUG_MSG("Fertig initialisiert"); + return &netcard->net.dev; +} + +void rtl8168b_remove_device(struct cdi_device* device) +{ + struct rtl8168b_device* netcard = (struct rtl8168b_device*) device; + int i; + + cdi_pci_free_memory((struct cdi_pci_device*) netcard->net.dev.bus_data); + + cdi_mem_free(netcard->tx_buffer_area); + cdi_mem_free(netcard->rx_buffer_area); + + for (i = 0; i < RX_BUFFER_COUNT; i++) { + netcard->rx_buffer[i].command &= 0x40000000; + netcard->rx_buffer[i].address = (uintptr_t) NULL; + + cdi_mem_free(netcard->rx_area[i]); + } + + for (i = 0; i < TX_BUFFER_COUNT; i++) { + netcard->tx_buffer[i].command &= 0x40000000; + netcard->tx_buffer[i].address = (uintptr_t) NULL; + + cdi_mem_free(netcard->tx_area[i]); + } +} + +void rtl8168b_send_packet(struct cdi_net_device* device, + void *data, size_t size) +{ + struct rtl8168b_device* netcard = (struct rtl8168b_device*) device; + + if (size > TX_BUFFER_SIZE) { + DEVICE_MSG("[RTL8111B/8168B] Paket zu groß!\n"); + + return; + } + + int i; + + do { + i = netcard->tx_index; + } while (!__sync_bool_compare_and_swap(&netcard->tx_index, i, + (i + 1) % TX_BUFFER_COUNT)); + + if (netcard->tx_buffer[i].command & DC_OWN) { + DEBUG_MSG("Tx-Buffer ist schon besetzt"); + return; + } + + + memcpy(netcard->tx_area[i]->vaddr, data, size); + netcard->tx_buffer[i].vlan = 0; + + DEVICE_MSG("[RTL8111B/8168B] Sende via Deskriptor %i\n", i); + + if (i == TX_BUFFER_COUNT - 1) { + netcard->tx_buffer[i].command = size | DC_FS | DC_LS | DC_OWN | DC_EOR; + } else { + netcard->tx_buffer[i].command = size | DC_FS | DC_LS | DC_OWN; + } + + write_register_8(netcard, REG_TRANSMIT_POLL, TPR_PACKET_WAITING); +} + + +static void rtl8168b_unown_rx_desc(struct rtl8168b_device* netcard, int i) +{ + if (i == RX_BUFFER_COUNT - 1) { + netcard->rx_buffer[i].command = RX_BUFFER_SIZE | DC_OWN | DC_EOR; + } else { + netcard->rx_buffer[i].command = RX_BUFFER_SIZE | DC_OWN; + } +} + +static void rtl8168b_handle_receive_ok(struct rtl8168b_device* netcard) +{ + int i; + + do { + i = netcard->rx_index; + } while (!__sync_bool_compare_and_swap(&netcard->rx_index, i, + (i + 1) % RX_BUFFER_COUNT)); + + if (netcard->rx_buffer[i].command & DC_OWN) { + netcard->rx_index = i; + + if (!(netcard->rx_buffer[0].command & DC_OWN)) { + // FIXME: Make atomic + i = 0; + netcard->rx_index = 1; + } else { + DEVICE_MSG("[RTL8111B/8168B] Empfangsdeskriptor sollte %i sein, " + "dort ist aber nichts.\n", i); + return; + } + } + + + size_t sz = netcard->rx_buffer[i].command & DC_LENGTH; + + DEVICE_MSG("[RTL8111B/8168B] Empfange %i B von Deskriptor %i\n", + (int)sz - 4, i); + + if (sz < 5) + { + rtl8168b_unown_rx_desc(netcard, i); + return; + } + + cdi_net_receive((struct cdi_net_device*) netcard, + netcard->rx_area[i]->vaddr, sz - 4); + + rtl8168b_unown_rx_desc(netcard, i); +} + +static void rtl8168b_handle_transmit_ok(struct rtl8168b_device* netcard) +{ + (void) netcard; +} + +static void rtl8168b_handle_link_change(struct rtl8168b_device* netcard) +{ + uint8_t status = read_register_8(netcard, REG_PHY_STATUS); + + if ((status & PS_LINK) != PS_LINK) { + DEVICE_MSG("[RTL8111B/8168B] Link down\n"); + } else { + uint16_t speed = (status & PS_FULLDUP) << 15; + + if (status & PS_1000) { + speed |= 1000; + } else if (status & PS_100) { + speed |= 100; + } else if (status & PS_10) { + speed |= 10; + } + + DEVICE_MSG("[RTL8111B/8168B] Link up (%uMb/s %s-duplex)\n", + speed & 0x7FFF, (speed & 0x8000) ? "Full" : "Half"); + } +} + +static void rtl8168b_handle_interrupt(struct cdi_device* device) +{ + struct rtl8168b_device* netcard = (struct rtl8168b_device*) device; + uint16_t isr = read_register_16(netcard, REG_INTERRUPT_STATUS); + + write_register_16(netcard, REG_INTERRUPT_STATUS, isr); + + DEVICE_MSG("[RTL8111B/8168B] Interrupt: %x\n", isr); + + if (isr & ISR_RECEIVE_OK) { + rtl8168b_handle_receive_ok(netcard); + } + + if (isr & ISR_TRANSMIT_OK) { + rtl8168b_handle_transmit_ok(netcard); + } + + if (isr & ISR_LINK_CHANGE) { + rtl8168b_handle_link_change(netcard); + } +} -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:55 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:55 +0100 Subject: [tyndur-devel] [PATCH v2 10/24] rtl8168b: Makefile.all In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-11-git-send-email-max@tyndur.org> + Füge Makefile.all hinzu, damit rtl8168b auch gebaut wird. Signed-off-by: Max Reitz --- src/modules/cdi/rtl8168b/Makefile.all | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/modules/cdi/rtl8168b/Makefile.all diff --git a/src/modules/cdi/rtl8168b/Makefile.all b/src/modules/cdi/rtl8168b/Makefile.all new file mode 100644 index 0000000..f677bda --- /dev/null +++ b/src/modules/cdi/rtl8168b/Makefile.all @@ -0,0 +1,6 @@ +shopt -s extglob +source $LOST_BUILDMK_ROOT/config.sh + +echo "LD $1/modules/rtl8168b" +$LOST_TOOLS_LD $LDSCRIPT -ortl8168b.mod *.o --start-group $2 --end-group +$LOST_TOOLS_STRIP -s rtl8168b.mod -o $1/modules/rtl8168b -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:56 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:56 +0100 Subject: [tyndur-devel] [PATCH v2 11/24] ata: Temporary buffer for ATAPI commands In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-12-git-send-email-max@tyndur.org> ! ATAPI commands need to be of size 12. However, general SCSI packets may have any length and thus the SCSI command to be sent has to be copied to a temporary buffer before it can be passed to the ATAPI device. Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/ata/atapi.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/modules/cdi/ata/atapi.c b/src/modules/cdi/ata/atapi.c index 31c7263..78f7f29 100644 --- a/src/modules/cdi/ata/atapi.c +++ b/src/modules/cdi/ata/atapi.c @@ -28,6 +28,7 @@ #include #include +#include #include "cdi.h" #include "cdi/storage.h" @@ -92,6 +93,12 @@ void atapi_remove_device(struct cdi_device* device) int atapi_request(struct cdi_scsi_device* scsi,struct cdi_scsi_packet* packet) { + uint8_t atapi_request[12]; + if (packet->cmdsize > 12) { + return -1; + } + memcpy(atapi_request, packet->command, sizeof(atapi_request)); + struct ata_device *dev = (struct ata_device*)scsi; struct ata_request request = { .dev = dev, @@ -111,9 +118,9 @@ int atapi_request(struct cdi_scsi_device* scsi,struct cdi_scsi_packet* packet) } }, .block_count = 1, - .block_size = packet->cmdsize, + .block_size = sizeof(atapi_request), .blocks_done = 0, - .buffer = packet->command, + .buffer = atapi_request, .error = 0 }; -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:57 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:57 +0100 Subject: [tyndur-devel] [PATCH v2 12/24] ata: Initialize ata_device to 0 In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-13-git-send-email-max@tyndur.org> * struct ata_device inherits cdi_storage_device or cdi_scsi_device. Since this driver does not know all of the fields contained therein for sure, the object has to be initialized to 0. Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/ata/device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/cdi/ata/device.c b/src/modules/cdi/ata/device.c index baedd72..db47832 100644 --- a/src/modules/cdi/ata/device.c +++ b/src/modules/cdi/ata/device.c @@ -222,7 +222,7 @@ void ata_init_controller(struct ata_controller* controller) // Jetzt werden die einzelnen geraete identifiziert. Hier ist es // anscheinend geschickter mit dem Slave zu beginnen. for (i = 1; i >= 0; i--) { - struct ata_device* dev = malloc(sizeof(*dev)); + struct ata_device* dev = calloc(1, sizeof(*dev)); dev->controller = controller; dev->id = i; dev->partition_list = cdi_list_create(); -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:58 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:58 +0100 Subject: [tyndur-devel] [PATCH v2 13/24] cdi/scsi: Neu geschrieben In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-14-git-send-email-max@tyndur.org> * Die týndur-Implementierung von cdi/scsi war im Prinzip nur zur Benutzung mit dem CDI-ATAPI-Treiber geeignet. Dieser Patch macht die Implementierung allgemeiner und auch für andere (zukünftige) SCSI-CDI-Treiber einsatzfähig. Signed-off-by: Max Reitz --- src/modules/cdi/lib/cdi.c | 26 ++- src/modules/cdi/lib/scsi/disk.c | 506 ++++++++++++++++++++++++++++++---------- 2 files changed, 404 insertions(+), 128 deletions(-) diff --git a/src/modules/cdi/lib/cdi.c b/src/modules/cdi/lib/cdi.c index 5d519b4..11b3009 100644 --- a/src/modules/cdi/lib/cdi.c +++ b/src/modules/cdi/lib/cdi.c @@ -19,13 +19,17 @@ #include "cdi.h" #include "cdi/audio.h" #include "cdi/fs.h" +#include "cdi/misc.h" #include "cdi/pci.h" +#include "cdi/scsi.h" #include "cdi/storage.h" extern void cdi_storage_driver_register(struct cdi_storage_driver* driver); extern void cdi_audio_driver_register(struct cdi_audio_driver* driver); extern void cdi_tyndur_net_device_init(struct cdi_device* device); +void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev); + static list_t* drivers = NULL; static void cdi_tyndur_run_drivers(void); @@ -149,6 +153,7 @@ static void cdi_tyndur_init_pci_devices(void) } if (device != NULL) { + cdi_osdep_new_device(driver, device); cdi_list_push(driver->devices, device); printf("cdi: %x.%x.%x: Benutze Treiber %s\n", pci->bus, pci->dev, pci->function, driver->name); @@ -214,9 +219,8 @@ static void cdi_tyndur_run_drivers(void) } } - /* Netzwerk ist schon initialisiert und SCSI hat kein RPC-Interface, - * sondern muss mit scsidisk gelinkt werden. */ - if (driver->type != CDI_NETWORK && driver->type != CDI_SCSI) { + /* Netzwerk ist schon initialisiert */ + if (driver->type != CDI_NETWORK) { init_service_register((char*) driver->name); } } @@ -277,6 +281,22 @@ list_t* cdi_tyndur_get_drivers(void) return drivers; } +void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev) +{ + if (!dev) { + return; + } + + dev->driver = drv; + cdi_list_push(drv->devices, dev); + + switch ((int)drv->type) { + case CDI_SCSI: + cdi_scsi_device_init(CDI_UPCAST(dev, struct cdi_scsi_device, dev)); + break; + } +} + /** * Wenn main nicht von einem Treiber ueberschrieben wird, ist hier der * Einsprungspunkt. Die Standardfunktion ruft nur cdi_init() auf. Treiber, die diff --git a/src/modules/cdi/lib/scsi/disk.c b/src/modules/cdi/lib/scsi/disk.c index 49f408e..2f2b7bd 100644 --- a/src/modules/cdi/lib/scsi/disk.c +++ b/src/modules/cdi/lib/scsi/disk.c @@ -1,6 +1,7 @@ /* * Copyright (C) 2008 Mathias Gottschlag * Copyright (C) 2009 Kevin Wolf + * Copyright (C) 2015 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 @@ -26,14 +27,10 @@ #include #include +#include #include #include -#define big_endian_word(x) ((((x) & 0xFF) << 8) | (((x) & 0xFF00) >> 8)) -#define big_endian_dword(x) \ - ((big_endian_word((x) & 0xFFFF) << 16) | \ - (big_endian_word((x) >> 16))) - static int cdi_scsi_disk_read(struct cdi_storage_device* device, uint64_t start, uint64_t count, void* buffer); @@ -44,84 +41,272 @@ struct cdi_storage_driver cdi_scsi_disk_driver = { .drv = { .name = "scsidisk", .type = CDI_STORAGE, + .bus = CDI_SCSI, }, .read_blocks = cdi_scsi_disk_read, .write_blocks = cdi_scsi_disk_write, }; -// TODO: This is kind of incomplete... -.- -// FIXME Das ist alles sehr CD-ROM-spezifisch hartkodiert... -union cdrom_command +typedef struct scsi_storage_device { + struct cdi_storage_device dev; + struct cdi_scsi_device* backend; + int lun; +} scsi_storage_device_t; + + +#define SCSI_REQUEST_SENSE 0x03 +#define SCSI_INQUIRY 0x12 +#define SCSI_START_STOP_UNIT 0x1b +#define SCSI_READ_CAPACITY 0x25 +#define SCSI_READ10 0x28 +#define SCSI_WRITE10 0x2a +#define SCSI_READ16 0x88 +#define SCSI_WRITE16 0x8a + +struct scsi_cmd { + uint8_t operation; +} __attribute__((packed)); + +struct scsi_cmd_request_sense { + struct scsi_cmd cmd; + uint8_t rsvd1; + uint16_t rsvd2; + uint8_t alloc_len; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_inquiry { + struct scsi_cmd cmd; + uint8_t rsvd; + uint8_t page; + uint16_t alloc_len; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_start_stop_unit { + struct scsi_cmd cmd; + uint8_t immed; + uint8_t rsvd; + uint8_t power_condition_modifier; + uint8_t flags_power_condition; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_read_capacity { + struct scsi_cmd cmd; + uint8_t rsvd1; + uint32_t lba; + uint32_t rsvd2; +} __attribute__((packed)); + +struct scsi_cmd_read10 { + struct scsi_cmd cmd; + uint8_t rsvd1; + uint32_t lba; + uint8_t rsvd2; + uint16_t length; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_write10 { + struct scsi_cmd cmd; + uint8_t rsvd1; + uint32_t lba; + uint8_t rsvd2; + uint16_t length; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_read16 { + struct scsi_cmd cmd; + uint8_t flags; + uint64_t lba; + uint32_t length; + uint8_t group; + uint8_t control; +} __attribute__((packed)); + +struct scsi_cmd_write16 { + struct scsi_cmd cmd; + uint8_t flags; + uint64_t lba; + uint32_t length; + uint8_t group; + uint8_t control; +} __attribute__((packed)); + +struct scsi_sense { + uint8_t resp_code; + uint8_t segment; + uint8_t flags; + uint32_t information; + uint8_t add_sense_length; + uint32_t cmd_spec_info; + uint8_t add_sense_code; + uint8_t add_sense_code_qual; + uint8_t field_replacable_unit_code; + uint8_t sense_key_spec1; + uint8_t sense_key_spec2; + uint8_t sense_key_spec3; +} __attribute__((packed)); + +struct scsi_inq_answer { + uint8_t dev_type; + uint8_t type_mod; + uint8_t version; + uint8_t resp_data_format; + uint8_t add_length; + uint8_t flags1, flags2, flags3; + char vendor[8]; + char product[16]; + uint32_t revision; + char serial[8]; + uint8_t vendor_unique[12]; + uint8_t clocking; + uint8_t rsvd1; + uint16_t version_desc[8]; + uint8_t rsvd2[22]; +} __attribute__((packed)); + + +static int scsi_read10(scsi_storage_device_t* dev, uint32_t sector, + uint16_t nb_sectors, char* buffer) { - struct - { - uint8_t opcode; - uint8_t reserved0; - uint32_t address; - uint8_t reserved1; - uint16_t length; - uint8_t reserved2; - } __attribute__ ((packed)) dfl; - struct - { - uint8_t opcode; - uint8_t reserved0; - uint32_t address; - uint32_t length; - uint16_t reserved1; - } __attribute__ ((packed)) ext; -}; - -static int cdrom_read_partial( - struct cdi_scsi_device *device, uint32_t sector, uint64_t nb_sectors, - char *buffer) -{ - struct cdi_scsi_packet packet; - struct cdi_scsi_driver* drv = (struct cdi_scsi_driver*) device->dev.driver; - union cdrom_command* cmd = (union cdrom_command*) &packet.command; + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + struct scsi_cmd_read10 r10 = { + .cmd = { + .operation = SCSI_READ10 + }, + .lba = cdi_cpu_to_be32(sector), + .length = cdi_cpu_to_be16(nb_sectors) + }; + struct cdi_scsi_packet pkt = { + .lun = dev->lun, + .buffer = buffer, + .bufsize = (size_t)nb_sectors * dev->dev.block_size, + .direction = CDI_SCSI_READ, + .cmdsize = sizeof(r10) + }; + memcpy(pkt.command, &r10, sizeof(r10)); + + if (drv->request(dev->backend, &pkt)) { + return -1; + } - nb_sectors = (uint32_t) nb_sectors; + return 0; +} - memset(&packet, 0, sizeof(packet)); - packet.direction = CDI_SCSI_READ; - packet.buffer = buffer; - packet.bufsize = 2048 * nb_sectors; - packet.cmdsize = 12; +static int scsi_write10(scsi_storage_device_t* dev, uint32_t sector, + uint16_t nb_sectors, char* buffer) +{ + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + struct scsi_cmd_write10 w10 = { + .cmd = { + .operation = SCSI_WRITE10 + }, + .lba = cdi_cpu_to_be32(sector), + .length = cdi_cpu_to_be16(nb_sectors) + }; + struct cdi_scsi_packet pkt = { + .lun = dev->lun, + .buffer = buffer, + .bufsize = (size_t)nb_sectors * dev->dev.block_size, + .direction = CDI_SCSI_WRITE, + .cmdsize = sizeof(w10) + }; + memcpy(pkt.command, &w10, sizeof(w10)); + + if (drv->request(dev->backend, &pkt)) { + return -1; + } - cmd->ext.opcode = 0xA8; - cmd->ext.address = big_endian_dword(sector); - cmd->ext.length = big_endian_dword(nb_sectors); + return 0; +} - if (drv->request(device, &packet)) { +static int scsi_read16(scsi_storage_device_t* dev, uint64_t sector, + uint32_t nb_sectors, char* buffer) +{ + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + struct scsi_cmd_read16 r16 = { + .cmd = { + .operation = SCSI_READ16 + }, + .lba = cdi_cpu_to_be64(sector), + .length = cdi_cpu_to_be32(nb_sectors) + }; + struct cdi_scsi_packet pkt = { + .lun = dev->lun, + .buffer = buffer, + .bufsize = (size_t)nb_sectors * dev->dev.block_size, + .direction = CDI_SCSI_READ, + .cmdsize = sizeof(r16) + }; + memcpy(pkt.command, &r16, sizeof(r16)); + + if (drv->request(dev->backend, &pkt)) { return -1; } - return nb_sectors; + return 0; } -static int cdrom_capacity(struct cdi_scsi_device *device, - uint32_t* num_sectors, uint32_t* sector_size) +static int scsi_write16(scsi_storage_device_t* dev, uint64_t sector, + uint32_t nb_sectors, char* buffer) { - uint32_t buffer[32]; - struct cdi_scsi_packet packet; - struct cdi_scsi_driver* drv = (struct cdi_scsi_driver*) device->dev.driver; - union cdrom_command* cmd = (union cdrom_command*) &packet.command; - - memset(&packet, 0, sizeof(packet)); - packet.direction = CDI_SCSI_READ; - packet.buffer = buffer; - packet.bufsize = sizeof(buffer); - packet.cmdsize = 12; + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + struct scsi_cmd_write16 w16 = { + .cmd = { + .operation = SCSI_WRITE16 + }, + .lba = cdi_cpu_to_be64(sector), + .length = cdi_cpu_to_be32(nb_sectors) + }; + struct cdi_scsi_packet pkt = { + .lun = dev->lun, + .buffer = buffer, + .bufsize = (size_t)nb_sectors * dev->dev.block_size, + .direction = CDI_SCSI_WRITE, + .cmdsize = sizeof(w16) + }; + memcpy(pkt.command, &w16, sizeof(w16)); + + if (drv->request(dev->backend, &pkt)) { + return -1; + } - cmd->ext.opcode = 0x25; + return 0; +} - if (drv->request(device, &packet)) { +static int scsi_get_capacity(scsi_storage_device_t* dev) +{ + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + uint32_t capacity[2] = { 0 }; + struct scsi_cmd_read_capacity read_cap = { + .cmd = { + .operation = SCSI_READ_CAPACITY + } + }; + struct cdi_scsi_packet pkt = { + .lun = dev->lun, + .buffer = capacity, + .bufsize = sizeof(capacity), + .direction = CDI_SCSI_READ, + .cmdsize = sizeof(read_cap) + }; + memcpy(pkt.command, &read_cap, sizeof(read_cap)); + + if (drv->request(dev->backend, &pkt)) { return -1; } - *num_sectors = big_endian_dword(buffer[0]); - *sector_size = big_endian_dword(buffer[1]); + dev->dev.block_count = (uint64_t)cdi_be32_to_cpu(capacity[0]) + 1; + dev->dev.block_size = cdi_be32_to_cpu(capacity[1]); return 0; } @@ -160,27 +345,28 @@ static void cdrom_sense(FsSCSIDevice *device, uint32_t index) } #endif -int cdrom_init(struct cdi_scsi_device* device) +static int scsi_init(scsi_storage_device_t* dev) { - // Send inquiry command - struct cdi_scsi_driver* drv = (struct cdi_scsi_driver*) device->dev.driver; - struct cdi_scsi_packet packet; - uint32_t status; - unsigned char inqdata[96]; - - memset(&packet, 0, sizeof(packet)); - memset(inqdata, 0xF0, sizeof(inqdata)); - - packet.direction = CDI_SCSI_READ; - packet.buffer = inqdata; - packet.bufsize = 96; - packet.cmdsize = 12; - - ((union cdrom_command*)packet.command)->dfl.opcode = 0x12; - packet.command[4] = 96; - status = drv->request(device, &packet); - if (status) - { + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, + struct cdi_scsi_driver, drv); + struct scsi_inq_answer inqdata; + + struct scsi_cmd_inquiry inq = { + .cmd = { + .operation = SCSI_INQUIRY, + }, + .alloc_len = cdi_cpu_to_be16(sizeof(inqdata)) + }; + struct cdi_scsi_packet inq_pkt = { + .lun = dev->lun, + .buffer = &inqdata, + .bufsize = sizeof(inqdata), + .direction = CDI_SCSI_READ, + .cmdsize = sizeof(inq) + }; + memcpy(inq_pkt.command, &inq, sizeof(inq)); + + if (drv->request(dev->backend, &inq_pkt)) { return -1; } @@ -189,56 +375,109 @@ int cdrom_init(struct cdi_scsi_device* device) return KE_ERROR_UNKNOWN; #endif - // Start drive - packet.direction = CDI_SCSI_NODATA; - ((union cdrom_command*)packet.command)->dfl.opcode = 0x1B; - packet.buffer = 0; - packet.bufsize = 0; - packet.command[4] = 0; - packet.command[4] = 3; - packet.cmdsize = 12; - - status = drv->request(device, &packet); - if (status) - { - printf("cdrom %d: Start failed, %x\n", index, status); - return -1; + struct scsi_cmd_start_stop_unit ssu = { + .cmd = { + .operation = SCSI_START_STOP_UNIT, + }, + .flags_power_condition = 0x3 // LOEJ+START + }; + struct cdi_scsi_packet ssu_pkt = { + .lun = dev->lun, + .direction = CDI_SCSI_NODATA, + .cmdsize = sizeof(ssu) + }; + memcpy(ssu_pkt.command, &ssu, sizeof(ssu)); + + if ((inqdata.dev_type & 0x1f) == 0x05) { // CD/DVD + if (drv->request(dev->backend, &ssu_pkt)) { + printf("cdrom: Start failed\n"); + return -1; + } + dev->dev.block_size = 2048; + } else { + dev->dev.block_size = 512; } return 0; } -static int cdi_scsi_disk_read(struct cdi_storage_device* device, - uint64_t start, uint64_t count, void* buffer) +static int cdi_scsi_disk_read(struct cdi_storage_device* dev, uint64_t start, + uint64_t count, void* buffer) { - struct cdi_scsi_device* dev = device->dev.backdev; + scsi_storage_device_t* scsi_dev = CDI_UPCAST(dev, scsi_storage_device_t, + dev); + bool read16 = (start >> 32) || ((start + count) >> 32); while (count > 0) { int ret; + uint32_t it_count; + + if (read16) { + it_count = count > 0xffffffffu ? 0xffffffffu : count; + } else { + it_count = count > 0xffff ? 0xffff : count; + } - ret = cdrom_read_partial(dev, start, count, buffer); - if (ret <= 0) { + if (read16) { + ret = scsi_read16(scsi_dev, start, it_count, buffer); + } else { + ret = scsi_read10(scsi_dev, start, it_count, buffer); + } + if (ret < 0) { return -1; } - start += ret; - buffer += ret; - count -= ret; + + start += it_count; + count -= it_count; + + buffer = (void*) ((uintptr_t)buffer + it_count * dev->block_size); } return 0; } -static int cdi_scsi_disk_write(struct cdi_storage_device* device, - uint64_t start, uint64_t count, void* buffer) +static int cdi_scsi_disk_write(struct cdi_storage_device* dev, uint64_t start, + uint64_t count, void* buffer) { - return -1; + scsi_storage_device_t* scsi_dev = CDI_UPCAST(dev, scsi_storage_device_t, + dev); + bool write16 = (start >> 32) || ((start + count) >> 32); + + while (count > 0) { + int ret; + uint32_t it_count; + + if (write16) { + it_count = count > 0xffffffffu ? 0xffffffffu : count; + } else { + it_count = count > 0xffff ? 0xffff : count; + } + + if (write16) { + ret = scsi_write16(scsi_dev, start, it_count, buffer); + } else { + ret = scsi_write10(scsi_dev, start, it_count, buffer); + } + if (ret < 0) { + return -1; + } + + start += it_count; + count -= it_count; + + buffer = (void*) ((uintptr_t)buffer + it_count * dev->block_size); + } + + return 0; } -static void driver_init(void) +static void driver_init(struct cdi_driver* drv) { static int initialised = 0; if (!initialised) { + cdi_scsi_disk_driver.drv.name = strdup(drv->name); + cdi_driver_register(&cdi_scsi_disk_driver.drv); cdi_storage_driver_init(&cdi_scsi_disk_driver); } @@ -246,30 +485,47 @@ static void driver_init(void) int cdi_scsi_disk_init(struct cdi_scsi_device* device) { - struct cdi_storage_device* frontdev; - uint32_t num_sectors = 0, sector_size = 0; + int luns = device->lun_count ?: 1; - driver_init(); + driver_init(device->dev.driver); - if (cdrom_init(device)) { - return -1; - } + for (int lun = 0; lun < luns; lun++) { + scsi_storage_device_t* dev = calloc(1, sizeof(*dev)); + bool inquiry_failed; + + dev->backend = device; + dev->lun = lun; + + inquiry_failed = scsi_init(dev) < 0; - frontdev = calloc(1, sizeof(*frontdev)); - frontdev->dev.driver = (struct cdi_driver*) &cdi_scsi_disk_driver; - frontdev->dev.name = device->dev.name; + if (scsi_get_capacity(dev) < 0) { + if (inquiry_failed) { + /* We're fine as long as either INQUIRY or READ_CAPACITY work - + * but if both fail, there's not a lot we can do. */ + fprintf(stderr, + "[scsi] Failed to initialize LUN %i of device %s\n", + lun, device->dev.name); - // Groesse des Mediums bestimmen. Falls keins eingelegt ist, brauchen wir - // trotzdem zumindest eine Sektorgroesse. - cdrom_capacity(device, &num_sectors, §or_size); - frontdev->block_size = sector_size ? sector_size : 2048; - frontdev->block_count = num_sectors; + free(dev); + continue; + } - frontdev->dev.backdev = device; - device->frontdev = frontdev; + /* Apparently this is common for CD-ROM devices; at least the old + * SCSI implementation didn't care about it. A default block_size + * has been set by scsi_init(). */ + dev->dev.block_count = 0xffffffffu; + } + + dev->dev.dev.driver = (struct cdi_driver*) &cdi_scsi_disk_driver; + if (luns > 1) { + asprintf((char**) &dev->dev.dev.name, "%s_l%i", + device->dev.name, lun); + } else { + dev->dev.dev.name = strdup(device->dev.name); + } - // LostIO-Verzeichnisknoten anlegen - cdi_storage_device_init(frontdev); + cdi_storage_device_init(&dev->dev); + } return 0; } -- 2.6.3 From max at tyndur.org Sun Dec 13 01:10:59 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:10:59 +0100 Subject: [tyndur-devel] [PATCH v2 14/24] lock.h: locked_decrement() In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-15-git-send-email-max@tyndur.org> + Wenn man schon locked_increment() hat, dann macht sich locked_decrement() auch ganz gut. Signed-off-by: Max Reitz --- src/include/lock.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/include/lock.h b/src/include/lock.h index fc73b3f..89ddb4a 100644 --- a/src/include/lock.h +++ b/src/include/lock.h @@ -63,6 +63,16 @@ static inline uint32_t locked_increment(volatile uint32_t* var) return __sync_fetch_and_add(var, 1); } +/** + * Eine Variable dekrementieren, während der Bus gesperrt ist. + * + * @param var Pointer auf die zu dekrementierende Variable + */ +static inline uint32_t locked_decrement(volatile uint32_t* var) +{ + return __sync_fetch_and_sub(var, 1); +} + /* Mutexe **************************************/ -- 2.6.3 From max at tyndur.org Sun Dec 13 01:11:00 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:11:00 +0100 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_v2_15/24=5D_servmgr=3A_Workarou?= =?utf-8?q?nd_f=C3=BCr_zyklische_Abh=C3=A4ngigkeiten?= In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-16-git-send-email-max@tyndur.org> ! Es kann passieren, dass Service A auf B wartet, der auf C wartet. Dann melden sich diese beide beim servmgr, der dann für B darauf warten soll, dass B gestartet ist, und für A, dass B fertig ist. Da týndur leider keine Popupthreads für IPC benutzt, existiert aber nur ein wartender Thread. Wenn A später als B angefragt hat, wird also A ewig auf B warten, da B nie weiterlaufen kann, auch wenn C fertig ist. Ein einfacher Workaround ist es, zu einem Zeitpunkt immer nur einem anfragenden Service/Prozess das Privileg zu gestatten, den servmgr selbst zu benutzen, um zu warten. Alle anderen bekommen ein EAGAIN, wenn der Zielservice noch nicht angemeldet ist, und müssen das Warten selber übernehmen (hier als wiederholte Anfragen bei servmgr implementiert). Signed-off-by: Max Reitz --- src/modules/lib/servmgr.c | 11 +++++++++-- src/modules/servmgr/rpcif.c | 22 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/modules/lib/servmgr.c b/src/modules/lib/servmgr.c index 3909350..429be73 100644 --- a/src/modules/lib/servmgr.c +++ b/src/modules/lib/servmgr.c @@ -26,6 +26,7 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -36,8 +37,14 @@ static pid_t servmgr_pid(void); bool servmgr_need(const char* service_name) { - return rpc_get_dword(servmgr_pid(), "NEEDSERV", strlen(service_name) + 1, - (char*) service_name); + int result; + + do { + result = rpc_get_int(servmgr_pid(), "NEEDSERV", + strlen(service_name) + 1, (char*) service_name); + } while (result == -EAGAIN); + + return result == 1; } /** diff --git a/src/modules/servmgr/rpcif.c b/src/modules/servmgr/rpcif.c index 6bbce79..645c8c5 100644 --- a/src/modules/servmgr/rpcif.c +++ b/src/modules/servmgr/rpcif.c @@ -40,6 +40,8 @@ #include #include #include +#include +#include #include "servmgr.h" static void rpc_needserv(pid_t pid, uint32_t cid, size_t data_size, void* data); @@ -64,7 +66,7 @@ static void rpc_needserv(pid_t pid, uint32_t cid, size_t data_size, void* data) // Pruefen ob der Name nullterminiert ist if (!data_size || (name[data_size - 1] != 0)) { - rpc_send_dword_response(pid, cid, 0); + rpc_send_int_response(pid, cid, 0); return; } @@ -73,10 +75,26 @@ static void rpc_needserv(pid_t pid, uint32_t cid, size_t data_size, void* data) } else { // Noch nicht konfiguriert. Jetzt bleibt nur hoffen, dass der Dienst // schon geladen wurde, sich aber noch nicht registriert hat. + + static volatile uint32_t semaphore; + if (locked_increment(&semaphore) > 0) { + locked_decrement(&semaphore); + // Wir können nicht mehrere gleichzeitig warten lassen, weil das sonst + // zyklische Abhängigkeiten geben könnte + if (service_running(name)) { + rpc_send_int_response(pid, cid, true); + } else { + rpc_send_int_response(pid, cid, -EAGAIN); + } + return; + } + result = service_wait(name, 10000); + + locked_decrement(&semaphore); } - rpc_send_dword_response(pid, cid, result); + rpc_send_int_response(pid, cid, result); } /** -- 2.6.3 From max at tyndur.org Sun Dec 13 01:11:01 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:11:01 +0100 Subject: [tyndur-devel] [PATCH v2 16/24] cdi/misc: Implementiere cdi_elapsed_ms() In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-17-git-send-email-max@tyndur.org> + Implementiere die cdi/misc-Funktion cdi_elapsed_ms(). Signed-off-by: Max Reitz --- src/modules/cdi/lib/misc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/modules/cdi/lib/misc.c b/src/modules/cdi/lib/misc.c index 1b965af..2f34d87 100644 --- a/src/modules/cdi/lib/misc.c +++ b/src/modules/cdi/lib/misc.c @@ -183,3 +183,7 @@ void cdi_sleep_ms(uint32_t ms) } +uint64_t cdi_elapsed_ms(void) +{ + return get_tick_count() / 1000; +} -- 2.6.3 From max at tyndur.org Sun Dec 13 01:11:02 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:11:02 +0100 Subject: [tyndur-devel] [PATCH v2 17/24] cdi/usb: Add headers In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-18-git-send-email-max@tyndur.org> + Add headers to be used by USB host controller drivers, the USB bus driver, and USB device drivers. These headers are still lacking functions for periodic transactions (interrupt/isochronous), hotplugging, suspend/resume, etc. pp.. Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/include/cdi-osdep.h | 21 +++ src/modules/cdi/include/cdi/usb-structures.h | 174 ++++++++++++++++++++++ src/modules/cdi/include/cdi/usb.h | 211 +++++++++++++++++++++++++++ src/modules/cdi/include/cdi/usb_hcd.h | 168 +++++++++++++++++++++ 4 files changed, 574 insertions(+) create mode 100644 src/modules/cdi/include/cdi/usb-structures.h create mode 100644 src/modules/cdi/include/cdi/usb.h create mode 100644 src/modules/cdi/include/cdi/usb_hcd.h diff --git a/src/modules/cdi/include/cdi-osdep.h b/src/modules/cdi/include/cdi-osdep.h index 94eba73..3ec20a1 100644 --- a/src/modules/cdi/include/cdi-osdep.h +++ b/src/modules/cdi/include/cdi-osdep.h @@ -58,6 +58,27 @@ typedef struct /** * \german + * OS-spezifische Daten zu USB-Geräten. + * \endgerman + * \english + * OS-specific USB device data. + * \endenglish + * \thuringiansaxonian + * Vonnä jäwailschn Bedriebssysdääm abhängsche Dåhdn, nu für so USB-Geräide. + * \endthuringiansaxonian + * \french + * Des données tributaires du système d'exploitation pour des périphériques + * USB. + * \endfrench + * \japanese + * USB??????????OS???????? + * \endjapanese + */ +typedef struct { +} cdi_usb_device_osdep; + +/** + * \german * OS-spezifische Daten fuer einen ISA-DMA-Zugriff * \endgerman * \english diff --git a/src/modules/cdi/include/cdi/usb-structures.h b/src/modules/cdi/include/cdi/usb-structures.h new file mode 100644 index 0000000..fecb609 --- /dev/null +++ b/src/modules/cdi/include/cdi/usb-structures.h @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2015 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. + */ + +/** + * This file contains some standard USB structures such as descriptors. + */ + +#ifndef _CDI_USB_STRUCTURES_H_ +#define _CDI_USB_STRUCTURES_H_ + +#include + + +struct cdi_usb_device_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint16_t bcd_usb; + uint8_t b_device_class; + uint8_t b_device_sub_class; + uint8_t b_device_protocol; + uint8_t b_max_packet_size_0; + uint16_t id_vendor; + uint16_t id_product; + uint16_t bcd_device; + uint8_t i_manufacturer; + uint8_t i_product; + uint8_t i_serial_number; + uint8_t b_num_configurations; +} __attribute__((packed)); + +struct cdi_usb_configuration_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint16_t w_total_length; + uint8_t b_num_interfaces; + uint8_t b_configuration_value; + uint8_t i_configuration; + uint8_t bm_attributes; + uint8_t b_max_power; +} __attribute__((packed)); + +struct cdi_usb_string_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint16_t b_string[127]; +} __attribute__((packed)); + +struct cdi_usb_interface_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint8_t b_interface_number; + uint8_t b_alternate_setting; + uint8_t b_num_endpoints; + uint8_t b_interface_class; + uint8_t b_interface_sub_class; + uint8_t b_interface_protocol; + uint8_t i_interface; +} __attribute__((packed)); + + +struct cdi_usb_endpoint_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint8_t b_endpoint_address; + uint8_t bm_attributes; + uint16_t w_max_packet_size; + uint8_t b_interval; +} __attribute__((packed)); + +/// cdi_usb_endpoint_descriptor.b_endpoint_address +#define CDI_USB_EPDSC_EPADR_ID(x) ((x) & 0xf) +#define CDI_USB_EPDSC_EPADR_DIR (1 << 7) // IN if set + +/// cdi_usb_endpoint_descriptor.bm_attributes +#define CDI_USB_EPDSC_ATTR_XFER_TYPE_MASK 0x03 // Transfer Type mask +#define CDI_USB_EPDSC_ATTR_CONTROL 0x00 +#define CDI_USB_EPDSC_ATTR_ISOCHRONOUS 0x01 +#define CDI_USB_EPDSC_ATTR_BULK 0x02 +#define CDI_USB_EPDSC_ATTR_INTERRUPT 0x03 +#define CDI_USB_EPDSC_ATTR_SYNC_TYPE_MASK 0x0c // Synchronization Type mask +#define CDI_USB_EPDSC_ATTR_NO_SYNC 0x00 +#define CDI_USB_EPDSC_ATTR_ASYNC 0x04 +#define CDI_USB_EPDSC_ATTR_ADAPTIVE 0x08 +#define CDI_USB_EPDSC_ATTR_SYNC 0x0c +#define CDI_USB_EPDSC_ATTR_USAGE_TYPE_MASK 0x30 // Usage Type mask +#define CDI_USB_EPDSC_ATTR_DATA 0x00 +#define CDI_USB_EPDSC_ATTR_FEEDBACK 0x10 +#define CDI_USB_EPDSC_ATTR_IMPL_FB_DATA 0x20 // Implicit Feedback Data + +/// cdi_usb_endpoint_descriptor.w_max_packet_size +#define CDI_USB_EPDSC_MPS_MPS(x) ((x) & 0x7ff) + + +struct cdi_usb_setup_packet { + uint8_t bm_request_type; + uint8_t b_request; + uint16_t w_value; + uint16_t w_index; + uint16_t w_length; +} __attribute__((packed)); + +/// Values constituting cdi_usb_setup_packet.bm_request_type +#define CDI_USB_CREQ_DEVICE 0 +#define CDI_USB_CREQ_INTERFACE 1 +#define CDI_USB_CREQ_ENDPOINT 2 +#define CDI_USB_CREQ_OTHER 3 +#define CDI_USB_CREQ_CLASS (1 << 5) +#define CDI_USB_CREQ_OUT (0 << 7) +#define CDI_USB_CREQ_IN (1 << 7) + +/// Control request numbers (cdi_usb_setup_packet.b_request) +enum { + CDI_USB_CREQ_GET_STATUS = 0, + CDI_USB_CREQ_CLEAR_FEATURE = 1, + CDI_USB_CREQ_SET_FEATURE = 3, + CDI_USB_CREQ_SET_ADDRESS = 5, + CDI_USB_CREQ_GET_DESCRIPTOR = 6, + CDI_USB_CREQ_SET_DESCRIPTOR = 7, + CDI_USB_CREQ_GET_CONFIGURATION = 8, + CDI_USB_CREQ_SET_CONFIGURATION = 9, + CDI_USB_CREQ_GET_INTERFACE = 10, + CDI_USB_CREQ_SET_INTERFACE = 11, + CDI_USB_CREQ_SYNCH_FRAME = 12, +}; + +/// Descriptor IDs +enum { + CDI_USB_DESC_DEVICE = 1, + CDI_USB_DESC_CONFIGURATION = 2, + CDI_USB_DESC_STRING = 3, + CDI_USB_DESC_INTERFACE = 4, + CDI_USB_DESC_ENDPOINT = 5, +}; + +/// USB class codes. Defined here because they are not part of their respective +/// specifications. + +/// USB class codes on the device level +#define CDI_USB_DEVCLS_NONE 0x00 // No device-wide class, use interfaces +#define CDI_USB_DEVCLS_CCDCC 0x02 // Communications and CDC Control +#define CDI_USB_DEVCLS_HUB 0x09 // Hub +#define CDI_USB_DEVCLS_BILLBD 0x11 // Billboard Device +#define CDI_USB_DEVCLS_DIAG 0xdc // Diagnostic Device +#define CDI_USB_DEVCLS_MISC 0xef // Miscellaneous +#define CDI_USB_DEVCLS_VENDOR 0xff // Vendor-specific + +/// USB class codes on the interface level +#define CDI_USB_IFCCLS_AUDIO 0x01 // Audio +#define CDI_USB_IFCCLS_CCDCC CDI_USB_DEVCLS_CCDCC +#define CDI_USB_IFCCLS_HID 0x03 // Human Interface Device +#define CDI_USB_IFCCLS_PHYS 0x05 // Physical +#define CDI_USB_IFCCLS_IMAGE 0x06 // Image +#define CDI_USB_IFCCLS_PRINTER 0x07 // Printer +#define CDI_USB_IFCCLS_STORAGE 0x08 // Mass Storage +#define CDI_USB_IFCCLS_CDCDATA 0x0a // CDC Data +#define CDI_USB_IFCCLS_SMARTCD 0x0b // Smart Card +#define CDI_USB_IFCCLS_CNTTSEC 0x0d // Content Security +#define CDI_USB_IFCCLS_VIDEO 0x0e // Video +#define CDI_USB_IFCCLS_HEALTH 0x0f // Personal Healthcare +#define CDI_USB_IFCCLS_AV 0x10 // Audio/Video +#define CDI_USB_IFCCLS_DIAG CDI_USB_DEVCLS_DIAG +#define CDI_USB_IFCCLS_WIRELESS 0xe0 // Wireless Controller +#define CDI_USB_IFCCLS_MISC CDI_USB_DEVCLS_MISC +#define CDI_USB_IFCCLS_APP 0xfe // Application-specific +#define CDI_USB_IFCCLS_VENDOR CDI_USB_DEVCLS_VENDOR + +#endif diff --git a/src/modules/cdi/include/cdi/usb.h b/src/modules/cdi/include/cdi/usb.h new file mode 100644 index 0000000..d30e7b0 --- /dev/null +++ b/src/modules/cdi/include/cdi/usb.h @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2015 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. + */ + +#ifndef _CDI_USB_H_ +#define _CDI_USB_H_ + +#include + +#include +#include +#include + + +struct cdi_usb_hc; + +typedef enum { + CDI_USB_CONTROL, + CDI_USB_BULK, + CDI_USB_INTERRUPT, + CDI_USB_ISOCHRONOUS, +} cdi_usb_endpoint_type_t; + +typedef enum { + CDI_USB_LOW_SPEED, + CDI_USB_FULL_SPEED, + CDI_USB_HIGH_SPEED, + CDI_USB_SUPERSPEED, +} cdi_usb_speed_t; + +typedef enum { + CDI_USB_OK = 0, + + CDI_USB_DRIVER_ERROR, + CDI_USB_TIMEOUT, + CDI_USB_BABBLE, + CDI_USB_STALL, + CDI_USB_HC_ERROR, + CDI_USB_BAD_RESPONSE, +} cdi_usb_transmission_result_t; + +/* Bitmask of CDI_USB_PORT_* and CDI_USB_*_SPEED values */ +typedef uint32_t cdi_usb_port_status_t; + +#define CDI_USB_PORT_SPEED_MASK (0x7) +#define CDI_USB_PORT_CONNECTED (1 << 3) + +struct cdi_usb_hub; + +/** + * Describes a hub driver. This is not a CDI driver in the common sense. When + * used for a root hub, it is part of the host controller driver; when used for + * a device hub, it is only used internally by the USB driver. + */ +struct cdi_usb_hub_driver { + /** + * Makes a port unreachable by traffic which reaches the hub. @index is a + * 0-based index. + */ + void (*port_disable)(struct cdi_usb_hub* hub, int index); + + /** + * Makes a port reachable by traffic which reaches the hub and drives the + * reset signal for at least 50 ms on the port. @index is a 0-based index. + */ + void (*port_reset_enable)(struct cdi_usb_hub* hub, int index); + + /** + * Retrieves information about the given port. This should only be called + * after the port has been reset using port_reset_enable(). @index is a + * 0-based index. + */ + cdi_usb_port_status_t (*port_status)(struct cdi_usb_hub* hub, int index); +}; + +/** + * Describes a USB hub. This is not a CDI device in the common sense. When used + * for a root hub, it is part of the host controller object; when used for a + * device hub, it is only used internally by the USB driver. + * + * For this reason, there is no struct cdi_usb_hub_driver object reachable from + * this object. If it is a root hub, the hub driver will be part of the host + * controller driver. Otherwise (if it is a device hub), the hub driver will be + * internal to the USB driver. + */ +struct cdi_usb_hub { + /** + * Number of ports provided by this hub. + */ + int ports; +}; + +/** + * Describes a USB device as given to cdi_provide_device() and the init_device() + * function provided by USB device drivers. + */ +struct cdi_usb_device { + struct cdi_bus_data bus_data; + + /** + * Various information which can be used for identifying a USB device. + */ + uint16_t vendor_id, product_id; + uint8_t class_id, subclass_id, protocol_id; + + /** + * If negative, this logical device is defined on the USB device level; + * otherwise, this is the index of the interface which this logical device + * represents. + */ + int interface; + + /** + * Number of endpoints available for this device; this does include the + * default pipe (endpoint 0) which can be used both on the physical device + * level and from any interface. + */ + int endpoint_count; + + cdi_usb_device_osdep meta; +}; + +/** + * Describes the USB bus driver. + */ +struct cdi_usb_driver { + struct cdi_driver drv; + + /** + * Returns the endpoint descriptor for endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev in @desc. + */ + void (*get_endpoint_descriptor)(struct cdi_usb_device* dev, int ep, + struct cdi_usb_endpoint_descriptor* desc); + + /** + * Executes a control transfer to endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev, using @setup for the setup stage + * and @data for the data stage. The length and direction of transfer is + * deduced from @setup. + */ + cdi_usb_transmission_result_t + (*control_transfer)(struct cdi_usb_device* dev, int ep, + const struct cdi_usb_setup_packet* setup, + void* data); + + /** + * Executes a bulk transfer to endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev, transferring @size bytes from/to + * @data. The direction of transfer is deduced from the endpoint given. + */ + cdi_usb_transmission_result_t (*bulk_transfer)(struct cdi_usb_device* dev, + int ep, void* data, + size_t size); +}; + +/** + * Describes patterns for USB devices a device driver can use to specify which + * devices it can handle. + */ +struct cdi_usb_bus_device_pattern { + struct cdi_bus_device_pattern pattern; + + /** + * Any field may be negative to signify "don't care". + */ + int vendor_id, product_id; + int class_id, subclass_id, protocol_id; +}; + + +/** + * Registers a USB bus driver. Should be called by the CDI library, ideally, + * after the USB bus driver's init() function has run. + */ +void cdi_usb_driver_register(struct cdi_usb_driver* drv); + +/** + * Returns the endpoint descriptor for endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev in @desc. + */ +void cdi_usb_get_endpoint_descriptor(struct cdi_usb_device* dev, int ep, + struct cdi_usb_endpoint_descriptor* desc); + +/** + * Executes a control transfer to endpoint @ep (0 up until + * dev->endpoint_count - 1) of device @dev, using @setup for the setup stage and + * @data for the data stage. The length and direction of transfer is deduced + * from @setup. + */ +cdi_usb_transmission_result_t + cdi_usb_control_transfer(struct cdi_usb_device* dev, int ep, + const struct cdi_usb_setup_packet* setup, + void* data); + +/** + * Executes a bulk transfer to endpoint @ep (0 up until dev->endpoint_count - 1) + * of device @dev, transferring @size bytes from/to @data. The direction of + * transfer is deduced from the endpoint given. + */ +cdi_usb_transmission_result_t cdi_usb_bulk_transfer(struct cdi_usb_device* dev, + int ep, void* data, + size_t size); + +#endif diff --git a/src/modules/cdi/include/cdi/usb_hcd.h b/src/modules/cdi/include/cdi/usb_hcd.h new file mode 100644 index 0000000..842ae73 --- /dev/null +++ b/src/modules/cdi/include/cdi/usb_hcd.h @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2015 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. + */ + +#ifndef _CDI_USB_HCD_H_ +#define _CDI_USB_HCD_H_ + +#include +#include + + +/** + * Opaque value identifying a transaction for the host controller. + */ +typedef void* cdi_usb_hc_transaction_t; + +typedef enum { + CDI_USB_IN, + CDI_USB_OUT, + CDI_USB_SETUP, +} cdi_usb_transfer_token_t; + + +/** + * Describes a USB host controller. + */ +struct cdi_usb_hc { + struct cdi_device dev; + + /** + * Root hub provided by the HC. The hub driver responsible for this root hub + * is part of the cdi_usb_hcd structure. + */ + struct cdi_usb_hub rh; +}; + +/** + * Describes the USB bus provided by a USB host controller and its root hub. All + * buses are handled by the USB bus driver, therefore there are no "identifying" + * fields in this structure. + */ +struct cdi_usb_hc_bus { + struct cdi_bus_data bus_data; + struct cdi_usb_hc* hc; +}; + +/** + * Contains information about a communication pipe to a USB device endpoint used + * for USB transactions. + */ +struct cdi_usb_hc_ep_info { + /// Device ID + uint8_t dev; + + /// Endpoint ID + uint8_t ep; + + /// Endpoint type + cdi_usb_endpoint_type_t ep_type; + + /// Transfer speed + cdi_usb_speed_t speed; + + /// Maximum packet size for this endpoint + size_t mps; + + /** + * For low and full speed devices operated behind a high speed hub: @tt_addr + * is the USB device address of the translating USB hub (the last high speed + * hub), @tt_port is the port (0-based index) of that hub where the first + * non-high-speed device of this device's tree is plugged into. + */ + uint8_t tt_addr, tt_port; +}; + +/** + * Describes a single USB transmission. + */ +struct cdi_usb_hc_transmission { + /// Transaction this transmission is part of + cdi_usb_hc_transaction_t ta; + + /// Type of transfer to be performed + cdi_usb_transfer_token_t token; + + /// Buffer to be used (may be NULL if @size is 0) + void* buffer; + + /// Number of bytes to be transferred (may be 0, and may exceed the MPS) + size_t size; + + /** + * Toggle bit to be used for the first packet issued in this transmission. + * If @size exceeds the MPS and thus multiple packets are transmitted, the + * toggle bit will be toggled automatically. + */ + unsigned toggle; + + /** + * Result of this transmission. Only valid after wait_transaction() has + * returned. + */ + cdi_usb_transmission_result_t* result; +}; + +/** + * Describes a USB host controller driver. + */ +struct cdi_usb_hcd { + struct cdi_driver drv; + + /** + * USB hub driver which is responsible for all root hubs provided by this + * HCD. + */ + struct cdi_usb_hub_driver rh_drv; + + /** + * Creates a USB transaction. A transaction consists of one or more + * transmissions to a single endpoint. + */ + cdi_usb_hc_transaction_t + (*create_transaction)(struct cdi_usb_hc* hc, + const struct cdi_usb_hc_ep_info* target); + + /** + * Enters a transmission into a transaction. The transmission will not be + * executed until start_transaction() is invoked. + * + * This function may not be called after start_transaction() has been used. + */ + void (*enqueue)(struct cdi_usb_hc* hc, + const struct cdi_usb_hc_transmission* trans); + + /** + * Starts asynchronous execution of a transaction. + */ + void (*start_transaction)(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta); + + /** + * Wait until all transmissions enqueued in the given transaction have been + * executed. The variables pointed to by the @result field of the + * transmissions enqueued will only be valid when this function returns. + * + * This function may not be invoked before start_transaction(). + */ + void (*wait_transaction)(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta); + + /** + * Destroys a transaction. Transmissions which have not been waited on may + * or may not have been executed by the time this function returns. The + * values of the variables pointed to by the @result field of unwaited-for + * transmissions are not valid, neither are the buffer contents for + * receiving transmissions. + */ + void (*destroy_transaction)(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta); +}; + +#endif -- 2.6.3 From max at tyndur.org Sun Dec 13 01:11:03 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:11:03 +0100 Subject: [tyndur-devel] [PATCH v2 18/24] cdi/usb: Implementierung der CDI-Bibliothek In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-19-git-send-email-max@tyndur.org> + Implementierung der CDI-Bibliothek für CDI.usb für týndur Signed-off-by: Max Reitz --- src/modules/cdi/include/cdi-osdep.h | 3 + src/modules/cdi/lib/cdi.c | 81 +++++++ src/modules/cdi/lib/usb.c | 403 +++++++++++++++++++++++++++++++++++ src/modules/cdi/lib/usb_dd.c | 173 +++++++++++++++ src/modules/cdi/lib/usb_hcd.c | 413 ++++++++++++++++++++++++++++++++++++ src/modules/include/usb-ipc.h | 103 +++++++++ 6 files changed, 1176 insertions(+) create mode 100644 src/modules/cdi/lib/usb.c create mode 100644 src/modules/cdi/lib/usb_dd.c create mode 100644 src/modules/cdi/lib/usb_hcd.c create mode 100644 src/modules/include/usb-ipc.h diff --git a/src/modules/cdi/include/cdi-osdep.h b/src/modules/cdi/include/cdi-osdep.h index 3ec20a1..42fa39d 100644 --- a/src/modules/cdi/include/cdi-osdep.h +++ b/src/modules/cdi/include/cdi-osdep.h @@ -13,6 +13,7 @@ #include #include +#include #define CDI_STANDALONE #define TYNDUR @@ -75,6 +76,8 @@ typedef struct * \endjapanese */ typedef struct { + pid_t pid; + int id; } cdi_usb_device_osdep; /** diff --git a/src/modules/cdi/lib/cdi.c b/src/modules/cdi/lib/cdi.c index 11b3009..fa8a217 100644 --- a/src/modules/cdi/lib/cdi.c +++ b/src/modules/cdi/lib/cdi.c @@ -23,11 +23,20 @@ #include "cdi/pci.h" #include "cdi/scsi.h" #include "cdi/storage.h" +#include "cdi/usb.h" +#include "cdi/usb_hcd.h" extern void cdi_storage_driver_register(struct cdi_storage_driver* driver); extern void cdi_audio_driver_register(struct cdi_audio_driver* driver); +extern void cdi_usb_driver_register(struct cdi_usb_driver* driver); extern void cdi_tyndur_net_device_init(struct cdi_device* device); +extern void cdi_osdep_handle_usb_device(struct cdi_usb_bus_device_pattern* p); + +extern void cdi_osdep_provide_hc(struct cdi_usb_hc* hc); + +extern void cdi_osdep_set_up_usb_dd_ipc(struct cdi_driver* drv); + void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev); static list_t* drivers = NULL; @@ -219,6 +228,10 @@ static void cdi_tyndur_run_drivers(void) } } + if (driver->bus == CDI_USB) { + cdi_osdep_set_up_usb_dd_ipc(driver); + } + /* Netzwerk ist schon initialisiert */ if (driver->type != CDI_NETWORK) { init_service_register((char*) driver->name); @@ -270,6 +283,10 @@ void cdi_driver_register(struct cdi_driver* driver) cdi_audio_driver_register((struct cdi_audio_driver*) driver); break; + case CDI_USB: + cdi_usb_driver_register((struct cdi_usb_driver*) driver); + break; + default: break; } @@ -281,6 +298,66 @@ list_t* cdi_tyndur_get_drivers(void) return drivers; } +void cdi_handle_bus_device(struct cdi_driver* drv, + struct cdi_bus_device_pattern* pattern) +{ + (void)drv; + + switch ((int)pattern->bus_type) { + case CDI_USB: + cdi_osdep_handle_usb_device( + CDI_UPCAST(pattern, struct cdi_usb_bus_device_pattern, + pattern)); + break; + } +} + +extern void cdi_osdep_provide_usb_device_to(struct cdi_usb_device* dev, + pid_t pid); + +int cdi_provide_device(struct cdi_bus_data* device) +{ + if (device->bus_type != CDI_USB) { + return -1; + } + + struct cdi_usb_device* usb_dev = CDI_UPCAST(device, struct cdi_usb_device, + bus_data); + pid_t pid; + char name[32]; + + sprintf(name, "usb-%04x-%04x", usb_dev->vendor_id, usb_dev->product_id); + pid = init_service_get(name); + if (pid > 0) { + cdi_osdep_provide_usb_device_to(usb_dev, pid); + return 0; + } + + sprintf(name, "usb-%02x-%02x-%02x", usb_dev->class_id, usb_dev->subclass_id, + usb_dev->protocol_id); + pid = init_service_get(name); + if (pid > 0) { + cdi_osdep_provide_usb_device_to(usb_dev, pid); + return 0; + } + + sprintf(name, "usb-%02x-%02x-?", usb_dev->class_id, usb_dev->subclass_id); + pid = init_service_get(name); + if (pid > 0) { + cdi_osdep_provide_usb_device_to(usb_dev, pid); + return 0; + } + + sprintf(name, "usb-%02x-?-?", usb_dev->class_id); + pid = init_service_get(name); + if (pid > 0) { + cdi_osdep_provide_usb_device_to(usb_dev, pid); + return 0; + } + + return -1; +} + void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev) { if (!dev) { @@ -294,6 +371,10 @@ void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev) case CDI_SCSI: cdi_scsi_device_init(CDI_UPCAST(dev, struct cdi_scsi_device, dev)); break; + + case CDI_USB_HCD: + cdi_osdep_provide_hc(CDI_UPCAST(dev, struct cdi_usb_hc, dev)); + break; } } diff --git a/src/modules/cdi/lib/usb.c b/src/modules/cdi/lib/usb.c new file mode 100644 index 0000000..69da43f --- /dev/null +++ b/src/modules/cdi/lib/usb.c @@ -0,0 +1,403 @@ +/* + * Copyright (c) 2015 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cdi.h" +#include "cdi/lists.h" +#include "cdi/misc.h" +#include "cdi/usb.h" +#include "cdi/usb_hcd.h" + + +static struct cdi_usb_driver* usb_drv; +static struct cdi_usb_device** dev_list; +static size_t dev_list_size; + +extern void cdi_osdep_new_device(struct cdi_driver* drv, + struct cdi_device* dev); + +#define RPC(name) \ + static void name(pid_t src, uint32_t corr_id, size_t length, void* data) + +#define rpc_int_resp(val) rpc_send_int_response(src, corr_id, val) + + +struct transmission { + cdi_usb_hc_transaction_t ta; + + void* buffer; + size_t size; + + cdi_usb_transmission_result_t* result_ptr; + + struct { + cdi_usb_transmission_result_t result; + char data[]; + } *shm; + uint32_t shmid; +}; + +struct ipc_usb_hc { + struct cdi_usb_hc cdi; + pid_t pid; + int id; + + list_t* open_transmissions; +}; + + +RPC(hc_found); +RPC(get_endpoint_descriptor); +RPC(control_transfer); +RPC(bulk_transfer); + + +void cdi_usb_driver_register(struct cdi_usb_driver* drv) +{ + assert(!usb_drv); + usb_drv = drv; + + // Wait for all USB device drivers + servmgr_need("usb-storage"); + + register_message_handler(USB_IPC_HC_FOUND, hc_found); + register_message_handler(USB_IPC_GET_ENDPOINT_DESCRIPTOR, + get_endpoint_descriptor); + register_message_handler(USB_IPC_CONTROL_TRANSFER, control_transfer); + register_message_handler(USB_IPC_BULK_TRANSFER, bulk_transfer); +} + + +static void rh_port_down(struct cdi_usb_hub* hub, int index) +{ + struct cdi_usb_hc* hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_port_param par = { + .hc = ipc_hc->id, + .index = index + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_RH_PORT_DISABLE, sizeof(par), + (char*) &par); +} + + +static void rh_port_up(struct cdi_usb_hub* hub, int index) +{ + struct cdi_usb_hc* hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_port_param par = { + .hc = ipc_hc->id, + .index = index + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_RH_PORT_RESET_ENABLE, sizeof(par), + (char*) &par); +} + + +static cdi_usb_port_status_t rh_port_status(struct cdi_usb_hub* hub, int index) +{ + struct cdi_usb_hc* hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_port_param par = { + .hc = ipc_hc->id, + .index = index + }; + + return rpc_get_dword(ipc_hc->pid, USB_IPC_RH_PORT_STATUS, sizeof(par), + (char*) &par); +} + + +static cdi_usb_hc_transaction_t + hc_create_transaction(struct cdi_usb_hc* hc, + const struct cdi_usb_hc_ep_info* target) +{ + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_create_transaction_param par = { + .hc = ipc_hc->id, + .target = *target + }; + + response_t* resp = rpc_get_response(ipc_hc->pid, + USB_IPC_HC_CREATE_TRANSACTION, + sizeof(par), (char*) &par); + if (resp->data_length != sizeof(cdi_usb_hc_transaction_t)) { + free(resp->data); + free(resp); + return NULL; + } + + cdi_usb_hc_transaction_t ta = *(cdi_usb_hc_transaction_t*) resp->data; + free(resp->data); + free(resp); + + return ta; +} + + +static void hc_enqueue(struct cdi_usb_hc* hc, + const struct cdi_usb_hc_transmission* trans) +{ + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct transmission* int_trans = calloc(1, sizeof(*trans)); + + int_trans->ta = trans->ta; + int_trans->result_ptr = trans->result; + + int_trans->shmid = create_shared_memory(trans->size + + sizeof(*trans->result)); + int_trans->shm = open_shared_memory(int_trans->shmid); + + if (trans->token == CDI_USB_IN) { + int_trans->buffer = trans->buffer; + int_trans->size = trans->size; + } else { + memcpy(int_trans->shm->data, trans->buffer, trans->size); + } + + list_push(ipc_hc->open_transmissions, int_trans); + + struct usb_ipc_hc_enqueue_param par = { + .hc = ipc_hc->id, + .trans = *trans, + .shm = int_trans->shmid + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_HC_ENQUEUE, sizeof(par), (char*) &par); +} + + +static void hc_start_transaction(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta) +{ + struct ipc_usb_hc *ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_start_transaction_param par = { + .hc = ipc_hc->id, + .ta = ta + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_HC_START_TRANSACTION, + sizeof(par), (char*) &par); +} + + +static void hc_wait_transaction(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta) +{ + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_wait_transaction_param par = { + .hc = ipc_hc->id, + .ta = ta + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_HC_WAIT_TRANSACTION, sizeof(par), + (char*) &par); + + int i = 0; + struct transmission* int_trans; + while ((int_trans = list_get_element_at(ipc_hc->open_transmissions, i++))) { + if (int_trans->ta == ta) { + list_remove(ipc_hc->open_transmissions, --i); + + if (int_trans->result_ptr) { + *int_trans->result_ptr = int_trans->shm->result; + } + + if (int_trans->buffer) { + memcpy(int_trans->buffer, int_trans->shm->data, + int_trans->size); + } + + close_shared_memory(int_trans->shmid); + free(int_trans); + } + } +} + + +static void hc_destroy_transaction(struct cdi_usb_hc* hc, + cdi_usb_hc_transaction_t ta) +{ + struct ipc_usb_hc* ipc_hc = CDI_UPCAST(hc, struct ipc_usb_hc, cdi); + struct usb_ipc_hc_wait_transaction_param par = { + .hc = ipc_hc->id, + .ta = ta + }; + + rpc_get_int(ipc_hc->pid, USB_IPC_HC_DESTROY_TRANSACTION, sizeof(par), + (char*) &par); + + int i = 0; + struct transmission* int_trans; + while ((int_trans = list_get_element_at(ipc_hc->open_transmissions, i++))) { + if (int_trans->ta == ta) { + list_remove(ipc_hc->open_transmissions, --i); + close_shared_memory(int_trans->shmid); + free(int_trans); + } + } +} + + +static struct cdi_usb_hcd wrapper_hcd = { + .drv = { + .name = "ipc-hci", + .type = CDI_USB_HCD, + }, + + .rh_drv = { + .port_disable = rh_port_down, + .port_reset_enable = rh_port_up, + .port_status = rh_port_status, + }, + + .create_transaction = hc_create_transaction, + .enqueue = hc_enqueue, + .start_transaction = hc_start_transaction, + .wait_transaction = hc_wait_transaction, + .destroy_transaction = hc_destroy_transaction, +}; + + +RPC(hc_found) +{ + struct usb_ipc_hc_found_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + struct cdi_usb_hcd* hcd = malloc(sizeof(*hcd)); + *hcd = wrapper_hcd; + hcd->drv.name = strdup(par->drv_name); + + struct ipc_usb_hc* hc = calloc(1, sizeof(*hc)); + hc->pid = src; + hc->id = par->id; + hc->open_transmissions = list_create(); + hc->cdi.dev.driver = &hcd->drv; + hc->cdi.dev.name = strdup(par->hc_name); + hc->cdi.rh = par->rh; + + struct cdi_usb_hc_bus* hcb = calloc(1, sizeof(*hcb)); + hcb->bus_data.bus_type = CDI_USB_HCD; + hcb->hc = &hc->cdi; + + cdi_osdep_new_device(&usb_drv->drv, + usb_drv->drv.init_device(&hcb->bus_data)); + + rpc_int_resp(1); +} + + +RPC(get_endpoint_descriptor) +{ + struct usb_ipc_get_endpoint_descriptor_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->dev < 0 || par->dev >= (int)dev_list_size || !dev_list[par->dev]) { + rpc_int_resp(0); + return; + } + + struct cdi_usb_device* dev = dev_list[par->dev]; + struct cdi_usb_endpoint_descriptor desc; + + usb_drv->get_endpoint_descriptor(dev, par->ep, &desc); + rpc_send_response(src, corr_id, sizeof(desc), (char*) &desc); +} + + +RPC(control_transfer) +{ + struct usb_ipc_control_transfer_param* par = data; + if (length != sizeof(*par)) { + rpc_send_dword_response(src, corr_id, CDI_USB_DRIVER_ERROR); + return; + } + + if (par->dev < 0 || par->dev >= (int)dev_list_size || !dev_list[par->dev]) { + rpc_send_dword_response(src, corr_id, CDI_USB_DRIVER_ERROR); + return; + } + + struct cdi_usb_device* dev = dev_list[par->dev]; + void* shm = open_shared_memory(par->shm); + + cdi_usb_transmission_result_t res; + res = usb_drv->control_transfer(dev, par->ep, &par->setup, shm); + + close_shared_memory(par->shm); + + rpc_send_dword_response(src, corr_id, res); +} + + +RPC(bulk_transfer) +{ + struct usb_ipc_bulk_transfer_param* par = data; + if (length != sizeof(*par)) { + rpc_send_dword_response(src, corr_id, CDI_USB_DRIVER_ERROR); + return; + } + + if (par->dev < 0 || par->dev >= (int)dev_list_size || !dev_list[par->dev]) { + rpc_send_dword_response(src, corr_id, CDI_USB_DRIVER_ERROR); + return; + } + + struct cdi_usb_device* dev = dev_list[par->dev]; + void* shm = open_shared_memory(par->shm); + + cdi_usb_transmission_result_t res; + res = usb_drv->bulk_transfer(dev, par->ep, shm, par->size); + + close_shared_memory(par->shm); + + rpc_send_dword_response(src, corr_id, res); +} + + +void cdi_osdep_provide_usb_device_to(struct cdi_usb_device* usb_dev, pid_t pid); + +void cdi_osdep_provide_usb_device_to(struct cdi_usb_device* usb_dev, pid_t pid) +{ + int idx; + for (idx = 0; idx < (int)dev_list_size && !dev_list[idx]; idx++); + + if (idx == (int)dev_list_size) { + dev_list_size = (dev_list_size + 4) * 3 / 2; + dev_list = realloc(dev_list, dev_list_size * sizeof(*dev_list)); + memset(dev_list + idx, 0, (dev_list_size - idx) * sizeof(*dev_list)); + } + + dev_list[idx] = usb_dev; + dev_list[idx]->meta.pid = getpid(); + dev_list[idx]->meta.id = idx; + + rpc_get_int(pid, USB_IPC_DEVICE_FOUND, + sizeof(*dev_list[idx]), (char*) dev_list[idx]); +} diff --git a/src/modules/cdi/lib/usb_dd.c b/src/modules/cdi/lib/usb_dd.c new file mode 100644 index 0000000..e1a379d --- /dev/null +++ b/src/modules/cdi/lib/usb_dd.c @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2015 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cdi.h" +#include "cdi/misc.h" +#include "cdi/usb.h" + + +extern void cdi_osdep_new_device(struct cdi_driver* drv, + struct cdi_device* dev); + +static struct cdi_driver* usb_dd; + +#define RPC(name) \ + static void name(pid_t src, uint32_t corr_id, size_t length, void* data) + +#define rpc_int_resp(val) rpc_send_int_response(src, corr_id, val) + + +RPC(device_found) +{ + struct cdi_usb_device* dev = data; + if (length != sizeof(*dev)) { + rpc_int_resp(0); + return; + } + + cdi_osdep_new_device(usb_dd, usb_dd->init_device(&dev->bus_data)); + + rpc_int_resp(0); +} + + +void cdi_osdep_set_up_usb_dd_ipc(struct cdi_driver* drv); + +void cdi_osdep_set_up_usb_dd_ipc(struct cdi_driver* drv) +{ + assert(!usb_dd); + usb_dd = drv; + + register_message_handler(USB_IPC_DEVICE_FOUND, device_found); +} + + +void cdi_osdep_handle_usb_device(struct cdi_usb_bus_device_pattern* p); + +void cdi_osdep_handle_usb_device(struct cdi_usb_bus_device_pattern* p) +{ + char alias[32]; + + if (p->vendor_id < 0 && p->product_id < 0) { + assert(p->class_id >= 0); + if (p->protocol_id < 0) { + assert(p->subclass_id < 0); + snprintf(alias, 32, "usb-%02x-?-?", p->class_id); + } else if (p->subclass_id < 0) { + snprintf(alias, 32, "usb-%02x-%02x-?", p->class_id, p->subclass_id); + } else { + snprintf(alias, 32, "usb-%02x-%02x-%02x", p->class_id, + p->subclass_id, p->protocol_id); + } + } else if (p->class_id < 0 && p->subclass_id < 0 && p->protocol_id < 0) { + assert(p->vendor_id >= 0 && p->product_id >= 0); + snprintf(alias, 32, "usb-%04x-%04x", p->vendor_id, p->product_id); + } else { + assert(0); + } + + init_service_register(alias); +} + + +void cdi_usb_get_endpoint_descriptor(struct cdi_usb_device* dev, int ep, + struct cdi_usb_endpoint_descriptor* desc) +{ + struct usb_ipc_get_endpoint_descriptor_param par = { + .dev = dev->meta.id, + .ep = ep + }; + + response_t* resp = rpc_get_response(dev->meta.pid, + USB_IPC_GET_ENDPOINT_DESCRIPTOR, + sizeof(par), (char*) &par); + assert(resp->data_length == sizeof(*desc)); + + memcpy(desc, resp->data, sizeof(*desc)); + free(resp->data); + free(resp); +} + + +cdi_usb_transmission_result_t + cdi_usb_control_transfer(struct cdi_usb_device* dev, int ep, + const struct cdi_usb_setup_packet* setup, + void* data) +{ + uint32_t shmid = 0; + void* shm = NULL; + + if (setup->w_length) { + shmid = create_shared_memory(setup->w_length); + shm = open_shared_memory(shmid); + if (setup->bm_request_type & CDI_USB_CREQ_OUT) { + memcpy(shm, data, setup->w_length); + } + } + + struct usb_ipc_control_transfer_param par = { + .dev = dev->meta.id, + .ep = ep, + .setup = *setup, + .shm = shmid + }; + + cdi_usb_transmission_result_t res; + res = rpc_get_dword(dev->meta.pid, USB_IPC_CONTROL_TRANSFER, + sizeof(par), (char*) &par); + + if (setup->w_length) { + if (setup->bm_request_type & CDI_USB_CREQ_IN) { + memcpy(data, shm, setup->w_length); + } + close_shared_memory(shmid); + } + + return res; +} + + +cdi_usb_transmission_result_t cdi_usb_bulk_transfer(struct cdi_usb_device* dev, + int ep, void* data, + size_t size) +{ + uint32_t shmid = create_shared_memory(size); + void* shm = open_shared_memory(shmid); + + // FIXME (We don't know which direction the transfer is going) + memcpy(shm, data, size); + + struct usb_ipc_bulk_transfer_param par = { + .dev = dev->meta.id, + .ep = ep, + .size = size, + .shm = shmid + }; + + cdi_usb_transmission_result_t res; + res = rpc_get_dword(dev->meta.pid, USB_IPC_BULK_TRANSFER, + sizeof(par), (char*) &par); + + // FIXME (Same as above) + memcpy(data, shm, size); + + close_shared_memory(shmid); + + return res; +} diff --git a/src/modules/cdi/lib/usb_hcd.c b/src/modules/cdi/lib/usb_hcd.c new file mode 100644 index 0000000..b06722c --- /dev/null +++ b/src/modules/cdi/lib/usb_hcd.c @@ -0,0 +1,413 @@ +/* + * Copyright (c) 2015 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cdi/misc.h" +#include "cdi/usb_hcd.h" + + +extern void cdi_osdep_device_found(void); + + +struct transmission { + uint32_t shmid; + void* mapping; +}; + +struct transaction { + cdi_usb_hc_transaction_t ta; + list_t* transmissions; + bool started; +}; + +struct usb_hc { + struct cdi_usb_hc* cdi; + + struct transaction** transactions; + size_t transactions_size; + size_t transactions_first_free; +}; + +static struct usb_hc** hc_list; +static size_t hc_list_size, hc_list_elements; + +#define RPC(name) \ + static void name(pid_t src, uint32_t corr_id, size_t length, void* data) + +#define rpc_int_resp(val) rpc_send_int_response(src, corr_id, val) + + +RPC(hc_create_transaction); +RPC(hc_enqueue); +RPC(hc_start_transaction); +RPC(hc_wait_transaction); +RPC(hc_destroy_transaction); + +RPC(rh_port_disable); +RPC(rh_port_reset_enable); +RPC(rh_port_status); + + +static void set_up_hc_ipc(void) +{ + static bool ipc_set_up; + + if (ipc_set_up) { + return; + } + ipc_set_up = true; + + register_message_handler(USB_IPC_HC_CREATE_TRANSACTION, + hc_create_transaction); + register_message_handler(USB_IPC_HC_ENQUEUE, hc_enqueue); + register_message_handler(USB_IPC_HC_START_TRANSACTION, + hc_start_transaction); + register_message_handler(USB_IPC_HC_WAIT_TRANSACTION, hc_wait_transaction); + register_message_handler(USB_IPC_HC_DESTROY_TRANSACTION, + hc_destroy_transaction); + + register_message_handler(USB_IPC_RH_PORT_DISABLE, rh_port_disable); + register_message_handler(USB_IPC_RH_PORT_RESET_ENABLE, + rh_port_reset_enable); + register_message_handler(USB_IPC_RH_PORT_STATUS, rh_port_status); +} + + +RPC(hc_create_transaction) +{ + struct usb_ipc_hc_create_transaction_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct usb_hc* hc = hc_list[par->hc]; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->cdi->dev.driver, + struct cdi_usb_hcd, drv); + struct transaction* ta = malloc(sizeof(*ta)); + + *ta = (struct transaction){ + .ta = hcd->create_transaction(hc->cdi, &par->target), + .transmissions = list_create() + }; + + size_t idx; + for (idx = hc->transactions_first_free; + idx < hc->transactions_size && hc->transactions[idx]; idx++); + + if (idx >= hc->transactions_size) { + size_t old_ts = hc->transactions_size; + hc->transactions_size = (hc->transactions_size + 4) * 2 / 3; + hc->transactions = realloc(hc->transactions, + hc->transactions_size + * sizeof(*hc->transactions)); + memset(hc->transactions + old_ts, 0, + (hc->transactions_size - old_ts) * sizeof(*hc->transactions)); + } + hc->transactions[idx] = ta; + hc->transactions_first_free = idx + 1; + + cdi_usb_hc_transaction_t ipc_ta = (cdi_usb_hc_transaction_t)(uintptr_t)idx; + rpc_send_response(src, corr_id, sizeof(ipc_ta), (char*) &ipc_ta); +} + + +RPC(hc_enqueue) +{ + struct usb_ipc_hc_enqueue_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct usb_hc* hc = hc_list[par->hc]; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->cdi->dev.driver, + struct cdi_usb_hcd, drv); + + if ((uintptr_t)par->trans.ta > hc->transactions_size || + !hc->transactions[(uintptr_t)par->trans.ta]) + { + rpc_int_resp(0); + return; + } + + struct transaction* ta = hc->transactions[(uintptr_t)par->trans.ta]; + + if (ta->started) { + rpc_int_resp(0); + return; + } + + struct transmission* tm = malloc(sizeof(*tm)); + *tm = (struct transmission) { + .shmid = par->shm, + .mapping = open_shared_memory(par->shm) + }; + + par->trans.ta = ta->ta; + par->trans.result = tm->mapping; + par->trans.buffer = par->trans.result + 1; + + hcd->enqueue(hc->cdi, &par->trans); + + list_push(ta->transmissions, tm); + + rpc_int_resp(0); +} + + +RPC(hc_start_transaction) +{ + struct usb_ipc_hc_start_transaction_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct usb_hc* hc = hc_list[par->hc]; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->cdi->dev.driver, + struct cdi_usb_hcd, drv); + + if ((uintptr_t)par->ta > hc->transactions_size || + !hc->transactions[(uintptr_t)par->ta]) + { + rpc_int_resp(0); + return; + } + + struct transaction* ta = hc->transactions[(uintptr_t)par->ta]; + ta->started = true; + + hcd->start_transaction(hc->cdi, ta->ta); + + rpc_int_resp(0); +} + + +RPC(hc_wait_transaction) +{ + struct usb_ipc_hc_wait_transaction_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct usb_hc* hc = hc_list[par->hc]; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->cdi->dev.driver, + struct cdi_usb_hcd, drv); + + if ((uintptr_t)par->ta > hc->transactions_size || + !hc->transactions[(uintptr_t)par->ta]) + { + rpc_int_resp(0); + return; + } + + struct transaction* ta = hc->transactions[(uintptr_t)par->ta]; + + if (!ta->started) { + rpc_int_resp(0); + return; + } + + hcd->wait_transaction(hc->cdi, ta->ta); + + struct transmission* tm; + while ((tm = list_pop(ta->transmissions))) { + close_shared_memory(tm->shmid); + free(tm); + } + + rpc_int_resp(0); +} + + +RPC(hc_destroy_transaction) +{ + struct usb_ipc_hc_wait_transaction_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct usb_hc* hc = hc_list[par->hc]; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->cdi->dev.driver, + struct cdi_usb_hcd, drv); + + if ((uintptr_t)par->ta > hc->transactions_size || + !hc->transactions[(uintptr_t)par->ta]) + { + rpc_int_resp(0); + return; + } + + struct transaction* ta = hc->transactions[(uintptr_t)par->ta]; + + hcd->destroy_transaction(hc->cdi, ta->ta); + + hc->transactions[(uintptr_t)par->ta] = NULL; + if (hc->transactions_first_free > (uintptr_t)par->ta) { + hc->transactions_first_free = (uintptr_t)par->ta; + } + + struct transmission* tm; + while ((tm = list_pop(ta->transmissions))) { + close_shared_memory(tm->shmid); + free(tm); + } + + list_destroy(ta->transmissions); + free(ta); + + rpc_int_resp(0); +} + + +RPC(rh_port_disable) +{ + struct usb_ipc_hc_port_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct cdi_usb_hc* hc = hc_list[par->hc]->cdi; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->dev.driver, struct cdi_usb_hcd, + drv); + + hcd->rh_drv.port_disable(&hc->rh, par->index); + + rpc_int_resp(0); +} + + +RPC(rh_port_reset_enable) +{ + struct usb_ipc_hc_port_param* par = data; + if (length != sizeof(*par)) { + rpc_int_resp(0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_int_resp(0); + return; + } + + struct cdi_usb_hc* hc = hc_list[par->hc]->cdi; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->dev.driver, struct cdi_usb_hcd, + drv); + + hcd->rh_drv.port_reset_enable(&hc->rh, par->index); + + rpc_int_resp(0); +} + + +RPC(rh_port_status) +{ + struct usb_ipc_hc_port_param* par = data; + if (length != sizeof(*par)) { + rpc_send_dword_response(src, corr_id, 0); + return; + } + + if (par->hc < 0 || par->hc > (int)hc_list_elements || !hc_list[par->hc]) { + rpc_send_dword_response(src, corr_id, 0); + return; + } + + struct cdi_usb_hc* hc = hc_list[par->hc]->cdi; + struct cdi_usb_hcd* hcd = CDI_UPCAST(hc->dev.driver, struct cdi_usb_hcd, + drv); + + rpc_send_dword_response(src, corr_id, + hcd->rh_drv.port_status(&hc->rh, par->index)); +} + + +void cdi_osdep_provide_hc(struct cdi_usb_hc* cdi_hc); + +void cdi_osdep_provide_hc(struct cdi_usb_hc* cdi_hc) +{ + struct usb_hc* hc = calloc(1, sizeof(*hc)); + hc->cdi = cdi_hc; + + set_up_hc_ipc(); + + if (hc_list_size == hc_list_elements) { + size_t old_hls = hc_list_size; + hc_list_size = (hc_list_size + 4) * 2 / 3; + hc_list = realloc(hc_list, hc_list_size * sizeof(*hc_list)); + memset(hc_list + old_hls, 0, + (hc_list_size - old_hls) * sizeof(*hc_list)); + } + + int id = hc_list_elements; + hc_list[hc_list_elements++] = hc; + + static pid_t usb_pid; + if (!usb_pid) { + servmgr_need("usb"); + usb_pid = init_service_get("usb"); + } + + struct usb_ipc_hc_found_param par; + + strncpy(par.drv_name, cdi_hc->dev.driver->name, 31); + par.drv_name[31] = 0; + + strncpy(par.hc_name, cdi_hc->dev.name, 31); + par.hc_name[31] = 0; + + par.id = id; + par.rh = cdi_hc->rh; + + rpc_get_int(usb_pid, USB_IPC_HC_FOUND, sizeof(par), (char*) &par); +} diff --git a/src/modules/include/usb-ipc.h b/src/modules/include/usb-ipc.h new file mode 100644 index 0000000..ae6ccf0 --- /dev/null +++ b/src/modules/include/usb-ipc.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2015 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. + */ + +#ifndef _USB_IPC_H +#define _USB_IPC_H + +#include + +#include +#include + + +// Provided by HCDs +#define USB_IPC_HC_CREATE_TRANSACTION "HCCRTTA" +#define USB_IPC_HC_ENQUEUE "HCNQXFR" +#define USB_IPC_HC_START_TRANSACTION "HCSTTTA" +#define USB_IPC_HC_WAIT_TRANSACTION "HCWAITA" +#define USB_IPC_HC_DESTROY_TRANSACTION "HCDTYTA" + +#define USB_IPC_RH_PORT_DISABLE "RHPRTDN" +#define USB_IPC_RH_PORT_RESET_ENABLE "RHPRTUP" +#define USB_IPC_RH_PORT_STATUS "RHPRTST" + + +struct usb_ipc_hc_create_transaction_param { + int hc; + struct cdi_usb_hc_ep_info target; +}; + +struct usb_ipc_hc_enqueue_param { + int hc; + // @buffer and @result are ignored; @result is put at the start of the SHM + // provided, @buffer has to be placed directly afterwards + struct cdi_usb_hc_transmission trans; + uint32_t shm; +}; + +struct usb_ipc_hc_start_transaction_param { + int hc; + cdi_usb_hc_transaction_t ta; +}; + +struct usb_ipc_hc_wait_transaction_param { + int hc; + cdi_usb_hc_transaction_t ta; +}; + +struct usb_ipc_hc_destroy_transaction_param { + int hc; + cdi_usb_hc_transaction_t ta; +}; + +struct usb_ipc_hc_port_param { + int hc, index; +}; + + +// Provided by the bus driver +#define USB_IPC_HC_FOUND "HCFOUND" + +#define USB_IPC_GET_ENDPOINT_DESCRIPTOR "GEPDESC" + +#define USB_IPC_CONTROL_TRANSFER "CTRLXFR" +#define USB_IPC_BULK_TRANSFER "BULKXFR" + + +struct usb_ipc_hc_found_param { + char drv_name[32], hc_name[32]; + int id; + struct cdi_usb_hub rh; +}; + +struct usb_ipc_get_endpoint_descriptor_param { + int dev, ep; +}; + +struct usb_ipc_control_transfer_param { + int dev, ep; + struct cdi_usb_setup_packet setup; + uint32_t shm; // @buffer +}; + +struct usb_ipc_bulk_transfer_param { + int dev, ep; + size_t size; + uint32_t shm; // @buffer +}; + + +// Provided by device drivers +#define USB_IPC_DEVICE_FOUND "USBPLUG" + + +// The parameter for device_found() is a struct cdi_usb_device. + +#endif -- 2.6.3 From max at tyndur.org Sun Dec 13 01:11:04 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:11:04 +0100 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_v2_19/24=5D_usb=3A_Entferne_t?= =?utf-8?q?=C3=BDndur-eigene_Treiber?= In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-20-git-send-email-max@tyndur.org> - Diese Treiber sollen durch den CDI-Treiber ersetzt werden. Dieser besitzt noch zwar weder UHCI- noch MSD-CBI-Unterstützung (bspw. Diskettenlaufwerke), ist aber tatsächlich besser getestet und EHCI und Bulk-only-MSD reichen für normale USB-Sticks aus bzw. sind sogar besser geeignet. Signed-off-by: Max Reitz --- src/modules/usb/include/usb-ddrv.h | 59 ----- src/modules/usb/include/usb-hc.h | 141 ----------- src/modules/usb/include/usb-hub.h | 49 ---- src/modules/usb/include/usb-server.h | 153 ------------ src/modules/usb/include/usb-structs.h | 176 -------------- src/modules/usb/include/usb-trans.h | 66 ----- src/modules/usb/msd/Makefile.all | 8 - src/modules/usb/msd/init.c | 206 ---------------- src/modules/usb/msd/lostio.c | 218 ----------------- src/modules/usb/msd/main.c | 65 ----- src/modules/usb/msd/msd-bo.c | 104 -------- src/modules/usb/msd/msd-bo.h | 99 -------- src/modules/usb/msd/msd-cbi.c | 86 ------- src/modules/usb/msd/msd-cbi.h | 71 ------ src/modules/usb/msd/msd.h | 154 ------------ src/modules/usb/msd/rpc.c | 89 ------- src/modules/usb/msd/scsi.c | 240 ------------------ src/modules/usb/msd/scsi.h | 219 ----------------- src/modules/usb/msd/usb-lib.c | 207 ---------------- src/modules/usb/msd/usb-lib.h | 124 ---------- src/modules/usb/uhci/Makefile.all | 8 - src/modules/usb/uhci/ctrl.c | 96 -------- src/modules/usb/uhci/init.c | 265 -------------------- src/modules/usb/uhci/main.c | 73 ------ src/modules/usb/uhci/rpc.c | 297 ----------------------- src/modules/usb/uhci/trans.c | 316 ------------------------ src/modules/usb/uhci/uhci.h | 269 -------------------- src/modules/usb/usb1/Makefile.all | 8 - src/modules/usb/usb1/detect.c | 444 ---------------------------------- src/modules/usb/usb1/hub.c | 180 -------------- src/modules/usb/usb1/main.c | 59 ----- src/modules/usb/usb1/rpc.c | 350 --------------------------- src/modules/usb/usb1/transfer.c | 123 ---------- src/modules/usb/usb1/usb.h | 209 ---------------- 34 files changed, 5231 deletions(-) delete mode 100644 src/modules/usb/include/usb-ddrv.h delete mode 100644 src/modules/usb/include/usb-hc.h delete mode 100644 src/modules/usb/include/usb-hub.h delete mode 100644 src/modules/usb/include/usb-server.h delete mode 100644 src/modules/usb/include/usb-structs.h delete mode 100644 src/modules/usb/include/usb-trans.h delete mode 100644 src/modules/usb/msd/Makefile.all delete mode 100644 src/modules/usb/msd/init.c delete mode 100644 src/modules/usb/msd/lostio.c delete mode 100644 src/modules/usb/msd/main.c delete mode 100644 src/modules/usb/msd/msd-bo.c delete mode 100644 src/modules/usb/msd/msd-bo.h delete mode 100644 src/modules/usb/msd/msd-cbi.c delete mode 100644 src/modules/usb/msd/msd-cbi.h delete mode 100644 src/modules/usb/msd/msd.h delete mode 100644 src/modules/usb/msd/rpc.c delete mode 100644 src/modules/usb/msd/scsi.c delete mode 100644 src/modules/usb/msd/scsi.h delete mode 100644 src/modules/usb/msd/usb-lib.c delete mode 100644 src/modules/usb/msd/usb-lib.h delete mode 100644 src/modules/usb/uhci/Makefile.all delete mode 100644 src/modules/usb/uhci/ctrl.c delete mode 100644 src/modules/usb/uhci/init.c delete mode 100644 src/modules/usb/uhci/main.c delete mode 100644 src/modules/usb/uhci/rpc.c delete mode 100644 src/modules/usb/uhci/trans.c delete mode 100644 src/modules/usb/uhci/uhci.h delete mode 100644 src/modules/usb/usb1/Makefile.all delete mode 100644 src/modules/usb/usb1/detect.c delete mode 100644 src/modules/usb/usb1/hub.c delete mode 100644 src/modules/usb/usb1/main.c delete mode 100644 src/modules/usb/usb1/rpc.c delete mode 100644 src/modules/usb/usb1/transfer.c delete mode 100644 src/modules/usb/usb1/usb.h diff --git a/src/modules/usb/include/usb-ddrv.h b/src/modules/usb/include/usb-ddrv.h deleted file mode 100644 index d213cd7..0000000 --- a/src/modules/usb/include/usb-ddrv.h +++ /dev/null @@ -1,59 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_DDRV_H -#define USB_DDRV_H - -#include - - -#define RPC_USB_DDRV_PLUG_FNN "USB_DDRV_PLUG" - -// Ein Wert ist -1, wenn der Treiber alle Geräte annimmt. -struct usb_handle_devs { - int vendor_id, device_id; - int class, subclass, protocol; -}; - -// Wird vom USB-Bustreiber an einen Gerätetreiber weitergegeben, wenn ein Gerät -// gefunden wurde -struct usb_prov_dev { - // Handelt es sich beim Gerät um ein Interface und nicht um ein physisches - // Gerät, so ist dies true - bool is_interface; - // Geräte-ID (vom USB-Bustreiber zu verwenden) - void* dev_id; - // Interface-ID - int interface_id; - // Typ des Geräts - struct usb_handle_devs type; -}; - -#endif diff --git a/src/modules/usb/include/usb-hc.h b/src/modules/usb/include/usb-hc.h deleted file mode 100644 index a70598a..0000000 --- a/src/modules/usb/include/usb-hc.h +++ /dev/null @@ -1,141 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_HC_H -#define USB_HC_H - -#include -#include - -#include "usb-structs.h" -#include "usb-trans.h" - -// Beschreibt einen USB-Hostcontroller -struct usb_hc { - // Anzahl der Rootports - int root_ports; - // Für den HCD frei verfügbar - void* hcd_avail; - // PID des HCD - pid_t pid; - // USB-Adressenzähler - int addr_counter; -}; - -// RPC-Funktion für USB-Bustreiber (vorrangig Roothubfunktionen) -#define RPC_USB_HC_CONTROL_FNN "HC_CONTROL" -// RPC-Funktion für USB-Bustreiber (vorrangig Transferfunktionen) -#define RPC_USB_HC_TRANSFER_FNN "HC_TRANSFER" - -// PIDs für IN, OUT und SETUP -#define USB_OUT 0xE1 -#define USB_IN 0x69 -#define USB_SETUP 0x2D - -// Roothubfunktionen -enum usb_hc_ctrl_rt { - // Portstatus überprüfen - HC_CTRL_SCAN_PORT = 0, - // Port deaktivieren - HC_CTRL_DSBL_PORT, - // Resetsignal treiben - HC_CTRL_RESET, - // Pipe erstellen - HC_CTRL_ADD_PIPE -}; - - -// Allgemeiner HC_CONTROL-Request -struct usb_hc_ctrl_req { - // Funktion - enum usb_hc_ctrl_rt type; - // Hostcontroller - struct usb_hc hc; -}; - -// Rootport überprüfen -struct usb_hc_ctrl_scan { - struct usb_hc_ctrl_req req; - // Portindex - int port; -}; - -// Rootport deaktivieren -struct usb_hc_ctrl_dsbl { - struct usb_hc_ctrl_req req; - // Portindex - int port; -}; - -// Resetsignal an einem Rootport treiben -struct usb_hc_ctrl_rst { - struct usb_hc_ctrl_req req; - // Portindex - int port; -}; - -// Pipe hinzufügen -struct usb_hc_ctrl_appe { - struct usb_hc_ctrl_req req; - // Informationen zur neuen Pipe - struct usb_trans_pipe pipe; -}; - -// Allgemeine Transferanfrage -struct usb_hc_trs { - // Funktion - enum usb_trans_type type; - // Hostcontroller - struct usb_hc hc; - // Pipe, durch die der Transfer laufen soll - struct usb_trans_pipe pipe; -}; - -// Controltransfer -struct usb_hc_trs_ctrl { - struct usb_hc_trs trs; - // SETUP-Paket - struct usb_setup_packet setup; - // SHM-Bereich für die Datenphase - uint32_t shm; -}; - -// Bulktransfer -struct usb_hc_trs_bulk { - struct usb_hc_trs trs; - // true zum Empfangen - bool receive; - // Anzahl der zu übertragenden Bytes - size_t length; - // SHM-Bereich für die übertragenen Daten - uint32_t shm; -}; - -#endif diff --git a/src/modules/usb/include/usb-hub.h b/src/modules/usb/include/usb-hub.h deleted file mode 100644 index 478c2bc..0000000 --- a/src/modules/usb/include/usb-hub.h +++ /dev/null @@ -1,49 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_HUB_H -#define USB_HUB_H - -// In den unteren acht Bits wird die Geschwindigkeit gespeichert (viel Platz -// für zukünftige Neuerungen...) -#define USB_HUB_SPEED 0xFF -// Low Speed -#define USB_HUB_LOW_SPEED (1 << 0) -// Full Speed -#define USB_HUB_FULL_SPEED (1 << 1) -// High Speed -#define USB_HUB_HIGH_SPEED (1 << 2) - -// Gerät angeschlossen -#define USB_HUB_DEVICE (1 << 8) -// Status hat sich seit der letzten Anfrage verändert -#define USB_HUB_CHANGE (1 << 9) - -#endif diff --git a/src/modules/usb/include/usb-server.h b/src/modules/usb/include/usb-server.h deleted file mode 100644 index 6a98707..0000000 --- a/src/modules/usb/include/usb-server.h +++ /dev/null @@ -1,153 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_SERVER_H -#define USB_SERVER_H - -#include -#include - -#include "usb-ddrv.h" -#include "usb-server.h" -#include "usb-structs.h" -#include "usb-trans.h" - -// Funktion zum Registrieren von Hostcontrollern -#define RPC_USB_SV_REGHC_FNN "HCD_REG" -// Funktion zum Registrieren von Gerätetreibern -#define RPC_USB_SV_REGDD_FNN "DDRV_REG" -// Funktion zur Steuerung von Geräten (Daten austauschen, etc.) -#define RPC_USB_SV_DEVCT_FNN "DEV_CTL" - -// Beschreibt eine USB-Pipe -struct usb_pipe { - // Da die von normalen Treibern sowieso nicht dereferenziert werden dürfen, - // können diese Strukturen auch undefiniert bleiben. - // USB-Gerät (nur für den Bustreiber) - struct usb_device* dev; - // Endpunktdeskriptor (nur für den Bustreiber) - struct usb_endpoint_descriptor* ep; - - // Pipe, zur Kommunikation mit dem Hostcontroller - struct usb_trans_pipe trs; -}; - -// Funktionen für DEV_CTRL (mit Rückgabetypen) -enum usb_devctrl_reqtype { - // Gibt Anzahl der Endpunkte zurück - USB_DC_NUM_ENDPOINTS = 0, // int - // Gibt die Interface-ID zurück - USB_DC_GET_IFC_ID, // int - // Gibt den Endpunktdeskriptor zurück - USB_DC_GET_ENDPOINT, // response - // Gibt eine Pipe zurück (und erstellt sie) - USB_DC_GET_PIPE, // response - // Entstallt eine Pipe/den Endpunkt - USB_DC_CLEAR_HALT, // int - // Führt einen Controltransfer durch - USB_DC_TRSF_CONTROL, // int - // Führt einen Bulktransfer durch - USB_DC_TRSF_BULK // int -}; - -// Allgemeine Anfrage für DEV_CTRL -struct usb_devctrl_req { - // Typ der Anrage - enum usb_devctrl_reqtype type; -}; - -// Gibt die Anzahl der Endpunkte zurück -struct usb_devctrl_gne { - struct usb_devctrl_req req; - // Abzufragendes USB-Gerät - struct usb_prov_dev dev; -}; - -// Gibt die Interface-ID zurück -struct usb_devctrl_gii { - struct usb_devctrl_req req; - // Abzufrasgendes USB-Gerät - struct usb_prov_dev dev; -}; - -// Gibt den Endpunktdeskriptor zurück -struct usb_devctrl_gep { - struct usb_devctrl_req req; - // USB-Gerät - struct usb_prov_dev dev; - // Nummer des Endpunkts (nicht die ID) - int ep_number; -}; - -// Erstellt eine USB-Pipe und gibt sie zurück -struct usb_devctrl_gpp { - struct usb_devctrl_req req; - // USB-Gerät - struct usb_prov_dev dev; - // Nummer des Zielendpunkts - int ep_number; -}; - -// Entstallt eine Pipe resp. den Endpunkt -struct usb_devctrl_clh { - struct usb_devctrl_req req; - // USB-Pipe - struct usb_pipe pipe; -}; - -// Führt einen Controltransfer durch -struct usb_devctrl_tct { - struct usb_devctrl_req req; - // Ist EP0 das Ziel, so muss dev_id auf den Wert aus usb_prov_dev gesetzt - // werden, sonst pipe auf die entsprechende USB-Pipe. - union { - struct usb_pipe pipe; - void* dev_id; - } dst; - // true, wenn durch die Default Control Pipe übertragen werden soll - bool ep0; - // SETUP-Paket - struct usb_setup_packet setup; - // SHM-Bereich für die Datenphase - uint32_t shm; -}; - -// Führt einen Bulktransfer durch -struct usb_devctrl_tbk { - struct usb_devctrl_req req; - // USB-Pipe - struct usb_pipe pipe; - // Anzahl der zu übertragenden Bytes - size_t length; - // SHM-Bereich für die Daten - uint32_t shm; -}; - -#endif diff --git a/src/modules/usb/include/usb-structs.h b/src/modules/usb/include/usb-structs.h deleted file mode 100644 index f0b5927..0000000 --- a/src/modules/usb/include/usb-structs.h +++ /dev/null @@ -1,176 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_STRUCTS_H -#define USB_STRUCTS_H - -#include - -#define USB_LOW_SPEED USB_HUB_LOW_SPEED -#define USB_FULL_SPEED USB_HUB_FULL_SPEED -#define USB_HIGH_SPEED USB_HUB_HIGH_SPEED - -enum usb_state { - USB_ATTACHED = 0, - USB_POWERED, - USB_DEFAULT, - USB_ADDRESS, - USB_CONFIGURED, - USB_SUSPENDED -}; - -struct usb_setup_packet { - uint8_t type; - uint8_t request; - uint16_t value; - uint16_t index; - uint16_t length; -} __attribute__((packed)); - -struct usb_descriptor { - uint8_t length; - uint8_t desc_type; -} __attribute__((packed)); - - -#define USB_SETUP_OUT (0 << 7) -#define USB_SETUP_IN (1 << 7) -#define USB_SETUP_REQ_STD (0 << 5) -#define USB_SETUP_REQ_CLS (1 << 5) -#define USB_SETUP_REQ_VDR (2 << 5) -#define USB_SETUP_REC_DEV 0 -#define USB_SETUP_REC_IF 1 -#define USB_SETUP_REC_EP 2 -#define USB_SETUP_REC_OTH 3 - -enum usb_setup_requst { - USB_GET_STATUS = 0, - USB_CLEAR_FEATURE = 1, - USB_SET_FEATURE = 3, - USB_SET_ADDRESS = 5, - USB_GET_DESCRIPTOR = 6, - USB_SET_DESCRIPTOR = 7, - USB_GET_CONFIGURATION = 8, - USB_SET_CONFIGURATION = 9, - USB_GET_INTERFACE = 10, - USB_SET_INTERFACE = 11, - USB_SYNC_FRAME = 12 -}; - -enum usb_features { - USB_ENDPOINT_HALT = 0, - USB_DEVICE_REMOTE_WAKEUP = 1, - USB_TEST_MODE = 2 -}; - -enum usb_setup_descriptor { - USB_DESC_DEVICE = 1 << 8, - USB_DESC_CONFIGURATION = 2 << 8, - USB_DESC_STRING = 3 << 8, - USB_DESC_INTERFACE = 4 << 8, - USB_DESC_ENDPOINT = 5 << 8, - USB_DESC_DEVICE_QUALIFIER = 6 << 8, - USB_DESC_OTHER_SPEED_CONFIGURATION = 7 << 8, - USB_DESC_INTERFACE_POWER = 8 << 8, - - USB_DESC_HUB = 41 << 8 -}; - -struct usb_device_descriptor { - struct usb_descriptor desc; - uint16_t bcd_usb; - uint8_t class, subclass, protocol; - uint8_t mps0; - uint16_t vendor_id, device_id; - uint16_t bcd_device; - uint8_t i_manufacturer, i_product, i_serial; - uint8_t num_configurations; -} __attribute__((packed)); - -struct usb_config_descriptor -{ - struct usb_descriptor desc; - uint16_t total_length; - uint8_t num_interfaces; - uint8_t configuration_value; - uint8_t i_configuration; - unsigned rsvd0 : 5; - unsigned remote_wakeup : 1; - unsigned self_powered : 1; - unsigned rsvd1 : 1; - uint8_t max_power; -} __attribute__((packed)); - -struct usb_interface_descriptor -{ - struct usb_descriptor desc; - uint8_t interface_number; - uint8_t alternate_setting; - uint8_t num_endpoints; - uint8_t class, subclass, protocol; - uint8_t i_interface; -} __attribute__((packed)); - -#define USB_EP_CONTROL 0 -#define USB_EP_ISOCHRONOUS 1 -#define USB_EP_BULK 2 -#define USB_EP_INTERRUPT 3 - -struct usb_endpoint_descriptor { - struct usb_descriptor desc; - unsigned number : 4; - unsigned rsvd1 : 3; - unsigned direction : 1; - unsigned type : 2; - unsigned sync : 2; - unsigned usage : 2; - unsigned rsvd2 : 2; - unsigned mps : 11; - unsigned trans_opp : 2; - unsigned rsvd3 : 3; - uint8_t interval; -} __attribute__((packed)); - -struct usb_hub_descriptor { - struct usb_descriptor desc; - uint8_t nbr_ports; - unsigned power_mode : 2; - unsigned compound : 1; - unsigned oc_protect : 2; - unsigned tt : 2; - unsigned pindicator : 1; - uint8_t rsvd; - uint8_t pwr_on_2_pwr_good; - uint8_t hub_contr_current; - // Die folgenden Felder haben eine variable Größe -} __attribute__((packed)); - -#endif - diff --git a/src/modules/usb/include/usb-trans.h b/src/modules/usb/include/usb-trans.h deleted file mode 100644 index c5d3a1f..0000000 --- a/src/modules/usb/include/usb-trans.h +++ /dev/null @@ -1,66 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_TRANS_H -#define USB_TRANS_H - -#include - -#include "usb-hub.h" - -// Übertragunsart -enum usb_trans_type { - USB_TRST_CONTROL = 0, - USB_TRST_ISOCHRONOUS = 1, - USB_TRST_BULK = 2, - USB_TRST_INTERRUPT = 3 -}; - -// USB-Pipe für die Kommunikation mit dem Hostcontroller -struct usb_trans_pipe { - // USB-Adresse (0 bis 127) - int usb_addr; - // Endpunkt-ID - int ep_id; - // MPS des Endpunkts - int mps; - - // Typ der Pipe - enum usb_trans_type type; - // Geschwindigkeit des Geräts - int speed; - - // Toggle-Bit - int toggle; - // true, wenn der Endpunkt gestallt ist - bool stalled; -}; - -#endif diff --git a/src/modules/usb/msd/Makefile.all b/src/modules/usb/msd/Makefile.all deleted file mode 100644 index 0e16a2a..0000000 --- a/src/modules/usb/msd/Makefile.all +++ /dev/null @@ -1,8 +0,0 @@ -shopt -s extglob -source $LOST_BUILDMK_ROOT/config.sh - -CC="$CC -I../include" - -echo "LD $1/modules/usb-msd" -$LOST_TOOLS_LD $LDSCRIPT -ousb-msd.mod *.o --start-group $2 --end-group -$LOST_TOOLS_STRIP -s usb-msd.mod -o $1/modules/usb-msd diff --git a/src/modules/usb/msd/init.c b/src/modules/usb/msd/init.c deleted file mode 100644 index 7670194..0000000 --- a/src/modules/usb/msd/init.c +++ /dev/null @@ -1,206 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include - -#include "msd.h" -#include "msd-bo.h" -#include "msd-cbi.h" -#include "usb-lib.h" -#include "usb-structs.h" -#include "usb-trans.h" - -void got_bo_msd(struct usb_prov_dev* dev); -void got_cbi_msd(struct usb_prov_dev* dev); - -int is_valid_msd(struct usb_prov_dev* dev) -{ - if (dev->type.protocol == 0x50) { // Bulk-only - return 1; - } else if ((dev->type.subclass == 0x04) && !dev->type.protocol) { // UFI/CBI - return 1; - } - - return 0; -} - -void got_msd(struct usb_prov_dev* dev) -{ - if (dev->type.protocol == 0x50) { // Bulk-only - got_bo_msd(dev); - } else if ((dev->type.subclass == 0x04) && !dev->type.protocol) { // UFI/CBI - got_cbi_msd(dev); - } else { - abort(); - } -} - -void got_bo_msd(struct usb_prov_dev* dev) -{ - int endpoints = usb_get_number_of_endpoints(dev); - int ifc_id = usb_get_interface_id(dev); - struct usb_endpoint_descriptor* epd = calloc(1, sizeof(*epd)); - struct usb_endpoint_descriptor* bulk_in = malloc(sizeof(*bulk_in)); - struct usb_endpoint_descriptor* bulk_out = malloc(sizeof(*bulk_out)); - int bin = -1, bon = -1; - - int i; - for (i = 0; i < endpoints; i++) { - usb_get_endpoint_info(dev, i, epd); - - if (epd->type == USB_EP_BULK) { - if (epd->direction) { - memcpy(bulk_in, epd, sizeof(*epd)); - bin = i; - } else { - memcpy(bulk_out, epd, sizeof(*epd)); - bon = i; - } - } - } - - free(epd); - - if ((bin == -1) || (bon == -1)) { - free(bulk_in); - free(bulk_out); - return; - } - - struct usb_pipe* bip = calloc(1, sizeof(*bip)); - struct usb_pipe* bop = calloc(1, sizeof(*bop)); - - usb_create_pipe(dev, bin, bip); - usb_create_pipe(dev, bon, bop); - - int max_lun = 0; - usb_control_transfer(dev, NULL, &(struct usb_setup_packet){ - .type = USB_SETUP_IN | USB_SETUP_REQ_CLS | USB_SETUP_REC_IF, - .request = 0xFE, // GET MAX LUN - .index = ifc_id, - .length = 1, - }, &max_lun); - - for (i = 0; i <= max_lun; i++) { - struct msc_bo_dev* new = calloc(1, sizeof(*new)); - - new->gen.lun = i; - new->gen.issue_scsi_cmd = &msd_bo_issue_scsi_cmd; - new->gen.send = &msd_bo_send; - new->gen.recv = &msd_bo_recv; - - new->bulk_in = bulk_in; - new->bulk_out = bulk_out; - - new->bip = bip; - new->bop = bop; - - provide_msd(&new->gen); - } -} - -void got_cbi_msd(struct usb_prov_dev* dev) -{ - int endpoints = usb_get_number_of_endpoints(dev); - int ifc_id = usb_get_interface_id(dev); - struct usb_endpoint_descriptor* epd = calloc(1, sizeof(*epd)); - struct usb_endpoint_descriptor* bulk_in = malloc(sizeof(*bulk_in)); - struct usb_endpoint_descriptor* bulk_out = malloc(sizeof(*bulk_out)); - struct usb_endpoint_descriptor* interrupt = malloc(sizeof(*interrupt)); - int bin = -1, bon = -1, ien = -1; - - int i; - for (i = 0; i < endpoints; i++) { - usb_get_endpoint_info(dev, i, epd); - - if (epd->type == USB_EP_BULK) { - if (epd->direction) { - memcpy(bulk_in, epd, sizeof(*epd)); - bin = i; - } else { - memcpy(bulk_out, epd, sizeof(*epd)); - bon = i; - } - } else if (epd->type == USB_EP_INTERRUPT) { - memcpy(interrupt, epd, sizeof(*epd)); - ien = i; - } - } - - free(epd); - - if (dev->type.protocol || (ien == -1)) { - free(interrupt); - ien = -1; - } - - if ((bin == -1) || (bon == -1)) { - free(bulk_in); - free(bulk_out); - return; - } - - struct usb_pipe* bip = calloc(1, sizeof(*bip)); - struct usb_pipe* bop = calloc(1, sizeof(*bop)); - struct usb_pipe* ip; - - usb_create_pipe(dev, bin, bip); - usb_create_pipe(dev, bon, bop); - - if (ien != -1) { - ip = calloc(1, sizeof(*ip)); - usb_create_pipe(dev, ien, ip); - } - - struct msc_cbi_dev* new = calloc(1, sizeof(*new)); - - new->gen.lun = 0; - new->gen.issue_scsi_cmd = &msd_cbi_issue_scsi_cmd; - new->gen.send = &msd_cbi_send; - new->gen.recv = &msd_cbi_recv; - - new->dev = dev; - new->ifc_id = ifc_id; - - new->bulk_in = bulk_in; - new->bulk_out = bulk_out; - - new->bip = bip; - new->bop = bop; - - if (ien != -1) { - new->interrupt = interrupt; - new->ip = ip; - } - - provide_msd(&new->gen); -} diff --git a/src/modules/usb/msd/lostio.c b/src/modules/usb/msd/lostio.c deleted file mode 100644 index 6a2b503..0000000 --- a/src/modules/usb/msd/lostio.c +++ /dev/null @@ -1,218 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include -#include - -#include "msd.h" - -static size_t msd_read(lostio_filehandle_t* liofh, void* dest, - size_t blocksize, size_t blockcount); -static size_t msd_write(lostio_filehandle_t* liofh, size_t blocksize, - size_t blockcount, void* data); -static int msd_seek(lostio_filehandle_t* liofh, uint64_t offset, int origin); - -struct partition { - struct msc_gen_dev* dev; - uint64_t block_offset; - uint64_t blocks; -}; - -struct parttbl_entry { - uint8_t active; - uint8_t chs_start[3]; - uint8_t type; - uint8_t chs_end[3]; - uint32_t start; - uint32_t size; -} __attribute__((packed)); - -void init_lostio_interface(void) -{ - lostio_init(); - lostio_type_ramfile_use(); - lostio_type_directory_use(); - - typehandle_t* msds = calloc(1, sizeof(*msds)); - - msds->id = 42; - msds->read = &msd_read; - msds->write = &msd_write; - msds->seek = &msd_seek; - - lostio_register_typehandle(msds); -} - -static void detect_partitions(int msd_number, struct msc_gen_dev* dev) -{ - uint8_t* bootsec = malloc(512); - - msd_scsi_read(dev, 0, bootsec, 1); - - if (*(uint16_t*)&bootsec[510] != 0xAA55) { - free(bootsec); - return; - } - - struct parttbl_entry* ptbl = (struct parttbl_entry*)&bootsec[446]; - - int p; - for (p = 0; p < 4; p++) { - if (ptbl[p].size) { - if ((ptbl[p].type == 0x0F) || (ptbl[p].type == 0x85)) { - continue; - } - - struct partition* part = calloc(1, sizeof(*part)); - part->dev = dev; - part->block_offset = ptbl[p].start; - part->blocks = ptbl[p].size; - - char name[13]; - sprintf(name, "/msd%i_p%i", msd_number, p); - - vfstree_create_node(name, 42, ptbl[p].size * dev->block_size, part, - 0); - } - } - - free(bootsec); -} - -void provide_msd(struct msc_gen_dev* dev) -{ - static int msds = 0; - - uint64_t bc, bs; - if (!msd_scsi_init(dev, &bc, &bs)) { - return; - } - - printf("[usb-msd] New SCSI block device, %lli %lli-byte blocks: ", bc, bs); - uint64_t fs = bc * bs; - if (fs >= 10 << 30) { - printf("%lli GB\n", fs >> 30); - } else if (fs >= 10 << 20) { - printf("%lli MB\n", fs >> 20); - } else if (fs >= 10 << 10) { - printf("%lli kB\n", fs >> 10); - } else { - printf("%lli B\n", fs); - } - - dev->block_size = bs; - dev->blocks = bc; - - char name[10]; - int num = msds++; - sprintf(name, "/msd%i", num); - - struct partition* part = calloc(1, sizeof(*part)); - part->dev = dev; - part->blocks = bc; - - vfstree_create_node(name, 42, fs, part, 0); - - // Bei 2880 Sektoren ist es vermutlich eine Diskette (ja, es gibt noch - // andere Diskettenarten als 3,5" HD, aber die sind halt selten), und die - // hat keine Partitionen (verhindert das Erkennen scheinbarer Partitionen) - if (bc != 2880) { - detect_partitions(num, dev); - } - - // TODO: Das vielleicht erst irgendwie registrieren, wenn alle Geräte - // angekommen sind - init_service_register("usb-msd"); - - uint16_t* mem = calloc(1, 512); - msd_scsi_read(dev, 0, mem, 1); -} - -static size_t msd_read(lostio_filehandle_t* liofh, void* dest, - size_t blocksize, size_t blockcount) -{ - struct partition* part = liofh->node->data; - struct msc_gen_dev* dev = part->dev; - - uint64_t begin = liofh->pos / dev->block_size + part->block_offset; - - // Diese Debuginformationen mögen nervig erscheinen, sind aber sehr - // wichtig, da man oft nur so erkennen kann, dass überhaupt etwas - // geschieht - printf("[usb-msd] read from %lli (%i blocks)\n", begin, - (blockcount * blocksize + dev->block_size - 1) / dev->block_size); - size_t ret = msd_scsi_read(dev, begin, dest, - (blockcount * blocksize + dev->block_size - 1) / dev->block_size); - msd_seek(liofh, ret, SEEK_CUR); - - return ret; -} - -static size_t msd_write(lostio_filehandle_t* liofh, size_t blocksize, - size_t blockcount, void* data) -{ - return 0; -} - -static int msd_seek(lostio_filehandle_t* liofh, uint64_t offset, int origin) -{ - struct partition* part = liofh->node->data; - struct msc_gen_dev* dev = part->dev; - uint64_t new_pos = liofh->pos; - uint64_t size = part->blocks * dev->block_size; - - switch (origin) { - case SEEK_SET: - new_pos = offset; - break; - case SEEK_CUR: - new_pos += offset; - break; - case SEEK_END: - new_pos = size; - break; - } - - if (new_pos > size) { - return -1; - } else if (new_pos == size) { - liofh->flags |= LOSTIO_FLAG_EOF; - } else { - liofh->flags &= ~LOSTIO_FLAG_EOF; - } - - liofh->pos = new_pos; - return 0; -} diff --git a/src/modules/usb/msd/main.c b/src/modules/usb/msd/main.c deleted file mode 100644 index cebca5c..0000000 --- a/src/modules/usb/msd/main.c +++ /dev/null @@ -1,65 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include - -#include -#include -#include -#include - -#include "msd.h" - -extern pid_t usb_pid; - -int main(void) -{ - init_messaging(); - init_lostio_interface(); - - // Warten, damit, falls alle Treiber relativ gleichzeitig gestartet werden - // (Boot), dieser Prozess nach usb1 (und möglichst vor uhci) weiterläuft - msleep(1000); - - usb_pid = init_service_get("usb1"); - if (!usb_pid) - { - fprintf(stderr, "You need to start the usb1 driver first.\n"); - return 1; - } - - init_rpc_interface(); - register_msddrv(); - - for (;;) { - wait_for_rpc(); - } -} diff --git a/src/modules/usb/msd/msd-bo.c b/src/modules/usb/msd/msd-bo.c deleted file mode 100644 index 0007b80..0000000 --- a/src/modules/usb/msd/msd-bo.c +++ /dev/null @@ -1,104 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include - -#include "msd.h" -#include "msd-bo.h" -#include "usb-lib.h" - -int check_csw(struct msc_bo_dev* dev) -{ - struct msd_bo_csw csw; - - int ret = usb_bulk_transfer(dev->bip, &csw, sizeof(csw), true); - if (ret) { - return ret; - } - - // Man könnte wohl noch ein bisschen was überprüfen... - // Some checks might be nice... - return 0; -} - -int msd_bo_issue_scsi_cmd(struct msc_gen_dev* dev, const void* cmd_buffer, - size_t cmd_length, bool receive, size_t req_length) -{ - struct msc_bo_dev* bo = (struct msc_bo_dev*)dev; - - if (cmd_length > 16) { - cmd_length = 16; - } - - struct msd_bo_cbw cbw = { - .signature = CBW_SIGNATURE, - .data_transfer_length = req_length, - .flags = receive ? CBW_IN : CBW_OUT, - .lun = dev->lun, - .cb_length = cmd_length - }; - - memcpy(cbw.cb, cmd_buffer, cmd_length); - - int ret = usb_bulk_transfer(bo->bop, &cbw, sizeof(cbw), false); - if (ret || req_length) { - return ret; - } - - return check_csw(bo); -} - -int msd_bo_send(struct msc_gen_dev* dev, const void* buffer, size_t size) -{ - struct msc_bo_dev* bo = (struct msc_bo_dev*)dev; - - int ret = usb_bulk_transfer(bo->bop, (void*)buffer, size, false); - if (ret) { - return ret; - } - - return check_csw(bo); -} - -int msd_bo_recv(struct msc_gen_dev* dev, void* buffer, size_t size) -{ - struct msc_bo_dev* bo = (struct msc_bo_dev*)dev; - - int ret = usb_bulk_transfer(bo->bip, buffer, size, true); - if (ret) { - return ret; - } - - return check_csw(bo); -} diff --git a/src/modules/usb/msd/msd-bo.h b/src/modules/usb/msd/msd-bo.h deleted file mode 100644 index 49451cb..0000000 --- a/src/modules/usb/msd/msd-bo.h +++ /dev/null @@ -1,99 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef MSD_BO_H -#define MSD_BO_H - -#include -#include - -#include "msd.h" -#include "usb-ddrv.h" - -#define CBW_IN 0x80 -#define CBW_OUT 0x00 - -#define CBW_SIGNATURE 0x43425355 //'USBC' => USB command -#define CSW_SIGNATURE 0x53425355 //'USBS' => USB status - - -// Beschreibt ein MSC-Bulk-Only-Gerät -struct msc_bo_dev { - // Das MSD an sich - struct msc_gen_dev gen; - - // Bulk-Endpunkte - struct usb_endpoint_descriptor* bulk_in; - struct usb_endpoint_descriptor* bulk_out; - - // Pipes zu diesen Endpunkten - struct usb_pipe* bip; - struct usb_pipe* bop; -}; - -// Command Block Wrapper für BO-MSDs -struct msd_bo_cbw { - // CBW_SIGNATURE - uint32_t signature; - // Ein Wert, der im CSW wiederholt wird - uint32_t tag; - // Länge des Datentransfers - uint32_t data_transfer_length; - // Flags (vorrangig Richtung des Transfers) - uint8_t flags; - // LUN - uint8_t lun; - // Länge des Befehls - uint8_t cb_length; - // SCSI-Befehl - uint8_t cb[16]; -} __attribute__((packed)); - -// Command Status Wrapper für BO-MSDs -struct msd_bo_csw { - // CSW_SIGNATURE - uint32_t signature; - // Tagwert aus dem CBW - uint32_t tag; - // Größe der Daten, die erwartet, aber nicht übertragen wurden - uint32_t data_residue; - // Status - uint8_t status; -} __attribute__((packed)); - -// Sendet einen SCSI-Befehl -int msd_bo_issue_scsi_cmd(struct msc_gen_dev* dev, const void* buffer, - size_t cmd_length, bool receive, size_t req_length); -// Sendet Daten an das Gerät -int msd_bo_send(struct msc_gen_dev* dev, const void* buffer, size_t size); -// Empfängt Daten vom Gerät -int msd_bo_recv(struct msc_gen_dev* dev, void* buffer, size_t size); - -#endif diff --git a/src/modules/usb/msd/msd-cbi.c b/src/modules/usb/msd/msd-cbi.c deleted file mode 100644 index 66590fd..0000000 --- a/src/modules/usb/msd/msd-cbi.c +++ /dev/null @@ -1,86 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include - -#include "msd.h" -#include "msd-cbi.h" -#include "usb-lib.h" - -int msd_cbi_issue_scsi_cmd(struct msc_gen_dev* dev, const void* cmd_buffer, - size_t cmd_length, bool receive, size_t req_length) -{ - struct msc_cbi_dev* cbi = (struct msc_cbi_dev*)dev; - int8_t cmd[12] = { 0 }; - struct usb_setup_packet setup = { - .type = USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_IF, - .request = 0, - .value = 0, - .index = cbi->ifc_id, - .length = sizeof(cmd) - }; - - if (cmd_length > 12) { - cmd_length = 12; - } - - memcpy(cmd, cmd_buffer, cmd_length); - - int ret = usb_control_transfer(cbi->dev, NULL, &setup, cmd); - - /* - if (ret == -EHOSTDOWN) { - // kA, ob das etwas bringt - if (receive == true) { - usb_clear_halt(cbi->bip); - } else { - usb_clear_halt(cbi->bop); - } - } - */ - - return ret; -} - -int msd_cbi_send(struct msc_gen_dev* dev, const void* buffer, size_t size) -{ - struct msc_cbi_dev* cbi = (struct msc_cbi_dev*)dev; - return usb_bulk_transfer(cbi->bop, (void*)buffer, size, false); -} - -int msd_cbi_recv(struct msc_gen_dev* dev, void* buffer, size_t size) -{ - struct msc_cbi_dev* cbi = (struct msc_cbi_dev*)dev; - return usb_bulk_transfer(cbi->bip, buffer, size, true); -} diff --git a/src/modules/usb/msd/msd-cbi.h b/src/modules/usb/msd/msd-cbi.h deleted file mode 100644 index 66ff221..0000000 --- a/src/modules/usb/msd/msd-cbi.h +++ /dev/null @@ -1,71 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef MSD_CBI_H -#define MSD_CBI_H - -#include -#include - -#include "msd.h" -#include "usb-ddrv.h" - -// Beschreibt ein MSC-CBI-Gerät -struct msc_cbi_dev { - // Das MSD an sich - struct msc_gen_dev gen; - - // Das zugrunde liegende USB-Gerät - struct usb_prov_dev* dev; - // Die Interface-ID (MSDs sind immer Interfaces) - int ifc_id; - - // Alle wichtigen Endpunkte (EP0 ist in dev enthalten), interrupt darf NULL - // sein - struct usb_endpoint_descriptor* bulk_in; - struct usb_endpoint_descriptor* bulk_out; - struct usb_endpoint_descriptor* interrupt; - - // Die Pipes zu diesen Endpunkten, ip darf NULL sein - struct usb_pipe* bip; - struct usb_pipe* bop; - struct usb_pipe* ip; -}; - - -// Sendet einen SCSI-Befehl -int msd_cbi_issue_scsi_cmd(struct msc_gen_dev* dev, const void* buffer, - size_t cmd_length, bool receive, size_t req_length); -// Sendet Daten zum Gerät -int msd_cbi_send(struct msc_gen_dev* dev, const void* buffer, size_t size); -// Empfängt Daten vom Gerät -int msd_cbi_recv(struct msc_gen_dev* dev, void* buffer, size_t size); - -#endif diff --git a/src/modules/usb/msd/msd.h b/src/modules/usb/msd/msd.h deleted file mode 100644 index 3545df0..0000000 --- a/src/modules/usb/msd/msd.h +++ /dev/null @@ -1,154 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef MSD_H -#define MSD_H - -#include -#include -#include - -#include "usb-ddrv.h" - -// Beschreibt eine LUN eines MSC-Geräts (MSD) im Allgemeinen -struct msc_gen_dev { - // LUN - int lun; - - // Größe eines Blocks - uint64_t block_size; - // Anzahl Blöcke - uint64_t blocks; - - /** - * Führt einen SCSI-Befehl aus. - * - * @param dev Zielgerät - * @param cmd_buffer Speicherbereich, der den Befehl enthält - * @param cmd_length Länge dieses Befehls in Byte - * @param receive true, wenn empfangen werden soll - * @param req_length Länge der zu übertragenden Daten - * - * @return 0 bei Erfolg, sonst -errno - */ - int (*issue_scsi_cmd)(struct msc_gen_dev* dev, const void* cmd_buffer, - size_t cmd_length, bool receive, size_t req_length); - - /** - * Sendet Daten zu einem Gerät. - * - * @param dev Zielgerät - * @param buffer Speicherbereich, der die Daten enthält - * @param size Anzahl der zu sendenden Bytes - * - * @return 0 bei Erfolg, sonst -errno - */ - int (*send)(struct msc_gen_dev* dev, const void* buffer, size_t size); - - /** - * Empfängt Daten von einem Gerät. - * - * @param dev Quellgerät - * @param buffer Speicherbereich, in den die Daten gelesen werden sollen - * @param size Anzahl der zu empfangenden Bytes - * - * @return 0 bei Erfolg, sonst -errno - */ - int (*recv)(struct msc_gen_dev* dev, void* buffer, size_t size); -}; - -/** - * Wird von einem RPC-Handler aufgerufen, um zu prüfen, ob das neue Gerät ein - * MSD ist, mit dem der Treiber etwas anfangen kann. - * - * @param dev Neues Gerät - * - * @return 1 wenn das Gerät benutzt werden kann, sonst 0 - */ -int is_valid_msd(struct usb_prov_dev* dev); - -/** - * Wird von einem RPC-Handler aufgerufen, sobald ein neues MSD gefunden wurde. - * - * @param dev Neues Gerät - */ -void got_msd(struct usb_prov_dev* dev); - -/** - * Initialisiert das LostIO-Interface. - */ -void init_lostio_interface(void); - -/** - * Initialisiert das RPC-Interface. - */ -void init_rpc_interface(void); - -/** - * Initialisiert ein MSD auf SCSI-Ebene. - * - * @param dev Das MSD - * @param blocks Pointer zu einer Variable, in die die Anzahl der vorhandenen - * Blöcke geschrieben wird - * @param bsize Pointer zu einer Variable, in die die Größe eines Blocks - * geschrieben wird - * - * @return 1 bei Erfolg, sonst 0 - */ -int msd_scsi_init(struct msc_gen_dev* dev, uint64_t* blocks, - uint64_t* bsize); - -/** - * Liest von einem MSD per SCSI. - * - * @param dev Das MSD - * @param start_lba LBA des ersten zu lesenden Blocks - * @param dest Zielspeicherbereich - * @param blocks Anzahl der zu lesenden Blöcke - * - * @return Anzahl der gelesenen Bytes - */ -size_t msd_scsi_read(struct msc_gen_dev* dev, uint64_t start_lba, void* dest, - uint64_t blocks); - -/** - * Wird von einer MSD-spezifischen Initialisierungsroutine aufgerufen, um ein - * für die SCSI-Initialisierung vorbereitetes MSD bereitzustellen. - * - * @param dev Das neue Gerät - */ -void provide_msd(struct msc_gen_dev* dev); - -/** - * Registriert diesen Treiber beim USB-Bustreiber. - */ -void register_msddrv(void); - -#endif diff --git a/src/modules/usb/msd/rpc.c b/src/modules/usb/msd/rpc.c deleted file mode 100644 index d54fa01..0000000 --- a/src/modules/usb/msd/rpc.c +++ /dev/null @@ -1,89 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "msd.h" -#include "usb-ddrv.h" -#include "usb-server.h" - -extern pid_t usb_pid; - -void msd_got_device_handler(pid_t src, uint32_t corr_id, size_t length, - void* data) -{ - struct usb_prov_dev* pd; - int ret; - - if (length < sizeof(*pd)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - pd = malloc(sizeof(*pd)); - memcpy(pd, data, sizeof(*pd)); - - ret = is_valid_msd(pd); - rpc_send_int_response(src, corr_id, ret); - if (ret) { - got_msd(pd); - } - -} - -void init_rpc_interface(void) -{ - register_message_handler(RPC_USB_DDRV_PLUG_FNN, &msd_got_device_handler); -} - -void register_msddrv(void) -{ - struct usb_handle_devs hd = { - .vendor_id = -1, - .device_id = -1, - .class = 8, // MSC - .subclass = -1, - .protocol = -1 - }; - - if (!rpc_get_int(usb_pid, RPC_USB_SV_REGDD_FNN, sizeof(hd), (char*)&hd)) { - fprintf(stderr, "Could not register USB MSD driver.\n"); - } -} diff --git a/src/modules/usb/msd/scsi.c b/src/modules/usb/msd/scsi.c deleted file mode 100644 index 5a4e007..0000000 --- a/src/modules/usb/msd/scsi.c +++ /dev/null @@ -1,240 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include - -#include "msd.h" -#include "scsi.h" - -const char *dev_type[] = -{ - "magnetic disk", - "magnetic tape", - "printer", - "processor", - "write-once device", - "CD-ROM", - "scanner", - "optical medium", - "medium changer", - "communications device", - "?", - "?", - "RAID", - "enclosure services device", - "magnetic disk (simplified)", - "card reader/writer (optical)", - "?", - "object-based storage", - "automation/drive interface", - "?", - "?", - "?", - "?", - "?", - "?", - "?", - "?", - "?", - "?", - "?", - "well known LUN", - "?" -}; - -static int do_pkt(struct msc_gen_dev* dev, int receive, void* buffer1, - size_t length1, void* buffer2, size_t length2) -{ - while (dev->issue_scsi_cmd(dev, buffer1, length1, !!(receive == 1), - length2)) - { - struct scsi_sense sense; - struct scsi_cmd_request_sense req_sense = { - .cmd = { - .operation = SCSI_REQUEST_SENSE - }, - .alloc_len = sizeof(sense), - .lun = dev->lun - }; - - if (dev->issue_scsi_cmd(dev, &req_sense, sizeof(req_sense), true, - sizeof(sense))) - { - fprintf(stderr, "[usb-msd] Could not request sense\n"); - return -1; - } - - if (dev->recv(dev, &sense, sizeof(sense))) { - fprintf(stderr, "[usb-msd] Could not sense\n"); - return -1; - } - } - - if (receive == 1) { - return dev->recv(dev, buffer2, length2); - } else if (!receive) { - return dev->send(dev, buffer2, length2); - } - - return 0; -} - -int msd_scsi_init(struct msc_gen_dev* dev, uint64_t* blocks, uint64_t* bsize) -{ - struct scsi_inq_answer inq_ans; - struct scsi_cmd_inquiry inq = { - .cmd = { - .operation = SCSI_INQUIRY - }, - .alloc_len = htobe16(sizeof(inq_ans)), - .lun = dev->lun - }; - - if (do_pkt(dev, 1, &inq, sizeof(inq), &inq_ans, sizeof(inq_ans))) { - return 0; - } - - printf("[usb-msd] LUN %i: %s, ", dev->lun, dev_type[inq_ans.dev_type]); - - int last; - for (last = (int)sizeof(inq_ans.vendor) - 1; last >= 0; last--) { - if (inq_ans.vendor[last] != ' ') { - break; - } - } - - last++; - - int i; - - if (last) { - for (i = 0; i < last; i++) { - printf("%c", inq_ans.vendor[i]); - } - printf(" "); - } - - - for (last = (int)sizeof(inq_ans.product) - 1; last >= 0; last--) { - if (inq_ans.product[last] != ' ') { - break; - } - } - - last++; - - for (i = 0; i < last; i++) { - printf("%c", inq_ans.product[i]); - } - printf("\n"); - - // Erklär mir mal einer, wieso man das braucht - // I'd appreciate any explanation why this is required - int8_t zero[6] = { 0 }; - if (do_pkt(dev, -1, &zero, sizeof(zero), NULL, 0)) { - return 0; - } - - uint32_t cap_ans[2]; - struct scsi_cmd_read_capacity cap = { - .cmd = { - .operation = SCSI_READ_CAPACITY - } - }; - - if (do_pkt(dev, 1, &cap, sizeof(cap), &cap_ans, sizeof(cap_ans))) { - return 0; - } - - *blocks = (uint64_t)betoh32(cap_ans[0]) + 1LL; - *bsize = betoh32(cap_ans[1]); - - return 1; -} - -#define SCSI_READ 10 - -size_t msd_scsi_read(struct msc_gen_dev* dev, uint64_t start_lba, void* dest, - uint64_t blocks) -{ - #if SCSI_READ == 10 - struct scsi_cmd_read10 r10 = { - .cmd = { - .operation = SCSI_READ10 - }, - .lun = dev->lun, - .lba = htobe32(start_lba), - .length = htobe16(blocks) - }; - #elif SCSI_READ == 6 - struct scsi_cmd_read6 r6 = { - .cmd = { - .operation = SCSI_READ6 - }, - .lun = dev->lun, - .lba_lo = htobe16(start_lba), - .lba_hi = start_lba >> 16, - .length = blocks - }; - #else - struct scsi_cmd_read12 r12 = { - .cmd = { - .operation = SCSI_READ12 - }, - .lun = dev->lun, - .lba = htobe32(start_lba), - .length = htobe32(blocks) - }; - #endif - - #if SCSI_READ == 10 - if ((start_lba >> 32) || (blocks >> 16)) { - #elif SCSI_READ == 6 - if ((start_lba >> 21) || (blocks >> 8)) { - #else - if ((start_lba >> 32) || (blocks >> 32)) { - #endif - return 0; - } - - #if SCSI_READ == 10 - int ret = do_pkt(dev, 1, &r10, sizeof(r10), dest, blocks * dev->block_size); - #elif SCSI_READ == 6 - int ret = do_pkt(dev, 1, &r6, sizeof(r6), dest, blocks * dev->block_size); - #else - int ret = do_pkt(dev, 1, &r12, sizeof(r12), dest, blocks * dev->block_size); - #endif - - if (ret < 0) { - return 0; - } - return blocks * dev->block_size; -} diff --git a/src/modules/usb/msd/scsi.h b/src/modules/usb/msd/scsi.h deleted file mode 100644 index 98b88f9..0000000 --- a/src/modules/usb/msd/scsi.h +++ /dev/null @@ -1,219 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef SCSI_H -#define SCSI_H - -#include -#include - -// Funktionen zum Konvertieren von/zu Big Endian -#define htobe16(val) htons(val) -#define htobe32(val) htonl(val) -#define betoh16(val) htobe16(val) -#define betoh32(val) htobe32(val) - -// SCSI-Befehlscodes -#define SCSI_REQUEST_SENSE 0x03 -#define SCSI_READ6 0x08 -#define SCSI_INQUIRY 0x12 -#define SCSI_READ_CAPACITY 0x25 -#define SCSI_READ10 0x28 -#define SCSI_WRITE10 0x2A -#define SCSI_READ12 0xA8 - -// Minimaler SCSI-Befehl -struct scsi_cmd { - // Befehlscode - uint8_t operation; -} __attribute__((packed)); - -// REQUEST SENSE -// Gibt Informationen im Fehlerfall zurück. -struct scsi_cmd_request_sense { - struct scsi_cmd cmd; - unsigned rsvd1 : 5; - // LUN - unsigned lun : 3; - uint16_t rsvd2; - // Anzahl der Bytes, die der Host zu empfangen bereit ist - uint8_t alloc_len; - uint8_t control; -} __attribute__((packed)); - -// INQUIRY -// Gibt Informationen zum Gerät zurück. -struct scsi_cmd_inquiry { - struct scsi_cmd cmd; - unsigned evpd : 1; - unsigned rsvd : 4; - // LUN - unsigned lun : 3; - uint8_t page; - // Anzahl der Bytes, die der Host zu empfangen bereit ist - uint16_t alloc_len; - uint8_t control; -} __attribute__((packed)); - -// READ CAPACITY -// Gibt die LBA des letzten Blocks zurück sowie die Blockgröße -struct scsi_cmd_read_capacity { - struct scsi_cmd cmd; - unsigned reladdr : 1; - unsigned rsvd1 : 4; - // LUN - unsigned lun : 3; - uint32_t lba; - uint32_t rsvd2; -} __attribute__((packed)); - -// READ6 -// Liest Daten -struct scsi_cmd_read6 { - struct scsi_cmd cmd; - // Höherwertiger Teil der LBA - unsigned lba_hi : 5; - // LUN - unsigned lun : 3; - // Niederwertiger Teil der LBA - uint16_t lba_lo; - // Anzahl zu lesende Blöcke - uint8_t length; - uint8_t control; -} __attribute__((packed)); - -// READ10 -// Liest Daten -struct scsi_cmd_read10 { - struct scsi_cmd cmd; - unsigned reladdr : 1; - unsigned rsvd1 : 2; - unsigned fua : 1; - unsigned dpo : 1; - // LUN - unsigned lun : 3; - // LBA - uint32_t lba; - uint8_t rsvd2; - // Anzahl zu lesende Blöcke - uint16_t length; - uint8_t control; -} __attribute__((packed)); - -// READ12 -// Liest Daten -struct scsi_cmd_read12 { - struct scsi_cmd cmd; - unsigned reladdr : 1; - unsigned rsvd1 : 2; - unsigned fua : 1; - unsigned dpo : 1; - // LUN - unsigned lun : 3; - // LBA - uint32_t lba; - // Anzahl zu lesende Blöcke - uint32_t length; - uint16_t rsvd2; -} __attribute__((packed)); - -// WRITE10 -// Schreibt Daten -struct scsi_cmd_write10 { - struct scsi_cmd cmd; - unsigned reladdr : 1; - unsigned rsvd1 : 1; - unsigned ebp : 1; - unsigned fua : 1; - unsigned dpo : 1; - // LUN - unsigned lun : 3; - // LBA - uint32_t lba; - uint8_t rsvd2; - // Anzahl zu lesende Blöcke - uint16_t length; - uint8_t control; -} __attribute__((packed)); - -// Rückgabestruktur von SCSI SENSE -struct scsi_sense { - unsigned resp_code : 7; - unsigned valid : 1; - uint8_t segment; - unsigned sense_key : 4; - unsigned rsvd1 : 1; - unsigned ili : 1; - unsigned eom : 1; - unsigned filemark : 1; - uint32_t information; - uint8_t add_sense_length; - uint32_t cmd_spec_info; - uint8_t add_sense_code; - uint8_t add_sense_code_qual; - uint8_t field_replacable_unit_code; - unsigned sense_key_spec1 : 7; - unsigned sksv : 1; - uint8_t sense_key_spec2; - uint8_t sense_key_spec3; -} __attribute__((packed)); - -// Rückgabestruktur von SCSI INQUIRY -struct scsi_inq_answer { - // Gerätetype - unsigned dev_type : 5; - unsigned qualifier : 3; - unsigned type_mod : 7; - unsigned rmb : 1; - unsigned ansi_version : 3; - unsigned ecma_version : 3; - unsigned iso_version : 2; - unsigned resp_data_format : 4; - unsigned rsvd1 : 2; - unsigned trm_iop : 1; - unsigned aec : 1; - uint8_t add_length; - uint16_t rsvd2; - unsigned soft_rst : 1; - unsigned cmd_queue : 1; - unsigned rsvd3 : 1; - unsigned linked : 1; - unsigned sync : 1; - unsigned wbus16 : 1; - unsigned wbus32 : 1; - unsigned rel_adr : 1; - // Herstellername - char vendor[8]; - // Produktname - char product[16]; - uint32_t revision; -} __attribute__((packed)); - -#endif diff --git a/src/modules/usb/msd/usb-lib.c b/src/modules/usb/msd/usb-lib.c deleted file mode 100644 index fe4eb69..0000000 --- a/src/modules/usb/msd/usb-lib.c +++ /dev/null @@ -1,207 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include -#include - -#include "usb-ddrv.h" -#include "usb-server.h" -#include "usb-structs.h" -#include "usb-trans.h" - -pid_t usb_pid; - -int usb_get_number_of_endpoints(struct usb_prov_dev* dev) -{ - struct usb_devctrl_gne rq = { - .req = { - .type = USB_DC_NUM_ENDPOINTS - }, - .dev = *dev - }; - - return rpc_get_int(usb_pid, RPC_USB_SV_DEVCT_FNN, sizeof(rq), (char*)&rq); -} - -int usb_get_interface_id(struct usb_prov_dev* dev) -{ - struct usb_devctrl_gii giid = { - .req = { - .type = USB_DC_GET_IFC_ID - }, - .dev = *dev - }; - - return rpc_get_int(usb_pid, RPC_USB_SV_DEVCT_FNN, sizeof(giid), - (char*)&giid); -} - -void usb_get_endpoint_info(struct usb_prov_dev* dev, int ep_number, - struct usb_endpoint_descriptor* ep_desc) -{ - struct usb_devctrl_gep gep = { - .req = { - .type = USB_DC_GET_ENDPOINT - }, - .dev = *dev, - .ep_number = ep_number - }; - - response_t* r = rpc_get_response(usb_pid, RPC_USB_SV_DEVCT_FNN, - sizeof(gep), (char*)&gep); - - memcpy(ep_desc, r->data, sizeof(*ep_desc)); - free(r->data); - free(r); -} - -void usb_create_pipe(struct usb_prov_dev* dev, int ep_number, - struct usb_pipe* pipe) -{ - struct usb_devctrl_gpp gp = { - .req = { - .type = USB_DC_GET_PIPE - }, - .dev = *dev, - .ep_number = ep_number - }; - - response_t* r = rpc_get_response(usb_pid, RPC_USB_SV_DEVCT_FNN, - sizeof(gp), (char*)&gp); - - memcpy(pipe, r->data, sizeof(*pipe)); - free(r->data); - free(r); -} - -int usb_control_transfer(struct usb_prov_dev* dev, struct usb_pipe* pipe, - struct usb_setup_packet* setup, void* buffer) -{ - uint32_t shm = 0; - void* shm_addr = NULL; - - if (setup->length) { - shm = create_shared_memory(setup->length); - shm_addr = open_shared_memory(shm); - - if ((buffer != NULL) && !(setup->type & USB_SETUP_IN)) { - memcpy(shm_addr, buffer, setup->length); - } - } - - struct usb_devctrl_tct tctrq = { - .req = { - .type = USB_DC_TRSF_CONTROL - }, - .setup = *setup, - .shm = shm - }; - - if (pipe != NULL) { - tctrq.dst.pipe = *pipe; - tctrq.ep0 = false; - } else { - tctrq.dst.dev_id = dev->dev_id; - tctrq.ep0 = true; - } - - int ret = rpc_get_int(usb_pid, RPC_USB_SV_DEVCT_FNN, sizeof(tctrq), - (char*)&tctrq); - - if (setup->length && (buffer != NULL) && (setup->type & USB_SETUP_IN)) { - memcpy(buffer, shm_addr, setup->length); - } - - if (shm_addr != NULL) { - close_shared_memory(shm); - } - - return ret; -} - -int usb_bulk_transfer(struct usb_pipe* pipe, void* buffer, size_t length, - bool receive) -{ - uint32_t shm = 0; - void* shm_addr = NULL; - - if (length) { - shm = create_shared_memory(length); - shm_addr = open_shared_memory(shm); - - if ((buffer != NULL) && !receive) { - memcpy(shm_addr, buffer, length); - } - } - - struct usb_devctrl_tbk tbkrq = { - .req = { - .type = USB_DC_TRSF_BULK - }, - .pipe = *pipe, - .length = length, - .shm = shm - }; - - int ret = rpc_get_int(usb_pid, RPC_USB_SV_DEVCT_FNN, sizeof(tbkrq), - (char*)&tbkrq); - - if (length && (buffer != NULL) && receive) { - memcpy(buffer, shm_addr, length); - } - - if (shm_addr != NULL) { - close_shared_memory(shm); - } - - if (ret >= 0) { - pipe->trs.toggle = ret; - ret = 0; - } - - return ret; -} - -void usb_clear_halt(struct usb_pipe* pipe) -{ - struct usb_devctrl_clh clh = { - .req = { - .type = USB_DC_CLEAR_HALT - }, - .pipe = *pipe - }; - - rpc_get_int(usb_pid, RPC_USB_SV_DEVCT_FNN, sizeof(clh), (char*)&clh); -} diff --git a/src/modules/usb/msd/usb-lib.h b/src/modules/usb/msd/usb-lib.h deleted file mode 100644 index 2f7fb9b..0000000 --- a/src/modules/usb/msd/usb-lib.h +++ /dev/null @@ -1,124 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_LIB_H -#define USB_LIB_H - -#include -#include - -#include "usb-ddrv.h" -#include "usb-server.h" -#include "usb-structs.h" -#include "usb-trans.h" - -/* - * Hinweis: Wenn hier die Rede von einem USB-Gerät ist, so ist ein logisches - * USB-Gerät gemeint. Dies kann sowohl ein echtes (physisches) Gerät sein oder - * aber auch nur ein Interface dieses Geräts (meistens letzteres, da so gut - * wie alle USB-Geräteklassen auf Interfaceebene arbeiten und somit die meisten - * physischen USB-Geräte genau ein Interface besitzen). - */ - -/** - * Führt einen USB-Bulktransfer durch. - * - * @param pipe Zu verwendende Pipe - * @param buffer Zu bearbeitender Speicherbereich - * @param length Länge des Transfers - * @param receive true, wenn empfangen werden soll, sonst false. - * - * @return 0 bei Erfolg, sonst -errno. - */ -int usb_bulk_transfer(struct usb_pipe* pipe, void* buffer, size_t length, - bool receive); - -/** - * Entstallt einen Endpunkt. - * - * @param pipe Eine zum Endpunkt gehörige Pipe. - */ -void usb_clear_halt(struct usb_pipe* pipe); - -/** - * Führt einen USB-Controltransfer durch. - * - * @param dev Gerät - * @param pipe Zu verwendende Pipe oder NULL für die Default Control Pipe - * @param setup SETUP-Paket - * @param buffer Speicherbereich für die Datenphase - * - * @return 0 bei Erfolg, sonst -errno. - */ -int usb_control_transfer(struct usb_prov_dev* dev, struct usb_pipe* pipe, - struct usb_setup_packet* setup, void* buffer); - -/** - * Erstellt eine USB-Pipe. - * - * @param dev Zielgerät - * @param ep_number Nummer des Zielendpunkts (Nummer innerhalb des Interfaces, - * nicht ID!) - * @param pipe Speicherbereich, in dem die Daten der Pipe abgelegt werden - * sollen. - */ -void usb_create_pipe(struct usb_prov_dev* dev, int ep_number, - struct usb_pipe* pipe); - -/** - * Empfängt Informationen zu einem Endpunkt. - * - * @param dev Gerät - * @param ep_number Nummer des Endpunkts - * @param ep_desc Pointer zu einem Speicherbereich, der einen Endpoint- - * Descriptor aufnehmen kann. - */ -void usb_get_endpoint_info(struct usb_prov_dev* dev, int ep_number, - struct usb_endpoint_descriptor* ep_desc); - -/** - * Gibt die Interface-ID des logischen USB-Geräts zurück. - * - * @param dev Gerät - * - * @return Interface-ID - */ -int usb_get_interface_id(struct usb_prov_dev* dev); - -/** - * Gibt die Anzahl an Endpunkten des USB-Geräts zurück. - * - * @param dev Logisches Gerät - * - * @return Anzahl Endpunkte - */ -int usb_get_number_of_endpoints(struct usb_prov_dev* dev); - -#endif diff --git a/src/modules/usb/uhci/Makefile.all b/src/modules/usb/uhci/Makefile.all deleted file mode 100644 index cf401d2..0000000 --- a/src/modules/usb/uhci/Makefile.all +++ /dev/null @@ -1,8 +0,0 @@ -shopt -s extglob -source $LOST_BUILDMK_ROOT/config.sh - -CC="$CC -I../include" - -echo "LD $1/modules/uhci" -$LOST_TOOLS_LD $LDSCRIPT -ouhci.mod *.o --start-group $2 --end-group -$LOST_TOOLS_STRIP -s uhci.mod -o $1/modules/uhci diff --git a/src/modules/usb/uhci/ctrl.c b/src/modules/usb/uhci/ctrl.c deleted file mode 100644 index ad1c3a0..0000000 --- a/src/modules/usb/uhci/ctrl.c +++ /dev/null @@ -1,96 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include -#include - -#include "uhci.h" -#include "usb-hub.h" - -void uhci_reset_port(struct uhci* hc, int port) -{ - if (hc == NULL) - return; - - outw(hc->iobase + UHCI_RPORTS + port * 2, RPORT_ENABLE | RPORT_RESET | RPORT_CSC); - msleep(50); - uhci_set_port_status(hc, port, 1); - msleep(10); -} - -int uhci_scan_port(struct uhci* hc, int port) -{ - int ret; - - if (hc == NULL) - return 0; - - uint_fast16_t val = inw(hc->iobase + UHCI_RPORTS + port * 2); - - if (!(val & RPORT_DEVICE)) { - ret = 0; - } else { - ret = USB_HUB_DEVICE; - if (val & RPORT_LOSPD) { - ret |= USB_HUB_LOW_SPEED; - } else { - ret |= USB_HUB_FULL_SPEED; - } - - if (val & RPORT_CSC) { - ret |= USB_HUB_CHANGE; - outw(hc->iobase + UHCI_RPORTS + port * 2, (val & RPORT_ENABLE) | RPORT_CSC); - } - } - - return ret; -} - -void uhci_set_port_status(struct uhci* hc, int port, int status) -{ - if (hc == NULL) - return; - - int previous = inw(hc->iobase + UHCI_RPORTS + port * 2) & RPORT_ENABLE; - outw(hc->iobase + UHCI_RPORTS + port * 2, (status ? RPORT_ENABLE : 0) | RPORT_CSC); - msleep(50); - if (status && !previous) - { - outw(hc->iobase + UHCI_RPORTS + port * 2, RPORT_ENABLE | RPORT_RESUME); - msleep(20); - outw(hc->iobase + UHCI_RPORTS + port * 2, RPORT_ENABLE | RPORT_CSC); - msleep(10); - } -} diff --git a/src/modules/usb/uhci/init.c b/src/modules/usb/uhci/init.c deleted file mode 100644 index 7a4d6f6..0000000 --- a/src/modules/usb/uhci/init.c +++ /dev/null @@ -1,265 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "uhci.h" - -extern pid_t usb_pid, pci_pid; - - -extern struct uhci* uhcis[IRQ_COUNT]; -extern void uhci_irq_handler(uint8_t irq); - -static uint16_t pci_inw(struct pci_device* dev, int reg) -{ - struct pci_config_space_access csa = { - .type = PCI_CSA_READ16, - .bus = dev->bus, - .device = dev->device, - .function = dev->function, - .reg = reg - }; - - return rpc_get_dword(pci_pid, RPC_PCI_CONFIG_SPACE_ACCESS, sizeof(csa), - (char*)&csa); -} - -static void pci_outw(struct pci_device* dev, int reg, uint16_t val) -{ - struct pci_config_space_access csa = { - .type = PCI_CSA_WRITE16, - .bus = dev->bus, - .device = dev->device, - .function = dev->function, - .reg = reg, - .value = val - }; - - rpc_get_dword(pci_pid, RPC_PCI_CONFIG_SPACE_ACCESS, sizeof(csa), - (char*)&csa); -} - -void init_uhc(struct pci_device* dev, size_t devlen) -{ - devlen -= sizeof(*dev); - int8_t* data = (int8_t*)(dev + 1); - - struct pci_resource pres; - uint16_t iobase = 0, iosize = 0; - - while (devlen >= sizeof(pres)) { - memcpy(&pres, data, sizeof(pres)); - - devlen -= sizeof(pres); - data += sizeof(pres); - - if (pres.type == PCI_RESOURCE_PORT) { - iobase = pres.start; - iosize = pres.length; - break; - } - } - - if (!iobase) { - return; - } - - struct uhci* ndev = calloc(1, sizeof(*ndev)); - ndev->pci = dev; - ndev->iobase = iobase; - ndev->rh = calloc(1, sizeof(*ndev->rh)); - - ndev->rh->ports = (iosize - 0x10) >> 1; - - request_ports(iobase, iosize); - uhcis[dev->irq] = ndev; - register_intr_handler(TYNDUR_IRQ_OFFSET + dev->irq, &uhci_irq_handler); - - outw(iobase + UHCI_USBCMD, GRESET); - msleep(50); - outw(iobase + UHCI_USBCMD, 0); - - int i; - for (i = 2; i < ndev->rh->ports; i++) { - if (!(inw(iobase + UHCI_RPORTS + i*2) & 0x0080) || - (inw(iobase + UHCI_RPORTS + i*2) == 0xFFFF)) - { - // Ungültige Werte für einen Rootport, also ist das hier keiner - ndev->rh->ports = i; - break; - } - } - - // Mehr als sieben Rootports sind leicht ungewöhnlich - if (ndev->rh->ports > 7) { - ndev->rh->ports = 2; - } - - int legsup = pci_inw(dev, UHCI_PCI_LEGSUP); - int cmd = inw(iobase + UHCI_USBCMD); - int intr = inw(iobase + UHCI_USBINTR); - - if ((legsup & - ~(UHCI_LEGSUP_STATUS | UHCI_LEGSUP_NO_CHG | UHCI_LEGSUP_PIRQ)) || - (cmd & USB_RUN) || (cmd & CONFIGURE) || !(cmd & GLOB_SUSP_MODE) || - (intr & 0x000F)) - { - // Damit uns nicht die IRQs um die Ohren fliegen - outw(iobase + UHCI_USBSTS, 0x3F); - - // Einen Frame abwarten - msleep(1); - - // Alle LEGSUP-Statusbits löschen - pci_outw(dev, UHCI_PCI_LEGSUP, UHCI_LEGSUP_STATUS); - - // Resetten - outw(iobase + UHCI_USBCMD, HCRESET); - // Ende des Resets abwarten - for (i = 0; (inw(iobase + UHCI_USBCMD) & HCRESET) && (i < 50); i++) { - msleep(10); - } - - // Interrupts erstmal deaktivieren - outw(iobase + UHCI_USBINTR, 0); - // Den gesamten HC ebenso - outw(iobase + UHCI_USBCMD, 0); - - // Alle Ports "runterfahren" - for (i = 0; i < ndev->rh->ports; i++) { - outw(iobase + UHCI_RPORTS + i * 2, 0); - } - } - - ndev->frame_list = mem_allocate(4096, 0); - assert(ndev->frame_list); - ndev->p_frame_list = (uintptr_t)get_phys_addr(ndev->frame_list); - assert(ndev->p_frame_list); - ndev->frame_list_lock = 0; - - ndev->queue_head = mem_allocate(sizeof(*ndev->queue_head), 0); - assert(ndev->queue_head); - ndev->p_queue_head = (uintptr_t)get_phys_addr(ndev->queue_head); - assert(ndev->p_queue_head); - ndev->queue_head_lock = 0; - - // Alle Pointer auf "invalid" setzen - ndev->queue_head->next = 1; - ndev->queue_head->transfer = 1; - ndev->queue_head->q_first = NULL; - ndev->queue_head->q_last = NULL; - - for (i = 0; i < 1024; i++) { - ndev->frame_list[i] = ndev->p_queue_head | 2; - } - - // Alle 1000 µs ein Frame (Standardwert) - outb(iobase + UHCI_SOFMOD, 0x40); - // Unsere Frameliste eintragen - outl(iobase + UHCI_FRBASEADD, ndev->p_frame_list); - // Framenummer zurücksetzen - outw(iobase + UHCI_FRNUM, 0); - - // Hier setzen wir das PIRQ-Bit und deaktivieren sämtlichen Legacy Support - pci_outw(dev, UHCI_PCI_LEGSUP, UHCI_LEGSUP_PIRQ); - - // HC starten - outw(iobase + UHCI_USBCMD, USB_RUN | CONFIGURE | MAXP); - // Alle Interrupts aktivieren - outw(iobase + UHCI_USBINTR, 0xF); - - // Connect-Status-Change-Bit zurücksetzen - for (i = 0; i < ndev->rh->ports; i++) { - outw(iobase + UHCI_RPORTS + i * 2, RPORT_CSC); - } - - // Resumesignal treiben (egal, obs was bringt) - outw(iobase + UHCI_USBCMD, USB_RUN | CONFIGURE | MAXP | FORCE_GRESUME); - msleep(20); - outw(iobase + UHCI_USBCMD, USB_RUN | CONFIGURE | MAXP); - msleep(100); - - register_uhci(ndev); -} - -int check_pci(void) -{ - struct init_dev_desc* devs; - - int ret = init_dev_list(&devs); - if (ret < 0) { - return 0; - } - - int got_something = 0; - - int8_t* data = (int8_t*)devs; - for (;;) { - size_t size; - - if (ret < sizeof(*devs)) { - break; - } - - devs = (struct init_dev_desc*)data; - size = sizeof(*devs) + devs->bus_data_size; - if (ret < size) { - break; - } - - if (devs->type == 10) { - struct pci_device* dev = (struct pci_device*)devs->bus_data; - if ((dev->class_id == 0x0C) && (dev->subclass_id == 0x03) && - (dev->interface_id == 0x00)) - { - got_something = 1; - init_uhc(dev, size); - } - } - - ret -= size; - data += size; - } - - return got_something; -} diff --git a/src/modules/usb/uhci/main.c b/src/modules/usb/uhci/main.c deleted file mode 100644 index 452f2ad..0000000 --- a/src/modules/usb/uhci/main.c +++ /dev/null @@ -1,73 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include - -#include -#include -#include -#include - -#include "uhci.h" - -extern pid_t usb_pid, pci_pid; - -int main(void) -{ - init_messaging(); - - // Warten, damit dieser Prozess nach usb1 (und möglichst nach usb-msd) - // weiterläuft, sollten diese Treiber gleichzeitig gestartet werden - // (z. B. Bootvorgang) - msleep(2000); - - usb_pid = init_service_get("usb1"); - if (!usb_pid) { - fprintf(stderr, "You need to start the usb1 driver first.\n"); - return 1; - } - - pci_pid = init_service_get("pci"); - if (!pci_pid) { - fprintf(stderr, "You need to start the pci driver first.\n"); - return 1; - } - - init_rpc_interface(); - - if (!check_pci()) { - return 0; - } - - for (;;) { - wait_for_rpc(); - } -} diff --git a/src/modules/usb/uhci/rpc.c b/src/modules/usb/uhci/rpc.c deleted file mode 100644 index e3f862e..0000000 --- a/src/modules/usb/uhci/rpc.c +++ /dev/null @@ -1,297 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "uhci.h" -#include "usb-hc.h" -#include "usb-server.h" - -// #define DEBUG -// #define SHOW_ERRORS - -pid_t usb_pid, pci_pid; - -struct uhci* uhcis[IRQ_COUNT] = { NULL }; - -void uhci_irq_handler(uint8_t irq) -{ - if ((irq < TYNDUR_IRQ_OFFSET) || (irq >= TYNDUR_IRQ_OFFSET + IRQ_COUNT)) { - return; - } - - struct uhci* hc = uhcis[irq - TYNDUR_IRQ_OFFSET]; - - int status = inw(hc->iobase + UHCI_USBSTS); - - if (!status) { - return; - } - - if ((status & 0x10) || (status & 0x08)) { - fprintf(stderr, "[uhci] SERIOUS HOST ERROR CONDITION (STS: 0x%02X)\n", status); - return; - } - - if (status & 0x20) { - fprintf(stderr, "[uhci] Erm, wtf. HC halted...\n"); - return; - } - - if (status & ~0x03) { - return; - } - - struct uhci_td *td, *ntd; - - lock(&hc->queue_head_lock); - - td = (struct uhci_td*)hc->queue_head->q_first; - - while (td != NULL) { - /* - #ifdef DEBUG - printf("Active: %i (next: 0x%08X)\n", td->flags.bits.active, td->next); - #endif - */ - if (td->flags.bits.active) { - if (td->active_count--) { - break; - } else { - #ifdef DEBUG - printf("Active timeout on 0x%08X/0x%08X, QH -> 0x%08X/0x%08X\n", - td, td->ptd, hc->queue_head->q_first, - hc->queue_head->transfer); - #endif - td->flags.bits.active = 0; - } - } - - #ifdef DEBUG - printf("Finished td %s %i:%i, flags: 0x%08X\n", - (td->pid == 0x69) ? "from" : "to", td->addr, td->ep, - td->flags.flags); - #endif - - if (td->result != NULL) { - if (td->flags.bits.spd) { - #ifdef SHOW_ERRORS - printf("[uhci] Short packet detected\n"); - #endif - - if (hc->queue_head->transfer == td->ptd) { - hc->queue_head->transfer = td->next; - } - - *td->result = EMSGSIZE; - } else if ((status == 0x01) && !td->flags.bits.e_stall) { - #ifdef DEBUG - printf("[uhci] Fine, actual len: %i; max len: %i\n", - td->flags.bits.act_len, td->maxlen); - #endif - if (((td->flags.bits.act_len + 1) & 0x7FF) < ((td->maxlen + 1) & 0x7FF)) { - *td->result = EMSGSIZE; - } else { - *td->result = 0; - } - } else { - td->pipe->toggle = td->toggle; - // TODO Weitere Transfers aus der Liste entfernen - #ifdef SHOW_ERRORS - printf("[uhci] Something failed, errors: %c%c%c%c%c%c\n", - td->flags.bits.e_bitstuff ? 'b' : '-', - td->flags.bits.e_crc_timeout ? 'c' : '-', - td->flags.bits.e_nak ? 'n' : '-', - td->flags.bits.e_babble ? 'q' : '-', - td->flags.bits.e_buffer ? 'f' : '-', - td->flags.bits.e_stall ? 's' : '-'); - printf("[uhci] Next'll be: 0x%08X/0x%08X\n", td->next, - td->q_next); - #endif - - if (hc->queue_head->transfer == td->ptd) { - hc->queue_head->transfer = td->next; - } - - if (td->flags.bits.e_stall) { - *td->result = EHOSTDOWN; - } else { - *td->result = EAGAIN; - } - } - } - - ntd = td->q_next; - if (td->free_on_completion) { - mem_free(td, 4096); - } - - if (td->next & 1) { - td = ntd; - break; - } - - td = ntd; - } - - hc->queue_head->q_first = td; - if (td == NULL) { - hc->queue_head->q_last = NULL; - hc->queue_head->transfer = 1; - } - - unlock(&hc->queue_head_lock); - - outw(hc->iobase + UHCI_USBSTS, status); -} - -void uhci_hct_handler(pid_t src, uint32_t corr_id, size_t length, void* data) -{ - struct usb_hc_trs* trs = data; - void* shm; - int result; - - if (length < sizeof(*trs)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - - switch (trs->type) - { - case USB_TRST_CONTROL: - if (length < sizeof(struct usb_hc_trs_ctrl)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - struct usb_hc_trs_ctrl* ctrl = data; - - shm = open_shared_memory(ctrl->shm); - result = uhci_do_control(trs->hc.hcd_avail, &trs->pipe, - &ctrl->setup, shm); - close_shared_memory(ctrl->shm); - rpc_send_int_response(src, corr_id, result); - break; - case USB_TRST_BULK: - if (length < sizeof(struct usb_hc_trs_bulk)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - struct usb_hc_trs_bulk* bulk = data; - - shm = open_shared_memory(bulk->shm); - result = uhci_do_bulk(trs->hc.hcd_avail, &trs->pipe, - bulk->receive, shm, bulk->length); - close_shared_memory(bulk->shm); - - if (!result) { - // Irgendwie muss der Status des Bits ja zurück - result = trs->pipe.toggle; - } - rpc_send_int_response(src, corr_id, result); - break; - default: - rpc_send_int_response(src, corr_id, -EINVAL); - break; - } -} - -void uhci_hcc_handler(pid_t src, uint32_t corr_id, size_t length, void* data) -{ - struct usb_hc_ctrl_req* rq = data; - struct usb_hc_ctrl_scan* rqsc = data; - struct usb_hc_ctrl_dsbl* rqdbl = data; - struct usb_hc_ctrl_rst* rqrst = data; - - if (length < sizeof(*rq)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - switch (rq->type) - { - case HC_CTRL_SCAN_PORT: - if (length < sizeof(*rqsc)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - int result = uhci_scan_port(rq->hc.hcd_avail, rqsc->port); - rpc_send_int_response(src, corr_id, result); - break; - case HC_CTRL_DSBL_PORT: - if (length < sizeof(*rqdbl)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - uhci_set_port_status(rq->hc.hcd_avail, rqdbl->port, 0); - rpc_send_int_response(src, corr_id, 1); - break; - case HC_CTRL_RESET: - if (length < sizeof(*rqrst)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - uhci_reset_port(rq->hc.hcd_avail, rqrst->port); - rpc_send_int_response(src, corr_id, 1); - break; - case HC_CTRL_ADD_PIPE: - rpc_send_int_response(src, corr_id, 1); - break; - default: - rpc_send_int_response(src, corr_id, 0); - break; - } -} - -void init_rpc_interface(void) -{ - register_message_handler(RPC_USB_HC_CONTROL_FNN, &uhci_hcc_handler); - register_message_handler(RPC_USB_HC_TRANSFER_FNN, &uhci_hct_handler); -} - -void register_uhci(struct uhci* hc) -{ - struct usb_hc rhc = { - .root_ports = hc->rh->ports, - .hcd_avail = hc - }; - - if (!rpc_get_int(usb_pid, RPC_USB_SV_REGHC_FNN, sizeof(rhc), (char*)&rhc)) { - fprintf(stderr, "Could not register UHCI.\n"); - } -} diff --git a/src/modules/usb/uhci/trans.c b/src/modules/usb/uhci/trans.c deleted file mode 100644 index b9c82c2..0000000 --- a/src/modules/usb/uhci/trans.c +++ /dev/null @@ -1,316 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include - -#include "uhci.h" -#include "usb-hc.h" -#include "usb-trans.h" - -#define USB_SETUP_OUT (0 << 7) -#define USB_SETUP_IN (1 << 7) - -int insert_td(struct uhci* hc, uintptr_t ptd, struct uhci_td* td); - -int uhci_do_control(struct uhci* hc, struct usb_trans_pipe* pipe, - struct usb_setup_packet* setup, void* buffer) -{ - if ((pipe->speed != USB_HUB_LOW_SPEED) && - (pipe->speed != USB_HUB_FULL_SPEED)) - { - return -EPROTOTYPE; - } - - struct uhci_td* td = mem_allocate(sizeof(*td), 0); - assert(td); - uintptr_t ptd = (uintptr_t)get_phys_addr(td); - assert(ptd); - - volatile int status; - - do { - status = EALREADY; - - td->next = 1; - if (pipe->speed == USB_HUB_LOW_SPEED) { - td->flags.flags = 0x1D800000; - } else { - td->flags.flags = 0x19800000; - } - td->pid = USB_SETUP; - td->toggle = 0; - td->addr = pipe->usb_addr; - td->ep = pipe->ep_id; - - td->maxlen = sizeof(*setup) - 1; - td->buffer = (uintptr_t)get_phys_addr(setup); - - td->result = &status; - td->free_on_completion = 0; - - td->pipe = pipe; - - while (insert_td(hc, ptd, td)); - - while (status == EALREADY); - } while (status == EAGAIN); - - if (status) { - if (status == EHOSTDOWN) { - pipe->stalled = 1; - } - - mem_free(td, 4096); - return -status; - } - - pipe->toggle = 1; - - int remaining = setup->length; - int direction, done = 0; - - if (setup->type & USB_SETUP_IN) { - direction = USB_IN; - } else { - direction = USB_OUT; - } - - while (remaining) { - int this_len = (remaining > pipe->mps) ? pipe->mps : remaining; - - remaining -= this_len; - - do { - status = EALREADY; - - td->next = 1; - if (pipe->speed == USB_HUB_LOW_SPEED) { - td->flags.flags = 0x1D800000; - } else { - td->flags.flags = 0x19800000; - } - td->pid = direction; - td->toggle = pipe->toggle; - td->addr = pipe->usb_addr; - td->ep = pipe->ep_id; - - td->maxlen = (unsigned int)(this_len - 1) & 0x7FF; - td->buffer = (uintptr_t)get_phys_addr((void*)((uintptr_t)buffer + done)); - - td->result = &status; - td->free_on_completion = 0; - - while (insert_td(hc, ptd, td)); - - while (status == EALREADY); - } while (status == EAGAIN); - - if (status && (status != EMSGSIZE)) { - if (status == EHOSTDOWN) { - pipe->stalled = 1; - } - - mem_free(td, 4096); - return -status; - } - - pipe->toggle ^= 1; - done += this_len; - - if (status == EMSGSIZE) - break; - } - - if (direction == USB_IN) { - direction = USB_OUT; - } else { - direction = USB_IN; - } - - do { - status = EALREADY; - - td->next = 1; - if (pipe->speed == USB_HUB_LOW_SPEED) { - td->flags.flags = 0x1D800000; - } else { - td->flags.flags = 0x19800000; - } - td->pid = direction; - td->toggle = 1; - td->addr = pipe->usb_addr; - td->ep = pipe->ep_id; - - td->maxlen = 0x7FF; - td->buffer = 0; - - td->result = &status; - td->free_on_completion = 0; - - while (insert_td(hc, ptd, td)); - - while (status == EALREADY); - } while (status == EAGAIN); - - if (status == EHOSTDOWN) { - pipe->stalled = 1; - } - - pipe->toggle = 0; - mem_free(td, sizeof(*td)); - - return -status; -} - -int uhci_do_bulk(struct uhci* hc, struct usb_trans_pipe* pipe, bool receive, - void* buffer, size_t length) -{ - if ((pipe->speed != USB_HUB_LOW_SPEED) && - (pipe->speed != USB_HUB_FULL_SPEED)) - { - return -EPROTOTYPE; - } - - if (!length) { - return -EINVAL; - } - - // printf("Issuing bulk (%i B expected)\n", length); - - struct uhci_td* td = mem_allocate(sizeof(*td), 0); - assert(td); - uintptr_t ptd = (uintptr_t)get_phys_addr(td); - assert(ptd); - - volatile int res; - - int remaining = length; - int done = 0; - - while (remaining) { - // printf("%i B to go\n", remaining); - int this_len = (remaining > pipe->mps) ? pipe->mps : remaining; - - remaining -= this_len; - - td->next = 1; - if (pipe->speed == USB_LOW_SPEED) { - td->flags.flags = 0x1D800000; - } else { - td->flags.flags = 0x19800000; - } - if (receive) { - td->pid = USB_IN; - } else { - td->pid = USB_OUT; - } - td->toggle = pipe->toggle; - td->addr = pipe->usb_addr; - td->ep = pipe->ep_id; - - td->maxlen = (unsigned int)(this_len - 1) & 0x7FF; - td->buffer = (uintptr_t) - get_phys_addr((void *)((uintptr_t)buffer + done)); - - res = EALREADY; - td->result = &res; - td->free_on_completion = 0; - td->pipe = pipe; - - // printf("Inserting TD\n"); - while (insert_td(hc, ptd, td)); - - // printf("Waiting for transmission\n"); - while (res == EALREADY); - // printf("Thru\n"); - - if (res == EAGAIN) { - remaining += this_len; - continue; - } - - if (res) { - return -res; - } - - pipe->toggle ^= 1; - - done += this_len; - } - - if (res == EHOSTDOWN) { - pipe->stalled = 1; - } - - mem_free(td, sizeof(*td)); - - return -res; -} - -int insert_td(struct uhci* hc, uintptr_t ptd, struct uhci_td* td) -{ - struct uhci_td *last; - - lock(&hc->queue_head_lock); - - td->ptd = ptd; - td->q_next = NULL; - td->active_count = 10; - - if (hc->queue_head->transfer & 1) { - // Insert as first element to be done - if (hc->queue_head->q_last != NULL) { - hc->queue_head->q_last->q_next = td; - } else { - hc->queue_head->q_first = td; - } - - hc->queue_head->q_last = td; - hc->queue_head->transfer = ptd; - } else { - last = (struct uhci_td*)hc->queue_head->q_last; - #ifdef DEBUG - printf("[uhci] Will append to 0x%08X (p 0x%08X)\n", last, hc->queue_head->transfer); - #endif - - last->q_next = td; - hc->queue_head->q_last = td; - last->next = ptd | 4; - } - - unlock(&hc->queue_head_lock); - - return 0; -} diff --git a/src/modules/usb/uhci/uhci.h b/src/modules/usb/uhci/uhci.h deleted file mode 100644 index c6b758c..0000000 --- a/src/modules/usb/uhci/uhci.h +++ /dev/null @@ -1,269 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef UHCI_H -#define UHCI_H - -#include -#include - -#include "usb-hc.h" -#include "usb-trans.h" - - -#define TYNDUR_IRQ_OFFSET 0x20 -#define IRQ_COUNT 0x10 - -// Befehlsregister (uint16_t) -#define UHCI_USBCMD 0x00 -// Statusregister (uint16_t) -#define UHCI_USBSTS 0x02 -// Interruptregister (uint16_t) -#define UHCI_USBINTR 0x04 -// Aktuelle Framenummer (uint16_t) -#define UHCI_FRNUM 0x06 -// Pointer zur Frameliste (uint32_t) -#define UHCI_FRBASEADD 0x08 -// Bestimmt die genaue Länge eines Frames (uint8_t) -#define UHCI_SOFMOD 0x0C -// Array von Informationen über je einen Rootport (je uint16_t) -#define UHCI_RPORTS 0x10 - -#define MAXP (1 << 7) -#define CONFIGURE (1 << 6) -// Resumesignal treiben -#define FORCE_GRESUME (1 << 4) -// Alle Rootports befinden sich im Suspendmodus -#define GLOB_SUSP_MODE (1 << 3) -// RESET-Signal wird an allen Rootports getrieben -#define GRESET (1 << 2) -// HC wird zurückgesetzt -#define HCRESET (1 << 1) -// Normaler Ausführungsmodus -#define USB_RUN (1 << 0) - -// Setzt einen Rootport zurück -#define RPORT_RESET (1 << 9) -// Gibt an, ob ein Low-Speed-Gerät angeschlossen ist -#define RPORT_LOSPD (1 << 8) -// Treibt das Resume-Signal auf dem Bus -#define RPORT_RESUME (1 << 6) -// Schaltet den Rootport ein -#define RPORT_ENABLE (1 << 2) -// Wird gesetzt, wenn ein Gerät angeschlossen oder entfernt wird -#define RPORT_CSC (1 << 1) -// Gibt an, ob ein Gerät angeschlossen ist -#define RPORT_DEVICE (1 << 0) - -// Das Legacy-Support-Register im PCI-Konfigurationsraum (uint16_t) -#define UHCI_PCI_LEGSUP 0xC0 - -// Alle Statusbits, die mittels Schreiben einer 1 gelöscht werden -#define UHCI_LEGSUP_STATUS 0x8F00 -// Unveränderliche Bits (RO oder reserviert) -#define UHCI_LEGSUP_NO_CHG 0x5040 -// Interrupt wird als PCI-IRQ ausgeführt -#define UHCI_LEGSUP_PIRQ 0x2000 - - -struct uhci_td { - volatile uint32_t next; - - union { - volatile uint32_t flags; - struct { - volatile unsigned act_len : 11; - unsigned rsvd1 : 6; - volatile unsigned e_bitstuff : 1; - volatile unsigned e_crc_timeout : 1; - volatile unsigned e_nak : 1; - volatile unsigned e_babble : 1; - volatile unsigned e_buffer : 1; - volatile unsigned e_stall : 1; - volatile unsigned active : 1; - unsigned ioc : 1; - unsigned isochronous : 1; - unsigned low_speed : 1; - volatile unsigned errors : 2; - unsigned spd : 1; - unsigned rsvd2 : 2; - } __attribute__((packed)) bits; - } __attribute__((packed)) flags; - - unsigned pid : 8; - unsigned addr : 7; - unsigned ep : 4; - unsigned toggle : 1; - unsigned rsvd : 1; - unsigned maxlen : 11; - - uint32_t buffer; - - // Diese Felder sind für den HCD - - // Nächster TD in der Warteschlange - struct uhci_td* q_next; - - // Pointer zu einer Variable, die einen errno-Wert aufnimmt - volatile int* result; - - // Ist dieses Flag gesetzt, so wird der TD nach Abarbeitung vom IRQ-Handler - // automatisch freigegeben - int free_on_completion; - - // Physische Adresse - uintptr_t ptd; - - // Entsprechende Pipe - struct usb_trans_pipe* pipe; - - int active_count; -} __attribute__((packed)); - -struct uhci_qh { - volatile uint32_t next; - volatile uint32_t transfer; - - // Diese Felder sind für den HCD - - // Erster TD in der Warteschlange (oder NULL, wenn keiner) - volatile struct uhci_td* q_first; - - // Letzter TD in der Warteschlange (oder NULL, wenn keiner) - volatile struct uhci_td* q_last; -} __attribute__((packed)); - -// Beschreibt ein UHCI -struct uhci { - // PCI-Geräteinformationen - struct pci_device* pci; - // Beginn des I/O-Raums - uint16_t iobase; - - // Physische Adresse der Framelist - uintptr_t p_frame_list; - // Virtuelle Adresse der Framelist - uint32_t* frame_list; - // Lock für den Zugriff auf diese Liste - lock_t frame_list_lock; - - // Physische Adresse des (bisher einzigen) QHs - uintptr_t p_queue_head; - // Virtuelle Adresse des QHs - struct uhci_qh* queue_head; - // Lock für den Zugriff auf diesen - lock_t queue_head_lock; - - // Roothub, der durch das UHCI bereitgestellt wird - struct usb_rh* rh; -}; - -// Beschreibt einen Roothub (nur für UHCI) -struct usb_rh { - // Anzahl der Rootports - int ports; -}; - - -/** - * Durchsucht alle verfügbaren PCI-Geräte nach einem UHCI. - * - * @return 1, wenn ein UHCI gefunden wurde, sonst 0. - */ -int check_pci(void); - -/** - * Initialisiert das RPC-Interface (Handler) - */ -void init_rpc_interface(void); - -/** - * Registriert einen UHCI als HC beim USB-Treiber. - * - * @param hc Das zu registrierende UHCI - */ -void register_uhci(struct uhci* hc); - -/** - * Führt einen Bulktransfer durch. - * - * @param hc UHCI - * @param pipe Pipe, durch die die Übertragung stattfinden soll - * @param receive true, wenn empfangen werden soll, sonst false - * @param buffer Zielspeicherbereich - * @param length (Angestrebte) Länge des Transfers - * - * @return 0 bei Erfolg, sonst -errno - */ -int uhci_do_bulk(struct uhci* hc, struct usb_trans_pipe* pipe, bool receive, - void* buffer, size_t length); - -/** - * Führt einen Controltransfer durch. - * - * @param hc UHCI - * @param pipe Pipe, durch die die Übertragung stattfinden soll - * @param setup SETUP-Paket - * @param buffer Speicherbereich, mit dem während der Datenphase gearbeitet - * werden soll. - * - * @return 0 bei Erfolg, sonst -errno - */ -int uhci_do_control(struct uhci* hc, struct usb_trans_pipe* pipe, - struct usb_setup_packet* setup, void* buffer); - -/** - * Treibt das Resetsignal auf einem Port und aktiviert ihn. - * - * @param hc UHCI - * @param port Der Portindex - */ -void uhci_reset_port(struct uhci* hc, int port); - -/** - * Überprüft einen Port. - * - * @param hc UHCI - * @param port Zu überprüfender Port - * - * @return Status (USB_HUB_...) - */ -int uhci_scan_port(struct uhci* hc, int port); - -/** - * Aktiviert oder deaktiviert einen Port. - * - * @param hc UHCI - * @param port Portindex - * @param status 1 zum Aktivieren, 0 zum Deaktivieren - */ -void uhci_set_port_status(struct uhci* hc, int port, int status); - -#endif diff --git a/src/modules/usb/usb1/Makefile.all b/src/modules/usb/usb1/Makefile.all deleted file mode 100644 index 54993c8..0000000 --- a/src/modules/usb/usb1/Makefile.all +++ /dev/null @@ -1,8 +0,0 @@ -shopt -s extglob -source $LOST_BUILDMK_ROOT/config.sh - -CC="$CC -I../include" - -echo "LD $1/modules/usb1" -$LOST_TOOLS_LD $LDSCRIPT -ousb1.mod *.o --start-group $2 --end-group -$LOST_TOOLS_STRIP -s usb1.mod -o $1/modules/usb1 diff --git a/src/modules/usb/usb1/detect.c b/src/modules/usb/usb1/detect.c deleted file mode 100644 index a99fd36..0000000 --- a/src/modules/usb/usb1/detect.c +++ /dev/null @@ -1,444 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include -#include -#include - -#include "usb.h" -#include "usb-ddrv.h" -#include "usb-hc.h" -#include "usb-hub.h" - -extern void usb_found_hub(struct usb_device* dev); - -extern list_t* usb_ddrvs; -extern list_t* unhandled_devs; - -static const char* speed_name(int speed) -{ - switch (speed) { - case USB_LOW_SPEED: - return "low speed"; - case USB_FULL_SPEED: - return "full speed"; - case USB_HIGH_SPEED: - return "high speed"; - default: - return "unknown speed"; - } -} - -static int fits(int is, int want) -{ - if (want == -1) - return -1; - return !!(is == want); -} - -static int provide_to(struct usb_device* dev, int ifc_num, - struct usb_interface_descriptor* ifc, struct usb_ddrv* drv) -{ - struct usb_prov_dev pd = { - .is_interface = (ifc == NULL) ? false : true, - .dev_id = dev, - .interface_id = ifc_num, - .type = { - .vendor_id = dev->dev_desc->vendor_id, - .device_id = dev->dev_desc->device_id, - .class = (ifc == NULL) ? dev->dev_desc->class : ifc->class, - .subclass = (ifc == NULL) ? dev->dev_desc->subclass : ifc->subclass, - .protocol = (ifc == NULL) ? dev->dev_desc->protocol : ifc->protocol - } - }; - - return rpc_get_int(drv->pid, RPC_USB_DDRV_PLUG_FNN, sizeof(pd), (char*)&pd); -} - -static void dev_is_unhandled(struct usb_device* dev, int ifc_num, - struct usb_interface_descriptor* ifc) -{ - struct usb_prov_dev* pd = calloc(1, sizeof(*pd)); - - pd->is_interface = (ifc == NULL) ? false : true; - pd->dev_id = dev; - pd->interface_id = ifc_num; - pd->type.vendor_id = dev->dev_desc->vendor_id; - pd->type.device_id = dev->dev_desc->device_id; - pd->type.class = (ifc == NULL) ? dev->dev_desc->class : ifc->class; - pd->type.subclass = (ifc == NULL) ? dev->dev_desc->subclass : ifc->subclass; - pd->type.protocol = (ifc == NULL) ? dev->dev_desc->protocol : ifc->protocol; - - p(); - list_push(unhandled_devs, pd); - v(); -} - -#define check(svar, lname) int svar##f = fits(svar, drv->handle.lname); \ - if (!svar##f) { continue; } - -#define provto(check) if (check && provide_to(dev, ifc_num, ifc, drv)) \ - { break; } - -static void provide(struct usb_device* dev, int ifc_num, - struct usb_interface_descriptor* ifc) -{ - int vid, did, cls, scl, ptc; - - vid = dev->dev_desc->vendor_id; - did = dev->dev_desc->device_id; - if (ifc == NULL) { - cls = dev->dev_desc->class; - scl = dev->dev_desc->subclass; - ptc = dev->dev_desc->protocol; - } else { - cls = ifc->class; - scl = ifc->subclass; - ptc = ifc->protocol; - } - - int i, s = list_size(usb_ddrvs); - for (i = 0; i < s; i++) { - struct usb_ddrv* drv = list_get_element_at(usb_ddrvs, i); - - check(vid, vendor_id) - check(did, device_id) - check(cls, class) - check(scl, subclass) - check(ptc, protocol) - - // Wenn jemandem noch eine sinnvolle Kombination einfällt, kann er die - // gern hier eintragen. - // If anyone knows another combination making sense, he is encouraged - // to insert it here. - provto(vidf && didf) - provto(vidf && clsf && sclf && ptcf) - provto( clsf && sclf && ptcf) - provto(vidf && clsf && sclf) - provto( clsf && sclf) - provto(vidf && clsf) - provto( clsf) - } - - if (i == s) { - dev_is_unhandled(dev, ifc_num, ifc); - } -} - -void update_ep_trs_info(struct usb_pipe* ep) -{ - ep->trs.usb_addr = ep->dev->addr; - ep->trs.ep_id = ep->ep->number; - ep->trs.mps = ep->ep->mps; - ep->trs.type = ep->ep->type; - ep->trs.speed = ep->dev->speed; -} - -#define TNC(fnc) if ((ret = fnc)) { \ - printf("[usb1] err@%i, ret is %i\n" # fnc "\n", __LINE__, ret); return; } - -void enumerate_device(struct usb_device* dev) -{ - struct usb_hub* hub = dev->parent; - int p = dev->hub_port; - - printf("[usb1] Enumerating %s device on port %i of some hub\n", speed_name(dev->speed), dev->hub_port); - - hub->reset_port(hub, p); - dev->addr = 0; - dev->state = USB_DEFAULT; - - struct usb_pipe* ep0; - dev->ep0 = ep0 = calloc(1, sizeof(*ep0)); - - ep0->dev = dev; - ep0->ep = calloc(1, sizeof(*ep0->ep)); - ep0->ep->number = 0; - ep0->ep->mps = 8; - ep0->ep->type = USB_EP_CONTROL; - - update_ep_trs_info(ep0); - - ep0->trs.toggle = 0; - ep0->trs.stalled = false; - - if (hub->add_pipe != NULL) { - hub->add_pipe(hub, ep0); - } - - dev->dev_desc = calloc(1, sizeof(*dev->dev_desc)); - - int ret = usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_DEVICE | 0, 0, 8, dev->dev_desc); - - printf("[usb1] mps0 is %i B\n", dev->dev_desc->mps0); - - if (ret) { - fprintf(stderr, "[usb1] Warning: first transfer returned %i\n", ret); - } - - if (!dev->dev_desc->mps0) { - return; - } - - hub->reset_port(hub, p); - ep0->ep->mps = dev->dev_desc->mps0; - ep0->trs.stalled = false; - update_ep_trs_info(ep0); - - int address = dev->hc->addr_counter++; - TNC(usb_control_transfer(ep0, - USB_SETUP_OUT | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, USB_SET_ADDRESS, - address, 0, 0, NULL)); - dev->addr = address; - update_ep_trs_info(ep0); - - if (hub->add_pipe != NULL) { - hub->add_pipe(hub, dev->ep0); - } - - dev->state = USB_ADDRESS; - - TNC(usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_DEVICE | 0, 0, sizeof(*dev->dev_desc), - dev->dev_desc)); - - printf("[usb1] 0x%04X:0x%04X (%i configs)\n", dev->dev_desc->vendor_id, - dev->dev_desc->device_id, dev->dev_desc->num_configurations); - - int i; - - if (dev->dev_desc->i_manufacturer) { - uint8_t name[255]; - - TNC(usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_STRING | dev->dev_desc->i_manufacturer, - 0, 255, name)); - - printf("[usb1] -> Manufacturer: "); - for (i = 2; i < name[0]; i += 2) { - printf("%c", name[i]); - } - printf("\n"); - } - if (dev->dev_desc->i_product) { - uint8_t name[255]; - - TNC(usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_STRING | dev->dev_desc->i_product, - 0, 255, name)); - - printf("[usb1] -> Product: "); - for (i = 2; i < name[0]; i += 2) { - printf("%c", name[i]); - } - printf("\n"); - } - - dev->cfg_desc = calloc(dev->dev_desc->num_configurations, sizeof(*dev->cfg_desc)); - for (i = 0; i < dev->dev_desc->num_configurations; i++) { - uint8_t tmp[9]; - - // Erstmal Länge aller Deskriptoren herausfinden - TNC(usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_CONFIGURATION | i, 0, 9, tmp)); - dev->cfg_desc[i] = calloc(1, tmp[2] | (tmp[3] << 8)); - TNC(usb_control_transfer(ep0, - USB_SETUP_IN | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_CONFIGURATION | i, 0, - tmp[2] | (tmp[3] << 8), dev->cfg_desc[i])); - } - - if (dev->dev_desc->class == 9) { - usb_found_hub(dev); - return; - } - - if (dev->dev_desc->class) { - provide(dev, 0, NULL); - } - - struct usb_config_descriptor* best_cfg = dev->cfg_desc[0]; - - int c; - for (c = 0; c < dev->dev_desc->num_configurations; c++) { - struct usb_config_descriptor* cur_cfg = dev->cfg_desc[c]; - - if (cur_cfg->self_powered && !best_cfg->self_powered) { - // Wir wollen möglichst Self-powered - best_cfg = cur_cfg; - } else if (cur_cfg->max_power > best_cfg->max_power) { - // Und möglichst hohen Stromverbrauch (FIXME: Das aber nicht immer) - best_cfg = cur_cfg; - } - } - - dev->cur_cfg = best_cfg; - usb_set_configuration(dev, best_cfg->configuration_value); - - struct usb_interface_descriptor* cur_ifc = (struct usb_interface_descriptor*)(best_cfg + 1); - - for (i = 0; i < best_cfg->num_interfaces; i++) { - if (cur_ifc->class && !cur_ifc->alternate_setting) { - provide(dev, i, cur_ifc); - } - - cur_ifc = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(cur_ifc + 1) + - cur_ifc->num_endpoints); - } -} - -void scan_hub(struct usb_hub* hub) -{ - int p; - - printf("[usb1] Hub with %i ports detected.\n", hub->ports); - - for (p = 0; p < hub->ports; p++) { - hub->disable_port(hub, p); - } - - for (p = 0; p < hub->ports; p++) { - int stat = hub->scan_port(hub, p); - - if (stat & USB_HUB_DEVICE) { - struct usb_device* dev = calloc(1, sizeof(*dev)); - - dev->parent = hub; - dev->hub_port = p; - - if (hub->root_hub) { - dev->hc = hub->dev.hc; - } else { - dev->hc = hub->dev.usb->hc; - } - - dev->addr = 0; - dev->state = USB_ATTACHED; - dev->speed = stat & USB_HUB_SPEED; - - enumerate_device(dev); - } - } -} - -static void rh_addpipe(struct usb_hub* hub, struct usb_pipe* pipe) -{ - struct usb_hc* hc; - - if (hub->root_hub) { - hc = hub->dev.hc; - } else { - hc = hub->dev.usb->hc; - } - - struct usb_hc_ctrl_appe appe = { - .req = { - .type = HC_CTRL_ADD_PIPE, - .hc = *hc - }, - .pipe = pipe->trs - }; - - rpc_get_int(hc->pid, RPC_USB_HC_CONTROL_FNN, sizeof(appe), - (char*)&appe); -} - -static void rh_rst(struct usb_hub* hub, int port) -{ - struct usb_hc_ctrl_rst rqrst = { - .req = { - .type = HC_CTRL_RESET, - .hc = *hub->dev.hc - }, - .port = port - }; - - rpc_get_int(hub->dev.hc->pid, RPC_USB_HC_CONTROL_FNN, sizeof(rqrst), - (char*)&rqrst); -} - -static int rh_scan(struct usb_hub* hub, int port) -{ - struct usb_hc_ctrl_scan rqsc = { - .req = { - .type = HC_CTRL_SCAN_PORT, - .hc = *hub->dev.hc - }, - .port = port - }; - - return rpc_get_int(hub->dev.hc->pid, RPC_USB_HC_CONTROL_FNN, sizeof(rqsc), - (char*)&rqsc); -} - -static void rh_disable(struct usb_hub* hub, int port) -{ - struct usb_hc_ctrl_dsbl rqdbl = { - .req = { - .type = HC_CTRL_DSBL_PORT, - .hc = *hub->dev.hc - }, - .port = port - }; - - rpc_get_int(hub->dev.hc->pid, RPC_USB_HC_CONTROL_FNN, sizeof(rqdbl), - (char*)&rqdbl); -} - -void scan_hc(struct usb_hc* hc) -{ - struct usb_hub* rh = malloc(sizeof(*rh)); - - *rh = (struct usb_hub) { - .dev = { - .hc = hc - }, - .root_hub = true, - .ports = hc->root_ports, - .add_pipe = &rh_addpipe, - .disable_port = &rh_disable, - .reset_port = &rh_rst, - .scan_port = &rh_scan - }; - - scan_hub(rh); -} diff --git a/src/modules/usb/usb1/hub.c b/src/modules/usb/usb1/hub.c deleted file mode 100644 index b1d0597..0000000 --- a/src/modules/usb/usb1/hub.c +++ /dev/null @@ -1,180 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "usb.h" -#include "usb-hc.h" - -static int scan_hub_port(struct usb_hub* hub, int port) -{ - uint32_t status; - - usb_control_transfer(hub->dev.usb->ep0, - USB_SETUP_IN | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, USB_GET_STATUS, - 0, port + 1, 4, &status); - - if (!(status & (1 << 0))) { - return 0; - } - - if (status & (1 << 9)) { - return USB_HUB_DEVICE | USB_HUB_LOW_SPEED; - } else if (status & (1 << 10)) { - return USB_HUB_DEVICE | USB_HUB_HIGH_SPEED; - } else { - return USB_HUB_DEVICE | USB_HUB_FULL_SPEED; - } -} - -static void disable_hub_port(struct usb_hub *hub, int port) -{ - // Deaktivieren - usb_control_transfer(hub->dev.usb->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_CLEAR_FEATURE, 1, port + 1, 0, NULL); -} - -static void reset_hub_port(struct usb_hub* hub, int port) -{ - uint32_t cur; - - port++; - - usb_control_transfer(hub->dev.usb->ep0, - USB_SETUP_IN | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, USB_GET_STATUS, - 0, port, 4, &cur); - - // Zurücksetzen - usb_control_transfer(hub->dev.usb->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_SET_FEATURE, 4, port, 0, NULL); - - msleep(20); - - if (!(cur & (1 << 1)) || (cur & (1 << 2))) { - // Vorher deaktiviert oder im Suspendmodus, Resume treiben - usb_control_transfer(hub->dev.usb->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_CLEAR_FEATURE, 2, port, 0, NULL); - msleep(30); - } -} - -void usb_found_hub(struct usb_device* dev) -{ - struct usb_config_descriptor* ccfg; - struct usb_interface_descriptor* cint; - struct usb_hub* hub; - struct usb_hub_descriptor* chub; - uint8_t chub_buf[256]; - - dev->cur_cfg = NULL; - - int c, i; - - for (c = 0; c < dev->dev_desc->num_configurations; c++) { - ccfg = dev->cfg_desc[c]; - cint = (struct usb_interface_descriptor*)(dev->cfg_desc[c] + 1); - for (i = 0; i < ccfg->num_interfaces; i++) { - if (cint->class == 9) { - dev->cur_cfg = ccfg; - break; - } - - cint = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(cint + 1) + - cint->num_endpoints); - } - } - - if (dev->cur_cfg == NULL) { - return; - } - - usb_set_configuration(dev, dev->cur_cfg->configuration_value); - - hub = calloc(1, sizeof(*hub)); - if (hub == NULL) { - return; - } - - hub->dev.usb = dev; - hub->root_hub = false; - - usb_control_transfer(dev->ep0, - USB_SETUP_IN | USB_SETUP_REQ_CLS | USB_SETUP_REC_DEV, - USB_GET_DESCRIPTOR, USB_DESC_HUB, 0, 256, chub_buf); - - chub = (struct usb_hub_descriptor*)chub_buf; - - hub->ports = chub->nbr_ports; - - for (i = 1; i <= hub->ports; i++) { - // Strom einschalten - usb_control_transfer(dev->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_SET_FEATURE, 8, i, 0, NULL); - - msleep(chub->pwr_on_2_pwr_good * 2); - } - - msleep(100); - - for (i = 1; i <= hub->ports; i++) { - uint32_t status; - - usb_control_transfer(dev->ep0, - USB_SETUP_IN | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_GET_STATUS, 0, i, 4, &status); - - if (status & (1 << 1)) { - // Port deaktivieren - usb_control_transfer(dev->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_CLS | USB_SETUP_REC_OTH, - USB_CLEAR_FEATURE, 1, i, 0, NULL); - } - } - - hub->add_pipe = dev->parent->add_pipe; - hub->disable_port = &disable_hub_port; - hub->reset_port = &reset_hub_port; - hub->scan_port = &scan_hub_port; - - scan_hub(hub); -} diff --git a/src/modules/usb/usb1/main.c b/src/modules/usb/usb1/main.c deleted file mode 100644 index a7611ff..0000000 --- a/src/modules/usb/usb1/main.c +++ /dev/null @@ -1,59 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "usb.h" - -extern list_t* usb_hcs; -extern list_t* usb_ddrvs; -extern list_t* unhandled_devs; - -int main(void) -{ - usb_hcs = list_create(); - usb_ddrvs = list_create(); - unhandled_devs = list_create(); - - init_messaging(); - register_rpc_interface(); - - for (;;) { - wait_for_rpc(); - } -} diff --git a/src/modules/usb/usb1/rpc.c b/src/modules/usb/usb1/rpc.c deleted file mode 100644 index 51bf0ca..0000000 --- a/src/modules/usb/usb1/rpc.c +++ /dev/null @@ -1,350 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "usb.h" -#include "usb-ddrv.h" -#include "usb-hc.h" -#include "usb-server.h" - -list_t* usb_hcs; -list_t* usb_ddrvs; -list_t* unhandled_devs; - -extern void scan_hc(struct usb_hc* hc); - -static void hcd_handler(pid_t src, uint32_t corr_id, size_t length, void* data) -{ - struct usb_hc* hc; - - if (length < sizeof(*hc)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - hc = malloc(sizeof(*hc)); - memcpy(hc, data, sizeof(*hc)); - - hc->pid = src; - hc->addr_counter = 1; - list_push(usb_hcs, hc); - - scan_hc(hc); - - rpc_send_int_response(src, corr_id, 42); -} - -#define check(name) if ((dev->type.name != hd->name) && (hd->name != -1)) \ - { continue; } - -static void ddrv_handler(pid_t src, uint32_t corr_id, size_t length, void* data) -{ - struct usb_handle_devs* hd = data; - - if (length < sizeof(*hd)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - p(); - struct usb_ddrv* drv = calloc(1, sizeof(*drv)); - drv->pid = src; - drv->handle = *hd; - list_push(usb_ddrvs, drv); - - int i, s = list_size(unhandled_devs); - for (i = 0; i < s; i++) { - struct usb_prov_dev* dev = list_get_element_at(unhandled_devs, i); - - check(vendor_id) - check(device_id) - check(class) - check(subclass) - check(protocol) - - if (rpc_get_int(src, RPC_USB_DDRV_PLUG_FNN, sizeof(*dev), (char*)dev)) { - list_remove(unhandled_devs, i--); - s--; - } - } - v(); - - rpc_send_int_response(src, corr_id, 42); -} - -static void devctl_handler(pid_t src, uint32_t corr_id, size_t length, - void* data) -{ - struct usb_devctrl_req* rq = data; - - if (length < sizeof(*rq)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - struct usb_device* dev; - struct usb_interface_descriptor* ifc; - int ifcs, ifc_num, i, ret; - - switch (rq->type) { - case USB_DC_NUM_ENDPOINTS: - { - if (length < sizeof(struct usb_devctrl_gne)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - struct usb_devctrl_gne* ne = data; - dev = ne->dev.dev_id; - ifc = (struct usb_interface_descriptor*)(dev->cur_cfg + 1); - - ifcs = dev->cur_cfg->num_interfaces; - ifc_num = ne->dev.interface_id; - if ((ifc_num < 0) || (ifc_num >= ifcs)) { - ifc_num = 0; - } - - for (i = 0; i < ifcs; i++) { - if (i == ifc_num) { - rpc_send_int_response(src, corr_id, ifc->num_endpoints); - return; - } - - ifc = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(ifc + 1) + - ifc->num_endpoints); - } - // Hierhin sollte der Code nie gelangen - // Execution should never get here - break; - } - - case USB_DC_GET_IFC_ID: - { - if (length < sizeof(struct usb_devctrl_gii)) { - rpc_send_int_response(src, corr_id, 0); - return; - } - - struct usb_devctrl_gii* ii = data; - dev = ii->dev.dev_id; - ifc = (struct usb_interface_descriptor*)(dev->cur_cfg + 1); - - ifcs = dev->cur_cfg->num_interfaces; - // Das ist nicht die, die wir suchen - // This is not the one we're looking for - ifc_num = ii->dev.interface_id; - if ((ifc_num < 0) || (ifc_num >= ifcs)) { - ifc_num = 0; - } - - for (i = 0; i < ifcs; i++) { - if (i == ifc_num) { - rpc_send_int_response(src, corr_id, ifc->interface_number); - return; - } - - ifc = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(ifc + 1) + - ifc->num_endpoints); - } - // s. o. - break; - } - - case USB_DC_GET_ENDPOINT: - { - if (length < sizeof(struct usb_devctrl_gep)) { - // Hm, geht wohl nicht anders (und vertretbar, da dieser Fall - // nie auftreten sollte) - rpc_send_response(src, corr_id, 0, NULL); - return; - } - - struct usb_devctrl_gep* ge = data; - dev = ge->dev.dev_id; - ifc = (struct usb_interface_descriptor*)(dev->cur_cfg + 1); - - ifcs = dev->cur_cfg->num_interfaces; - ifc_num = ge->dev.interface_id; - if ((ifc_num < 0) || (ifc_num >= ifcs)) { - ifc_num = 0; - } - - for (i = 0; i < ifcs; i++) { - if (i == ifc_num) { - struct usb_endpoint_descriptor* epd = - (struct usb_endpoint_descriptor*)(ifc + 1); - rpc_send_response(src, corr_id, sizeof(*epd), - (char*)&epd[ge->ep_number]); - return; - } - - ifc = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(ifc + 1) + - ifc->num_endpoints); - } - // s. o. - break; - } - - case USB_DC_GET_PIPE: - { - if (length < sizeof(struct usb_devctrl_gpp)) { - // Auch hier wohl nur so möglich - rpc_send_response(src, corr_id, 0, NULL); - return; - } - - struct usb_devctrl_gpp* gp = data; - dev = gp->dev.dev_id; - ifc = (struct usb_interface_descriptor*)(dev->cur_cfg + 1); - - ifcs = dev->cur_cfg->num_interfaces; - ifc_num = gp->dev.interface_id; - if ((ifc_num < 0) || (ifc_num >= ifcs)) { - ifc_num = 0; - } - - for (i = 0; i < ifcs; i++) { - if (i == ifc_num) { - if ((gp->ep_number < 0) || - (gp->ep_number >= ifc->num_endpoints)) - { - gp->ep_number = 0; - } - - struct usb_endpoint_descriptor* epd = - (struct usb_endpoint_descriptor*)(ifc + 1); - epd += gp->ep_number; - - struct usb_pipe pp = { - .dev = dev, - .ep = epd, - .trs = { - .usb_addr = dev->addr, - .ep_id = epd->number, - .mps = epd->mps, - .type = epd->type, - .speed = dev->speed, - .toggle = 0, - .stalled = false - } - }; - - if (dev->parent->add_pipe != NULL) { - dev->parent->add_pipe(dev->parent, &pp); - } - - rpc_send_response(src, corr_id, sizeof(pp), (char*)&pp); - return; - } - - ifc = (struct usb_interface_descriptor*) - ((struct usb_endpoint_descriptor*)(ifc + 1) + - ifc->num_endpoints); - } - // s. o. - break; - } - - case USB_DC_CLEAR_HALT: - { - if (length < sizeof(struct usb_devctrl_clh)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - - struct usb_devctrl_clh* clrh = data; - usb_clear_halt(&clrh->pipe); - rpc_send_int_response(src, corr_id, 0); - break; - } - - case USB_DC_TRSF_CONTROL: - { - if (length < sizeof(struct usb_devctrl_tct)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - - struct usb_devctrl_tct* tctrq = data; - if (tctrq->ep0) { - dev = tctrq->dst.dev_id; - ret = usb_shm_control_transfer(dev->ep0, tctrq->setup.type, - tctrq->setup.request, tctrq->setup.value, - tctrq->setup.index, tctrq->setup.length, tctrq->shm); - } else { - ret = usb_shm_control_transfer(&tctrq->dst.pipe, - tctrq->setup.type, tctrq->setup.request, tctrq->setup.value, - tctrq->setup.index, tctrq->setup.length, tctrq->shm); - } - - rpc_send_int_response(src, corr_id, ret); - break; - } - - case USB_DC_TRSF_BULK: - { - if (length < sizeof(struct usb_devctrl_tbk)) { - rpc_send_int_response(src, corr_id, -EINVAL); - return; - } - - struct usb_devctrl_tbk* tbkrq = data; - ret = usb_shm_bulk_transfer(&tbkrq->pipe, tbkrq->shm, - tbkrq->length); - - // Senden des Togglebitstatus erwünscht - rpc_send_int_response(src, corr_id, ret); - break; - } - } -} - -void register_rpc_interface(void) -{ - register_message_handler(RPC_USB_SV_REGHC_FNN, &hcd_handler); - register_message_handler(RPC_USB_SV_REGDD_FNN, &ddrv_handler); - register_message_handler(RPC_USB_SV_DEVCT_FNN, &devctl_handler); - init_service_register("usb1"); -} diff --git a/src/modules/usb/usb1/transfer.c b/src/modules/usb/usb1/transfer.c deleted file mode 100644 index 8c96c4d..0000000 --- a/src/modules/usb/usb1/transfer.c +++ /dev/null @@ -1,123 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#include -#include - -#include -#include - -#include "usb.h" -#include "usb-ddrv.h" -#include "usb-hc.h" - -int usb_control_transfer(struct usb_pipe* pipe, int type, int request, - int value, int index, int length, void* buffer) -{ - uint32_t shm = 0; - void *shm_addr = NULL; - - if (length) { - shm = create_shared_memory(length); - shm_addr = open_shared_memory(shm); - - if ((buffer != NULL) && !(type & USB_SETUP_IN)) { - memcpy(shm_addr, buffer, length); - } - } - - int ret = usb_shm_control_transfer(pipe, type, request, value, index, - length, shm); - - if (length && (buffer != NULL) && (type & USB_SETUP_IN)) { - memcpy(buffer, shm_addr, length); - } - - if (shm_addr != NULL) { - close_shared_memory(shm); - } - - return ret; -} - -int usb_shm_control_transfer(struct usb_pipe* pipe, int type, int request, - int value, int index, int length, uint32_t shm) -{ - struct usb_hc_trs_ctrl trs = { - .trs = { - .type = USB_TRST_CONTROL, - .hc = *pipe->dev->hc, - .pipe = pipe->trs - }, - .setup = { - .type = type, - .request = request, - .value = value, - .index = index, - .length = length - }, - .shm = shm - }; - - return rpc_get_int(pipe->dev->hc->pid, RPC_USB_HC_TRANSFER_FNN, - sizeof(trs), (char*)&trs); -} - -int usb_shm_bulk_transfer(struct usb_pipe* pipe, uint32_t shm, size_t length) -{ - struct usb_hc_trs_bulk trs = { - .trs = { - .type = USB_TRST_BULK, - .hc = *pipe->dev->hc, - .pipe = pipe->trs - }, - .receive = pipe->ep->direction ? true : false, - .length = length, - .shm = shm - }; - - return rpc_get_int(pipe->dev->hc->pid, RPC_USB_HC_TRANSFER_FNN, - sizeof(trs), (char*)&trs); -} - -void usb_set_configuration(struct usb_device* dev, int config) -{ - usb_control_transfer(dev->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_STD | USB_SETUP_REC_DEV, - USB_SET_CONFIGURATION, config, 0, 0, NULL); - dev->state = USB_CONFIGURED; -} - -void usb_clear_halt(struct usb_pipe* pipe) -{ - usb_control_transfer(pipe->dev->ep0, - USB_SETUP_OUT | USB_SETUP_REQ_STD | USB_SETUP_REC_EP, - USB_CLEAR_FEATURE, USB_ENDPOINT_HALT, pipe->ep->number, 0, NULL); -} diff --git a/src/modules/usb/usb1/usb.h b/src/modules/usb/usb1/usb.h deleted file mode 100644 index 6efc729..0000000 --- a/src/modules/usb/usb1/usb.h +++ /dev/null @@ -1,209 +0,0 @@ -/***************************************************************************** -* Copyright (c) 2009-2010 Max Reitz * -* * -* THE COKE-WARE LICENSE * -* * -* Redistribution and use in source and binary forms, with or without * -* modification, are permitted provided that the following conditions are * -* met: * -* * -* 1. Redistributions of source code must retain the above copyright * -* notice, this list of conditions and the following disclaimer. * -* 2. Redistribution in binary form must reproduce the above copyright * -* notice and either the full list of conditions and the following * -* disclaimer or a link to both. * -* 3. If we meet one day and you think this stuff is worth it, you may buy * -* me a coke in return. * -* * -* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ?AS IS? AND ANY * -* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * -* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * -* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR * -* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * -* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * -* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * -* SUCH DAMAGE. * -*****************************************************************************/ - -#ifndef USB_H -#define USB_H - -#include -#include -#include - -#include "usb-ddrv.h" -#include "usb-hc.h" -#include "usb-hub.h" -#include "usb-server.h" -#include "usb-structs.h" -#include "usb-trans.h" - -struct usb_pipe; - -// Beschreibt einen Hub (sowohl Roothub als auch einen normalen). -struct usb_hub { - // Da ein Hub immer nur entweder Root- oder normaler Hub sein kann, macht - // sich eine Union hier ganz gut. - union { - struct usb_device* usb; - struct usb_hc* hc; - } dev; - // true, wenn es sich um einen Roothub handelt. - bool root_hub; - // Anzahl der Ports am Hub. - int ports; - - /** - * Wird aufgerufen, wenn eine neue Pipe benötigt wird. Ist bereits eine - * Pipe zur angegebenen Geräte-/Endpunkt-Kombination vorhanden, so sollte - * diese entfernt werden. Der Pointer darf NULL sein, um anzuzeigen, dass - * die Funktion nicht benötigt wird. Normale Hubs müssen jedoch dafür - * sorgen, dass die vom Hostcontroller (Roothub) bereitgestellte Funktion - * aufgerufen wird (es genügt, einfach den Pointer vom übergeordneten Hub - * zu kopieren, der Roothub muss dies erkennen können). - * - * @param hub Der Hub, an dem das Gerät hängt. - * @param pipe Die neu hinzuzufügende Pipe. - */ - void (*add_pipe)(struct usb_hub* hub, struct usb_pipe* pipe); - - /** - * Deaktiviert einen Port (zum Aktivieren wird reset_port verwendet). - * - * @param hub Hub mit dem zu deaktivierenden Port. - * @param port Zu deaktivierender Port. - */ - void (*disable_port)(struct usb_hub* hub, int port); - - /** - * Treibt das Resetsignal auf einem Port und aktiviert ihn. Wenn nötig, - * muss außerdem das Resume-Signal getrieben werden, damit nach dem Aufruf - * dieser Funktion mit dem Gerät kommuniziert werden kann. - * - * @param hub Hub, an dem das Gerät hängt. - * @param port Port, an dem es hängt. - */ - void (*reset_port)(struct usb_hub* hub, int port); - - /** - * Überprüft einen Port und gibt dessen Status zurück. - * - * @param hub Zu untersuchender Hub. - * @param port Entsprechender Port. - * - * @return Kombination aus den in usb-hub.h mit USB_HUB beginnenden Flags. - */ - int (*scan_port)(struct usb_hub* hub, int port); -}; - -// Beschreibt ein USB-Gerät -struct usb_device { - // Hub, an dem es hängt - struct usb_hub* parent; - // Index des Ports, an dem es sich befindet - int hub_port; - - // Zuständiger Hostcontroller - struct usb_hc* hc; - // Dort eindeutige USB-Adresse - int addr; - - // Aktueller Status - enum usb_state state; - // Aktuelle Geschwindigkeit - int speed; - - // Default Control Pipe - struct usb_pipe* ep0; - // Device-Descriptor - struct usb_device_descriptor* dev_desc; - // Alle Configuration-Descriptors - struct usb_config_descriptor** cfg_desc; - // Aktuelle Konfiguration - struct usb_config_descriptor* cur_cfg; -}; - -// USB-Gerätetreiber -struct usb_ddrv { - // Prozess-ID - pid_t pid; - // Gibt an, welche Geräte er behandeln kann - struct usb_handle_devs handle; -}; - -// Bisher nicht behandeltes USB-Gerät (oder Interface) -struct usb_unhandled { - // Das Gerät - struct usb_device* dev; - // Das Interface - struct usb_interface_descriptor* ifc; -}; - - -/** - * Initialisiert die RPC-Handler und trägt sich als usb1 bei init ein- - */ -void register_rpc_interface(void); - -/** - * Enumeriert alle Geräte an einem Hub - * - * @param hub Der Hub - */ -void scan_hub(struct usb_hub* hub); - -/** - * Entstallt einen Endpunkt. - * - * @param pipe Zum Endpunkt gehörige Pipe - */ -void usb_clear_halt(struct usb_pipe* pipe); - -/** - * Führt einen Controltransfer durch. - * - * @param pipe Die Pipe - * @param type type-Feld des SETUP-Pakets - * @param request request-Feld des SETUP-Pakets - * @param value value-Feld des SETUP-Pakets - * @param index index-Feld des SETUP-Pakets - * @param length length-Feld des SETUP-Pakets - * @param buffer Speicherbereich, mit dem während der Datenphase gearbeitet - * werden soll. - * - * @return 0 bei Erfolg, sonst -errno. - */ -int usb_control_transfer(struct usb_pipe* pipe, int type, int request, - int value, int index, int length, void* buffer); - -/** - * Führt einen Bulktransfer durch. - * - * @param pipe Die Pipe für den Transfer - * @param shm SHM-Bereich, mit dem gearbeitet werden soll - * @param length Länge des Transfers - * - * @return -errno bei Fehler, sonst neuer Wert des Toggle-Bits. - */ -int usb_shm_bulk_transfer(struct usb_pipe* pipe, uint32_t shm, size_t length); - -/** - * Führt einen Controltransfer durch. Entspricht usb_control_transfer, außer - * dass ein SHM-Bereich verwendet wird. - */ -int usb_shm_control_transfer(struct usb_pipe* pipe, int type, int request, - int value, int index, int length, uint32_t shm); - -/** - * Setzt die aktuelle Konfiguration eines Geräts. - * - * @param dev USB-Gerät - * @param config ID der neuen Konfiguration - */ -void usb_set_configuration(struct usb_device* dev, int config); - -#endif -- 2.6.3 From max at tyndur.org Sun Dec 13 01:11:05 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:11:05 +0100 Subject: [tyndur-devel] [PATCH v2 20/24] ehci: Add EHCI driver In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-21-git-send-email-max@tyndur.org> + This adds an EHCI USB host controller driver. It is missing support for periodic schedule (interrupt and isochronous transfers), hotplugging, suspend/resume, etc. pp.. Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/ehci/ehci.c | 640 ++++++++++++++++++++++++++++++++++++++++++++ src/modules/cdi/ehci/ehci.h | 292 ++++++++++++++++++++ src/modules/cdi/ehci/main.c | 69 +++++ 3 files changed, 1001 insertions(+) create mode 100644 src/modules/cdi/ehci/ehci.c create mode 100644 src/modules/cdi/ehci/ehci.h create mode 100644 src/modules/cdi/ehci/main.c diff --git a/src/modules/cdi/ehci/ehci.c b/src/modules/cdi/ehci/ehci.c new file mode 100644 index 0000000..7f3838c --- /dev/null +++ b/src/modules/cdi/ehci/ehci.c @@ -0,0 +1,640 @@ +/****************************************************************************** + * Copyright (c) 2015 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 +#include + +#include "ehci.h" + + +#define EHCI_TIMEOUT 1000 // ms + + +static void bios_handoff(struct cdi_pci_device *pci_dev, struct ehci *hc); +static void irq_handler(struct cdi_device *dev); + + +struct cdi_device *ehci_init_device(struct cdi_bus_data *bus_data) +{ + struct cdi_pci_device *pci_dev = (struct cdi_pci_device *)bus_data; + static int dev_counter = 0; + + if (pci_dev->class_id != PCI_CLASS_SERIAL_BUS_CONTROLLER || + pci_dev->subclass_id != PCI_SUBCLASS_USB_HOST_CONTROLLER || + pci_dev->interface_id != PCI_INTERFACE_EHCI) + { + return NULL; + } + + struct ehci *hc = calloc(1, sizeof(*hc)); + + hc->hc.dev.name = malloc(8); + sprintf((char *)hc->hc.dev.name, "ehci%i", dev_counter++); + + hc->retired_qhs = cdi_list_create(); + + cdi_pci_alloc_memory(pci_dev); + + struct cdi_pci_resource *res; + for (int i = 0; (res = cdi_list_get(pci_dev->resources, i)); i++) { + if (res->type == CDI_PCI_MEMORY && !res->index) { + hc->caps = res->address; + hc->regs = (struct ehci_registers *)((uintptr_t)res->address + + hc->caps->caplength); + break; + } + } + if (!hc->regs) { + goto fail; + } + + bios_handoff(pci_dev, hc); + + // Stop operation + hc->regs->usbcmd = (hc->regs->usbcmd & EHCI_CMD_RSVD) | EHCI_CMD_ITC(8); + + // Wait until HCHALTED becomes true + CDI_CONDITION_WAIT(hc->regs->usbsts & EHCI_STS_HCH, 50); + if (!(hc->regs->usbsts & EHCI_STS_HCH)) { + goto fail; + } + + hc->regs->usbcmd |= EHCI_CMD_HCRES; // Reset + CDI_CONDITION_WAIT(!(hc->regs->usbcmd & EHCI_CMD_HCRES), 50); + if (hc->regs->usbcmd & EHCI_CMD_HCRES) { + goto fail; + } + + if (hc->caps->hccparams & EHCI_HCC_64) { + // 64-bit addressing capability + hc->regs->ctrldssegment = 0; + } + + hc->regs->usbintr = 0; + hc->regs->usbsts = hc->regs->usbsts; + + hc->periodic_list_mem = cdi_mem_alloc(4096, 12 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + hc->periodic_list = hc->periodic_list_mem->vaddr; + for (int i = 0; i < 1024; i++) { + hc->periodic_list[i] = EHCI_T; + } + hc->regs->periodiclistbase = hc->periodic_list_mem->paddr.items[0].start; + + // Create anchor + struct cdi_mem_area *anchor_qh; + anchor_qh = cdi_mem_alloc(sizeof(ehci_fat_qh_t), 6 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + + hc->async_start = anchor_qh->vaddr; + memset(hc->async_start, 0, sizeof(*hc->async_start)); + + hc->async_start->cdi_mem_area = anchor_qh; + hc->async_start->paddr = anchor_qh->paddr.items[0].start + + offsetof(ehci_fat_qh_t, qh); + + hc->async_start->next = hc->async_start; + hc->async_start->previous = hc->async_start; + + hc->async_start->qh.next_qh = hc->async_start->paddr | EHCI_QH; + hc->async_start->qh.ep_state[0] = EHCI_QHES0_RL(0) | EHCI_QHES0_MPL(8) + | EHCI_QHES0_EP(0) | EHCI_QHES0_DEV(0) + | EHCI_QHES0_H | EHCI_QHES0_DTC + | EHCI_QHES0_EPS_HS; + hc->async_start->qh.ep_state[1] = EHCI_QHES1_MULT(1) | EHCI_QHES1_SCM(0xff); + + hc->async_start->qh.overlay.next_qtd = EHCI_T; + hc->async_start->qh.overlay.alt_next_qtd = EHCI_T; + + hc->regs->asynclistaddr = hc->async_start->paddr; + + // Start operation + hc->regs->usbcmd = (hc->regs->usbcmd & EHCI_CMD_RSVD) + | EHCI_CMD_ITC(8) | EHCI_CMD_ASE | EHCI_CMD_PSE + | EHCI_CMD_FLS_1K | EHCI_CMD_RS; + + // Wait until HCHalted becomes false and the schedules are running + CDI_CONDITION_WAIT((hc->regs->usbsts + & (EHCI_STS_ASS | EHCI_STS_PSS | EHCI_STS_HCH)) + == (EHCI_STS_ASS | EHCI_STS_PSS), 50); + if ((hc->regs->usbsts & (EHCI_STS_ASS | EHCI_STS_PSS | EHCI_STS_HCH)) + != (EHCI_STS_ASS | EHCI_STS_PSS)) + { + goto fail; + } + + cdi_register_irq(pci_dev->irq, &irq_handler, &hc->hc.dev); + + // async advance doorbell + hc->regs->usbintr = EHCI_INT_IAA; + + // Route everything to this HC + hc->regs->configflag |= EHCI_CF_CF; + + cdi_sleep_ms(5); + + hc->hc.rh.ports = EHCI_HCS_N_PORTS(hc->caps->hcsparams); + + // Power up all ports + for (int i = 0; i < hc->hc.rh.ports; i++) { + hc->regs->portsc[i] |= EHCI_PSC_PP; + } + + return &hc->hc.dev; + +fail: + if (hc->async_start) { + cdi_mem_free(hc->async_start->cdi_mem_area); + } + if (hc->periodic_list_mem) { + cdi_mem_free(hc->periodic_list_mem); + } + cdi_list_destroy(hc->retired_qhs); + free((void *)hc->hc.dev.name); + free(hc); + cdi_pci_free_memory(pci_dev); + + return NULL; +} + + +static void bios_handoff(struct cdi_pci_device *pci_dev, struct ehci *hc) +{ + int eecp = EHCI_HCC_EECP(hc->caps->hccparams); + + // Iterate through the list of capabilities + while (eecp >= 0x40) { + uint32_t cap = cdi_pci_config_readl(pci_dev, eecp); + + // Look for USBLEGSUP + if (EHCI_EEC_ID(cap) == EHCI_EECID_LEGSUP) { + // Set OS semaphore + cdi_pci_config_writeb(pci_dev, eecp + 3, 1); + + // Wait for BIOS semaphore to get unset + CDI_CONDITION_WAIT(!(cdi_pci_config_readb(pci_dev, eecp + 2) & 1), + 5000); + if (cdi_pci_config_readb(pci_dev, eecp + 2) & 1) { + // Okay Max, stay calm, don't swear + printf("[ehci] Warning: Clean BIOS handoff failed\n"); + // Disable all SMI sources (0xff000000 is the R/WC mask of this + // register) + cdi_pci_config_writel(pci_dev, eecp + 4, + cdi_pci_config_readl(pci_dev, eecp + 4) & 0xff000000u); + // Unset BIOS semaphore + cdi_pci_config_writeb(pci_dev, eecp + 2, 0); + } + } + + eecp = EHCI_EEC_NEXT(cap); + } +} + + +static void irq_handler(struct cdi_device *dev) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(dev, struct cdi_usb_hc, dev); + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + + uint32_t usbsts = hc->regs->usbsts; + hc->regs->usbsts = usbsts; + + // async advance doorbell + if (!(usbsts & EHCI_STS_IAA)) { + return; + } + + ehci_fat_qh_t *rqh; + while ((rqh = cdi_list_pop(hc->retired_qhs))) { + cdi_mem_free(rqh->cdi_mem_area); + } +} + + +void ehci_rh_port_down(struct cdi_usb_hub *hub, int index) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + + hc->regs->portsc[index] &= ~EHCI_PSC_PED; +} + + +void ehci_rh_port_up(struct cdi_usb_hub *hub, int index) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + uint32_t portsc = hc->regs->portsc[index]; + + if ((portsc & EHCI_PSC_LS_MASK) == EHCI_PSC_LS_K) { + // Low-speed device, release ownership + hc->regs->portsc[index] = portsc | EHCI_PSC_PO; + return; + } + + // Assert reset signal + hc->regs->portsc[index] = ((portsc & ~(EHCI_PSC_PO | EHCI_PSC_SUS | + EHCI_PSC_FPR | EHCI_PSC_PED)) + | EHCI_PSC_RES); + cdi_sleep_ms(50); + + // De-assert reset signal + hc->regs->portsc[index] = portsc & ~EHCI_PSC_RES; + CDI_CONDITION_WAIT(!(hc->regs->portsc[index] & EHCI_PSC_RES), 50); + + portsc = hc->regs->portsc[index]; + if (portsc & EHCI_PSC_RES) { + // Disable port, route on to companion controller + // (maybe it can do at least something with it) + hc->regs->portsc[index] = (portsc & ~EHCI_PSC_PED) | EHCI_PSC_PO; + } else if ((portsc & (EHCI_PSC_PED | EHCI_PSC_CCS)) == EHCI_PSC_CCS) { + // Device connected but port disabled: + // Full-speed device, release ownership + hc->regs->portsc[index] = portsc | EHCI_PSC_PO; + } +} + + +cdi_usb_port_status_t ehci_rh_port_status(struct cdi_usb_hub *hub, int index) +{ + struct cdi_usb_hc *cdi_hc = CDI_UPCAST(hub, struct cdi_usb_hc, rh); + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + uint32_t portsc = hc->regs->portsc[index]; + cdi_usb_port_status_t status = 0; + + if ((portsc & (EHCI_PSC_PED | EHCI_PSC_CCS)) == + (EHCI_PSC_PED | EHCI_PSC_CCS)) + { + // Device connected and port enabled + status |= CDI_USB_PORT_CONNECTED | CDI_USB_HIGH_SPEED; + } + + return status; +} + + +static void enter_async_qh(struct ehci *hc, ehci_fat_qh_t *qh) +{ + // Enter the new QH as last + + qh->next = hc->async_start; + qh->previous = hc->async_start->previous; + + qh->previous->next = qh; + hc->async_start->previous = qh; + + qh->qh.next_qh = hc->async_start->paddr | EHCI_QH; + qh->previous->qh.next_qh = qh->paddr | EHCI_QH; +} + + +static void retire_qh(struct ehci *hc, ehci_fat_qh_t *qh) +{ + qh->previous->qh.next_qh = qh->qh.next_qh; + + qh->previous->next = qh->next; + qh->next->previous = qh->previous; + + cdi_list_push(hc->retired_qhs, qh); + // Request async advance doorbell + hc->regs->usbcmd |= EHCI_CMD_IAADB; +} + + +static void retire_first_qtd(ehci_fat_qh_t *qh) +{ + ehci_fat_qtd_t *qtd = qh->first_qtd; + + assert(qtd); + + uint32_t err_mask = (qh->qh.ep_state[0] & EHCI_QHES0_EPS_MASK) + == EHCI_QHES0_EPS_HS + ? EHCI_QTDS_HS_ERR_MASK + : EHCI_QTDS_LFS_ERR_MASK; + + if (qtd->qtd.status & err_mask) { + if (qtd->qtd.status & EHCI_QTDS_BABBLE) { + *qtd->result = CDI_USB_BABBLE; + } else if (qtd->qtd.status & (EHCI_QTDS_XACTERR | EHCI_QTDS_P_ERR)) { + *qtd->result = CDI_USB_BAD_RESPONSE; + } else if (qtd->qtd.status & (EHCI_QTDS_BUFERR | EHCI_QTDS_MuF)) { + *qtd->result = CDI_USB_HC_ERROR; + } else if (qtd->qtd.status & EHCI_QTDS_HALTED) { + *qtd->result = CDI_USB_STALL; + } else { + *qtd->result = CDI_USB_HC_ERROR; + } + } + + if (qtd->write_back) { + size_t rem = qtd->write_back_size; + for (int i = 0; i < 5 && rem; i++) { + size_t sz = rem > 4096 ? 4096 : rem; + memcpy((void *)((uintptr_t)qtd->write_back + i * 4096), + qtd->buffers[i]->vaddr, sz); + rem -= sz; + } + assert(!rem); + } + + qh->first_qtd = qtd->next; + if (qh->last_qtd == qtd) { + qh->last_qtd = NULL; + } + + for (int i = 0; i < 5; i++) { + if (qtd->buffers[i]) { + cdi_mem_free(qtd->buffers[i]); + } + } + + cdi_mem_free(qtd->cdi_mem_area); +} + + +/** + * Enters a qTD into a QH that has not yet been entered into the async schedule. + */ +static void enter_qtd(ehci_fat_qh_t *qh, ehci_fat_qtd_t *qtd) +{ + bool first; + + qtd->qh = qh; + qtd->qtd.next_qtd = EHCI_T; + qtd->qtd.alt_next_qtd = EHCI_T; + + first = !qh->first_qtd; + + if (first) { + qh->first_qtd = qtd; + } else { + qh->last_qtd->next = qtd; + } + qh->last_qtd = qtd; + + if (first) { + qh->qh.overlay.next_qtd = qh->first_qtd->paddr; + } else { + qh->last_qtd->qtd.next_qtd = qtd->paddr; + } +} + + +cdi_usb_hc_transaction_t ehci_create_transaction(struct cdi_usb_hc *cdi_hc, + const struct cdi_usb_hc_ep_info *target) +{ + (void)cdi_hc; + + struct cdi_mem_area *qh_mem; + + qh_mem = cdi_mem_alloc(sizeof(ehci_fat_qh_t), 6 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + ehci_fat_qh_t *qh = qh_mem->vaddr; + + memset(qh, 0, sizeof(*qh)); + + int non_high_speed_control = target->speed != CDI_USB_HIGH_SPEED + && target->ep_type == CDI_USB_CONTROL; + + qh->qh.ep_state[0] = EHCI_QHES0_RL(0) + | (non_high_speed_control ? EHCI_QHES0_C : 0) + | EHCI_QHES0_MPL(target->mps) + | EHCI_QHES0_DTC + | EHCI_QHES0_EP(target->ep) + | EHCI_QHES0_DEV(target->dev); + switch (target->speed) { + case CDI_USB_LOW_SPEED: qh->qh.ep_state[0] |= EHCI_QHES0_EPS_LS; break; + case CDI_USB_FULL_SPEED: qh->qh.ep_state[0] |= EHCI_QHES0_EPS_FS; break; + case CDI_USB_HIGH_SPEED: qh->qh.ep_state[0] |= EHCI_QHES0_EPS_HS; break; + case CDI_USB_SUPERSPEED: + default: + abort(); + } + + qh->qh.ep_state[1] = EHCI_QHES1_MULT(1) + | EHCI_QHES1_PORT(target->tt_port + 1) + | EHCI_QHES1_HUB(target->tt_addr) + | EHCI_QHES1_SCM(0xff); + + qh->qh.overlay.next_qtd = EHCI_T; + qh->qh.overlay.alt_next_qtd = EHCI_T; + + qh->cdi_mem_area = qh_mem; + qh->paddr = qh_mem->paddr.items[0].start + offsetof(ehci_fat_qh_t, qh); + + return qh; +} + + +static void create_qtd(ehci_fat_qh_t *qh, cdi_usb_transfer_token_t token, + int toggle, void *buffer, size_t size, + cdi_usb_transmission_result_t *result) +{ + struct cdi_mem_area *qtd_mem; + + qtd_mem = cdi_mem_alloc(sizeof(ehci_fat_qtd_t), 6 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + ehci_fat_qtd_t *qtd = qtd_mem->vaddr; + + memset(qtd, 0, sizeof(*qtd)); + + qtd->qtd.status = EHCI_QTDS_DT(toggle) + | EHCI_QTDS_XFLEN(size) + | EHCI_QTDS_CP(0) + | EHCI_QTDS_CERR(3) + | EHCI_QTDS_ACTIVE; + + switch (token) { + case CDI_USB_IN: qtd->qtd.status |= EHCI_QTDS_PID_IN; break; + case CDI_USB_OUT: qtd->qtd.status |= EHCI_QTDS_PID_OUT; break; + case CDI_USB_SETUP: qtd->qtd.status |= EHCI_QTDS_PID_SETUP; break; + default: + abort(); + } + + if (token == CDI_USB_IN) { + qtd->write_back = buffer; + qtd->write_back_size = size; + } + + for (int i = 0; i < 5 && size; i++) { + size_t sz = size > 4096 ? 4096 : size; + + qtd->buffers[i] = cdi_mem_alloc(4096, 12 + | CDI_MEM_PHYS_CONTIGUOUS + | CDI_MEM_DMA_4G + | CDI_MEM_NOINIT); + qtd->qtd.buffers[i] = qtd->buffers[i]->paddr.items[0].start; + + if (token == CDI_USB_OUT || token == CDI_USB_SETUP) { + memcpy(qtd->buffers[i]->vaddr, + (void *)((uintptr_t)buffer + i * 4096), sz); + } + + size -= sz; + } + + qtd->cdi_mem_area = qtd_mem; + qtd->paddr = qtd_mem->paddr.items[0].start + offsetof(ehci_fat_qtd_t, qtd); + + qtd->result = result; + + enter_qtd(qh, qtd); +} + + +void ehci_enqueue(struct cdi_usb_hc *cdi_hc, + const struct cdi_usb_hc_transmission *trans) +{ + (void)cdi_hc; + + ehci_fat_qh_t *qh = trans->ta; + bool first = true; + size_t sz = trans->size; + char *buf = trans->buffer; + int toggle = trans->toggle; + size_t mps = EHCI_QHES0_READ_MPL(qh->qh.ep_state[0]); + + *trans->result = CDI_USB_OK; + + while (sz || first) { + first = false; + + size_t iter_sz = sz > 4096 * 5 ? 4096 * 5 : sz; + + create_qtd(qh, trans->token, toggle, buf, iter_sz, trans->result); + + buf += iter_sz; + sz -= iter_sz; + + toggle ^= ((iter_sz + mps - 1) / mps) & 1; + } +} + + +void ehci_start_transaction(struct cdi_usb_hc *cdi_hc, + cdi_usb_hc_transaction_t ta) +{ + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + ehci_fat_qh_t *qh = ta; + + enter_async_qh(hc, qh); +} + + +void ehci_wait_transaction(struct cdi_usb_hc *cdi_hc, + cdi_usb_hc_transaction_t ta) +{ + (void)cdi_hc; + + ehci_fat_qh_t *qh = ta; + bool had_timeout = false; + + irq_handler(&cdi_hc->dev); + + while (qh->first_qtd) { + if (!had_timeout) { + if (!(qh->qh.overlay.status & EHCI_QTDS_ACTIVE)) { + qh->qh.overlay.next_qtd = qh->first_qtd->paddr; + } + + uint64_t timestamp = cdi_elapsed_ms(); + + while ((qh->first_qtd->qtd.status & EHCI_QTDS_ACTIVE) && + cdi_elapsed_ms() - timestamp < EHCI_TIMEOUT) + { + // I don't really know who's to blame for this (qemu or + // týndur), and actually, I don't even want to know. + // As a matter of fact, qemu will not work on the async + // schedule if the async advance doorbell is set but the driver + // has not acknowledged this; however, the interrupt handler + // will always clear that bit and the interrupt handler is + // active all the time, so something is apparently sometimes + // blocking the handler from being executed. Force it here to + // prevent qemu's HC from stalling. + irq_handler(&cdi_hc->dev); + } + + had_timeout = qh->first_qtd->qtd.status & EHCI_QTDS_ACTIVE; + } + + if (had_timeout) { + // Stop this queue + qh->qh.overlay.next_qtd = EHCI_T; + qh->qh.overlay.status &= ~(uint32_t)EHCI_QTDS_ACTIVE; + + *qh->first_qtd->result = CDI_USB_TIMEOUT; + } + + retire_first_qtd(qh); + } +} + + +void ehci_destroy_transaction(struct cdi_usb_hc *cdi_hc, + cdi_usb_hc_transaction_t ta) +{ + struct ehci *hc = CDI_UPCAST(cdi_hc, struct ehci, hc); + ehci_fat_qh_t *qh = ta; + + qh->qh.overlay.next_qtd = EHCI_T; + + uint64_t timestamp = cdi_elapsed_ms(); + + while ((qh->qh.overlay.status & EHCI_QTDS_ACTIVE) && + cdi_elapsed_ms() - timestamp < EHCI_TIMEOUT) + { + // See above. + irq_handler(&cdi_hc->dev); + } + + if (qh->qh.overlay.status & EHCI_QTDS_ACTIVE) { + qh->qh.overlay.status &= ~(uint32_t)EHCI_QTDS_ACTIVE; + // Could probably be solved more elegantly, but we are in a timeout + // already, so waiting a bit longer will not be that big of a problem + // (the sleep is here to ensure that the HC is no longer trying to work + // on the qTDs belonging to this QH) + cdi_sleep_ms(100); + } + + while (qh->first_qtd) { + retire_first_qtd(qh); + } + retire_qh(hc, qh); +} diff --git a/src/modules/cdi/ehci/ehci.h b/src/modules/cdi/ehci/ehci.h new file mode 100644 index 0000000..d37f141 --- /dev/null +++ b/src/modules/cdi/ehci/ehci.h @@ -0,0 +1,292 @@ +/****************************************************************************** + * Copyright (c) 2015 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 EHCI_H +#define EHCI_H + +#include +#include + +#include +#include +#include +#include + + +struct ehci_registers { + uint32_t usbcmd; + uint32_t usbsts; + uint32_t usbintr; + uint32_t frindex; + uint32_t ctrldssegment; + uint32_t periodiclistbase; + uint32_t asynclistaddr; + uint8_t reserved[0x24]; + uint32_t configflag; + uint32_t portsc[]; +} __attribute__((packed)); + +struct ehci_capabilities { + uint8_t caplength; + uint8_t reserved; + uint16_t hciversion; + uint32_t hcsparams; + uint32_t hccparams; + uint64_t hcsp_portroute; +} __attribute__((packed)); + +struct ehci_qtd { + uint32_t next_qtd; + uint32_t alt_next_qtd; + uint32_t status; + uint32_t buffers[5]; + + // 64-bit structures + uint32_t ext_buffer[5]; +} __attribute__((packed)); + +struct ehci_qh { + uint32_t next_qh; + uint32_t ep_state[2]; + uint32_t current_qtd; + struct ehci_qtd overlay; +} __attribute__((packed)); + +typedef struct ehci_fat_qh ehci_fat_qh_t; +typedef struct ehci_fat_qtd ehci_fat_qtd_t; + +#define DIFF_TO_MULTIPLE(value, divisor) \ + ((divisor) - ((((value) + (divisor) - 1) % (divisor)) + 1)) + +struct ehci_fat_qh { + volatile struct ehci_qh qh; + + // Align the QH to 128 bytes (something with cache lines?). Otherwise, + // there may be conflicts between data we are writing to our very own + // fields provided by ehci_fat_qh_t and writes done by the HC to this + // structure. + uint8_t alignment[DIFF_TO_MULTIPLE(sizeof(struct ehci_qh), 128)]; + + struct cdi_mem_area *cdi_mem_area; + uintptr_t paddr; + + ehci_fat_qh_t *next, *previous; + ehci_fat_qtd_t *first_qtd, *last_qtd; +}; + +struct ehci_fat_qtd { + volatile struct ehci_qtd qtd; + + // Align the qTD to a multiple of 128 bytes. + uint8_t alignment[DIFF_TO_MULTIPLE(sizeof(struct ehci_qtd), 128)]; + + struct cdi_mem_area *cdi_mem_area; + uintptr_t paddr; + + ehci_fat_qh_t *qh; + ehci_fat_qtd_t *next; + + struct cdi_mem_area *buffers[5]; + + void *write_back; + size_t write_back_size; + + cdi_usb_transmission_result_t *result; +}; + +struct ehci { + struct cdi_usb_hc hc; + + volatile struct ehci_capabilities *caps; + volatile struct ehci_registers *regs; + + struct cdi_mem_area *periodic_list_mem; + volatile uint32_t *periodic_list; + + ehci_fat_qh_t *async_start; + cdi_list_t retired_qhs; +}; + +#define PCI_CLASS_SERIAL_BUS_CONTROLLER 0x0c +#define PCI_SUBCLASS_USB_HOST_CONTROLLER 0x03 +#define PCI_INTERFACE_EHCI 0x20 + +#define EHCI_T (1u << 0) +#define EHCI_ITD (0u << 1) +#define EHCI_QH (1u << 1) +#define EHCI_SITD (2u << 1) +#define EHCI_FSTN (3u << 1) + +// Operational Registers: USBCMD +#define EHCI_CMD_RSVD 0xff00ff00u // Consider async sched park mode reserved +#define EHCI_CMD_ITC(x) ((x) << 16) // Interrupt Threshold Control +#define EHCI_CMD_LHCRES (1 << 7) // Light Host Controller Reset +#define EHCI_CMD_IAADB (1 << 6) // Interrupt on Async Advance Doorbell +#define EHCI_CMD_ASE (1 << 5) // Async Schedule Enable +#define EHCI_CMD_PSE (1 << 4) // Periodic Schedule Enable +#define EHCI_CMD_FLS_1K (0 << 2) // Frame List Size: 1024 elements (default) +#define EHCI_CMD_HCRES (1 << 1) // Host Controller Reset +#define EHCI_CMD_RS (1 << 0) // Run/Stop (1 = Run) + +// Operational Registers: USBINTR +#define EHCI_INT_IAA (1 << 5) // Interrupt on Async Advance Enable +#define EHCI_INT_HSE (1 << 4) // Host System Error Enable +#define EHCI_INT_FLR (1 << 3) // Frame List Rollover Enable +#define EHCI_INT_PCD (1 << 2) // Port Change Detect Enable +#define EHCI_INT_USBERR (1 << 1) // USB Error Interrupt +#define EHCI_INT_USBINT (1 << 0) // USB Interrupt + +// Operational Registers: USBSTS +#define EHCI_STS_ASS (1 << 15) // Async Schedule Status +#define EHCI_STS_PSS (1 << 14) // Periodic Schedule Status +#define EHCI_STS_REC (1 << 13) // Reclamation bit +#define EHCI_STS_HCH (1 << 12) // Host Controller Halted +#define EHCI_STS_IAA EHCI_INT_IAA // Interrupt on Async Advance +#define EHCI_STS_HSE EHCI_INT_HSE // Host System Error +#define EHCI_STS_FLR EHCI_INT_FLR // Frame List Rollover +#define EHCI_STS_PCD EHCI_INT_PCD // Port Change Detect +#define EHCI_STS_USBERR EHCI_INT_USBERR // USB Error Interrupt +#define EHCI_STS_USBINT EHCI_INT_USBINT // USB Interrupt + +// Operational Registers: FRINDEX +#define EHCI_FI_FI(x) (((x) >> 3) & 0x3ff) // Frame Index + +// Operational Registers: CONFIGFLAG +#define EHCI_CF_CF (1 << 0) // Configure Flag + +// Operational Registers: PORTSC +#define EHCI_PSC_WKOC_E (1 << 22) // Wake on Over-current Enable +#define EHCI_PSC_WKDSCNNT_E (1 << 21) // Wake on Disconnect Enable +#define EHCI_PSC_WKCNNT_E (1 << 20) // Wake on Connect Enable +#define EHCI_PSC_PTC_DIS (0 << 16) // Port Test Control: disabled +#define EHCI_PSC_PTC_J (1 << 16) // Port Test Control: J-State +#define EHCI_PSC_PTC_K (2 << 16) // Port Test Control: K-State +#define EHCI_PSC_PTC_SE0 (3 << 16) // Port Test Control: SE0 Nak +#define EHCI_PSC_PTC_PKT (4 << 16) // Port Test Control: Packet +#define EHCI_PSC_PTC_FEN (5 << 16) // Port Test Control: Force Enable +#define EHCI_PSC_PI_OFF (0 << 14) // Port Indicators: off +#define EHCI_PSC_PI_AMBER (1 << 14) // Port Indicators: amber +#define EHCI_PSC_PI_GREEN (2 << 14) // Port Indicators: green +#define EHCI_PSC_PO (1 << 13) // Port Owner +#define EHCI_PSC_PP (1 << 12) // Port Power +#define EHCI_PSC_LS_MASK (3 << 10) // Line Status mask +#define EHCI_PSC_LS_SE0 (0 << 10) // Line Status: SE0 +#define EHCI_PSC_LS_K (1 << 10) // Line Status: K-State +#define EHCI_PSC_LS_J (2 << 10) // Line Status: J-State +#define EHCI_PSC_RES (1 << 8) // Port Reset +#define EHCI_PSC_SUS (1 << 7) // Suspend +#define EHCI_PSC_FPR (1 << 6) // Force Port Resume +#define EHCI_PSC_OCC (1 << 5) // Over-current Change +#define EHCI_PSC_OCA (1 << 4) // Over-current Active +#define EHCI_PSC_PEDC (1 << 3) // Port Enable/Disable Change +#define EHCI_PSC_PED (1 << 2) // Port Enabled/Disabled +#define EHCI_PSC_CSC (1 << 1) // Connect Status Change +#define EHCI_PSC_CCS (1 << 0) // Current Connect Status + +// Capability Registers: HCSPARAMS +#define EHCI_HCS_N_CC(x) (((x) >> 12) & 0xf) // Number of Companion + // Controllers +#define EHCI_HCS_N_PCC(x) (((x) >> 8) & 0xf) // Number of Ports per CC +#define EHCI_HCS_PRR (1 << 7) // Port Routing Rules +#define EHCI_HCS_PPC (1 << 4) // Port Power Control +#define EHCI_HCS_N_PORTS(x) (((x) >> 0) & 0xf) // Number of Ports + +// Capability Registers: HCCPARAMS +#define EHCI_HCC_EECP(x) (((x) >> 8) & 0xff) // EHCI Extended Capabilities + // Pointer +#define EHCI_HCC_64 (1 << 0) // 64-bit Addressing Capability + +// EHCI Extended Capability +#define EHCI_EEC_NEXT(x) (((x) >> 8) & 0xff) +#define EHCI_EEC_ID(x) (((x) >> 0) & 0xff) + +#define EHCI_EECID_LEGSUP 0x01 + +// QH.ep_state[0] (Endpoint Characteristics) +#define EHCI_QHES0_RL(x) ((uint32_t)(x) << 28) // Nak Count Reload +#define EHCI_QHES0_C (1 << 27) // Control Endpoint Flag +#define EHCI_QHES0_MPL(x) ((x) << 16) // Maximum Packet Length +#define EHCI_QHES0_READ_MPL(x) (((x) >> 16) & 0x7ff) +#define EHCI_QHES0_H (1 << 15) // Head of Reclamation List flag +#define EHCI_QHES0_DTC (1 << 14) // Data Toggle Control +#define EHCI_QHES0_EPS_MASK (3 << 12) // Endpoint Speed mask +#define EHCI_QHES0_EPS_FS (0 << 12) // Endpoint Speed: Full Speed +#define EHCI_QHES0_EPS_LS (1 << 12) // Endpoint Speed: Low Speed +#define EHCI_QHES0_EPS_HS (2 << 12) // Endpoint Speed: High Speed +#define EHCI_QHES0_EP(x) ((x) << 8) // Endpoint Number +#define EHCI_QHES0_I (1 << 7) // Inactive on Next Transaction +#define EHCI_QHES0_DEV(x) ((x) << 0) // Device Address + +// QH.ep_state[1] (Endpoint Capabilities) +#define EHCI_QHES1_MULT(x) ((uint32_t)(x) << 30) // High-Bandwidth Pipe + // Multiplier +#define EHCI_QHES1_PORT(x) ((x) << 23) // Port Number +#define EHCI_QHES1_HUB(x) ((x) << 16) // Hub Address +#define EHCI_QHES1_SCM(x) ((x) << 8) // Split Completion Mask +#define EHCI_QHES1_ISM(x) ((x) << 0) // Interrupt Schedule Mask + +// qTD.status (Token) +#define EHCI_QTDS_DT(x) ((uint32_t)(x) << 31) // Data Toggle +#define EHCI_QTDS_XFLEN(x) ((x) << 16) // Total Bytes to Transfer +#define EHCI_QTDS_IOC (1 << 15) // Interrupt on Complete +#define EHCI_QTDS_CP(x) ((x) << 12) // Current Page +#define EHCI_QTDS_CERR(x) ((x) << 10) // Error Counter +#define EHCI_QTDS_PID_OUT (0 << 8) // PID: OUT +#define EHCI_QTDS_PID_IN (1 << 8) // PID: IN +#define EHCI_QTDS_PID_SETUP (2 << 8) // PID: SETUP +#define EHCI_QTDS_ACTIVE (1 << 7) // Status: Active +#define EHCI_QTDS_HALTED (1 << 6) // Status: Halted +#define EHCI_QTDS_BUFERR (1 << 5) // Status: Data Buffer Error +#define EHCI_QTDS_BABBLE (1 << 4) // Status: Babble Detected +#define EHCI_QTDS_XACTERR (1 << 3) // Status: Transaction Error +#define EHCI_QTDS_MuF (1 << 2) // Status: Missed Microframe +#define EHCI_QTDS_SPLITXS (1 << 1) // Status: Split Transaction State +#define EHCI_QTDS_P_ERR (1 << 0) // Status: Ping State / Error + +// High Speed Error Mask +#define EHCI_QTDS_HS_ERR_MASK \ + (EHCI_QTDS_HALTED | EHCI_QTDS_BUFERR | EHCI_QTDS_BABBLE | \ + EHCI_QTDS_XACTERR | EHCI_QTDS_MuF) + +// Low/Full Speed Error Mask +#define EHCI_QTDS_LFS_ERR_MASK \ + (EHCI_QTDS_HS_ERR_MASK | EHCI_QTDS_P_ERR) + + +struct cdi_device *ehci_init_device(struct cdi_bus_data *bus_data); + +void ehci_rh_port_down(struct cdi_usb_hub *hub, int index); +void ehci_rh_port_up(struct cdi_usb_hub *hub, int index); +cdi_usb_port_status_t ehci_rh_port_status(struct cdi_usb_hub *hub, int index); + +cdi_usb_hc_transaction_t + ehci_create_transaction(struct cdi_usb_hc *hc, + const struct cdi_usb_hc_ep_info *target); +void ehci_enqueue(struct cdi_usb_hc *hc, + const struct cdi_usb_hc_transmission *trans); +void ehci_start_transaction(struct cdi_usb_hc *cdi_hc, + cdi_usb_hc_transaction_t ta); +void ehci_wait_transaction(struct cdi_usb_hc *hc, cdi_usb_hc_transaction_t ta); +void ehci_destroy_transaction(struct cdi_usb_hc *hc, + cdi_usb_hc_transaction_t ta); + +#endif diff --git a/src/modules/cdi/ehci/main.c b/src/modules/cdi/ehci/main.c new file mode 100644 index 0000000..5623819 --- /dev/null +++ b/src/modules/cdi/ehci/main.c @@ -0,0 +1,69 @@ +/****************************************************************************** + * Copyright (c) 2015 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 "ehci.h" + + +#define DRIVER_NAME "ehci" + + +static struct cdi_usb_hcd ehcd; + +static int ehcd_init(void) +{ + cdi_driver_init(&ehcd.drv); + return 0; +} + +static int ehcd_destroy(void) +{ + cdi_driver_destroy(&ehcd.drv); + return 0; +} + +static struct cdi_usb_hcd ehcd = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_USB_HCD, + .bus = CDI_PCI, + .init = ehcd_init, + .destroy = ehcd_destroy, + .init_device = ehci_init_device, + }, + + .rh_drv = { + .port_disable = ehci_rh_port_down, + .port_reset_enable = ehci_rh_port_up, + .port_status = ehci_rh_port_status, + }, + + .create_transaction = ehci_create_transaction, + .enqueue = ehci_enqueue, + .start_transaction = ehci_start_transaction, + .wait_transaction = ehci_wait_transaction, + .destroy_transaction = ehci_destroy_transaction, +}; + +CDI_DRIVER(DRIVER_NAME, ehcd) -- 2.6.3 From max at tyndur.org Sun Dec 13 01:11:06 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:11:06 +0100 Subject: [tyndur-devel] [PATCH v2 21/24] usb: Add USB 2.0 bus driver In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-22-git-send-email-max@tyndur.org> + This adds a USB 2.0 bus driver. It is missing support for USB 3.x (obviously), and also interrupt and isochronous transfers (and other things, too). Also, it is probably bad (e.g. there is only a single counter for device IDs instead of keeping track of which IDs are used and which are not). Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/usb/main.c | 61 +++ src/modules/cdi/usb/usb-hubs.h | 78 ++++ src/modules/cdi/usb/usb.c | 893 +++++++++++++++++++++++++++++++++++++++++ src/modules/cdi/usb/usb.h | 86 ++++ 4 files changed, 1118 insertions(+) create mode 100644 src/modules/cdi/usb/main.c create mode 100644 src/modules/cdi/usb/usb-hubs.h create mode 100644 src/modules/cdi/usb/usb.c create mode 100644 src/modules/cdi/usb/usb.h diff --git a/src/modules/cdi/usb/main.c b/src/modules/cdi/usb/main.c new file mode 100644 index 0000000..3add372 --- /dev/null +++ b/src/modules/cdi/usb/main.c @@ -0,0 +1,61 @@ +/****************************************************************************** + * Copyright (c) 2015 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 "usb.h" + + +#define DRIVER_NAME "usb" + + +static struct cdi_usb_driver usbd; + +static int usbd_init(void) +{ + cdi_driver_init(&usbd.drv); + return 0; +} + +static int usbd_destroy(void) +{ + cdi_driver_destroy(&usbd.drv); + return 0; +} + +static struct cdi_usb_driver usbd = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_USB, + .bus = CDI_USB_HCD, + .init = usbd_init, + .destroy = usbd_destroy, + .init_device = usb_init_hc, + }, + + .get_endpoint_descriptor = usb_get_endpoint_descriptor, + .control_transfer = usb_control_transfer, + .bulk_transfer = usb_bulk_transfer, +}; + +CDI_DRIVER(DRIVER_NAME, usbd) diff --git a/src/modules/cdi/usb/usb-hubs.h b/src/modules/cdi/usb/usb-hubs.h new file mode 100644 index 0000000..da8ac3e --- /dev/null +++ b/src/modules/cdi/usb/usb-hubs.h @@ -0,0 +1,78 @@ +/****************************************************************************** + * Copyright (c) 2015 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_HUBS_H +#define USB_HUBS_H + +#include + +#include "usb.h" + + +struct usb_hub_descriptor { + uint8_t b_length; + uint8_t b_descriptor_type; + uint8_t b_nbr_ports; + uint16_t w_hub_characteristics; + uint8_t b_pwr_on_2_pwr_good; + uint8_t b_hub_contr_current; + uint8_t device_removable[]; +} __attribute__((packed)); + + +enum { + USB_DESC_HUB = 0x29, +}; + +// hub device features +enum { + C_HUB_LOCAL_POWER = 0, + C_HUB_OVER_CURRENT, +}; + +// hub port features +enum { + PORT_CONNECTION = 0, + PORT_ENABLE, + PORT_SUSPEND, + PORT_OVER_CURRENT, + PORT_RESET, + + PORT_POWER = 8, + PORT_LOW_SPEED, + PORT_HIGH_SPEED, + + C_PORT_CONNECTION = 16, + C_PORT_ENABLE, + C_PORT_SUSPEND, + C_PORT_OVER_CURRENT, + C_PORT_RESET, + PORT_TEST, + PORT_INDICATOR, +}; + +typedef struct usb_hub_device { + struct cdi_usb_hub cdi; + usb_hub_t *hub; +} usb_hub_device_t; + +#endif diff --git a/src/modules/cdi/usb/usb.c b/src/modules/cdi/usb/usb.c new file mode 100644 index 0000000..62ef989 --- /dev/null +++ b/src/modules/cdi/usb/usb.c @@ -0,0 +1,893 @@ +/****************************************************************************** + * Copyright (c) 2015 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 + +#include "usb.h" +#include "usb-hubs.h" + + +// #define DEBUG + +#ifdef DEBUG +#define DPRINTF(...) printf(__VA_ARGS__) +#else +#define DPRINTF(...) +#endif + + +static int current_usb_addr = 1; + + +static cdi_usb_transmission_result_t + do_control_transfer(struct cdi_usb_hc *hc, int dev, cdi_usb_speed_t speed, + int tt_addr, int tt_port, const usb_endpoint_t *ep, + const struct cdi_usb_setup_packet *setup, void *data) +{ + struct cdi_usb_hcd *hcd = CDI_UPCAST(hc->dev.driver, struct cdi_usb_hcd, + drv); + cdi_usb_hc_transaction_t ta; + cdi_usb_transmission_result_t r1, r2, r3; + + ta = hcd->create_transaction(hc, &(struct cdi_usb_hc_ep_info){ + .dev = dev, + .ep = ep->ep, + .ep_type = ep->type, + .speed = speed, + .mps = ep->mps, + .tt_addr = tt_addr, + .tt_port = tt_port + }); + + hcd->enqueue(hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = CDI_USB_SETUP, + .buffer = (void *)setup, + .size = sizeof(*setup), + .toggle = 0, + .result = &r1 + }); + + if (data) { + hcd->enqueue(hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = setup->bm_request_type & CDI_USB_CREQ_IN + ? CDI_USB_IN : CDI_USB_OUT, + .buffer = data, + .size = setup->w_length, + .toggle = 1, + .result = &r2 + }); + } else { + r2 = CDI_USB_OK; + } + + hcd->enqueue(hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = setup->bm_request_type & CDI_USB_CREQ_IN ? CDI_USB_OUT + : CDI_USB_IN, + .toggle = 1, + .result = &r3 + }); + + hcd->start_transaction(hc, ta); + hcd->wait_transaction(hc, ta); + hcd->destroy_transaction(hc, ta); + + return r1 ? r1 : r2 ? r2 : r3; +} + +static cdi_usb_transmission_result_t + control_transfer(const usb_device_t *dev, const usb_endpoint_t *ep, + const struct cdi_usb_setup_packet *setup, void *data) +{ + return do_control_transfer(dev->hc, dev->id, dev->speed, dev->tt_addr, + dev->tt_port, ep, setup, data); +} + + +static int get_mps0(usb_hub_t *hub, int hub_port, cdi_usb_speed_t speed) +{ + cdi_usb_transmission_result_t res; + struct cdi_usb_device_descriptor device_descriptor; + int tt_addr = 0, tt_port = 0; + + if (speed < CDI_USB_HIGH_SPEED && hub->ldev) { + if (hub->ldev->dev->speed < CDI_USB_HIGH_SPEED) { + tt_addr = hub->ldev->dev->tt_addr; + tt_port = hub->ldev->dev->tt_port; + } else { + tt_addr = hub->ldev->dev->id; + tt_port = hub_port; + } + } + + res = do_control_transfer(hub->hc, 0, speed, tt_addr, tt_port, + &(usb_endpoint_t){ + .type = CDI_USB_CONTROL, + .mps = 8 + }, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_DEVICE << 8) | 0, + .w_length = 8 + }, &device_descriptor); + + if (res != CDI_USB_OK) { + return -EIO; + } + + return device_descriptor.b_max_packet_size_0; +} + + +static usb_device_t *enumerate(usb_hub_t *hub, int hub_port, + cdi_usb_speed_t speed, int mps0) +{ + cdi_usb_transmission_result_t res; + usb_device_t *dev = calloc(1, sizeof(*dev)); + + int new_id = current_usb_addr++; + if (new_id > 127) { + // TODO: Per-bus ID, and detection of device disconnects so we can reuse + // old (stale) addresses + printf("[usb] USB address space exhausted, cannot enumerate device\n"); + free(dev); + return NULL; + } + + dev->hc = hub->hc; + dev->speed = speed; + + if (speed < CDI_USB_HIGH_SPEED && hub->ldev) { + if (hub->ldev->dev->speed < CDI_USB_HIGH_SPEED) { + dev->tt_addr = hub->ldev->dev->tt_addr; + dev->tt_port = hub->ldev->dev->tt_port; + } else { + dev->tt_addr = hub->ldev->dev->id; + dev->tt_port = hub_port; + } + } + + dev->eps[0][0].type = CDI_USB_CONTROL; + dev->eps[0][0].mps = mps0; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_ADDRESS, + .w_value = new_id + }, NULL); + if (res != CDI_USB_OK) { + free(dev); + return NULL; + } + + dev->id = new_id; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_DEVICE << 8) | 0, + .w_length = sizeof(dev->dev_desc) + }, &dev->dev_desc); + if (res != CDI_USB_OK) { + free(dev); + return NULL; + } + + return dev; +} + + +#ifdef DEBUG +static void get_string_descriptor(usb_device_t *dev, int index, + struct cdi_usb_string_descriptor *dst) +{ + cdi_usb_transmission_result_t res; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_STRING << 8) | index, + .w_length = 1 + }, dst); + if (res != CDI_USB_OK || !dst->b_length) { + dst->b_length = 0; + return; + } + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_STRING << 8) | index, + .w_length = dst->b_length + }, dst); + if (res != CDI_USB_OK) { + dst->b_length = 0; + return; + } +} +#endif + + +static int get_configurations(usb_device_t *dev) +{ + cdi_usb_transmission_result_t res; + struct cdi_usb_configuration_descriptor config; + + dev->configs = calloc(dev->dev_desc.b_num_configurations, + sizeof(dev->configs[0])); + + for (int i = 0; i < (int)dev->dev_desc.b_num_configurations; i++) { + res = control_transfer(dev, &dev->eps[0][0], + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_CONFIGURATION << 8) | i, + .w_length = sizeof(config) + }, &config); + if (res != CDI_USB_OK) { + goto fail; + } + + dev->configs[i] = malloc(config.w_total_length); + res = control_transfer(dev, &dev->eps[0][0], + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (CDI_USB_DESC_CONFIGURATION << 8) | i, + .w_length = config.w_total_length + }, dev->configs[i]); + if (res != CDI_USB_OK) { + goto fail; + } + } + + return 0; + +fail: + for (int i = 0; i < (int)dev->dev_desc.b_num_configurations; i++) { + free(dev->configs[i]); + } + free(dev->configs); + return -EIO; +} + + +static bool compare_configurations(usb_device_t *dev, int i, int j) +{ + // TODO: Prefer self-powered + if (dev->configs[i]->b_max_power != dev->configs[j]->b_max_power) { + return dev->configs[i]->b_max_power > dev->configs[j]->b_max_power; + } + + if (dev->configs[i]->b_num_interfaces != dev->configs[j]->b_num_interfaces) + { + return dev->configs[i]->b_num_interfaces + > dev->configs[j]->b_num_interfaces; + } + + return false; +} + + +static int choose_configuration(usb_device_t *dev) +{ + cdi_usb_transmission_result_t res; + int best_config = 0; + + for (int i = 1; i < (int)dev->dev_desc.b_num_configurations; i++) { + if (compare_configurations(dev, i, best_config)) { + best_config = i; + } + } + + dev->config = dev->configs[best_config]; + + res = control_transfer(dev, &dev->eps[0][0], &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_CONFIGURATION, + .w_value = dev->config->b_configuration_value, + }, NULL); + if (res != CDI_USB_OK) { + dev->config = NULL; + return -EIO; + } + + return 0; +} + + +#ifdef DEBUG +static void debug_device(usb_device_t *dev) +{ + printf("[usb] new %s device %04x:%04x (%x.%x.%x) ", + dev->speed == CDI_USB_LOW_SPEED ? "low speed" : + dev->speed == CDI_USB_FULL_SPEED ? "full speed" : + dev->speed == CDI_USB_HIGH_SPEED ? "high speed" : + dev->speed == CDI_USB_SUPERSPEED ? "superspeed" : + "futurespeed", + dev->dev_desc.id_vendor, dev->dev_desc.id_product, + dev->dev_desc.b_device_class, + dev->dev_desc.b_device_sub_class, + dev->dev_desc.b_device_protocol); + + struct cdi_usb_string_descriptor str; + + if (dev->dev_desc.i_manufacturer) { + get_string_descriptor(dev, dev->dev_desc.i_manufacturer, &str); + for (int i = 0; i < (str.b_length - 2) / 2; i++) { + putchar(str.b_string[i] > 0x7f ? '?' : str.b_string[i]); + } + putchar(' '); + } + + if (dev->dev_desc.i_product) { + get_string_descriptor(dev, dev->dev_desc.i_product, &str); + for (int i = 0; i < (str.b_length - 2) / 2; i++) { + putchar(str.b_string[i] > 0x7f ? '?' : str.b_string[i]); + } + putchar(' '); + } + + putchar('\n'); +} + +static void debug_configuration(usb_device_t *dev) +{ + printf("[usb] chose configuration %i ", dev->config->b_configuration_value); + + struct cdi_usb_string_descriptor str; + + if (dev->config->i_configuration) { + get_string_descriptor(dev, dev->config->i_configuration, &str); + putchar('('); + for (int i = 0; i < (str.b_length - 2) / 2; i++) { + putchar(str.b_string[i] > 0x7f ? '?' : str.b_string[i]); + } + putchar(')'); + putchar(' '); + } + + printf("with %i interface(s):\n", dev->config->b_num_interfaces); + + struct cdi_usb_interface_descriptor *ifc = (void *)(dev->config + 1); + for (int i = 0; i < (int)dev->config->b_num_interfaces; i++) { + printf("[usb] "); + if (ifc->i_interface) { + get_string_descriptor(dev, ifc->i_interface, &str); + for (int j = 0; j < (int)dev->config->b_num_interfaces; j++) { + putchar(str.b_string[j] > 0x7f ? '?' : str.b_string[j]); + } + putchar(' '); + } + printf("%x.%x.%x\n", ifc->b_interface_class, + ifc->b_interface_sub_class, ifc->b_interface_protocol); + + ifc = (void *)((struct cdi_usb_endpoint_descriptor *)(ifc + 1) + + ifc->b_num_endpoints); + } +} +#endif + + +static void new_hub_device(usb_logical_device_t *ldev); + +static void new_hub(usb_hub_t *hub) +{ + for (int i = 0; i < hub->cdi->ports; i++) { + hub->drv->port_disable(hub->cdi, i); + } + + DPRINTF("[usb] new hub with %i ports detected\n", hub->cdi->ports); + + for (int i = 0; i < hub->cdi->ports; i++) { + hub->drv->port_reset_enable(hub->cdi, i); + cdi_sleep_ms(5); + + cdi_usb_port_status_t status = hub->drv->port_status(hub->cdi, i); + if (!(status & CDI_USB_PORT_CONNECTED)) { + hub->drv->port_disable(hub->cdi, i); + continue; + } + + int mps0 = get_mps0(hub, i, status & CDI_USB_PORT_SPEED_MASK); + if (mps0 < 0) { + printf("[usb] failed to inquire MPS0\n"); + hub->drv->port_disable(hub->cdi, i); + continue; + } + + // Reset the device a second time. Maybe the device is broken and still + // wants to send more data even though we only requested the first eight + // bytes of the device descriptor (which we cannot receive without + // knowing the MPS0). A reset will solve the problem and will not do any + // harm otherwise. + hub->drv->port_reset_enable(hub->cdi, i); + cdi_sleep_ms(5); + + status = hub->drv->port_status(hub->cdi, i); + if (!(status & CDI_USB_PORT_CONNECTED)) { + hub->drv->port_disable(hub->cdi, i); + continue; + } + + usb_device_t *dev = enumerate(hub, i, status & CDI_USB_PORT_SPEED_MASK, + mps0); + if (!dev) { + printf("[usb] failed to enumerate device\n"); + hub->drv->port_disable(hub->cdi, i); + continue; + } + +#ifdef DEBUG + debug_device(dev); +#endif + + usb_logical_device_t *ldev = NULL; + int ldev_ep_idx = 0; + bool interface_level; + + interface_level = dev->dev_desc.b_device_class == CDI_USB_DEVCLS_NONE; + if (!interface_level) { + ldev = calloc(1, sizeof(*ldev)); + ldev->cdi.bus_data.bus_type = CDI_USB; + ldev->cdi.interface = -1; + ldev->cdi.endpoint_count = 1; // EP0 + ldev->cdi.vendor_id = dev->dev_desc.id_vendor; + ldev->cdi.product_id = dev->dev_desc.id_product; + ldev->cdi.class_id = dev->dev_desc.b_device_class; + ldev->cdi.subclass_id = dev->dev_desc.b_device_sub_class; + ldev->cdi.protocol_id = dev->dev_desc.b_device_protocol; + + ldev->dev = dev; + + ldev->endpoints = malloc(1 * sizeof(ldev->endpoints[0])); + ldev->endpoints[0] = 0; // EP0 + ldev_ep_idx = 1; + } + + if (get_configurations(dev) < 0) { + printf("[usb] failed to get device configurations\n"); + free(ldev->endpoints); + free(ldev); + free(dev); + hub->drv->port_disable(hub->cdi, i); + continue; + } + if (choose_configuration(dev) < 0) { + printf("[usb] failed to select device configuration\n"); + free(ldev->endpoints); + free(ldev); + for (int j = 0; j < (int)dev->dev_desc.b_num_configurations; j++) { + free(dev->configs[j]); + } + free(dev->config); + free(dev); + hub->drv->port_disable(hub->cdi, i); + continue; + } + +#ifdef DEBUG + debug_configuration(dev); +#endif + + struct cdi_usb_interface_descriptor *ifc = (void *)(dev->config + 1); + for (int j = 0; j < (int)dev->config->b_num_interfaces; j++) { + if (interface_level) { + ldev = calloc(1, sizeof(*ldev)); + ldev->cdi.bus_data.bus_type = CDI_USB; + ldev->cdi.interface = j; + // Include EP0 + ldev->cdi.endpoint_count = ifc->b_num_endpoints + 1; + ldev->cdi.vendor_id = dev->dev_desc.id_vendor; + ldev->cdi.product_id = dev->dev_desc.id_product; + ldev->cdi.class_id = ifc->b_interface_class; + ldev->cdi.subclass_id = ifc->b_interface_sub_class; + ldev->cdi.protocol_id = ifc->b_interface_protocol; + + ldev->dev = dev; + + ldev->endpoints = malloc(ldev->cdi.endpoint_count + * sizeof(ldev->endpoints[0])); + ldev->endpoints[0] = 0; // EP0 + ldev_ep_idx = 1; + } else { + ldev->cdi.endpoint_count += ifc->b_num_endpoints; + ldev->endpoints = realloc(ldev->endpoints, + ldev->cdi.endpoint_count + * sizeof(ldev->endpoints[0])); + } + + struct cdi_usb_endpoint_descriptor *ep = (void *)(ifc + 1); + for (int k = 0; k < (int)ifc->b_num_endpoints; k++, ep++) { + bool in = ep->b_endpoint_address & CDI_USB_EPDSC_EPADR_DIR; + int id = CDI_USB_EPDSC_EPADR_ID(ep->b_endpoint_address); + + dev->eps[in][id].ep = id; + dev->eps[in][id].mps + = CDI_USB_EPDSC_MPS_MPS(ep->w_max_packet_size); + dev->eps[in][id].desc = ep; + + switch (ep->bm_attributes & CDI_USB_EPDSC_ATTR_XFER_TYPE_MASK) { + case CDI_USB_EPDSC_ATTR_CONTROL: + dev->eps[in][id].type = CDI_USB_CONTROL; + break; + case CDI_USB_EPDSC_ATTR_ISOCHRONOUS: + dev->eps[in][id].type = CDI_USB_ISOCHRONOUS; + break; + case CDI_USB_EPDSC_ATTR_BULK: + dev->eps[in][id].type = CDI_USB_BULK; + break; + case CDI_USB_EPDSC_ATTR_INTERRUPT: + dev->eps[in][id].type = CDI_USB_INTERRUPT; + break; + } + + ldev->endpoints[ldev_ep_idx++] = ((int)in << 4) | id; + } + + if (interface_level) { + if (ldev->cdi.class_id == CDI_USB_DEVCLS_HUB) { + new_hub_device(ldev); + } else { + cdi_provide_device(&ldev->cdi.bus_data); + } + } + + ifc = (void *)ep; + } + + if (!interface_level) { + if (ldev->cdi.class_id == CDI_USB_DEVCLS_HUB) { + new_hub_device(ldev); + } else { + cdi_provide_device(&ldev->cdi.bus_data); + } + } + } +} + +struct cdi_device *usb_init_hc(struct cdi_bus_data *bus_data) +{ + struct cdi_usb_hc_bus *hc_bus = CDI_UPCAST(bus_data, struct cdi_usb_hc_bus, + bus_data); + struct cdi_usb_hc *hc = hc_bus->hc; + struct cdi_usb_hcd *hcd = CDI_UPCAST(hc->dev.driver, struct cdi_usb_hcd, + drv); + + usb_hub_t *hub = calloc(1, sizeof(*hub)); + hub->cdi = &hc->rh; + hub->hc = hc; + hub->drv = &hcd->rh_drv; + + new_hub(hub); + + return NULL; +} + + +void usb_get_endpoint_descriptor(struct cdi_usb_device *dev, int ep, + struct cdi_usb_endpoint_descriptor *desc) +{ + usb_logical_device_t *ldev = CDI_UPCAST(dev, usb_logical_device_t, cdi); + + if (ep < 0 || ep >= dev->endpoint_count) { + return; + } + + int ep_idx = ldev->endpoints[ep]; + usb_endpoint_t *ep_info = &ldev->dev->eps[ep_idx >> 4][ep_idx & 0xf]; + + if (ep_idx) { + assert(ep_info->desc); + memcpy(desc, ep_info->desc, sizeof(*desc)); + } else { + *desc = (struct cdi_usb_endpoint_descriptor){ + .b_length = sizeof(*desc), + .b_descriptor_type = CDI_USB_DESC_ENDPOINT, + .b_endpoint_address = 0, + .bm_attributes = 0x00, + .w_max_packet_size = ep_info->mps, + .b_interval = 0 + }; + } +} + + +cdi_usb_transmission_result_t + usb_control_transfer(struct cdi_usb_device *dev, int ep, + const struct cdi_usb_setup_packet *setup, void *data) +{ + usb_logical_device_t *ldev = CDI_UPCAST(dev, usb_logical_device_t, cdi); + + if (ep < 0 || ep >= dev->endpoint_count) { + return CDI_USB_DRIVER_ERROR; + } + + int ep_idx = ldev->endpoints[ep]; + usb_endpoint_t *ep_info = &ldev->dev->eps[ep_idx >> 4][ep_idx & 0xf]; + + if (ep_info->type != CDI_USB_CONTROL) { + return CDI_USB_DRIVER_ERROR; + } + + return control_transfer(ldev->dev, ep_info, setup, data); +} + + +cdi_usb_transmission_result_t usb_bulk_transfer(struct cdi_usb_device *dev, + int ep, void *data, size_t size) +{ + usb_logical_device_t *ldev = CDI_UPCAST(dev, usb_logical_device_t, cdi); + + if (ep < 0 || ep >= dev->endpoint_count) { + return CDI_USB_DRIVER_ERROR; + } + + int ep_idx = ldev->endpoints[ep]; + usb_endpoint_t *ep_info = &ldev->dev->eps[ep_idx >> 4][ep_idx & 0xf]; + + if (ep_info->type != CDI_USB_BULK) { + return CDI_USB_DRIVER_ERROR; + } + + struct cdi_usb_hcd *hcd = CDI_UPCAST(ldev->dev->hc->dev.driver, + struct cdi_usb_hcd, drv); + cdi_usb_hc_transaction_t ta; + cdi_usb_transmission_result_t res; + + ta = hcd->create_transaction(ldev->dev->hc, &(struct cdi_usb_hc_ep_info){ + .dev = ldev->dev->id, + .ep = ep_info->ep, + .ep_type = ep_info->type, + .speed = ldev->dev->speed, + .mps = ep_info->mps, + .tt_addr = ldev->dev->tt_addr, + .tt_port = ldev->dev->tt_port + }); + + hcd->enqueue(ldev->dev->hc, &(struct cdi_usb_hc_transmission){ + .ta = ta, + .token = ep_idx >> 4 ? CDI_USB_IN : CDI_USB_OUT, + .buffer = data, + .size = size, + .toggle = ep_info->toggle, + .result = &res + }); + ep_info->toggle ^= (((size + ep_info->mps - 1) / ep_info->mps) & 1); + + hcd->start_transaction(ldev->dev->hc, ta); + hcd->wait_transaction(ldev->dev->hc, ta); + hcd->destroy_transaction(ldev->dev->hc, ta); + + return res; +} + + +static struct cdi_usb_hub_driver hub_drv; + + +static cdi_usb_transmission_result_t + hub_set_hub_feature(usb_hub_device_t *hub, uint16_t feature) +{ + return usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE + | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_FEATURE, + .w_value = feature + }, NULL); +} + + +static cdi_usb_transmission_result_t + hub_clear_port_feature(usb_hub_device_t *hub, + uint16_t feature, uint16_t port) +{ + return usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER + | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_CLEAR_FEATURE, + .w_value = feature, + .w_index = port + 1 + }, NULL); +} + + +static uint32_t hub_get_port_status(usb_hub_device_t *hub, uint16_t port) +{ + cdi_usb_transmission_result_t res; + uint32_t portsc; + + res = usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER + | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_STATUS, + .w_index = port + 1, + .w_length = sizeof(portsc) + }, &portsc); + if (res != CDI_USB_OK) { + return 0; + } + + return portsc; +} + + +static cdi_usb_transmission_result_t + hub_set_port_feature(usb_hub_device_t *hub, + uint16_t feature, uint16_t port) +{ + return usb_control_transfer(&hub->hub->ldev->cdi, 0, + &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_OTHER + | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_OUT, + .b_request = CDI_USB_CREQ_SET_FEATURE, + .w_value = feature, + .w_index = port + 1 + }, NULL); +} + + +static void new_hub_device(usb_logical_device_t *ldev) +{ + cdi_usb_transmission_result_t res; + usb_hub_device_t *hub; + struct usb_hub_descriptor *hub_desc = NULL; + + assert(ldev->cdi.interface < 0); + + hub = calloc(1, sizeof(*hub)); + + hub->hub = calloc(1, sizeof(*hub->hub)); + hub->hub->cdi = &hub->cdi; + hub->hub->drv = &hub_drv; + hub->hub->hc = ldev->dev->hc; + hub->hub->ldev = ldev; + + uint8_t hub_desc_length; + res = usb_control_transfer(&ldev->cdi, 0, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (USB_DESC_HUB << 8) | 0, + .w_length = 1 + }, &hub_desc_length); + if (res != CDI_USB_OK || + hub_desc_length < sizeof(struct usb_hub_descriptor)) + { + goto fail; + } + + hub_desc = malloc(hub_desc_length); + res = usb_control_transfer(&ldev->cdi, 0, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_DEVICE | CDI_USB_CREQ_CLASS + | CDI_USB_CREQ_IN, + .b_request = CDI_USB_CREQ_GET_DESCRIPTOR, + .w_value = (USB_DESC_HUB << 8) | 0, + .w_length = hub_desc_length + }, hub_desc); + if (res != CDI_USB_OK) { + goto fail; + } + + int ports = hub_desc->b_nbr_ports; + hub->cdi.ports = ports; + + // Enable port power + if (!(hub_desc->w_hub_characteristics & (1 << 1))) { + // Hub supports power switching, enable global power + if (hub_set_hub_feature(hub, C_HUB_LOCAL_POWER) != CDI_USB_OK) { + goto fail; + } + } + + if ((hub_desc->w_hub_characteristics & 0x3) == 1) { + // Hub supports per-port power switching, enable power for each port + for (int port = 0; port < ports; port++) { + if (hub_set_port_feature(hub, PORT_POWER, port) != CDI_USB_OK) { + goto fail; + } + } + } + + cdi_sleep_ms(hub_desc->b_pwr_on_2_pwr_good * 2); + + free(hub_desc); + + new_hub(hub->hub); + + return; + +fail: + free(hub_desc); + free(hub->hub); + free(hub); + return; +} + + +static void hub_port_down(struct cdi_usb_hub *cdi_hub, int index) +{ + usb_hub_device_t *hub = CDI_UPCAST(cdi_hub, usb_hub_device_t, cdi); + hub_clear_port_feature(hub, PORT_ENABLE, index); +} + + +static void hub_port_up(struct cdi_usb_hub *cdi_hub, int index) +{ + usb_hub_device_t *hub = CDI_UPCAST(cdi_hub, usb_hub_device_t, cdi); + + hub_clear_port_feature(hub, C_PORT_RESET, index); + hub_set_port_feature(hub, PORT_RESET, index); + + CDI_CONDITION_WAIT(hub_get_port_status(hub, index) & (1 << C_PORT_RESET), + 100); +} + + +static cdi_usb_port_status_t hub_port_status(struct cdi_usb_hub *cdi_hub, + int index) +{ + usb_hub_device_t *hub = CDI_UPCAST(cdi_hub, usb_hub_device_t, cdi); + + uint32_t portsc = hub_get_port_status(hub, index); + + cdi_usb_port_status_t status = 0; + + if (portsc & ((1 << PORT_ENABLE) | (1 << PORT_CONNECTION))) { + status |= CDI_USB_PORT_CONNECTED; + if (portsc & (1 << PORT_LOW_SPEED)) { + status |= CDI_USB_LOW_SPEED; + } else if (portsc & (1 << PORT_HIGH_SPEED)) { + status |= CDI_USB_HIGH_SPEED; + } else { + status |= CDI_USB_FULL_SPEED; + } + } + + return status; +} + + +static struct cdi_usb_hub_driver hub_drv = { + .port_disable = hub_port_down, + .port_reset_enable = hub_port_up, + .port_status = hub_port_status, +}; diff --git a/src/modules/cdi/usb/usb.h b/src/modules/cdi/usb/usb.h new file mode 100644 index 0000000..27dc061 --- /dev/null +++ b/src/modules/cdi/usb/usb.h @@ -0,0 +1,86 @@ +/****************************************************************************** + * Copyright (c) 2015 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_H +#define USB_H + +#include +#include + +#include +#include +#include + + +typedef struct usb_logical_device usb_logical_device_t; + +typedef struct usb_hub { + struct cdi_usb_hub *cdi; + + const struct cdi_usb_hub_driver *drv; + struct cdi_usb_hc *hc; + usb_logical_device_t *ldev; +} usb_hub_t; + +typedef struct usb_endpoint { + int ep; + cdi_usb_endpoint_type_t type; + int mps; + int toggle; + struct cdi_usb_endpoint_descriptor *desc; +} usb_endpoint_t; + +typedef struct usb_device { + struct cdi_usb_hc *hc; + int id; + cdi_usb_speed_t speed; + + int tt_addr, tt_port; + + // First index: OUT (0) / IN (1) + usb_endpoint_t eps[2][16]; + + struct cdi_usb_device_descriptor dev_desc; + struct cdi_usb_configuration_descriptor **configs, *config; +} usb_device_t; + +struct usb_logical_device { + struct cdi_usb_device cdi; + usb_device_t *dev; + int *endpoints; +}; + + +struct cdi_device *usb_init_hc(struct cdi_bus_data *bus_data); + +void usb_get_endpoint_descriptor(struct cdi_usb_device *dev, int ep, + struct cdi_usb_endpoint_descriptor *desc); + +cdi_usb_transmission_result_t + usb_control_transfer(struct cdi_usb_device *dev, int ep, + const struct cdi_usb_setup_packet *setup, void *data); + +cdi_usb_transmission_result_t usb_bulk_transfer(struct cdi_usb_device *dev, + int ep, void *data, + size_t size); + +#endif -- 2.6.3 From max at tyndur.org Sun Dec 13 01:11:07 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:11:07 +0100 Subject: [tyndur-devel] [PATCH v2 22/24] usb-storage: Add USB mass storage driver In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-23-git-send-email-max@tyndur.org> + This adds a USB mass storage device driver. It is missing proper error handling (e.g. for STALLed endpoints) and support for any USB MSD class other than bulk-only (e.g. control-bulk-interrupt for USB floppy drives). Signed-off-by: Max Reitz Acked-by: Kevin Wolf --- src/modules/cdi/usb-storage/main.c | 76 +++++++++++ src/modules/cdi/usb-storage/usb-storage.c | 216 ++++++++++++++++++++++++++++++ src/modules/cdi/usb-storage/usb-storage.h | 60 +++++++++ 3 files changed, 352 insertions(+) create mode 100644 src/modules/cdi/usb-storage/main.c create mode 100644 src/modules/cdi/usb-storage/usb-storage.c create mode 100644 src/modules/cdi/usb-storage/usb-storage.h diff --git a/src/modules/cdi/usb-storage/main.c b/src/modules/cdi/usb-storage/main.c new file mode 100644 index 0000000..aa896ca --- /dev/null +++ b/src/modules/cdi/usb-storage/main.c @@ -0,0 +1,76 @@ +/****************************************************************************** + * Copyright (c) 2015 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 "usb-storage.h" + + +#define DRIVER_NAME "usb-storage" + + +static struct cdi_scsi_driver drv; +static struct cdi_usb_bus_device_pattern bus_pattern; + +static int drv_init(void) +{ + cdi_scsi_driver_init(&drv); + cdi_handle_bus_device(&drv.drv, &bus_pattern.pattern); + return 0; +} + +static int drv_destroy(void) +{ + cdi_driver_destroy(&drv.drv); + return 0; +} + + +static struct cdi_scsi_driver drv = { + .drv = { + .name = DRIVER_NAME, + .type = CDI_SCSI, + .bus = CDI_USB, + .init = drv_init, + .destroy = drv_destroy, + .init_device = init_usb_device, + }, + + .request = usb_storage_request, +}; + +static struct cdi_usb_bus_device_pattern bus_pattern = { + .pattern = { + .bus_type = CDI_USB, + }, + + .vendor_id = -1, + .product_id = -1, + + .class_id = CDI_USB_IFCCLS_STORAGE, + .subclass_id = -1, + .protocol_id = -1, +}; + +CDI_DRIVER(DRIVER_NAME, drv) diff --git a/src/modules/cdi/usb-storage/usb-storage.c b/src/modules/cdi/usb-storage/usb-storage.c new file mode 100644 index 0000000..544c779 --- /dev/null +++ b/src/modules/cdi/usb-storage/usb-storage.c @@ -0,0 +1,216 @@ +/****************************************************************************** + * Copyright (c) 2015 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 + +#include "usb-storage.h" + + +#define CBW_IN 0x80 +#define CBW_OUT 0x00 + +#define CBW_SIGNATURE 0x43425355 // 'USBC' => USB command +#define CSW_SIGNATURE 0x53425355 // 'USBS' => USB status + +struct msd_bo_cbw { + uint32_t signature; + uint32_t tag; + uint32_t data_length; + uint8_t flags; + uint8_t lun; + uint8_t cb_length; + uint8_t cb[16]; +} __attribute__((packed)); + +struct msd_bo_csw { + uint32_t signature; + uint32_t tag; + uint32_t data_residue; + uint8_t status; +} __attribute__((packed)); + + +typedef enum usb_mass_storage_type { + USBMS_BULK_ONLY, +} usb_mass_storage_type_t; + + +typedef struct usb_mass_storage_device { + struct cdi_scsi_device dev; + struct cdi_usb_device usb; + + usb_mass_storage_type_t type; + int luns; + + // FIXME: put into own class + int bulk_in, bulk_out; +} usb_mass_storage_device_t; + + +static int bulk_only_reset(usb_mass_storage_device_t *dev) +{ + cdi_usb_transmission_result_t res; + + res = cdi_usb_control_transfer(&dev->usb, 0, &(struct cdi_usb_setup_packet){ + .bm_request_type = CDI_USB_CREQ_CLASS | CDI_USB_CREQ_INTERFACE + | CDI_USB_CREQ_OUT, + .b_request = USBMS_REQ_BOMSR, + .w_index = dev->usb.interface + }, NULL); + if (res != CDI_USB_OK) { + return -EIO; + } + + if (dev->luns > 0) { + return 0; + } + + uint8_t lun_count; + res = cdi_usb_control_transfer(&dev->usb, 0, &(struct cdi_usb_setup_packet) { + .bm_request_type = CDI_USB_CREQ_CLASS | CDI_USB_CREQ_INTERFACE + | CDI_USB_CREQ_IN, + .b_request = USBMS_REQ_GML, + .w_index = dev->usb.interface, + .w_length = 1 + }, &lun_count); + if (res == CDI_USB_OK) { + dev->luns = lun_count + 1; + } else { + dev->luns = 1; + } + + return 0; +} + + +struct cdi_device *init_usb_device(struct cdi_bus_data *bus_data) +{ + static int dev_counter = 0; + usb_mass_storage_device_t *dev = calloc(1, sizeof(*dev)); + dev->usb = *CDI_UPCAST(bus_data, struct cdi_usb_device, bus_data); + + if ((dev->usb.subclass_id == USBMS_SUBCLS_SCSILEG || + dev->usb.subclass_id == USBMS_SUBCLS_SCSI) && + dev->usb.protocol_id == USBMS_PROTO_BBB) + { + dev->type = USBMS_BULK_ONLY; + } else { + free(dev); + return NULL; + } + + if (dev->type == USBMS_BULK_ONLY) { + if (bulk_only_reset(dev) < 0) { + free(dev); + return NULL; + } + + struct cdi_usb_endpoint_descriptor ep_desc; + for (int ep = 1; ep < dev->usb.endpoint_count; ep++) { + cdi_usb_get_endpoint_descriptor(&dev->usb, ep, &ep_desc); + + if ((ep_desc.bm_attributes & CDI_USB_EPDSC_ATTR_XFER_TYPE_MASK) + == CDI_USB_EPDSC_ATTR_BULK) + { + bool is_in; + is_in = ep_desc.b_endpoint_address & CDI_USB_EPDSC_EPADR_DIR; + if (!dev->bulk_in && is_in) { + dev->bulk_in = ep; + } + if (!dev->bulk_out && !is_in) { + dev->bulk_out = ep; + } + } + } + + if (!dev->bulk_in || !dev->bulk_out) { + free(dev); + return NULL; + } + } + + asprintf((char **)&dev->dev.dev.name, "msd%i", dev_counter++); + + dev->dev.type = CDI_STORAGE; + dev->dev.lun_count = dev->luns; + + return &dev->dev.dev; +} + + +int usb_storage_request(struct cdi_scsi_device *device, + struct cdi_scsi_packet *packet) +{ + cdi_usb_transmission_result_t res; + usb_mass_storage_device_t *dev = CDI_UPCAST(device, + usb_mass_storage_device_t, dev); + + struct msd_bo_cbw cbw = { + .signature = CBW_SIGNATURE, + .tag = 42, + .data_length = packet->bufsize, + .flags = packet->direction == CDI_SCSI_READ ? CBW_IN : CBW_OUT, + .lun = packet->lun, + .cb_length = packet->cmdsize + }; + memcpy(cbw.cb, packet->command, sizeof(cbw.cb)); + + res = cdi_usb_bulk_transfer(&dev->usb, dev->bulk_out, &cbw, sizeof(cbw)); + if (res != CDI_USB_OK) { + bulk_only_reset(dev); + return -1; + } + + if (packet->direction == CDI_SCSI_READ) { + res = cdi_usb_bulk_transfer(&dev->usb, dev->bulk_in, + packet->buffer, packet->bufsize); + } else if (packet->direction == CDI_SCSI_WRITE) { + res = cdi_usb_bulk_transfer(&dev->usb, dev->bulk_out, + packet->buffer, packet->bufsize); + } + + if (res != CDI_USB_OK) { + bulk_only_reset(dev); + return -1; + } + + struct msd_bo_csw csw; + res = cdi_usb_bulk_transfer(&dev->usb, dev->bulk_in, &csw, sizeof(csw)); + + if (res != CDI_USB_OK || csw.signature != CSW_SIGNATURE || csw.tag != 42 || + csw.data_residue || csw.status) + { + bulk_only_reset(dev); + return -1; + } + + return 0; +} diff --git a/src/modules/cdi/usb-storage/usb-storage.h b/src/modules/cdi/usb-storage/usb-storage.h new file mode 100644 index 0000000..eed3462 --- /dev/null +++ b/src/modules/cdi/usb-storage/usb-storage.h @@ -0,0 +1,60 @@ +/****************************************************************************** + * Copyright (c) 2015 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_STORAGE_H +#define USB_STORAGE_H + +#include +#include + + +// Mass storage subclass codes +#define USBMS_SUBCLS_SCSILEG 0x00 // SCSI legacy ("command set not reported") +#define USBMS_SUBCLS_RBC 0x01 // RBC +#define USBMS_SUBCLS_MMC5 0x02 // MMC-5 (ATAPI) +#define USBMS_SUBCLS_UFI 0x04 // UFI for Floppy Disk Drives +#define USBMS_SUBCLS_SCSI 0x06 // SCSI transparent command set +#define USBMS_SUBCLS_LSDFS 0x07 // LSD FS for negotiating SCSI access +#define USBMS_SUBCLS_IEEE1667 0x08 // IEEE 1667 +#define USBMS_SUBCLS_VENDOR 0xff // Vendor-specific + +// Mass storage protocol codes +#define USBMS_PROTO_CBI_CCI 0x00 // Control/Bulk/Interrupt with + // Command Completion Interrupt +#define USBMS_PROTO_CBI 0x01 // CBI without CCI +#define USBMS_PROTO_BBB 0x50 // Bulk-only +#define USBMS_PROTO_UAS 0x62 // UAS +#define USBMS_PROTO_VENDOR 0xff // Vendor-specific + +// Mass storage request codes +#define USBMS_REQ_ADSC 0x00 // Accept Device-Specific Command (CBI) +#define USBMS_REQ_GETRQ 0xfc // Get Requests (LSD FS) +#define USBMS_REQ_PUTRQ 0xfd // Put Requests (LSD FS) +#define USBMS_REQ_GML 0xfe // Get Max LUN (Bulk-only) +#define USBMS_REQ_BOMSR 0xff // Bulk-Only Mass Storage Reset (Bulk-only) + + +struct cdi_device *init_usb_device(struct cdi_bus_data *bus_data); +int usb_storage_request(struct cdi_scsi_device *device, + struct cdi_scsi_packet *packet); + +#endif -- 2.6.3 From max at tyndur.org Sun Dec 13 01:11:08 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:11:08 +0100 Subject: [tyndur-devel] [PATCH v2 23/24] usb: Makefile.all In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-24-git-send-email-max@tyndur.org> + Füge Makefile.all hinzu, damit ehci, usb und usb-storage auch gebaut werden. Signed-off-by: Max Reitz --- src/modules/cdi/ehci/Makefile.all | 6 ++++++ src/modules/cdi/usb-storage/Makefile.all | 6 ++++++ src/modules/cdi/usb/Makefile.all | 6 ++++++ 3 files changed, 18 insertions(+) create mode 100644 src/modules/cdi/ehci/Makefile.all create mode 100644 src/modules/cdi/usb-storage/Makefile.all create mode 100644 src/modules/cdi/usb/Makefile.all diff --git a/src/modules/cdi/ehci/Makefile.all b/src/modules/cdi/ehci/Makefile.all new file mode 100644 index 0000000..4642017 --- /dev/null +++ b/src/modules/cdi/ehci/Makefile.all @@ -0,0 +1,6 @@ +shopt -s extglob +source $LOST_BUILDMK_ROOT/config.sh + +echo "LD $1/modules/ehci" +$LOST_TOOLS_LD $LDSCRIPT -oehci.mod *.o --start-group $2 --end-group +$LOST_TOOLS_STRIP -s ehci.mod -o $1/modules/ehci diff --git a/src/modules/cdi/usb-storage/Makefile.all b/src/modules/cdi/usb-storage/Makefile.all new file mode 100644 index 0000000..727f01d --- /dev/null +++ b/src/modules/cdi/usb-storage/Makefile.all @@ -0,0 +1,6 @@ +shopt -s extglob +source $LOST_BUILDMK_ROOT/config.sh + +echo "LD $1/modules/usb-storage" +$LOST_TOOLS_LD $LDSCRIPT -ousb-storage.mod *.o --start-group $2 --end-group +$LOST_TOOLS_STRIP -s usb-storage.mod -o $1/modules/usb-storage diff --git a/src/modules/cdi/usb/Makefile.all b/src/modules/cdi/usb/Makefile.all new file mode 100644 index 0000000..37a12aa --- /dev/null +++ b/src/modules/cdi/usb/Makefile.all @@ -0,0 +1,6 @@ +shopt -s extglob +source $LOST_BUILDMK_ROOT/config.sh + +echo "LD $1/modules/usb" +$LOST_TOOLS_LD $LDSCRIPT -ousb.mod *.o --start-group $2 --end-group +$LOST_TOOLS_STRIP -s usb.mod -o $1/modules/usb -- 2.6.3 From max at tyndur.org Sun Dec 13 01:11:09 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 01:11:09 +0100 Subject: [tyndur-devel] [PATCH v2 24/24] Images: USB-Stick-Image In-Reply-To: <1449965469-16714-1-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> Message-ID: <1449965469-16714-25-git-send-email-max@tyndur.org> + Die Makefileziele image-usb und test-qemu-usb erlauben es, ein Image für einen USB-Stick zu bauen. Signed-off-by: Max Reitz --- Makefile | 8 +- build/config/grub_usb.cfg | 11 +++ build/config/grub_usb_kernel2.cfg | 23 +++++ build/config/image_usb.sh | 3 + build/scripts/image_usb_grub1 | 122 ++++++++++++++++++++++++ build/scripts/image_usb_grub2 | 193 ++++++++++++++++++++++++++++++++++++++ buildmk.sh | 2 +- 7 files changed, 360 insertions(+), 2 deletions(-) create mode 100644 build/config/grub_usb.cfg create mode 100644 build/config/grub_usb_kernel2.cfg create mode 100644 build/config/image_usb.sh create mode 100755 build/scripts/image_usb_grub1 create mode 100755 build/scripts/image_usb_grub2 diff --git a/Makefile b/Makefile index 96a6b24..9a6e8a4 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ clean: clean_objs for file in *; do if [ -f "$$file/Makefile" -a \( ! -f "$$file/.nobuild" -o -f "$$file/.ignorenobuild" \) ]; then $(MAKE) -sC "$$file" clean; rm "$$file/Makefile"; fi done rm -f Makefile.local -.SILENT: all makefiles subdirs obj lbuilds-env enable-pascal disable-pascal clean softclean clean_objs clean_root updateroot image-floppy image-hd test-qemu test-qemu-hd +.SILENT: all makefiles subdirs obj lbuilds-env enable-pascal disable-pascal clean softclean clean_objs clean_root updateroot image-floppy image-hd image-usb test-qemu test-qemu-hd test-qemu-usb .PHONY: lbuilds-env # Code für lbuilds @@ -73,6 +73,9 @@ image-hd: updateroot image-cdrom: updateroot build/scripts/image_cdrom_$(BOOTLOADER) +image-usb: updateroot + build/scripts/image_usb_$(BOOTLOADER) + test-qemu-floppy: image-floppy $(QEMU) -serial stdio -fda build/images/floppy.img -boot a -net user -net nic,model=rtl8139 | tee qemu-serial.log @@ -82,6 +85,9 @@ test-qemu-hd: image-hd test-qemu-cdrom: image-cdrom $(QEMU) -serial stdio -cdrom build/images/cdrom.img -boot d -net user -net nic,model=rtl8139 | tee qemu-serial.log +test-qemu-usb: image-usb + $(QEMU) -serial stdio -device usb-ehci -drive if=none,id=usb-thumb,file=build/images/usb.img,format=raw -device usb-storage,drive=usb-thumb -net user -net nic,model=rtl8139 | tee qemu-serial.log + test-bochs: image-floppy bochs -f bochs.config diff --git a/build/config/grub_usb.cfg b/build/config/grub_usb.cfg new file mode 100644 index 0000000..bbd2d8e --- /dev/null +++ b/build/config/grub_usb.cfg @@ -0,0 +1,11 @@ +title tyndur (kernel1) +kernel /boot/tyndur debug=s +module /modules/init boot=file:/ +module /modules/pci +module /modules/usb-storage +module /modules/usb +module /modules/ehci +module /modules/ext2 +module /modules/console servmgr:/term servmgr:/term +module /modules/servmgr usb-storage:/msd0_p0|ext2:/ vterm:/vterm8/out ehci usb-storage usb ext2 console pci +boot diff --git a/build/config/grub_usb_kernel2.cfg b/build/config/grub_usb_kernel2.cfg new file mode 100644 index 0000000..73cbf6a --- /dev/null +++ b/build/config/grub_usb_kernel2.cfg @@ -0,0 +1,23 @@ +title tyndur (Deutsch) +kernel /boot/tyndur2 debug=s +module /modules/init boot=file:/ +module /modules/pci +module /modules/usb-storage +module /modules/usb +module /modules/ehci +module /modules/ext2 +module /modules/console servmgr:/term servmgr:/term +module /modules/servmgr usb-storage:/msd0_p0|ext2:/ vterm:/vterm8/out ehci ext2 console pci +boot + +title tyndur (English) +kernel /boot/tyndur2 debug=s +module /modules/init boot=file:/ env.LANG=en +module /modules/pci +module /modules/usb-storage +module /modules/usb +module /modules/ehci +module /modules/ext2 +module /modules/console servmgr:/term servmgr:/term +module /modules/servmgr usb-storage:/msd0_p0|ext2:/ vterm:/vterm8/out ehci ext2 console pci +boot diff --git a/build/config/image_usb.sh b/build/config/image_usb.sh new file mode 100644 index 0000000..73b07ef --- /dev/null +++ b/build/config/image_usb.sh @@ -0,0 +1,3 @@ +IMAGE_PATH="build/images/usb.img" +# Imagegroesse in Megabytes +IMAGE_SIZE=64 diff --git a/build/scripts/image_usb_grub1 b/build/scripts/image_usb_grub1 new file mode 100755 index 0000000..e04fbd1 --- /dev/null +++ b/build/scripts/image_usb_grub1 @@ -0,0 +1,122 @@ +#!/bin/bash + +# Copyright (c) 2008 The tyndur Project. All rights reserved. +# +# This code is derived from software contributed to the tyndur Project +# by Antoine Kaufmann. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. All advertising materials mentioning features or use of this software +# must display the following acknowledgement: +# This product includes software developed by the tyndur Project +# and its contributors. +# 4. Neither the name of the tyndur Project nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +ROOT=build/root +ROOT_COMMON=build/root-common +ROOT_HD=build/root-hd +ROOT_LOCAL=build/root-local +MOUNT=build/mnt +SUDO=sudo + +export PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin + +export LOST_BUILDMK_ROOT="`pwd`" +source build/config/image_usb.sh +source config.sh + +die() +{ + echo $1 + exit -1 +} + +# Wenn das Image neu erstellt werden muss, kommen noch ein paar schritte mehr +# dazu. +NEW=0 +if ! [ -f $IMAGE_PATH ]; then + # Leeres Image erstellen und formatieren + dd of=$IMAGE_PATH bs=$((1024 * 1024)) seek=$IMAGE_SIZE count=0 + build/scripts/create_partition $IMAGE_PATH + NEW=1 +fi + + +# Start und Ende der Partition einlesen +PARTITIONS="`build/scripts/get_partition $IMAGE_PATH`" +PART_START=`echo $PARTITIONS | awk '{print $1}'` +PART_SIZE=`echo $PARTITIONS | awk '{print $2}'` + +if [ $NEW -ne 0 ]; then + LOOP_DEV=`$SUDO losetup -f $IMAGE_PATH --sizelimit $(($PART_SIZE * 512)) -o $(($PART_START * 512)) --show` + if [ $? -ne 0 ]; then + die "Loop Device konnte nicht angelegt werden" + fi + + # Dateisystem erstellen + $SUDO $LOST_TOOLS_MKE2FS -T ext2 -F -q $LOOP_DEV || die "Dateisystem konnte nicht erstellt werden" + + $SUDO losetup -d $LOOP_DEV +fi + +# Partition mounten +mkdir -p $MOUNT +$SUDO mount $IMAGE_PATH $MOUNT -t ext2 -o loop,offset=$((${PART_START} * 512)) +$SUDO chmod 777 -R $MOUNT + +# Daten Kopieren +cp -rP --preserve=links,timestamps $ROOT/* $ROOT_COMMON/* $ROOT_HD/* $MOUNT/ +[ -d $ROOT_LOCAL ] && cp -rP --preserve=links,timestamps $ROOT_LOCAL/* $MOUNT/ + +# GRUB-Konfiguration kopieren +mkdir -p $MOUNT/boot/grub +rm -f $MOUNT/boot/grub/menu.lst +if [ -e $MOUNT/boot/tyndur2 ]; then + cat build/config/grub_usb_kernel2.cfg >> $MOUNT/boot/grub/menu.lst +fi +if [ -e $MOUNT/boot/tyndur ]; then + cat build/config/grub_usb.cfg >> $MOUNT/boot/grub/menu.lst +fi + + +# Grub kopieren +if [ $NEW -ne 0 ]; then + cp $LOST_GRUB_STAGESDIR/stage? $MOUNT/boot/grub/ +fi + +# Partition unmounten und loopback-Device freigeben +while fuser -m $MOUNT; do true; done +$SUDO umount $MOUNT +rmdir $MOUNT + + +# Grub installieren +if [ $NEW -ne 0 ]; then + $LOST_TOOLS_GRUB --no-floppy --batch > /dev/null < /dev/null 2>&1); then + GRUB2_BIN_PREFIX=grub +elif (which grub2-mkimage > /dev/null 2>&1); then + GRUB2_BIN_PREFIX=grub2 +else + die "Konnte grub-mkimage nicht finden" +fi +GRUB2_MKIMAGE=$GRUB2_BIN_PREFIX-mkimage + +if (which $GRUB2_BIN_PREFIX-setup > /dev/null 2>&1); then + GRUB2_SETUP=$GRUB2_BIN_PREFIX-setup +elif (which $GRUB2_BIN_PREFIX-bios-setup > /dev/null 2>&1); then + GRUB2_SETUP=$GRUB2_BIN_PREFIX-bios-setup +else + die "Konnte GRUB-setup-Tool nicht finden" +fi + + +convert_menu_lst() +{ + sed -ie 's/^title \(.*\)$/menuentry "\1" {/' "$1" + sed -ie 's/^kernel \([^ ]\+\)\(.*\)$/ multiboot \1 \1 \2/' "$1" + sed -ie 's/^module \([^ ]\+\)\(.*\)$/ module \1 \1 \2/' "$1" + sed -ie 's/^boot$/}/' "$1" + sed -ie 's/|/\\|/' "$1" +} + +# Wenn das Image neu erstellt werden muss, kommen noch ein paar schritte mehr +# dazu. +NEW=0 +if ! [ -f $IMAGE_PATH ]; then + # Leeres Image erstellen und formatieren + dd of=$IMAGE_PATH bs=$((1024 * 1024)) seek=$IMAGE_SIZE count=0 + build/scripts/create_partition $IMAGE_PATH + NEW=1 +fi + + +# Start und Ende der Partition einlesen +PARTITIONS="`build/scripts/get_partition $IMAGE_PATH`" +PART_START=`echo $PARTITIONS | awk '{print $1}'` +PART_SIZE=`echo $PARTITIONS | awk '{print $2}'` + +if [ $NEW -ne 0 ]; then + LOOP_DEV=`$SUDO losetup -f $IMAGE_PATH --sizelimit $(($PART_SIZE * 512)) -o $(($PART_START * 512)) --show` + if [ $? -ne 0 ]; then + die "Loop Device konnte nicht angelegt werden" + fi + + # Dateisystem erstellen + $SUDO $LOST_TOOLS_MKE2FS -T ext2 -F -q $LOOP_DEV || die "Dateisystem konnte nicht erstellt werden" + + $SUDO losetup -d $LOOP_DEV +fi + +LOOP_DEV=`$SUDO losetup -f $IMAGE_PATH --offset=$((${PART_START} * 512)) --show` +if [ $? -ne 0 ]; then + die "Loop Device konnte nicht angelegt werden" +fi + +# Partition mounten +mkdir -p $MOUNT +$SUDO mount $LOOP_DEV $MOUNT -t ext2 +$SUDO chmod 777 -R $MOUNT + +# Daten Kopieren +cp -rP --preserve=links,timestamps $ROOT/* $ROOT_COMMON/* $ROOT_HD/* $MOUNT/ +[ -d $ROOT_LOCAL ] && cp -rP --preserve=links,timestamps $ROOT_LOCAL/* $MOUNT/ + +# GRUB-Konfiguration kopieren +mkdir -p $MOUNT/boot/grub +rm -f $MOUNT/boot/grub/grub.cfg +echo "insmod multiboot" >> $MOUNT/boot/grub/grub.cfg +if [ -e $MOUNT/boot/tyndur2 ]; then + cat build/config/grub_usb_kernel2.cfg >> $MOUNT/boot/grub/grub.cfg +fi +if [ -e $MOUNT/boot/tyndur ]; then + cat build/config/grub_usb.cfg >> $MOUNT/boot/grub/grub.cfg +fi + +convert_menu_lst $MOUNT/boot/grub/grub.cfg + + +# Erstmal überlegen, welche Module wir dieses Mal brauchen könnten. +# Die Abhänngigkeiten ändern sich leider zu häufig als dass man sich auf +# eine feste Liste verlassen könnte. +dependencies="" + +function list_dependencies() +{ + local dep + local i + + for i in $1; do + if (echo $dependencies | grep " $i " > /dev/null); then + continue; + fi + dependencies="$dependencies $i " + + dep=$(grep "$i:" $LOST_GRUB2_MODULESDIR/moddep.lst | \ + sed -e 's/^.*: *//') + list_dependencies "$dep" + done +} + +list_dependencies "normal multiboot gzio" + +# Grub kopieren +if [ $NEW -ne 0 ]; then + ln -fs . $MOUNT/boot/grub/i386-pc + echo "$dependencies" | sed -e 's/ /\n/g' | xargs -isrc cp $LOST_GRUB2_MODULESDIR/src.mod $MOUNT/boot/grub +fi + +# Grub installieren +if [ $NEW -ne 0 ]; then + DISK_LOOP_DEV=`$SUDO losetup -f $IMAGE_PATH --show` + if [ $? -ne 0 ]; then + die "Loop Device konnte nicht angelegt werden" + fi + + $SUDO chmod 666 $LOOP_DEV + $SUDO chmod 666 $DISK_LOOP_DEV + + mkdir -p build/grub2 + echo "(hd0) $DISK_LOOP_DEV" > build/grub2/device.map + + cp $LOST_GRUB2_MODULESDIR/boot.img $MOUNT/boot/grub + $GRUB2_MKIMAGE --directory=$LOST_GRUB2_MODULESDIR --prefix="(hd0,1)/boot/grub/" -O i386-pc -o $MOUNT/boot/grub/core.img ext2 biosdisk part_msdos minicmd + $GRUB2_SETUP -m build/grub2/device.map -d $MOUNT/boot/grub "(hd0)" + + $SUDO losetup -d $DISK_LOOP_DEV +fi + +# Partition unmounten und loopback-Device freigeben +while fuser -m $MOUNT; do true; done +$SUDO umount $MOUNT +rmdir $MOUNT + +$SUDO losetup -d $LOOP_DEV diff --git a/buildmk.sh b/buildmk.sh index abbd359..c900ef4 100644 --- a/buildmk.sh +++ b/buildmk.sh @@ -264,7 +264,7 @@ clean: clean_objs for file in *; do if [ -f "\$\$file/Makefile" -a \( ! -f "\$\$file/.nobuild" -o -f "\$\$file/.ignorenobuild" \) ]; then \$(MAKE) -sC "\$\$file" clean; rm "\$\$file/Makefile"; fi done rm -f Makefile.local -.SILENT: all makefiles subdirs obj lbuilds-env enable-pascal disable-pascal clean softclean clean_objs clean_root updateroot image-floppy image-hd test-qemu test-qemu-hd +.SILENT: all makefiles subdirs obj lbuilds-env enable-pascal disable-pascal clean softclean clean_objs clean_root updateroot image-floppy image-hd image-usb test-qemu test-qemu-hd EOF # Auf alle Unterprozesse warten -- 2.6.3 From max at tyndur.org Sun Dec 13 17:26:18 2015 From: max at tyndur.org (Max Reitz) Date: Sun, 13 Dec 2015 17:26:18 +0100 Subject: [tyndur-devel] [PATCH] config.sh: -fno-pic Message-ID: <1450023978-7315-1-git-send-email-max@tyndur.org> * Manche Distributionen halten die Zeit wohl für gekommen, -fpic zum Default zu erheben. Für týndur ist das eine eher doofe Idee, deshalb sollten wir es mit -fno-pic abschalten. Signed-off-by: Max Reitz --- config.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config.sh b/config.sh index 259df3b..d6f1286 100644 --- a/config.sh +++ b/config.sh @@ -35,8 +35,8 @@ if [ "$LOST_ARCH" == "amd64" ]; then LOST_TOOLS_AR=$COMPILER_PREFIX"ar" LOST_TOOLS_AS=$COMPILER_PREFIX"as -64" LOST_TOOLS_LD=$COMPILER_PREFIX"ld -m elf_x86_64" - LOST_TOOLS_GCC=$COMPILER_PREFIX"gcc -m64 -g -c -fno-stack-protector -nostdinc -fno-leading-underscore -fno-omit-frame-pointer -Wall -fno-strict-aliasing -O0 -std=gnu11 -I . -iquote ." - LOST_TOOLS_GPP=$COMPILER_PREFIX"g++ -m64 -g -c -fno-stack-protector -fno-leading-underscore -fno-omit-frame-pointer -Wall -fno-strict-aliasing -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-use-cxa-atexit -O0" + LOST_TOOLS_GCC=$COMPILER_PREFIX"gcc -m64 -g -c -fno-stack-protector -nostdinc -fno-leading-underscore -fno-omit-frame-pointer -Wall -fno-strict-aliasing -fno-pic -O0 -std=gnu11 -I . -iquote ." + LOST_TOOLS_GPP=$COMPILER_PREFIX"g++ -m64 -g -c -fno-stack-protector -fno-leading-underscore -fno-omit-frame-pointer -Wall -fno-strict-aliasing -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-use-cxa-atexit -fno-pic -O0" LOST_TOOLS_OBJCOPY=$COMPILER_PREFIX"objcopy -B i386:x86-64" LOST_TOOLS_STRIP=$COMPILER_PREFIX"strip -F elf64-x86-64" else @@ -45,8 +45,8 @@ else LOST_TOOLS_AR=$COMPILER_PREFIX"ar" LOST_TOOLS_AS=$COMPILER_PREFIX"as -32" LOST_TOOLS_LD=$COMPILER_PREFIX"ld -m elf_i386" - LOST_TOOLS_GCC=$COMPILER_PREFIX"gcc -m32 -g -c -fno-stack-protector -nostdinc -fno-leading-underscore -fno-omit-frame-pointer -Wall -Werror -Wstrict-prototypes -fno-strict-aliasing -std=gnu11 -O2 -fno-builtin -I ." - LOST_TOOLS_GPP=$COMPILER_PREFIX"g++ -m32 -g -c -fno-stack-protector -fno-leading-underscore -fno-omit-frame-pointer -Wall -fno-strict-aliasing -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-use-cxa-atexit -O0" + LOST_TOOLS_GCC=$COMPILER_PREFIX"gcc -m32 -g -c -fno-stack-protector -nostdinc -fno-leading-underscore -fno-omit-frame-pointer -Wall -Werror -Wstrict-prototypes -fno-strict-aliasing -fno-pic -std=gnu11 -O2 -fno-builtin -I ." + LOST_TOOLS_GPP=$COMPILER_PREFIX"g++ -m32 -g -c -fno-stack-protector -fno-leading-underscore -fno-omit-frame-pointer -Wall -fno-strict-aliasing -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-use-cxa-atexit -fno-pic -O0" LOST_TOOLS_OBJCOPY=$COMPILER_PREFIX"objcopy -B i386:i386" LOST_TOOLS_STRIP=$COMPILER_PREFIX"strip -F elf32-i386" fi -- 2.6.3 From kevin at tyndur.org Sun Dec 13 19:03:47 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 13 Dec 2015 19:03:47 +0100 Subject: [tyndur-devel] =?iso-8859-1?q?=5BPATCH_1/2=5D_ftp=3A_fix_behandlu?= =?iso-8859-1?q?ng_von_Servermeldungen_mit_=DCberlangen_Zeilen?= In-Reply-To: <20120409161127.GD8432@silvanus> References: <1301929939-32132-1-git-send-email-m.nemo@gmx.net> <20120409154248.GC8432@silvanus> <20120409161127.GD8432@silvanus> Message-ID: <20151213180347.GA27033@silvanus> On Mon, Apr 09, 2012 at 06:11:27PM +0200, Kevin Wolf wrote: > On Mon, Apr 09, 2012 at 05:42:48PM +0200, Kevin Wolf wrote: > > On Mon, Apr 04, 2011 at 05:12:18PM +0200, Andreas Freimuth wrote: > > > ! ftp.c/response: kann jetzt auch mit Zeilen länger als LINE_LENGTH > > > umgehen > > > > > > Signed-off-by: Andreas Freimuth > > > --- > > > src/modules/c/ftp/ftp.c | 64 +++++++++++++++++++++------------------------- > > > 1 files changed, 29 insertions(+), 35 deletions(-) > > > > Noch ein Uraltpatch, der bisher nicht committet worden ist... Auf den > > ersten Blick sieht er vernünftig aus, allerdings ist hier noch irgendwas > > kaputt. Wenn ich mich testweise auf ftp.debian.org einlogge und ein ls > > mache, gibt es rot. > > Das Problem ist, dass der Patch nicht berücksichtigt, dass fgets() unter > tyndur nicht blockiert, bis es eine komplette Zeile gelesen hat, sondern > nur das zurückliefert, was schon angekommen ist. Entweder tut es das jetzt nicht mehr oder ich habe das Problem nur zufällig nicht mehr reproduzieren können. Jedenfalls habe ich die Patchserie jetzt einfach mal reingenommen. Hat ja auch lang genug gewartet. Kevin From kevin at tyndur.org Sun Dec 20 16:47:43 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 20 Dec 2015 16:47:43 +0100 Subject: [tyndur-devel] [PATCH v2 01/24] cdi/pci: Ausrichtung von BARs an Pageframes In-Reply-To: <1449965469-16714-2-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> <1449965469-16714-2-git-send-email-max@tyndur.org> Message-ID: <20151220154743.GA2886@silvanus> On Sun, Dec 13, 2015 at 01:10:46AM +0100, Max Reitz wrote: > ! BARs müssen nicht immer an 4k (oder was auch immer die Pageframegröße > ist) ausgerichtet sein. Wenn sie das nicht sind, sollte die > CDI-Bibliothek entsprechend handeln. > > Signed-off-by: Max Reitz > --- > src/modules/cdi/lib/pci.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/src/modules/cdi/lib/pci.c b/src/modules/cdi/lib/pci.c > index 1df92dd..0dd07f8 100644 > --- a/src/modules/cdi/lib/pci.c > +++ b/src/modules/cdi/lib/pci.c > @@ -16,6 +16,7 @@ > #include > > #include > +#include > #include > #include > #include > @@ -191,7 +192,10 @@ void cdi_pci_alloc_memory(struct cdi_pci_device* device) > > for (i = 0; (res = cdi_list_get(device->resources, i)); i++) { > if (res->type == CDI_PCI_MEMORY) { > - res->address = mem_allocate_physical(res->length, res->start, 0); > + uintptr_t offset = res->start % PAGE_SIZE; > + void* mapping = mem_allocate_physical(res->length + offset, > + res->start - offset, 0); > + res->address = (void*) ((uintptr_t)mapping + offset); > } > } > } Wie im IRC gesagt ist das eher theoretisch, weil kernel2 bis heute kein SYSCALL_MEM_FREE_PHYSICAL implementiert, aber wenn man den kernel1-Code als Definition des Syscalls heranzieht, muss der Parameter pagealignt sein. Insofern, wenn du die Serie eh nochmal schickst, kannst du bei der Gelegenheit auch den Syscall in cdi_pci_free_memory() anpassen. Kevin From kevin at tyndur.org Sun Dec 20 17:29:21 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 20 Dec 2015 17:29:21 +0100 Subject: [tyndur-devel] [PATCH v2 11/24] ata: Temporary buffer for ATAPI commands In-Reply-To: <1449965469-16714-12-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> <1449965469-16714-12-git-send-email-max@tyndur.org> Message-ID: <20151220162921.GB2886@silvanus> On Sun, Dec 13, 2015 at 01:10:56AM +0100, Max Reitz wrote: > ! ATAPI commands need to be of size 12. However, general SCSI packets > may have any length and thus the SCSI command to be sent has to be > copied to a temporary buffer before it can be passed to the ATAPI > device. > > Signed-off-by: Max Reitz > Acked-by: Kevin Wolf > --- > src/modules/cdi/ata/atapi.c | 11 +++++++++-- > 1 file changed, 9 insertions(+), 2 deletions(-) > > diff --git a/src/modules/cdi/ata/atapi.c b/src/modules/cdi/ata/atapi.c > index 31c7263..78f7f29 100644 > --- a/src/modules/cdi/ata/atapi.c > +++ b/src/modules/cdi/ata/atapi.c > @@ -28,6 +28,7 @@ > > #include > #include > +#include > > #include "cdi.h" > #include "cdi/storage.h" > @@ -92,6 +93,12 @@ void atapi_remove_device(struct cdi_device* device) > > int atapi_request(struct cdi_scsi_device* scsi,struct cdi_scsi_packet* packet) > { > + uint8_t atapi_request[12]; > + if (packet->cmdsize > 12) { > + return -1; > + } > + memcpy(atapi_request, packet->command, sizeof(atapi_request)); Das ist nicht die neueste Version des Patchs, die du ins CDI-Repo gepusht hast (und die alte hatte mein Acked-by nicht, obwohl es oben steht). Kevin From kevin at tyndur.org Sun Dec 20 18:54:12 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 20 Dec 2015 18:54:12 +0100 Subject: [tyndur-devel] [PATCH v2 13/24] cdi/scsi: Neu geschrieben In-Reply-To: <1449965469-16714-14-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> <1449965469-16714-14-git-send-email-max@tyndur.org> Message-ID: <20151220175410.GC2886@silvanus> On Sun, Dec 13, 2015 at 01:10:58AM +0100, Max Reitz wrote: > * Die týndur-Implementierung von cdi/scsi war im Prinzip nur zur > Benutzung mit dem CDI-ATAPI-Treiber geeignet. Dieser Patch macht die > Implementierung allgemeiner und auch für andere (zukünftige) > SCSI-CDI-Treiber einsatzfähig. > > Signed-off-by: Max Reitz > --- > src/modules/cdi/lib/cdi.c | 26 ++- > src/modules/cdi/lib/scsi/disk.c | 506 ++++++++++++++++++++++++++++++---------- > 2 files changed, 404 insertions(+), 128 deletions(-) > > diff --git a/src/modules/cdi/lib/cdi.c b/src/modules/cdi/lib/cdi.c > index 5d519b4..11b3009 100644 > --- a/src/modules/cdi/lib/cdi.c > +++ b/src/modules/cdi/lib/cdi.c > @@ -19,13 +19,17 @@ > #include "cdi.h" > #include "cdi/audio.h" > #include "cdi/fs.h" > +#include "cdi/misc.h" > #include "cdi/pci.h" > +#include "cdi/scsi.h" > #include "cdi/storage.h" > > extern void cdi_storage_driver_register(struct cdi_storage_driver* driver); > extern void cdi_audio_driver_register(struct cdi_audio_driver* driver); > extern void cdi_tyndur_net_device_init(struct cdi_device* device); > > +void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev); > + > static list_t* drivers = NULL; > > static void cdi_tyndur_run_drivers(void); > @@ -149,6 +153,7 @@ static void cdi_tyndur_init_pci_devices(void) > } > > if (device != NULL) { > + cdi_osdep_new_device(driver, device); > cdi_list_push(driver->devices, device); Hier wird das Gerät immer noch in die Liste des Treibers gesteckt... > printf("cdi: %x.%x.%x: Benutze Treiber %s\n", > pci->bus, pci->dev, pci->function, driver->name); > @@ -214,9 +219,8 @@ static void cdi_tyndur_run_drivers(void) > } > } > > - /* Netzwerk ist schon initialisiert und SCSI hat kein RPC-Interface, > - * sondern muss mit scsidisk gelinkt werden. */ > - if (driver->type != CDI_NETWORK && driver->type != CDI_SCSI) { > + /* Netzwerk ist schon initialisiert */ > + if (driver->type != CDI_NETWORK) { > init_service_register((char*) driver->name); > } Commit a4514ab beschreibt diesen Hunk mit "Diese Änderung beseitigt den nutzlosen atapi-Service." Mit dieser Serie ist der Service wieder da (mit leerem Verzeichnisbaum), und zwar interessanterweise gleich zweimal: [ INIT ] service_register 'ata' = 3 [ INIT ] service_register 'atapi' = 3 [ INIT ] service_register 'atapi' = 3 Dafür ist jetzt der nutzlose scsidisk-Service weg, auch was wert. ;-) > } > @@ -277,6 +281,22 @@ list_t* cdi_tyndur_get_drivers(void) > return drivers; > } > > +void cdi_osdep_new_device(struct cdi_driver* drv, struct cdi_device* dev) > +{ > + if (!dev) { > + return; > + } > + > + dev->driver = drv; > + cdi_list_push(drv->devices, dev); ...aber hier ist das schon einmal passiert. Ich vermute, cdi_tyndur_init_pci_devices() sollte das dann bleiben lassen. > + switch ((int)drv->type) { > + case CDI_SCSI: > + cdi_scsi_device_init(CDI_UPCAST(dev, struct cdi_scsi_device, dev)); > + break; > + } > +} > + > /** > * Wenn main nicht von einem Treiber ueberschrieben wird, ist hier der > * Einsprungspunkt. Die Standardfunktion ruft nur cdi_init() auf. Treiber, die > diff --git a/src/modules/cdi/lib/scsi/disk.c b/src/modules/cdi/lib/scsi/disk.c > index 49f408e..2f2b7bd 100644 > --- a/src/modules/cdi/lib/scsi/disk.c > +++ b/src/modules/cdi/lib/scsi/disk.c > @@ -1,6 +1,7 @@ > /* > * Copyright (C) 2008 Mathias Gottschlag > * Copyright (C) 2009 Kevin Wolf > + * Copyright (C) 2015 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 > @@ -26,14 +27,10 @@ > #include > > #include > +#include > #include > #include > > -#define big_endian_word(x) ((((x) & 0xFF) << 8) | (((x) & 0xFF00) >> 8)) > -#define big_endian_dword(x) \ > - ((big_endian_word((x) & 0xFFFF) << 16) | \ > - (big_endian_word((x) >> 16))) > - > static int cdi_scsi_disk_read(struct cdi_storage_device* device, > uint64_t start, uint64_t count, void* buffer); > > @@ -44,84 +41,272 @@ struct cdi_storage_driver cdi_scsi_disk_driver = { > .drv = { > .name = "scsidisk", > .type = CDI_STORAGE, > + .bus = CDI_SCSI, > }, > .read_blocks = cdi_scsi_disk_read, > .write_blocks = cdi_scsi_disk_write, > }; > > -// TODO: This is kind of incomplete... -.- > -// FIXME Das ist alles sehr CD-ROM-spezifisch hartkodiert... > > -union cdrom_command > +typedef struct scsi_storage_device { > + struct cdi_storage_device dev; > + struct cdi_scsi_device* backend; > + int lun; > +} scsi_storage_device_t; > + > + > +#define SCSI_REQUEST_SENSE 0x03 > +#define SCSI_INQUIRY 0x12 > +#define SCSI_START_STOP_UNIT 0x1b > +#define SCSI_READ_CAPACITY 0x25 > +#define SCSI_READ10 0x28 > +#define SCSI_WRITE10 0x2a > +#define SCSI_READ16 0x88 > +#define SCSI_WRITE16 0x8a > + > +struct scsi_cmd { > + uint8_t operation; > +} __attribute__((packed)); > + > +struct scsi_cmd_request_sense { > + struct scsi_cmd cmd; > + uint8_t rsvd1; > + uint16_t rsvd2; > + uint8_t alloc_len; > + uint8_t control; > +} __attribute__((packed)); > + > +struct scsi_cmd_inquiry { > + struct scsi_cmd cmd; > + uint8_t rsvd; > + uint8_t page; > + uint16_t alloc_len; > + uint8_t control; > +} __attribute__((packed)); > + > +struct scsi_cmd_start_stop_unit { > + struct scsi_cmd cmd; > + uint8_t immed; > + uint8_t rsvd; > + uint8_t power_condition_modifier; > + uint8_t flags_power_condition; > + uint8_t control; > +} __attribute__((packed)); > + > +struct scsi_cmd_read_capacity { > + struct scsi_cmd cmd; > + uint8_t rsvd1; > + uint32_t lba; > + uint32_t rsvd2; > +} __attribute__((packed)); > + > +struct scsi_cmd_read10 { > + struct scsi_cmd cmd; > + uint8_t rsvd1; > + uint32_t lba; > + uint8_t rsvd2; > + uint16_t length; > + uint8_t control; > +} __attribute__((packed)); > + > +struct scsi_cmd_write10 { > + struct scsi_cmd cmd; > + uint8_t rsvd1; > + uint32_t lba; > + uint8_t rsvd2; > + uint16_t length; > + uint8_t control; > +} __attribute__((packed)); > + > +struct scsi_cmd_read16 { > + struct scsi_cmd cmd; > + uint8_t flags; > + uint64_t lba; > + uint32_t length; > + uint8_t group; > + uint8_t control; > +} __attribute__((packed)); > + > +struct scsi_cmd_write16 { > + struct scsi_cmd cmd; > + uint8_t flags; > + uint64_t lba; > + uint32_t length; > + uint8_t group; > + uint8_t control; > +} __attribute__((packed)); > + > +struct scsi_sense { > + uint8_t resp_code; > + uint8_t segment; > + uint8_t flags; > + uint32_t information; > + uint8_t add_sense_length; > + uint32_t cmd_spec_info; > + uint8_t add_sense_code; > + uint8_t add_sense_code_qual; > + uint8_t field_replacable_unit_code; > + uint8_t sense_key_spec1; > + uint8_t sense_key_spec2; > + uint8_t sense_key_spec3; > +} __attribute__((packed)); Diese Struct sieht unbenutzt aus. > +struct scsi_inq_answer { > + uint8_t dev_type; > + uint8_t type_mod; > + uint8_t version; > + uint8_t resp_data_format; > + uint8_t add_length; > + uint8_t flags1, flags2, flags3; > + char vendor[8]; > + char product[16]; > + uint32_t revision; > + char serial[8]; > + uint8_t vendor_unique[12]; > + uint8_t clocking; > + uint8_t rsvd1; > + uint16_t version_desc[8]; > + uint8_t rsvd2[22]; > +} __attribute__((packed)); > + > + > +static int scsi_read10(scsi_storage_device_t* dev, uint32_t sector, > + uint16_t nb_sectors, char* buffer) > { > - struct > - { > - uint8_t opcode; > - uint8_t reserved0; > - uint32_t address; > - uint8_t reserved1; > - uint16_t length; > - uint8_t reserved2; > - } __attribute__ ((packed)) dfl; > - struct > - { > - uint8_t opcode; > - uint8_t reserved0; > - uint32_t address; > - uint32_t length; > - uint16_t reserved1; > - } __attribute__ ((packed)) ext; > -}; > - > -static int cdrom_read_partial( > - struct cdi_scsi_device *device, uint32_t sector, uint64_t nb_sectors, > - char *buffer) > -{ > - struct cdi_scsi_packet packet; > - struct cdi_scsi_driver* drv = (struct cdi_scsi_driver*) device->dev.driver; > - union cdrom_command* cmd = (union cdrom_command*) &packet.command; > + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, > + struct cdi_scsi_driver, drv); > + struct scsi_cmd_read10 r10 = { > + .cmd = { > + .operation = SCSI_READ10 > + }, > + .lba = cdi_cpu_to_be32(sector), > + .length = cdi_cpu_to_be16(nb_sectors) Mach doch gleich ein Komma auch hinter das letzte initialisierte Feld, dann muss der nächste Patch, der ein neues Feld setzt, keine Zeile anfassen, die ihn gar nicht interessiert. (Gilt für alle Initialisierungen im ganzen Patch.) > + }; > + struct cdi_scsi_packet pkt = { > + .lun = dev->lun, > + .buffer = buffer, > + .bufsize = (size_t)nb_sectors * dev->dev.block_size, > + .direction = CDI_SCSI_READ, > + .cmdsize = sizeof(r10) > + }; > + memcpy(pkt.command, &r10, sizeof(r10)); > + > + if (drv->request(dev->backend, &pkt)) { > + return -1; > + } > > - nb_sectors = (uint32_t) nb_sectors; > + return 0; > +} > > - memset(&packet, 0, sizeof(packet)); > - packet.direction = CDI_SCSI_READ; > - packet.buffer = buffer; > - packet.bufsize = 2048 * nb_sectors; > - packet.cmdsize = 12; > +static int scsi_write10(scsi_storage_device_t* dev, uint32_t sector, > + uint16_t nb_sectors, char* buffer) > +{ > + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, > + struct cdi_scsi_driver, drv); > + struct scsi_cmd_write10 w10 = { > + .cmd = { > + .operation = SCSI_WRITE10 > + }, > + .lba = cdi_cpu_to_be32(sector), > + .length = cdi_cpu_to_be16(nb_sectors) > + }; > + struct cdi_scsi_packet pkt = { > + .lun = dev->lun, > + .buffer = buffer, > + .bufsize = (size_t)nb_sectors * dev->dev.block_size, > + .direction = CDI_SCSI_WRITE, > + .cmdsize = sizeof(w10) > + }; > + memcpy(pkt.command, &w10, sizeof(w10)); > + > + if (drv->request(dev->backend, &pkt)) { > + return -1; > + } > > - cmd->ext.opcode = 0xA8; > - cmd->ext.address = big_endian_dword(sector); > - cmd->ext.length = big_endian_dword(nb_sectors); > + return 0; > +} > > - if (drv->request(device, &packet)) { > +static int scsi_read16(scsi_storage_device_t* dev, uint64_t sector, > + uint32_t nb_sectors, char* buffer) > +{ > + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, > + struct cdi_scsi_driver, drv); > + struct scsi_cmd_read16 r16 = { > + .cmd = { > + .operation = SCSI_READ16 > + }, > + .lba = cdi_cpu_to_be64(sector), > + .length = cdi_cpu_to_be32(nb_sectors) > + }; > + struct cdi_scsi_packet pkt = { > + .lun = dev->lun, > + .buffer = buffer, > + .bufsize = (size_t)nb_sectors * dev->dev.block_size, > + .direction = CDI_SCSI_READ, > + .cmdsize = sizeof(r16) > + }; > + memcpy(pkt.command, &r16, sizeof(r16)); > + > + if (drv->request(dev->backend, &pkt)) { > return -1; > } > > - return nb_sectors; > + return 0; > } > > -static int cdrom_capacity(struct cdi_scsi_device *device, > - uint32_t* num_sectors, uint32_t* sector_size) > +static int scsi_write16(scsi_storage_device_t* dev, uint64_t sector, > + uint32_t nb_sectors, char* buffer) > { > - uint32_t buffer[32]; > - struct cdi_scsi_packet packet; > - struct cdi_scsi_driver* drv = (struct cdi_scsi_driver*) device->dev.driver; > - union cdrom_command* cmd = (union cdrom_command*) &packet.command; > - > - memset(&packet, 0, sizeof(packet)); > - packet.direction = CDI_SCSI_READ; > - packet.buffer = buffer; > - packet.bufsize = sizeof(buffer); > - packet.cmdsize = 12; > + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, > + struct cdi_scsi_driver, drv); > + struct scsi_cmd_write16 w16 = { > + .cmd = { > + .operation = SCSI_WRITE16 > + }, > + .lba = cdi_cpu_to_be64(sector), > + .length = cdi_cpu_to_be32(nb_sectors) > + }; > + struct cdi_scsi_packet pkt = { > + .lun = dev->lun, > + .buffer = buffer, > + .bufsize = (size_t)nb_sectors * dev->dev.block_size, > + .direction = CDI_SCSI_WRITE, > + .cmdsize = sizeof(w16) > + }; > + memcpy(pkt.command, &w16, sizeof(w16)); > + > + if (drv->request(dev->backend, &pkt)) { > + return -1; > + } > > - cmd->ext.opcode = 0x25; > + return 0; > +} > > - if (drv->request(device, &packet)) { > +static int scsi_get_capacity(scsi_storage_device_t* dev) > +{ > + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, > + struct cdi_scsi_driver, drv); > + uint32_t capacity[2] = { 0 }; > + struct scsi_cmd_read_capacity read_cap = { > + .cmd = { > + .operation = SCSI_READ_CAPACITY > + } > + }; > + struct cdi_scsi_packet pkt = { > + .lun = dev->lun, > + .buffer = capacity, > + .bufsize = sizeof(capacity), > + .direction = CDI_SCSI_READ, > + .cmdsize = sizeof(read_cap) > + }; > + memcpy(pkt.command, &read_cap, sizeof(read_cap)); > + > + if (drv->request(dev->backend, &pkt)) { > return -1; > } > > - *num_sectors = big_endian_dword(buffer[0]); > - *sector_size = big_endian_dword(buffer[1]); > + dev->dev.block_count = (uint64_t)cdi_be32_to_cpu(capacity[0]) + 1; > + dev->dev.block_size = cdi_be32_to_cpu(capacity[1]); > > return 0; > } > @@ -160,27 +345,28 @@ static void cdrom_sense(FsSCSIDevice *device, uint32_t index) > } > #endif > > -int cdrom_init(struct cdi_scsi_device* device) > +static int scsi_init(scsi_storage_device_t* dev) > { > - // Send inquiry command > - struct cdi_scsi_driver* drv = (struct cdi_scsi_driver*) device->dev.driver; > - struct cdi_scsi_packet packet; > - uint32_t status; > - unsigned char inqdata[96]; > - > - memset(&packet, 0, sizeof(packet)); > - memset(inqdata, 0xF0, sizeof(inqdata)); > - > - packet.direction = CDI_SCSI_READ; > - packet.buffer = inqdata; > - packet.bufsize = 96; > - packet.cmdsize = 12; > - > - ((union cdrom_command*)packet.command)->dfl.opcode = 0x12; > - packet.command[4] = 96; > - status = drv->request(device, &packet); > - if (status) > - { > + struct cdi_scsi_driver* drv = CDI_UPCAST(dev->backend->dev.driver, > + struct cdi_scsi_driver, drv); > + struct scsi_inq_answer inqdata; > + > + struct scsi_cmd_inquiry inq = { > + .cmd = { > + .operation = SCSI_INQUIRY, > + }, > + .alloc_len = cdi_cpu_to_be16(sizeof(inqdata)) > + }; > + struct cdi_scsi_packet inq_pkt = { > + .lun = dev->lun, > + .buffer = &inqdata, > + .bufsize = sizeof(inqdata), > + .direction = CDI_SCSI_READ, > + .cmdsize = sizeof(inq) > + }; > + memcpy(inq_pkt.command, &inq, sizeof(inq)); > + > + if (drv->request(dev->backend, &inq_pkt)) { > return -1; > } > > @@ -189,56 +375,109 @@ int cdrom_init(struct cdi_scsi_device* device) > return KE_ERROR_UNKNOWN; > #endif > > - // Start drive > - packet.direction = CDI_SCSI_NODATA; > - ((union cdrom_command*)packet.command)->dfl.opcode = 0x1B; > - packet.buffer = 0; > - packet.bufsize = 0; > - packet.command[4] = 0; > - packet.command[4] = 3; > - packet.cmdsize = 12; > - > - status = drv->request(device, &packet); > - if (status) > - { > - printf("cdrom %d: Start failed, %x\n", index, status); > - return -1; > + struct scsi_cmd_start_stop_unit ssu = { > + .cmd = { > + .operation = SCSI_START_STOP_UNIT, > + }, > + .flags_power_condition = 0x3 // LOEJ+START > + }; > + struct cdi_scsi_packet ssu_pkt = { > + .lun = dev->lun, > + .direction = CDI_SCSI_NODATA, > + .cmdsize = sizeof(ssu) > + }; > + memcpy(ssu_pkt.command, &ssu, sizeof(ssu)); > + > + if ((inqdata.dev_type & 0x1f) == 0x05) { // CD/DVD > + if (drv->request(dev->backend, &ssu_pkt)) { > + printf("cdrom: Start failed\n"); > + return -1; > + } > + dev->dev.block_size = 2048; > + } else { > + dev->dev.block_size = 512; > } > > return 0; > } > > -static int cdi_scsi_disk_read(struct cdi_storage_device* device, > - uint64_t start, uint64_t count, void* buffer) > +static int cdi_scsi_disk_read(struct cdi_storage_device* dev, uint64_t start, > + uint64_t count, void* buffer) > { > - struct cdi_scsi_device* dev = device->dev.backdev; > + scsi_storage_device_t* scsi_dev = CDI_UPCAST(dev, scsi_storage_device_t, > + dev); > + bool read16 = (start >> 32) || ((start + count) >> 32); > > while (count > 0) { > int ret; > + uint32_t it_count; > + > + if (read16) { > + it_count = count > 0xffffffffu ? 0xffffffffu : count; > + } else { > + it_count = count > 0xffff ? 0xffff : count; > + } > > - ret = cdrom_read_partial(dev, start, count, buffer); > - if (ret <= 0) { > + if (read16) { > + ret = scsi_read16(scsi_dev, start, it_count, buffer); > + } else { > + ret = scsi_read10(scsi_dev, start, it_count, buffer); > + } Die zwei if-Blöcke könnte man eigentlich auch in einen zusammenpacken. > + if (ret < 0) { > return -1; > } > - start += ret; > - buffer += ret; > - count -= ret; > + > + start += it_count; > + count -= it_count; > + > + buffer = (void*) ((uintptr_t)buffer + it_count * dev->block_size); > } > > return 0; > } > > -static int cdi_scsi_disk_write(struct cdi_storage_device* device, > - uint64_t start, uint64_t count, void* buffer) > +static int cdi_scsi_disk_write(struct cdi_storage_device* dev, uint64_t start, > + uint64_t count, void* buffer) > { > - return -1; > + scsi_storage_device_t* scsi_dev = CDI_UPCAST(dev, scsi_storage_device_t, > + dev); > + bool write16 = (start >> 32) || ((start + count) >> 32); > + > + while (count > 0) { > + int ret; > + uint32_t it_count; > + > + if (write16) { > + it_count = count > 0xffffffffu ? 0xffffffffu : count; > + } else { > + it_count = count > 0xffff ? 0xffff : count; > + } > + > + if (write16) { > + ret = scsi_write16(scsi_dev, start, it_count, buffer); > + } else { > + ret = scsi_write10(scsi_dev, start, it_count, buffer); > + } > + if (ret < 0) { > + return -1; > + } > + > + start += it_count; > + count -= it_count; > + > + buffer = (void*) ((uintptr_t)buffer + it_count * dev->block_size); > + } > + > + return 0; > } Und bis auf scsi_read10/16 durch scsi_write10/16 zu ersetzen scheint das das gleiche zu sein wie oben. Mit je einem Funktionspointer für die beiden Varianten müsste man das doch in eine einzige Instanz eindampfen können. > -static void driver_init(void) > +static void driver_init(struct cdi_driver* drv) > { > static int initialised = 0; > > if (!initialised) { > + cdi_scsi_disk_driver.drv.name = strdup(drv->name); > + > cdi_driver_register(&cdi_scsi_disk_driver.drv); > cdi_storage_driver_init(&cdi_scsi_disk_driver); > } Aha, hier kommt der zweite atapi-Service her. Das ist also einfach nur der umbenannte scsidisk. :-) > @@ -246,30 +485,47 @@ static void driver_init(void) > > int cdi_scsi_disk_init(struct cdi_scsi_device* device) > { > - struct cdi_storage_device* frontdev; > - uint32_t num_sectors = 0, sector_size = 0; > + int luns = device->lun_count ?: 1; > > - driver_init(); > + driver_init(device->dev.driver); > > - if (cdrom_init(device)) { > - return -1; > - } > + for (int lun = 0; lun < luns; lun++) { > + scsi_storage_device_t* dev = calloc(1, sizeof(*dev)); > + bool inquiry_failed; > + > + dev->backend = device; > + dev->lun = lun; > + > + inquiry_failed = scsi_init(dev) < 0; > > - frontdev = calloc(1, sizeof(*frontdev)); > - frontdev->dev.driver = (struct cdi_driver*) &cdi_scsi_disk_driver; > - frontdev->dev.name = device->dev.name; > + if (scsi_get_capacity(dev) < 0) { > + if (inquiry_failed) { > + /* We're fine as long as either INQUIRY or READ_CAPACITY work - > + * but if both fail, there's not a lot we can do. */ > + fprintf(stderr, > + "[scsi] Failed to initialize LUN %i of device %s\n", > + lun, device->dev.name); > > - // Groesse des Mediums bestimmen. Falls keins eingelegt ist, brauchen wir > - // trotzdem zumindest eine Sektorgroesse. > - cdrom_capacity(device, &num_sectors, §or_size); > - frontdev->block_size = sector_size ? sector_size : 2048; > - frontdev->block_count = num_sectors; > + free(dev); > + continue; > + } > > - frontdev->dev.backdev = device; > - device->frontdev = frontdev; > + /* Apparently this is common for CD-ROM devices; at least the old > + * SCSI implementation didn't care about it. A default block_size > + * has been set by scsi_init(). */ > + dev->dev.block_count = 0xffffffffu; Ich weiß nicht, ob es an diesem Default liegt, aber ata:/atapi10 hat bei mir mit leerem Laufwerk jetzt eine Dateigröße von -2048. Das finde ich eher nicht so geschickt. > + } > + > + dev->dev.dev.driver = (struct cdi_driver*) &cdi_scsi_disk_driver; > + if (luns > 1) { > + asprintf((char**) &dev->dev.dev.name, "%s_l%i", > + device->dev.name, lun); > + } else { > + dev->dev.dev.name = strdup(device->dev.name); > + } > > - // LostIO-Verzeichnisknoten anlegen > - cdi_storage_device_init(frontdev); > + cdi_storage_device_init(&dev->dev); > + } > > return 0; > } Kevin From kevin at tyndur.org Sun Dec 20 18:55:27 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 20 Dec 2015 18:55:27 +0100 Subject: [tyndur-devel] =?iso-8859-1?q?=5BPATCH_v2_06/24=5D_cdi/scsi=2C_cd?= =?iso-8859-1?q?i=2Eh=3A_Remove_t=FDndur-specific_fields?= In-Reply-To: <1449965469-16714-7-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> <1449965469-16714-7-git-send-email-max@tyndur.org> Message-ID: <20151220175526.GD2886@silvanus> On Sun, Dec 13, 2015 at 01:10:51AM +0100, Max Reitz wrote: > - Remove týndur-specific fields from non-OS-dependant structures > > Signed-off-by: Max Reitz > Acked-by: Kevin Wolf > Signed-off-by: Kevin Wolf Der hier darf erst nach dem SCSI-Rewrite kommen, sonst baut es nicht. Kevin From kevin at tyndur.org Sun Dec 20 20:18:57 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 20 Dec 2015 20:18:57 +0100 Subject: [tyndur-devel] =?iso-8859-1?q?=5BPATCH_v2_15/24=5D_servmgr=3A_Wor?= =?iso-8859-1?q?karound_f=FCr_zyklische_Abh=E4ngigkeiten?= In-Reply-To: <1449965469-16714-16-git-send-email-max@tyndur.org> References: <1449965469-16714-1-git-send-email-max@tyndur.org> <1449965469-16714-16-git-send-email-max@tyndur.org> Message-ID: <20151220191857.GE2886@silvanus> On Sun, Dec 13, 2015 at 01:11:00AM +0100, Max Reitz wrote: > ! Es kann passieren, dass Service A auf B wartet, der auf C wartet. Dann > melden sich diese beide beim servmgr, der dann für B darauf warten > soll, dass B gestartet ist, Ich glaube, du meinst "dass C gestartet ist" > und für A, dass B fertig ist. Da týndur > leider keine Popupthreads für IPC benutzt, existiert aber nur ein > wartender Thread. Wenn A später als B angefragt hat, wird also A ewig > auf B warten, da B nie weiterlaufen kann, auch wenn C fertig ist. Das entscheidende Problem ist, dass rpc_needserv() nicht asynchron programmiert ist, sondern eine Busywait-Schleife aufruft. Dafür muss dann natürlich die innere Schleife zuerst fertigwerden bevor die äußere wieder zum Zug kommt. Die innere Schleife ist diejenige des späteren Aufrufs, die Commitmessage hat das also meines Erachtens falschrum. > Ein einfacher Workaround ist es, zu einem Zeitpunkt immer nur einem > anfragenden Service/Prozess das Privileg zu gestatten, den servmgr > selbst zu benutzen, um zu warten. Alle anderen bekommen ein EAGAIN, > wenn der Zielservice noch nicht angemeldet ist, und müssen das Warten > selber übernehmen (hier als wiederholte Anfragen bei servmgr > implementiert). > > Signed-off-by: Max Reitz Der Fix ist kaputt weil Busywait, aber das ist die Implementierung von servmgr eh auch schon und von daher ist funktionierend kaputt vielleicht vorzuziehen und der Patch so gesehen in Ordnung. Der richtige Fix wäre es, den servmgr-RPC-Handler asynchron zu machen. Kevin From kevin at tyndur.org Wed Dec 23 20:23:26 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Wed, 23 Dec 2015 20:23:26 +0100 Subject: [tyndur-devel] [PATCH 0/2] kernel2: VM86-Instruktionsemulator-Fixes Message-ID: <1450898608-15059-1-git-send-email-kevin@tyndur.org> Damit geht bunt auf meinem Laptop wieder. Kevin Wolf (2): kernel2: vm86: ip nach Zwei-Byte-Instruktionen gefixt kernel2: vm86: in/out-Instruktionen mit Immediate-Port src/kernel2/src/arch/i386/vm86.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) -- 2.1.4 From kevin at tyndur.org Wed Dec 23 20:23:27 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Wed, 23 Dec 2015 20:23:27 +0100 Subject: [tyndur-devel] [PATCH 1/2] kernel2: vm86: ip nach Zwei-Byte-Instruktionen gefixt In-Reply-To: <1450898608-15059-1-git-send-email-kevin@tyndur.org> References: <1450898608-15059-1-git-send-email-kevin@tyndur.org> Message-ID: <1450898608-15059-2-git-send-email-kevin@tyndur.org> ! kernel2: Der Instruktionsemulator hat ip immer nur um ein Byte hochgezählt, auch wenn die Instruktion in Wirklichkeit zwei Bytes hat. Das ist natürlich falsch. Signed-off-by: Kevin Wolf --- src/kernel2/src/arch/i386/vm86.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kernel2/src/arch/i386/vm86.c b/src/kernel2/src/arch/i386/vm86.c index 5686667..566f2af 100644 --- a/src/kernel2/src/arch/i386/vm86.c +++ b/src/kernel2/src/arch/i386/vm86.c @@ -389,7 +389,7 @@ int vm86_exception(interrupt_stack_frame_t* isf) case 0x66ed: /* inl eax, dx */ isf->eax = inl(isf->edx); - isf->eip++; + isf->eip += 2; break; case 0xee: /* outb dx, al */ @@ -404,7 +404,7 @@ int vm86_exception(interrupt_stack_frame_t* isf) case 0x66ef: /* outl dx, eax */ outl(isf->edx, isf->eax); - isf->eip++; + isf->eip += 2; break; case 0xfa: /* cli */ -- 2.1.4 From kevin at tyndur.org Wed Dec 23 20:23:28 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Wed, 23 Dec 2015 20:23:28 +0100 Subject: [tyndur-devel] [PATCH 2/2] kernel2: vm86: in/out-Instruktionen mit Immediate-Port In-Reply-To: <1450898608-15059-1-git-send-email-kevin@tyndur.org> References: <1450898608-15059-1-git-send-email-kevin@tyndur.org> Message-ID: <1450898608-15059-3-git-send-email-kevin@tyndur.org> + kernel2: Der Instruktionsemulator unterstützt jetzt auch die in/out-Varianten, die die Portnummer als Immediatewert statt in dx bekommen. Signed-off-by: Kevin Wolf --- src/kernel2/src/arch/i386/vm86.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/kernel2/src/arch/i386/vm86.c b/src/kernel2/src/arch/i386/vm86.c index 566f2af..eb81476 100644 --- a/src/kernel2/src/arch/i386/vm86.c +++ b/src/kernel2/src/arch/i386/vm86.c @@ -375,6 +375,38 @@ int vm86_exception(interrupt_stack_frame_t* isf) isf->eip = emulator_pop(isf); break; + case 0xe4: /* in al, imm8 */ + isf->eax &= ~0xFF; + isf->eax |= inb(ops[1]); + isf->eip += 2; + break; + + case 0xe5: /* in ax, imm8 */ + isf->eax &= ~0xFFFF; + isf->eax |= inw(ops[1]); + isf->eip += 2; + break; + + case 0x66e5: /* in eax, imm8 */ + isf->eax = inl(ops[1]); + isf->eip += 3; + break; + + case 0xe6: /* out imm8, al */ + outb(ops[1], isf->eax); + isf->eip += 2; + break; + + case 0xe7: /* out imm8, ax */ + outw(ops[1], isf->eax); + isf->eip += 2; + break; + + case 0x66e7: /* outl imm8, eax */ + outl(ops[1], isf->eax); + isf->eip += 3; + break; + case 0xec: /* inb al, dx */ isf->eax &= ~0xFF; isf->eax |= inb(isf->edx); -- 2.1.4 From kevin at tyndur.org Sun Dec 27 22:50:02 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 27 Dec 2015 22:50:02 +0100 Subject: [tyndur-devel] [PATCH 2/2] kernel2: vm86: in/out-Instruktionen mit Immediate-Port In-Reply-To: <1450898608-15059-3-git-send-email-kevin@tyndur.org> References: <1450898608-15059-1-git-send-email-kevin@tyndur.org> <1450898608-15059-3-git-send-email-kevin@tyndur.org> Message-ID: <20151227215002.GA8445@silvanus> On Wed, Dec 23, 2015 at 08:23:28PM +0100, Kevin Wolf wrote: > + kernel2: Der Instruktionsemulator unterstützt jetzt auch die > in/out-Varianten, die die Portnummer als Immediatewert statt in dx > bekommen. > > Signed-off-by: Kevin Wolf > --- > src/kernel2/src/arch/i386/vm86.c | 32 ++++++++++++++++++++++++++++++++ > 1 file changed, 32 insertions(+) > > diff --git a/src/kernel2/src/arch/i386/vm86.c b/src/kernel2/src/arch/i386/vm86.c > index 566f2af..eb81476 100644 > --- a/src/kernel2/src/arch/i386/vm86.c > +++ b/src/kernel2/src/arch/i386/vm86.c > @@ -375,6 +375,38 @@ int vm86_exception(interrupt_stack_frame_t* isf) > isf->eip = emulator_pop(isf); > break; > > + case 0xe4: /* in al, imm8 */ > + isf->eax &= ~0xFF; > + isf->eax |= inb(ops[1]); > + isf->eip += 2; > + break; > + > + case 0xe5: /* in ax, imm8 */ > + isf->eax &= ~0xFFFF; > + isf->eax |= inw(ops[1]); > + isf->eip += 2; > + break; > + > + case 0x66e5: /* in eax, imm8 */ > + isf->eax = inl(ops[1]); Das muss selbstverständlich ops[2] sein, den Präfix muss man ja mitrechnen. > + isf->eip += 3; > + break; > + > + case 0xe6: /* out imm8, al */ > + outb(ops[1], isf->eax); > + isf->eip += 2; > + break; > + > + case 0xe7: /* out imm8, ax */ > + outw(ops[1], isf->eax); > + isf->eip += 2; > + break; > + > + case 0x66e7: /* outl imm8, eax */ > + outl(ops[1], isf->eax); Und hier auch. > + isf->eip += 3; > + break; > + > case 0xec: /* inb al, dx */ > isf->eax &= ~0xFF; > isf->eax |= inb(isf->edx); Kevin From kevin at tyndur.org Sun Dec 27 23:47:55 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 27 Dec 2015 23:47:55 +0100 Subject: [tyndur-devel] [PATCH v2 0/3] kernel2: VM86-Instruktionsemulator-Fixes Message-ID: <1451256478-12089-1-git-send-email-kevin@tyndur.org> Damit geht bunt auf meinem Laptop wieder. v2: - 'in eax, imm8' und 'outl imm8, eax' gefixt - Zusätzlicher Patch 3: Jetzt geht es sogar von bunt zurück zu unbunt auf dem Laptop Kevin Wolf (3): kernel2: vm86: ip nach Zwei-Byte-Instruktionen gefixt kernel2: vm86: in/out-Instruktionen mit Immediate-Port kernel2: vm86: Reihenfolge auf dem Stack für int/iret gefixt src/kernel2/src/arch/i386/vm86.c | 44 ++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) -- 2.1.4 From kevin at tyndur.org Sun Dec 27 23:47:56 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 27 Dec 2015 23:47:56 +0100 Subject: [tyndur-devel] [PATCH v2 1/3] kernel2: vm86: ip nach Zwei-Byte-Instruktionen gefixt In-Reply-To: <1451256478-12089-1-git-send-email-kevin@tyndur.org> References: <1451256478-12089-1-git-send-email-kevin@tyndur.org> Message-ID: <1451256478-12089-2-git-send-email-kevin@tyndur.org> ! kernel2: Der Instruktionsemulator hat ip immer nur um ein Byte hochgezählt, auch wenn die Instruktion in Wirklichkeit zwei Bytes hat. Das ist natürlich falsch. Signed-off-by: Kevin Wolf --- src/kernel2/src/arch/i386/vm86.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kernel2/src/arch/i386/vm86.c b/src/kernel2/src/arch/i386/vm86.c index 5686667..566f2af 100644 --- a/src/kernel2/src/arch/i386/vm86.c +++ b/src/kernel2/src/arch/i386/vm86.c @@ -389,7 +389,7 @@ int vm86_exception(interrupt_stack_frame_t* isf) case 0x66ed: /* inl eax, dx */ isf->eax = inl(isf->edx); - isf->eip++; + isf->eip += 2; break; case 0xee: /* outb dx, al */ @@ -404,7 +404,7 @@ int vm86_exception(interrupt_stack_frame_t* isf) case 0x66ef: /* outl dx, eax */ outl(isf->edx, isf->eax); - isf->eip++; + isf->eip += 2; break; case 0xfa: /* cli */ -- 2.1.4 From kevin at tyndur.org Sun Dec 27 23:47:57 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 27 Dec 2015 23:47:57 +0100 Subject: [tyndur-devel] [PATCH v2 2/3] kernel2: vm86: in/out-Instruktionen mit Immediate-Port In-Reply-To: <1451256478-12089-1-git-send-email-kevin@tyndur.org> References: <1451256478-12089-1-git-send-email-kevin@tyndur.org> Message-ID: <1451256478-12089-3-git-send-email-kevin@tyndur.org> + kernel2: Der Instruktionsemulator unterstützt jetzt auch die in/out-Varianten, die die Portnummer als Immediatewert statt in dx bekommen. Signed-off-by: Kevin Wolf --- src/kernel2/src/arch/i386/vm86.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/kernel2/src/arch/i386/vm86.c b/src/kernel2/src/arch/i386/vm86.c index 566f2af..53bb04e 100644 --- a/src/kernel2/src/arch/i386/vm86.c +++ b/src/kernel2/src/arch/i386/vm86.c @@ -375,6 +375,38 @@ int vm86_exception(interrupt_stack_frame_t* isf) isf->eip = emulator_pop(isf); break; + case 0xe4: /* in al, imm8 */ + isf->eax &= ~0xFF; + isf->eax |= inb(ops[1]); + isf->eip += 2; + break; + + case 0xe5: /* in ax, imm8 */ + isf->eax &= ~0xFFFF; + isf->eax |= inw(ops[1]); + isf->eip += 2; + break; + + case 0x66e5: /* in eax, imm8 */ + isf->eax = inl(ops[2]); + isf->eip += 3; + break; + + case 0xe6: /* out imm8, al */ + outb(ops[1], isf->eax); + isf->eip += 2; + break; + + case 0xe7: /* out imm8, ax */ + outw(ops[1], isf->eax); + isf->eip += 2; + break; + + case 0x66e7: /* outl imm8, eax */ + outl(ops[2], isf->eax); + isf->eip += 3; + break; + case 0xec: /* inb al, dx */ isf->eax &= ~0xFF; isf->eax |= inb(isf->edx); -- 2.1.4 From kevin at tyndur.org Sun Dec 27 23:47:58 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 27 Dec 2015 23:47:58 +0100 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_v2_3/3=5D_kernel2=3A_vm86=3A_Re?= =?utf-8?q?ihenfolge_auf_dem_Stack_f=C3=BCr_int/iret_gefixt?= In-Reply-To: <1451256478-12089-1-git-send-email-kevin@tyndur.org> References: <1451256478-12089-1-git-send-email-kevin@tyndur.org> Message-ID: <1451256478-12089-4-git-send-email-kevin@tyndur.org> ! kernel2: eflags, cs und eip wurden vom Emulator genau falschrum auf den Stack gepusht (und beim Rücksprung wieder gepopt). Was an sich kein Problem wäre, wenn das BIOS täte, was man naiverweise erwartet, nämlich mit INT reinzuspringen und mit IRET wieder raus. Es scheint aber auch Code da draußen zu geben, der es bevorzugt, stattdessen mit RETF 0x2 wieder zurückzuspringen und dann sollte der Stackinhalt besser so sein wie auf echter Hardware. Signed-off-by: Kevin Wolf --- src/kernel2/src/arch/i386/vm86.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/kernel2/src/arch/i386/vm86.c b/src/kernel2/src/arch/i386/vm86.c index 53bb04e..9ee62c9 100644 --- a/src/kernel2/src/arch/i386/vm86.c +++ b/src/kernel2/src/arch/i386/vm86.c @@ -352,9 +352,9 @@ int vm86_exception(interrupt_stack_frame_t* isf) uint16_t intr = ops[1] & 0xff; uint16_t* ivt_entry = bios_data.ivt[intr]; - emulator_push(isf, isf->eip + 2); - emulator_push(isf, isf->cs); emulator_push(isf, isf->eflags); + emulator_push(isf, isf->cs); + emulator_push(isf, isf->eip + 2); isf->eip = ivt_entry[0]; isf->cs = ivt_entry[1]; @@ -370,9 +370,9 @@ int vm86_exception(interrupt_stack_frame_t* isf) } // Ansonsten muss es ganz normal emuliert werden - emulator_pop(isf); - isf->cs = emulator_pop(isf); isf->eip = emulator_pop(isf); + isf->cs = emulator_pop(isf); + emulator_pop(isf); break; case 0xe4: /* in al, imm8 */ -- 2.1.4 From kevin at tyndur.org Sun Dec 27 23:54:41 2015 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 27 Dec 2015 23:54:41 +0100 Subject: [tyndur-devel] [PATCH] kernel2: Auf der seriellen Konsole CRLF benutzen Message-ID: <1451256881-12732-1-git-send-email-kevin@tyndur.org> ! kernel2: Mit CRLF statt nur LF werden die Ausgaben lesbarer. Mit 'qemu -serial stdout' ist das nicht nötig, aber sowohl mit qemu in der Standardeinstellung als auch mit screen und einer echten seriellen Konsole hilft das. Signed-off-by: Kevin Wolf --- src/kernel2/src/console.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/kernel2/src/console.c b/src/kernel2/src/console.c index 1a6aea2..50afd28 100644 --- a/src/kernel2/src/console.c +++ b/src/kernel2/src/console.c @@ -419,6 +419,10 @@ void con_putc(const char c) { static char last_char = 0; + if (c == '\n') { + outb(0x3f8, '\r'); + while ((inb(0x3fd) & 0x20) == 0) asm("nop"); + } outb(0xe9, c); outb(0x3f8, c); while ((inb(0x3fd) & 0x20) == 0) asm("nop"); -- 2.1.4