summaryrefslogtreecommitdiff
path: root/nuttx/drivers/usbhost/usbhost_devaddr.c
blob: eb121c2ee944a2345d42f1e74dfd0fb2f3c4af35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*******************************************************************************
 * drivers/usbhost/usbhost_devaddr.c
 * Manage USB device addresses
 *
 *   Copyright (C) 2013 Gregory Nutt. All rights reserved.
 *   Authors: Gregory Nutt <gnutt@nuttx.org>
 *
 * 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. Neither the name NuttX 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 OWNER 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.
 *
 *******************************************************************************/

/*******************************************************************************
 * Included Files
 *******************************************************************************/

#include <nuttx/config.h>

#include <string.h>
#include <errno.h>
#include <debug.h>

#include <nuttx/kmalloc.h>
#include <nuttx/usb/usbhost_devaddr.h>

/*******************************************************************************
 * Pre-processor Definitions
 *******************************************************************************/

/*******************************************************************************
 * Private Functions
 *******************************************************************************/

/****************************************************************************
 * Name: usbhost_takesem and usbhost_givesem
 *
 * Description:
 *   This is just a wrapper to handle the annoying behavior of semaphore
 *   waits that return due to the receipt of a signal.
 *
 *******************************************************************************/

static void usbhost_takesem(FAR struct usbhost_devaddr_s *hcd)
{
  /* Take the semaphore (perhaps waiting) */

  while (sem_wait(&hcd->exclsem) != 0)
    {
      /* The only case that an error should occur here is if the wait was
       * awakened by a signal.
       */

      ASSERT(errno == EINTR);
    }
}

#define usbhost_givesem(hcd) sem_post(&hcd->exclsem)

/*******************************************************************************
 * Name: usbhost_devaddr_hash
 *
 * Description:
 *   Create a hash value from a device address.
 *
 *******************************************************************************/

static inline uint8_t usbhost_devaddr_hash(uint8_t devaddr)
{
  uint8_t ret = devaddr;

  ret ^= (devaddr >> 2);
  ret ^= (devaddr >> 3);
  return ret & USBHOST_DEVADDR_HASHMASK;
}

/*******************************************************************************
 * Name: usbhost_devaddr_allocate
 *
 * Description:
 *   Allocate a new unique device address for this HCD.
 *
 * Assumptions:
 *   Caller hold the exclsem
 *
 *******************************************************************************/

static int usbhost_devaddr_allocate(FAR struct usbhost_devaddr_s *hcd)
{
  uint8_t startaddr = hcd->next;
  uint8_t devaddr;
  int index;
  int bitno;

  /* Loop until we find a valid device address */

  for (;;)
    {
      /* Try the next device address */

      devaddr = hcd->next;
      if (hcd->next >= 0x7f)
        {
          hcd->next = 1;
        }
      else
        {
          hcd->next++;
        }

      /* Is this address already allocated? */

      index = devaddr >> 5;
      bitno = devaddr & 0x1f;
      if ((hcd->alloctab[index] & (1 << bitno)) == 0)
        {
          /* No... allocate it now */

          hcd->alloctab[index] |= (1 << bitno);
          return (int)devaddr;
        }

      /* This address has already been allocated.  The followign logic will
       * prevent (unexpected) infinite loops.
       */

      if (startaddr == devaddr)
        {
          /* We are back where we started... the are no free device address */

          return -ENOMEM;
        }
    }
}

/*******************************************************************************
 * Public Functions
 *******************************************************************************/

/*******************************************************************************
 * Name: usbhost_devaddr_initialize
 *
 * Description:
 *   Initialize the caller provided struct usbhost_devaddr_s instance in
 *   preparation for the management of device addresses on behalf of an HCD.
 *
 *******************************************************************************/

void usbhost_devaddr_initialize(FAR struct usbhost_devaddr_s *hcd)
{
  DEBUGASSERT(hcd);

  memset(hcd, 0, sizeof(struct usbhost_devaddr_s));
  sem_init(&hcd->exclsem, 0, 1);
  hcd->next = 1;
}

