From d2d9dbe02102801e2f52380fb5bd11d8cbcf786c Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Sun, 20 Sep 2009 07:19:24 +0200 Subject: [PATCH] some cleanup, incl. free'ing of allocated stuff (without malloc fail) --- usb/core/core.c | 103 +++++++++++++++++++++--------------------------- usb/core/usb.c | 16 -------- usb/host/host.h | 2 +- usb/host/ohci.c | 34 ++++++---------- 4 files changed, 59 insertions(+), 96 deletions(-) diff --git a/usb/core/core.c b/usb/core/core.c index ff853ff..fe1ae9e 100644 --- a/usb/core/core.c +++ b/usb/core/core.c @@ -261,13 +261,10 @@ u16 usb_submit_irp(usb_irp *irp) char *td_buf_ptr = irp->buffer; char mybuf[64]; - //u8 togl=irp->dev->epTogl[(irp->endpoint & 0x7F)]; u8 togl = irp->dev->epTogl[(irp->endpoint & 0x7F)]; - //u8 togl=0; switch (irp->type) { case USB_CTRL: - /* alle requests mit dem gleichen algorithmus zerteilen * das einzige ist der spezielle get_Device_descriptor request * bei dem eine laenge von 64 angegeben ist. @@ -275,7 +272,7 @@ u16 usb_submit_irp(usb_irp *irp) * dann reichen die ersten 8 byte. */ - /***************** Setup Stage ***********************/ + /***************** Setup Stage ***********************/ td = usb_create_transfer_descriptor(irp); td->pid = USB_PID_SETUP; td->buffer = irp->buffer; @@ -285,44 +282,41 @@ u16 usb_submit_irp(usb_irp *irp) memcpy(mybuf, td->buffer, td->actlen); togl = 0; - td->togl = togl; /* start with data0 */ - if (togl == 0) - togl = 1; - else - togl = 0; - /**** send token ****/ + /* start with data0 */ + td->togl = togl; + togl = togl ? 0 : 1; + + /**** send token ****/ hcdi_enqueue(td); -#if 0 - break; - memcpy(td->buffer, mybuf, td->actlen); -#endif - /***************** Data Stage ***********************/ - /** - * You can see at bit 7 of bmRequestType if this stage is used, - * default requests are always 8 byte greate, from - * host to device. Stage 3 is only neccessary if the request - * expected datas from the device. - * bit7 - 1 = from device to host -> yes we need data stage - * bit7 - 0 = from host to device -> no send zero packet - * - * nach einem setup token kann nur ein IN token in stage 3 folgen - * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt. - * - * - * bit7 = 1 - * Device to Host - * - es kommen noch Daten mit PID_IN an - * - host beendet mit PID_OUT DATA1 Zero - * bit7 - 0 - * Host zu Device (wie set address) - * - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung - */ - usb_device_request *setup = (usb_device_request *) irp->buffer; + /***************** Data Stage ***********************/ + /** + * You can see at bit 7 of bmRequestType if this stage is used, + * default requests are always 8 byte greate, from + * host to device. Stage 3 is only neccessary if the request + * expected datas from the device. + * bit7 - 1 = from device to host -> yes we need data stage + * bit7 - 0 = from host to device -> no send zero packet + * + * nach einem setup token kann nur ein IN token in stage 3 folgen + * nie aber ein OUT. Ein Zero OUT wird nur als Bestaetigung benoetigt. + * + * + * bit7 = 1 + * Device to Host + * - es kommen noch Daten mit PID_IN an + * - host beendet mit PID_OUT DATA1 Zero + * bit7 - 0 + * Host zu Device (wie set address) + * - device sendet ein PID_IN DATA1 Zero Packet als bestaetigung + */ + memcpy(mybuf, irp->buffer, td->actlen); + usb_device_request *setup = (usb_device_request *) mybuf; u8 bmRequestType = setup->bmRequestType; + free(td); - if (bmRequestType & 0x80) { /* check bit 7 of bmRequestType */ - + /* check bit 7 of bmRequestType */ + if (bmRequestType & 0x80) { /* schleife die die tds generiert */ while (runloop) { td = usb_create_transfer_descriptor(irp); @@ -339,54 +333,50 @@ u16 usb_submit_irp(usb_irp *irp) td->pid = USB_PID_IN; td->togl = togl; - if (togl == 0) - togl = 1; - else - togl = 0; + togl = togl ? 0 : 1; /* wenn device descriptor von adresse 0 angefragt wird werden nur * die ersten 8 byte abgefragt */ if (setup->bRequest == GET_DESCRIPTOR && (setup->wValue & 0xff) == 1 && td->devaddress == 0) { - runloop = 0; /* stop loop */ + /* stop loop */ + runloop = 0; } - /**** send token ****/ - printf("togl: %d\n", togl); + /**** send token ****/ hcdi_enqueue(td); /* pruefe ob noch weitere Pakete vom Device abgeholt werden muessen */ restlength = restlength - irp->epsize; + free(td); } } - /***************** Status Stage ***********************/ + /***************** Status Stage ***********************/ /* Zero packet for end */ td = usb_create_transfer_descriptor(irp); td->togl = 1; /* zero data packet = always DATA1 packet */ td->actlen = 0; td->buffer = NULL; - /** - * bit7 = 1, host beendet mit PID_OUT DATA1 Zero - * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung - */ - if (bmRequestType & 0x80) { /* check bit 7 of bmRequestType */ + /** + * bit7 = 1, host beendet mit PID_OUT DATA1 Zero + * bit7 = 0, device sendet ein PID_IN DATA1 Zero Packet als bestaetigung + */ + /* check bit 7 of bmRequestType */ + if (bmRequestType & 0x80) { td->pid = USB_PID_OUT; } else { td->pid = USB_PID_IN; } - /**** send token ****/ - printf("togl: %d\n", togl); + /**** send token ****/ hcdi_enqueue(td); free(td); - - break; - case USB_BULK: + case USB_BULK: core.stdout("bulk\r\n"); //u8 runloop=1; //u16 restlength = irp->len; @@ -424,7 +414,6 @@ u16 usb_submit_irp(usb_irp *irp) else togl = 0; /**** send token ****/ - printf("togl: %d\n", togl); hcdi_enqueue(td); free(td); } diff --git a/usb/core/usb.c b/usb/core/usb.c index bfb37a6..0f85c63 100644 --- a/usb/core/usb.c +++ b/usb/core/usb.c @@ -135,7 +135,6 @@ u8 usb_reset(usb_device *dev) */ u8 usb_control_msg(usb_device *dev, u8 requesttype, u8 request, u16 value, u16 index, u16 length,char *buf, u16 size, u16 timeout) { - //usb_control_msg(dev, 0x80, GET_DESCRIPTOR, 1, 0, 8, buf, 8, 0); usb_irp *irp = (usb_irp*)malloc(sizeof(usb_irp)); irp->dev = dev; irp->endpoint = 0; @@ -143,29 +142,14 @@ u8 usb_control_msg(usb_device *dev, u8 requesttype, u8 request, u16 value, u16 i irp->epsize = dev->bMaxPacketSize0; irp->type = USB_CTRL; -#if 0 - buf[0]=(char)requesttype; - buf[1]=(char)request; - buf[2]=(char)(value >> 8); - buf[3]=(char)(value); - buf[4]=(char)(index >> 8); - buf[5]=(char)(index); - // lenght buf are the only where the order is inverted - buf[6]=(char)(length); - buf[7]=(char)(length >> 8); -#endif -#if 1 - //should be the right way around? :O buf[0]=(char)requesttype; buf[1]=(char)request; buf[2]=(char)(value); buf[3]=(char)(value >> 8); buf[4]=(char)(index); buf[5]=(char)(index >> 8); - // lenght buf are the only where the order is inverted buf[6]=(char)(length); buf[7]=(char)(length >> 8); -#endif irp->buffer = buf; irp->len = length; diff --git a/usb/host/host.h b/usb/host/host.h index 9aa6a77..878e551 100644 --- a/usb/host/host.h +++ b/usb/host/host.h @@ -50,7 +50,7 @@ void hcdi_init(); /** * Enqueue a transfer descriptor. */ -u8 hcdi_enqueue(usb_transfer_descriptor *td); +u8 hcdi_enqueue(const usb_transfer_descriptor *td); /** * Remove an transfer descriptor from transfer queue. */ diff --git a/usb/host/ohci.c b/usb/host/ohci.c index 71062f8..ba50b50 100644 --- a/usb/host/ohci.c +++ b/usb/host/ohci.c @@ -51,18 +51,8 @@ static struct general_td *allocate_general_td(size_t bsize) struct general_td *td; td = (struct general_td *)memalign(16, sizeof(struct general_td)); td->flags = ACCESS_LE(0); - // TODO !! nexttd? - //td->nexttd = ACCESS_LE(virt_to_phys(td)); td->nexttd = ACCESS_LE(0); - if(bsize == 0) { - td->cbp = td->be = ACCESS_LE(0); - } else { - //align it to 4kb? :O - //td->cbp = ACCESS_LE(virt_to_phys(memalign(4096, bsize))); //memailgn required here? - td->cbp = ACCESS_LE(virt_to_phys(malloc(bsize))); - memset(phys_to_virt(ACCESS_LE(td->cbp)), 0, bsize); - td->be = ACCESS_LE(ACCESS_LE(td->cbp) + bsize - 1); - } + td->cbp = td->be = ACCESS_LE(0); return td; } @@ -172,9 +162,10 @@ static void dbg_td_flag(u32 flag) printf("********************************************************\n"); } -static void general_td_fill(struct general_td *dest, usb_transfer_descriptor *src) +static void general_td_fill(struct general_td *dest, const usb_transfer_descriptor *src) { - (void) memcpy((void*) (phys_to_virt(ACCESS_LE(dest->cbp))), src->buffer, src->actlen); + dest->cbp = ACCESS_LE(virt_to_phys(src->buffer)); + dest->be = ACCESS_LE(ACCESS_LE(dest->cbp) + src->actlen - 1); dest->flags &= ACCESS_LE(~OHCI_TD_DIRECTION_PID_MASK); switch(src->pid) { case USB_PID_SETUP: @@ -230,7 +221,7 @@ static void dump_address(void *addr, u32 size, const char* str) * Enqueue a transfer descriptor. */ u8 first = 0; -u8 hcdi_enqueue(usb_transfer_descriptor *td) { +u8 hcdi_enqueue(const usb_transfer_descriptor *td) { static struct general_td *tSetup,*tData; static u32 tSetupbuffer, tDatabuffer, tStatusbuffer; static u32 tSetupblen, tDatablen, tStatusblen; @@ -304,11 +295,11 @@ u8 hcdi_enqueue(usb_transfer_descriptor *td) { } #endif - udelay(1000000); + udelay(100000); u32 current = read32(OHCI0_HC_CTRL_CURRENT_ED); printf("current: 0x%08X\n", current); printf("+++++++++++++++++++++++++++++\n"); - udelay(10000000); + udelay(100000); dump_address(tSetup, sizeof(struct general_td), "tSetup(after)"); dump_address((void*) phys_to_virt(ACCESS_LE(tSetup->cbp)), tSetupblen, "tSetup->cbp(after)"); @@ -330,16 +321,15 @@ u8 hcdi_enqueue(usb_transfer_descriptor *td) { /* disable control list */ write32(OHCI0_HC_CONTROL, read32(OHCI0_HC_CONTROL)&~OHCI_CTRL_CLE); - /* + /* TODO * TD should be free'd after taking it from the done queue. - * but we are very very dirty and do it anyway :p */ - /* only when a buffer is allocated */ -#if 0 - if(td->actlen) - free((void*)tStatusbuffer); +#if 1 + free(tSetup); + free(tData); free(tStatus); + free(dummyconfig); #endif printf("hcdi_enqueue, done!\n"); return 0; -- 2.25.1