revert between 56095 -> 55830 in arch
[AROS.git] / workbench / devs / USB / stack / misc.c
blob87ce842aa3e82a6eead337f15f686ff85fc02102
1 /*
2 Copyright (C) 2006 by Michal Schulz
3 $Id$
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "misc.h"
23 #include <proto/exec.h>
26 * TODO: fix up all the mess below. too many resource allocations, a bit too complex
29 void DumpDescriptor(usb_descriptor_t *desc)
31 bug("[USB] Descriptor dump:\n");
32 bug("[USB] bLength = %d\n", desc->bLength);
34 if (desc->bDescriptorType == UDESC_DEVICE)
36 bug("[USB] bDescriptorType = %d (Device)\n", desc->bDescriptorType);
37 usb_device_descriptor_t *d = (usb_device_descriptor_t *)desc;
38 bug("[USB] bcdUSB = 0x%04x\n", AROS_LE2WORD(d->bcdUSB));
39 bug("[USB] bDeviceClass = 0x%02x\n", d->bDeviceClass);
40 bug("[USB] bDeviceSubClass = 0x%02x\n", d->bDeviceSubClass);
41 bug("[USB] bDeviceProtocol = 0x%02x\n", d->bDeviceProtocol);
42 bug("[USB] bMaxPacketSize = 0x%04x\n", d->bMaxPacketSize);
43 bug("[USB] idVendor = 0x%04x\n", d->idVendor);
44 bug("[USB] idProduct = 0x%04x\n", AROS_LE2WORD(d->idProduct));
45 bug("[USB] bcdDevice = 0x%04x\n", AROS_LE2WORD(d->bcdDevice));
46 bug("[USB] iManufacturer = 0x%02x\n", d->iManufacturer);
47 bug("[USB] iProduct = 0x%02x\n", d->iProduct);
48 bug("[USB] iSerialNumber = 0x%02x\n", d->iSerialNumber);
49 bug("[USB] bNumConfigurations = %d\n", d->bNumConfigurations);
51 else if (desc->bDescriptorType == UDESC_CONFIG)
53 bug("[USB] bDescriptorType = %d (Config)\n", desc->bDescriptorType);
54 usb_config_descriptor_t *d = (usb_config_descriptor_t *)desc;
55 bug("[USB] wTotalLength = %d\n", AROS_LE2WORD(d->wTotalLength));
56 bug("[USB] bNumInterface = %d\n", d->bNumInterface);
57 bug("[USB] bConfigurationValue = %d\n", d->bConfigurationValue);
58 bug("[USB] iConfiguration = %d\n", d->iConfiguration);
59 bug("[USB] bmAttributes = 0x%02x ", d->bmAttributes);
60 if (d->bmAttributes & UC_SELF_POWERED)
61 bug("SELF_POWERED ");
62 if (d->bmAttributes & UC_BUS_POWERED)
63 bug("BUS_POWERED ");
64 if (d->bmAttributes & UC_REMOTE_WAKEUP)
65 bug("REMOTE_WAKEUP ");
66 bug("\n");
67 bug("[USB] bMaxPower = %d mA\n", d->bMaxPower * UC_POWER_FACTOR);
69 else if (desc->bDescriptorType == UDESC_HUB)
71 usb_hub_descriptor_t *d = (usb_hub_descriptor_t *)desc;
72 int i;
73 bug("[USB] bDescriptorType = %d (Hub)\n", desc->bDescriptorType);
74 bug("[USB] bNbrPorts = %d\n", d->bNbrPorts);
75 bug("[USB] wHubCharacteristics = 0x%04x\n", AROS_LE2WORD(d->wHubCharacteristics));
76 bug("[USB] bPwrOn2PwrGood = %d ms\n", d->bPwrOn2PwrGood * UHD_PWRON_FACTOR);
77 bug("[USB] bHubContrCurrent = %d\n", d->bHubContrCurrent);
78 bug("[USB] DeviceRemovable = ");
79 for (i=0; i < 32; i++)
80 bug("%02x", d->DeviceRemovable[i]);
81 bug("\n");
83 else if (desc->bDescriptorType == UDESC_ENDPOINT)
85 bug("[USB] bDescriptorType = %d (Endpoint)\n", desc->bDescriptorType);
86 usb_endpoint_descriptor_t *d = (usb_endpoint_descriptor_t *)desc;
87 bug("[USB] bEndpointAddress = %02x\n", d->bEndpointAddress);
88 bug("[USB] bmAttributes = %02x\n", d->bmAttributes);
89 bug("[USB] wMaxPacketSize = %d\n", AROS_LE2WORD(d->wMaxPacketSize));
90 bug("[USB] bInterval = %d\n", d->bInterval);
92 else
93 bug("[USB] bDescriptorType = %d\n", desc->bDescriptorType);
97 * usb_delay() stops waits for specified amount of miliseconds. It uses the timerequest
98 * of specified USB device. No pre-allocation of signals is required.
100 void USBDelay(struct timerequest *tr, uint32_t msec)
102 /* Allocate a signal within this task context */
103 tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit = AllocSignal(-1);
104 tr->tr_node.io_Message.mn_ReplyPort->mp_SigTask = FindTask(NULL);
106 /* Specify the request */
107 tr->tr_node.io_Command = TR_ADDREQUEST;
108 tr->tr_time.tv_secs = msec / 1000;
109 tr->tr_time.tv_micro = 1000 * (msec % 1000);
111 /* Wait */
112 DoIO((struct IORequest *)tr);
114 /* The signal is not needed anymore */
115 FreeSignal(tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit);
116 tr->tr_node.io_Message.mn_ReplyPort->mp_SigTask = NULL;
120 * usbtimer() sets a timer to wake the process up after timeout expires
121 * NOTE: it assumes your task and sigbit fields are filled in already
123 uint32_t USBTimer(struct timerequest *tr, uint32_t msec)
125 /* Allocate a signal within this task context */
126 tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit = AllocSignal(-1);
127 tr->tr_node.io_Message.mn_ReplyPort->mp_SigTask = FindTask(NULL);
129 /* Specify the request */
130 tr->tr_node.io_Command = TR_ADDREQUEST;
131 tr->tr_time.tv_secs = msec / 1000;
132 tr->tr_time.tv_micro = 1000 * (msec % 1000);
134 /* Wait */
135 SendIO((struct IORequest *)tr);
137 return (tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit);
141 * usbtimerdone() completes the above
143 void USBTimerDone(struct timerequest *tr)
145 WaitPort(tr->tr_node.io_Message.mn_ReplyPort);
146 /* since we created port and we issued only one request, it's only one message */
147 GetMsg(tr->tr_node.io_Message.mn_ReplyPort);
148 /* The signal is not needed anymore */
149 FreeSignal(tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit);
150 tr->tr_node.io_Message.mn_ReplyPort->mp_SigTask = NULL;
154 struct timerequest *USBCreateTimer()
156 struct timerequest *tr = NULL;
157 struct MsgPort *mp = NULL;
159 mp = CreateMsgPort();
160 if (mp)
162 tr = (struct timerequest *)CreateIORequest(mp, sizeof(struct timerequest));
163 if (tr)
165 FreeSignal(mp->mp_SigBit);
166 if (!OpenDevice((STRPTR)"timer.device", UNIT_MICROHZ, (struct IORequest *)tr, 0))
167 return tr;
169 DeleteIORequest((struct IORequest *)tr);
170 mp->mp_SigBit = AllocSignal(-1);
172 DeleteMsgPort(mp);
175 return NULL;
178 void USBDeleteTimer(struct timerequest *tr)
180 if (tr)
182 tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit = AllocSignal(-1);
183 CloseDevice((struct IORequest *)tr);
184 DeleteMsgPort(tr->tr_node.io_Message.mn_ReplyPort);
185 DeleteIORequest((struct IORequest *)tr);