/*******************************************************************************
 * Name: usbhost_devaddr_create
 *
 * Description:
 *   Create a new unique device address for this HCD.  Bind the void* arg to the
 *   the device address and return the newly allocated device address.
 *
 *******************************************************************************/

int usbhost_devaddr_create(FAR struct usbhost_devaddr_s *hcd,
                           FAR void *associate)
{
  FAR struct usbhost_devhash_s *hentry;
  uint8_t hvalue;
  int devaddr;

  /* Allocate a hash table entry */

  hentry = (FAR struct usbhost_devhash_s *)kmm_malloc(sizeof(struct usbhost_devhash_s));
  if (!hentry)
    {
      udbg("ERROR: Failed to allocate a hash table entry\n");
      return -ENOMEM;
    }

  /* Get exclusive access to the HCD device address data */

  usbhost_takesem(hcd);

  /* Allocate a device address */

  devaddr = usbhost_devaddr_allocate(hcd);
  if (devaddr < 0)
    {
      udbg("ERROR: Failed to allocate a device address\n");
      free(hentry);
    }
  else
    {
      /* Initialize the hash table entry */

      hentry->devaddr = devaddr;
      hentry->payload = associate;

      /* Add the new device address to the hash table */

      hvalue = usbhost_devaddr_hash(devaddr);
      hentry->flink = hcd->hashtab[hvalue];
      hcd->hashtab[hvalue] = hentry;

      /* Try to re-use the lowest numbered device addresses */

      if (hcd->next > devaddr)
        {
          hcd->next = devaddr;
        }
    }

  usbhost_givesem(hcd);
  return devaddr;
}

/*******************************************************************************
 * Name: usbhost_devaddr_find
 *
 * Description:
 *   Given a device address, find the void* value that was bound to the device
 *   address by usbhost_devaddr_create() when the device address was allocated.
 *
 *******************************************************************************/

FAR void *usbhost_devaddr_find(FAR struct usbhost_devaddr_s *hcd,
                               uint8_t devaddr)
{
  FAR struct usbhost_devhash_s *hentry;
  uint8_t hvalue;

  /* Get exclusive access to the HCD device address data */

  hvalue = usbhost_devaddr_hash(devaddr);
  usbhost_takesem(hcd);

  /* Check each entry in the hash table */

  for (hentry = hcd->hashtab[hvalue]; hentry; hentry = hentry->flink)
    {
      /* Is this the address we are looking for? */

      if (hentry->devaddr == devaddr)
        {
          /* Yes.. return the payload from the hash table entry */

          usbhost_givesem(hcd);
          return hentry->payload;
        }
    }

  /* Didn't find the device address */

  usbhost_givesem(hcd);
  return NULL;
}

/*******************************************************************************
 * Name: usbhost_devaddr_destroy
 *
 * Description:
 *   Release a device address previously allocated by usbhost_devaddr_destroy()
 *   and destroy the association with the void* data.
 *
 *******************************************************************************/

void usbhost_devaddr_destroy(FAR struct usbhost_devaddr_s *hcd, uint8_t devaddr)
{
  FAR struct usbhost_devhash_s *hentry;
  FAR struct usbhost_devhash_s *prev;
  uint8_t hvalue;

  /* Get exclusive access to the HCD device address data */

  hvalue = usbhost_devaddr_hash(devaddr);
  usbhost_takesem(hcd);

  /* Search the hast table for the matching entry */

  for (hentry = hcd->hashtab[hvalue], prev = NULL;
       hentry;
       prev = hentry, hentry = hentry->flink)
    {
      /* Is this the address we are looking for? */

      if (hentry->devaddr == devaddr)
        {
          /* Yes.. remove the entry from the hash list */

          if (prev)
            {
              prev->flink = hentry->flink;
            }
          else
            {
              hcd->hashtab[hvalue] = hentry->flink;
            }

          /* And release the entry */

          kmm_free(hentry);
          break;
        }
    }

  usbhost_givesem(hcd);
}