Deleted Added
full compact
usb_parse.c (192984) usb_parse.c (194228)
1/* $FreeBSD: head/sys/dev/usb/usb_parse.c 192984 2009-05-28 17:36:36Z thompsa $ */
1/* $FreeBSD: head/sys/dev/usb/usb_parse.c 194228 2009-06-15 01:02:43Z thompsa $ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.

--- 16 unchanged lines hidden (view full) ---

26
27#include <dev/usb/usb.h>
28#include <dev/usb/usb_mfunc.h>
29
30#include <dev/usb/usb_core.h>
31#include <dev/usb/usb_parse.h>
32
33/*------------------------------------------------------------------------*
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.

--- 16 unchanged lines hidden (view full) ---

26
27#include <dev/usb/usb.h>
28#include <dev/usb/usb_mfunc.h>
29
30#include <dev/usb/usb_core.h>
31#include <dev/usb/usb_parse.h>
32
33/*------------------------------------------------------------------------*
34 * usb2_desc_foreach
34 * usb_desc_foreach
35 *
36 * This function is the safe way to iterate across the USB config
37 * descriptor. It contains several checks against invalid
38 * descriptors. If the "desc" argument passed to this function is
39 * "NULL" the first descriptor, if any, will be returned.
40 *
41 * Return values:
42 * NULL: End of descriptors
43 * Else: Next descriptor after "desc"
44 *------------------------------------------------------------------------*/
45struct usb_descriptor *
35 *
36 * This function is the safe way to iterate across the USB config
37 * descriptor. It contains several checks against invalid
38 * descriptors. If the "desc" argument passed to this function is
39 * "NULL" the first descriptor, if any, will be returned.
40 *
41 * Return values:
42 * NULL: End of descriptors
43 * Else: Next descriptor after "desc"
44 *------------------------------------------------------------------------*/
45struct usb_descriptor *
46usb2_desc_foreach(struct usb_config_descriptor *cd,
46usb_desc_foreach(struct usb_config_descriptor *cd,
47 struct usb_descriptor *_desc)
48{
49 uint8_t *desc_next;
50 uint8_t *start;
51 uint8_t *end;
52 uint8_t *desc;
53
54 /* be NULL safe */

--- 24 unchanged lines hidden (view full) ---

79 if (desc[0] < 3)
80 return (NULL); /* too short descriptor */
81
82 /* Return start of next descriptor. */
83 return ((struct usb_descriptor *)desc);
84}
85
86/*------------------------------------------------------------------------*
47 struct usb_descriptor *_desc)
48{
49 uint8_t *desc_next;
50 uint8_t *start;
51 uint8_t *end;
52 uint8_t *desc;
53
54 /* be NULL safe */

--- 24 unchanged lines hidden (view full) ---

79 if (desc[0] < 3)
80 return (NULL); /* too short descriptor */
81
82 /* Return start of next descriptor. */
83 return ((struct usb_descriptor *)desc);
84}
85
86/*------------------------------------------------------------------------*
87 * usb2_idesc_foreach
87 * usb_idesc_foreach
88 *
89 * This function will iterate the interface descriptors in the config
90 * descriptor. The parse state structure should be zeroed before
91 * calling this function the first time.
92 *
93 * Return values:
94 * NULL: End of descriptors
95 * Else: A valid interface descriptor
96 *------------------------------------------------------------------------*/
97struct usb_interface_descriptor *
88 *
89 * This function will iterate the interface descriptors in the config
90 * descriptor. The parse state structure should be zeroed before
91 * calling this function the first time.
92 *
93 * Return values:
94 * NULL: End of descriptors
95 * Else: A valid interface descriptor
96 *------------------------------------------------------------------------*/
97struct usb_interface_descriptor *
98usb2_idesc_foreach(struct usb_config_descriptor *cd,
98usb_idesc_foreach(struct usb_config_descriptor *cd,
99 struct usb_idesc_parse_state *ps)
100{
101 struct usb_interface_descriptor *id;
102 uint8_t new_iface;
103
104 /* retrieve current descriptor */
105 id = (struct usb_interface_descriptor *)ps->desc;
106 /* default is to start a new interface */
107 new_iface = 1;
108
109 while (1) {
110 id = (struct usb_interface_descriptor *)
99 struct usb_idesc_parse_state *ps)
100{
101 struct usb_interface_descriptor *id;
102 uint8_t new_iface;
103
104 /* retrieve current descriptor */
105 id = (struct usb_interface_descriptor *)ps->desc;
106 /* default is to start a new interface */
107 new_iface = 1;
108
109 while (1) {
110 id = (struct usb_interface_descriptor *)
111 usb2_desc_foreach(cd, (struct usb_descriptor *)id);
111 usb_desc_foreach(cd, (struct usb_descriptor *)id);
112 if (id == NULL)
113 break;
114 if ((id->bDescriptorType == UDESC_INTERFACE) &&
115 (id->bLength >= sizeof(*id))) {
116 if (ps->iface_no_last == id->bInterfaceNumber)
117 new_iface = 0;
118 ps->iface_no_last = id->bInterfaceNumber;
119 break;

--- 12 unchanged lines hidden (view full) ---

132 }
133
134 /* store and return current descriptor */
135 ps->desc = (struct usb_descriptor *)id;
136 return (id);
137}
138
139/*------------------------------------------------------------------------*
112 if (id == NULL)
113 break;
114 if ((id->bDescriptorType == UDESC_INTERFACE) &&
115 (id->bLength >= sizeof(*id))) {
116 if (ps->iface_no_last == id->bInterfaceNumber)
117 new_iface = 0;
118 ps->iface_no_last = id->bInterfaceNumber;
119 break;

--- 12 unchanged lines hidden (view full) ---

132 }
133
134 /* store and return current descriptor */
135 ps->desc = (struct usb_descriptor *)id;
136 return (id);
137}
138
139/*------------------------------------------------------------------------*
140 * usb2_edesc_foreach
140 * usb_edesc_foreach
141 *
142 * This function will iterate all the endpoint descriptors within an
143 * interface descriptor. Starting value for the "ped" argument should
144 * be a valid interface descriptor.
145 *
146 * Return values:
147 * NULL: End of descriptors
148 * Else: A valid endpoint descriptor
149 *------------------------------------------------------------------------*/
150struct usb_endpoint_descriptor *
141 *
142 * This function will iterate all the endpoint descriptors within an
143 * interface descriptor. Starting value for the "ped" argument should
144 * be a valid interface descriptor.
145 *
146 * Return values:
147 * NULL: End of descriptors
148 * Else: A valid endpoint descriptor
149 *------------------------------------------------------------------------*/
150struct usb_endpoint_descriptor *
151usb2_edesc_foreach(struct usb_config_descriptor *cd,
151usb_edesc_foreach(struct usb_config_descriptor *cd,
152 struct usb_endpoint_descriptor *ped)
153{
154 struct usb_descriptor *desc;
155
156 desc = ((struct usb_descriptor *)ped);
157
152 struct usb_endpoint_descriptor *ped)
153{
154 struct usb_descriptor *desc;
155
156 desc = ((struct usb_descriptor *)ped);
157
158 while ((desc = usb2_desc_foreach(cd, desc))) {
158 while ((desc = usb_desc_foreach(cd, desc))) {
159 if (desc->bDescriptorType == UDESC_INTERFACE) {
160 break;
161 }
162 if (desc->bDescriptorType == UDESC_ENDPOINT) {
163 if (desc->bLength < sizeof(*ped)) {
164 /* endpoint index is invalid */
165 break;
166 }
167 return ((struct usb_endpoint_descriptor *)desc);
168 }
169 }
170 return (NULL);
171}
172
173/*------------------------------------------------------------------------*
159 if (desc->bDescriptorType == UDESC_INTERFACE) {
160 break;
161 }
162 if (desc->bDescriptorType == UDESC_ENDPOINT) {
163 if (desc->bLength < sizeof(*ped)) {
164 /* endpoint index is invalid */
165 break;
166 }
167 return ((struct usb_endpoint_descriptor *)desc);
168 }
169 }
170 return (NULL);
171}
172
173/*------------------------------------------------------------------------*
174 * usb2_get_no_descriptors
174 * usbd_get_no_descriptors
175 *
176 * This function will count the total number of descriptors in the
177 * configuration descriptor of type "type".
178 *------------------------------------------------------------------------*/
179uint8_t
175 *
176 * This function will count the total number of descriptors in the
177 * configuration descriptor of type "type".
178 *------------------------------------------------------------------------*/
179uint8_t
180usb2_get_no_descriptors(struct usb_config_descriptor *cd, uint8_t type)
180usbd_get_no_descriptors(struct usb_config_descriptor *cd, uint8_t type)
181{
182 struct usb_descriptor *desc = NULL;
183 uint8_t count = 0;
184
181{
182 struct usb_descriptor *desc = NULL;
183 uint8_t count = 0;
184
185 while ((desc = usb2_desc_foreach(cd, desc))) {
185 while ((desc = usb_desc_foreach(cd, desc))) {
186 if (desc->bDescriptorType == type) {
187 count++;
188 if (count == 0xFF)
189 break; /* crazy */
190 }
191 }
192 return (count);
193}
194
195/*------------------------------------------------------------------------*
186 if (desc->bDescriptorType == type) {
187 count++;
188 if (count == 0xFF)
189 break; /* crazy */
190 }
191 }
192 return (count);
193}
194
195/*------------------------------------------------------------------------*
196 * usb2_get_no_alts
196 * usbd_get_no_alts
197 *
198 * Return value:
199 * Number of alternate settings for the given interface descriptor pointer.
200 *------------------------------------------------------------------------*/
201uint8_t
197 *
198 * Return value:
199 * Number of alternate settings for the given interface descriptor pointer.
200 *------------------------------------------------------------------------*/
201uint8_t
202usb2_get_no_alts(struct usb_config_descriptor *cd,
202usbd_get_no_alts(struct usb_config_descriptor *cd,
203 struct usb_interface_descriptor *id)
204{
205 struct usb_descriptor *desc;
206 uint8_t n = 0;
207 uint8_t ifaceno;
208
209 ifaceno = id->bInterfaceNumber;
210
211 desc = (struct usb_descriptor *)id;
212
203 struct usb_interface_descriptor *id)
204{
205 struct usb_descriptor *desc;
206 uint8_t n = 0;
207 uint8_t ifaceno;
208
209 ifaceno = id->bInterfaceNumber;
210
211 desc = (struct usb_descriptor *)id;
212
213 while ((desc = usb2_desc_foreach(cd, desc))) {
213 while ((desc = usb_desc_foreach(cd, desc))) {
214 if ((desc->bDescriptorType == UDESC_INTERFACE) &&
215 (desc->bLength >= sizeof(*id))) {
216 id = (struct usb_interface_descriptor *)desc;
217 if (id->bInterfaceNumber == ifaceno) {
218 n++;
219 if (n == 0xFF)
220 break; /* crazy */
221 } else
222 break; /* end */
223 }
224 }
225 return (n);
226}
214 if ((desc->bDescriptorType == UDESC_INTERFACE) &&
215 (desc->bLength >= sizeof(*id))) {
216 id = (struct usb_interface_descriptor *)desc;
217 if (id->bInterfaceNumber == ifaceno) {
218 n++;
219 if (n == 0xFF)
220 break; /* crazy */
221 } else
222 break; /* end */
223 }
224 }
225 return (n);
226}