source: mainline/uspace/lib/usbdev/src/devdrv.c@ 3e23316

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e23316 was a6a5b25, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libusbdev: Add searching for pipes using ep and ep description.

  • Property mode set to 100644
File size: 13.8 KB
RevLine 
[6105fc0]1/*
2 * Copyright (c) 2011 Vojtech Horky
[4291215]3 * Copyright (c) 2011 Jan Vesely
[6105fc0]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
[160b75e]29/** @addtogroup libusbdev
[6105fc0]30 * @{
31 */
32/** @file
33 * USB device driver framework.
34 */
[7d521e24]35#include <usb/dev/driver.h>
36#include <usb/dev/request.h>
[6105fc0]37#include <usb/debug.h>
[1a38701]38#include <usb/dev.h>
[6105fc0]39#include <errno.h>
[69334af]40#include <str_error.h>
[6105fc0]41#include <assert.h>
42
[69334af]43/** Count number of pipes the driver expects.
44 *
45 * @param drv USB driver.
46 * @return Number of pipes (excluding default control pipe).
47 */
[b06d35a]48static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
[6105fc0]49{
[ab27e01]50 size_t count;
[64e3dad]51 for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
[6105fc0]52 return count;
53}
[a76b01b4]54
[0b4e7ca]55/** Change interface setting of a device.
56 * This function selects new alternate setting of an interface by issuing
57 * proper USB command to the device and also creates new USB pipes
58 * under @c dev->pipes.
59 *
60 * @warning This function is intended for drivers working at interface level.
61 * For drivers controlling the whole device, you need to change interface
62 * manually using usb_request_set_interface() and creating new pipes
63 * with usb_pipe_initialize_from_configuration().
64 *
[3f2af64]65 * @warning This is a wrapper function that does several operations that
66 * can fail and that cannot be rollbacked easily. That means that a failure
67 * during the SET_INTERFACE request would result in having a device with
68 * no pipes at all (except the default control one). That is because the old
69 * pipes needs to be unregistered at HC first and the new ones could not
70 * be created.
71 *
[0b4e7ca]72 * @param dev USB device.
73 * @param alternate_setting Alternate setting to choose.
74 * @param endpoints New endpoint descriptions.
75 * @return Error code.
76 */
[f4138ac]77int usb_device_select_interface(usb_device_t *usb_dev,
78 uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
[0b4e7ca]79{
[f4138ac]80 assert(usb_dev);
81
82 if (usb_dev->interface_no < 0) {
[0b4e7ca]83 return EINVAL;
84 }
85
86 /* Change the interface itself. */
[f4138ac]87 int rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
88 usb_dev->interface_no, alternate_setting);
[0b4e7ca]89 if (rc != EOK) {
90 return rc;
91 }
92
[f4138ac]93 /* Change current alternative */
94 usb_dev->alternate_interfaces.current = alternate_setting;
95
96 /* Destroy existing pipes. */
97 usb_device_destroy_pipes(usb_dev);
98
[0b4e7ca]99 /* Create new pipes. */
[b06d35a]100 rc = usb_device_create_pipes(usb_dev, endpoints);
[c1b1944]101
102 return rc;
103}
104
105/** Retrieve basic descriptors from the device.
106 *
[4fa0a384]107 * @param[in] ctrl_pipe Control endpoint pipe.
[c1b1944]108 * @param[out] descriptors Where to store the descriptors.
109 * @return Error code.
110 */
[945d66c]111static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
[c1b1944]112{
[945d66c]113 assert(usb_dev);
114 assert(usb_dev->descriptors.configuration == NULL);
[c1b1944]115
[4fa0a384]116 /* It is worth to start a long transfer. */
[945d66c]117 usb_pipe_start_long_transfer(&usb_dev->ctrl_pipe);
[4fa0a384]118
[c1b1944]119 /* Get the device descriptor. */
[945d66c]120 int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
121 &usb_dev->descriptors.device);
[c1b1944]122 if (rc != EOK) {
[4fa0a384]123 goto leave;
[c1b1944]124 }
125
126 /* Get the full configuration descriptor. */
127 rc = usb_request_get_full_configuration_descriptor_alloc(
[945d66c]128 &usb_dev->ctrl_pipe, 0,
129 (void **) &usb_dev->descriptors.configuration,
130 &usb_dev->descriptors.configuration_size);
[c1b1944]131
[4fa0a384]132leave:
[945d66c]133 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
[4fa0a384]134
135 return rc;
[c1b1944]136}
137
[7fc260ff]138/** Cleanup structure initialized via usb_device_retrieve_descriptors.
139 *
140 * @param[in] descriptors Where to store the descriptors.
141 */
[945d66c]142static void usb_device_release_descriptors(usb_device_t *usb_dev)
[7fc260ff]143{
[945d66c]144 assert(usb_dev);
145 free(usb_dev->descriptors.configuration);
146 usb_dev->descriptors.configuration = NULL;
147 usb_dev->descriptors.configuration_size = 0;
[7fc260ff]148}
149
[c1b1944]150/** Create pipes for a device.
151 *
152 * This is more or less a wrapper that does following actions:
153 * - allocate and initialize pipes
154 * - map endpoints to the pipes based on the descriptions
155 * - registers endpoints with the host controller
156 *
157 * @param[in] wire Initialized backing connection to the host controller.
158 * @param[in] endpoints Endpoints description, NULL terminated.
159 * @param[in] config_descr Configuration descriptor of active configuration.
160 * @param[in] config_descr_size Size of @p config_descr in bytes.
161 * @param[in] interface_no Interface to map from.
162 * @param[in] interface_setting Interface setting (default is usually 0).
163 * @param[out] pipes_ptr Where to store array of created pipes
164 * (not NULL terminated).
165 * @param[out] pipes_count_ptr Where to store number of pipes
[ab27e01]166 * (set to NULL if you wish to ignore the count).
[c1b1944]167 * @return Error code.
168 */
[b06d35a]169int usb_device_create_pipes(usb_device_t *usb_dev,
170 const usb_endpoint_description_t **endpoints)
[c1b1944]171{
[b06d35a]172 assert(usb_dev);
173 assert(usb_dev->descriptors.configuration);
174 assert(usb_dev->pipes == NULL);
175 assert(usb_dev->pipes_count == 0);
[c1b1944]176
[b06d35a]177 size_t pipe_count = count_pipes(endpoints);
[c1b1944]178 if (pipe_count == 0) {
179 return EOK;
180 }
181
[b06d35a]182 usb_endpoint_mapping_t *pipes =
183 calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
[c1b1944]184 if (pipes == NULL) {
185 return ENOMEM;
186 }
187
[441633f]188 /* Now initialize. */
[b06d35a]189 for (size_t i = 0; i < pipe_count; i++) {
[c1b1944]190 pipes[i].description = endpoints[i];
[b06d35a]191 pipes[i].interface_no = usb_dev->interface_no;
192 pipes[i].interface_setting =
193 usb_dev->alternate_interfaces.current;
[c1b1944]194 }
195
196 /* Find the mapping from configuration descriptor. */
[b06d35a]197 int rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
198 usb_dev->descriptors.configuration,
199 usb_dev->descriptors.configuration_size, &usb_dev->wire);
[c1b1944]200 if (rc != EOK) {
[441633f]201 free(pipes);
202 return rc;
[c1b1944]203 }
204
[441633f]205 /* Register created pipes. */
[b06d35a]206 for (size_t i = 0; i < pipe_count; i++) {
[c1b1944]207 if (pipes[i].present) {
[b77931d]208 rc = usb_pipe_register(&pipes[i].pipe,
[bd575647]209 pipes[i].descriptor->poll_interval);
[c1b1944]210 if (rc != EOK) {
211 goto rollback_unregister_endpoints;
212 }
213 }
214 }
215
[b06d35a]216 usb_dev->pipes = pipes;
217 usb_dev->pipes_count = pipe_count;
[c1b1944]218
219 return EOK;
220
221 /*
222 * Jump here if something went wrong after endpoints have
223 * been registered.
224 * This is also the target when the registration of
225 * endpoints fails.
226 */
227rollback_unregister_endpoints:
[b06d35a]228 for (size_t i = 0; i < pipe_count; i++) {
[c1b1944]229 if (pipes[i].present) {
[bd575647]230 usb_pipe_unregister(&pipes[i].pipe);
[c1b1944]231 }
232 }
233
234 free(pipes);
[0b4e7ca]235 return rc;
236}
[159b91f4]237
[c1b1944]238/** Destroy pipes previously created by usb_device_create_pipes.
239 *
[2dc5a9f]240 * @param[in] usb_dev USB device.
[c1b1944]241 */
[2dc5a9f]242void usb_device_destroy_pipes(usb_device_t *usb_dev)
[c1b1944]243{
[2dc5a9f]244 assert(usb_dev);
245 assert(usb_dev->pipes || usb_dev->pipes_count == 0);
[c1b1944]246 /* Destroy the pipes. */
[2dc5a9f]247 for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
[8a01a0b]248 usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
[2dc5a9f]249 i, usb_dev->pipes[i].present ? "" : "not ");
250 if (usb_dev->pipes[i].present)
251 usb_pipe_unregister(&usb_dev->pipes[i].pipe);
[c1b1944]252 }
[2dc5a9f]253 free(usb_dev->pipes);
254 usb_dev->pipes = NULL;
255 usb_dev->pipes_count = 0;
[c1b1944]256}
257
[0f4bff8]258usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
259{
260 assert(usb_dev);
261 return &usb_dev->ctrl_pipe;
262}
263
[a6a5b25]264usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
265 const usb_endpoint_description_t *desc)
266{
267 assert(usb_dev);
268 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
269 if (usb_dev->pipes[i].description == desc)
270 return &usb_dev->pipes[i];
271 }
272 return NULL;
273}
274
275usb_endpoint_mapping_t * usb_device_get_mapped_ep(
276 usb_device_t *usb_dev, usb_endpoint_t ep)
277{
278 assert(usb_dev);
279 for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
280 if (usb_dev->pipes[i].pipe.endpoint_no == ep)
281 return &usb_dev->pipes[i];
282 }
283 return NULL;
284}
285
[8e10ef4]286int usb_device_get_iface_number(usb_device_t *usb_dev)
287{
288 assert(usb_dev);
289 return usb_dev->interface_no;
290}
291
[945d66c]292const usb_standard_device_descriptor_t *
293usb_device_get_device_descriptor(usb_device_t *usb_dev)
294{
295 assert(usb_dev);
296 return &usb_dev->descriptors.device;
297}
298
299const void * usb_device_get_configuration_descriptor(
300 usb_device_t *usb_dev, size_t *size)
301{
302 assert(usb_dev);
303 if (size)
304 *size = usb_dev->descriptors.configuration_size;
305 return usb_dev->descriptors.configuration;
306}
307
[8e10ef4]308const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
309 usb_device_t *usb_dev)
310{
311 assert(usb_dev);
312 return &usb_dev->alternate_interfaces;
313}
314
[7d9cd62]315/** Initialize new instance of USB device.
[6ee6e6f]316 *
[7d9cd62]317 * @param[in] usb_dev Pointer to the new device.
[6ee6e6f]318 * @param[in] ddf_dev Generic DDF device backing the USB one.
319 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
320 * @param[out] errstr_ptr Where to store description of context
321 * (in case error occurs).
322 * @return Error code.
323 */
[7d9cd62]324int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
325 const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
[6ee6e6f]326{
[7d9cd62]327 assert(usb_dev != NULL);
[6ee6e6f]328 assert(ddf_dev != NULL);
329
[7fc260ff]330 *errstr_ptr = NULL;
331
[7d9cd62]332 usb_dev->ddf_dev = ddf_dev;
333 usb_dev->driver_data = NULL;
334 usb_dev->descriptors.configuration = NULL;
335 usb_dev->pipes_count = 0;
336 usb_dev->pipes = NULL;
[6ee6e6f]337
[94fbf78]338 usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
[c8c758d]339 if (!usb_dev->bus_session) {
340 *errstr_ptr = "device bus session create";
341 return ENOMEM;
342 }
343
[c24c157d]344 /* Get assigned params */
345 devman_handle_t hc_handle;
346 usb_address_t address;
347
[56fd7cf]348 int rc = usb_get_info_by_handle(ddf_dev_get_handle(ddf_dev),
[c24c157d]349 &hc_handle, &address, &usb_dev->interface_no);
350 if (rc != EOK) {
351 *errstr_ptr = "device parameters retrieval";
352 return rc;
353 }
354
[c0fdc0e]355 /* Initialize hc connection. */
[c24c157d]356 usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
[c0fdc0e]357
[3f2af64]358 /* Initialize backing wire and control pipe. */
[c24c157d]359 rc = usb_device_connection_initialize(
[bd575647]360 &usb_dev->wire, &usb_dev->hc_conn, address);
[3f2af64]361 if (rc != EOK) {
[7fc260ff]362 *errstr_ptr = "device connection initialization";
363 return rc;
364 }
365
366 /* This pipe was registered by the hub driver,
367 * during device initialization. */
[c0fdc0e]368 rc = usb_pipe_initialize_default_control(
369 &usb_dev->ctrl_pipe, &usb_dev->wire);
[7fc260ff]370 if (rc != EOK) {
371 *errstr_ptr = "default control pipe initialization";
[3f2af64]372 return rc;
373 }
374
[6e3c005]375 /* Open hc connection for pipe registration. */
[c0fdc0e]376 rc = usb_hc_connection_open(&usb_dev->hc_conn);
377 if (rc != EOK) {
378 *errstr_ptr = "hc connection open";
379 return rc;
380 }
381
[3f2af64]382 /* Retrieve standard descriptors. */
[945d66c]383 rc = usb_device_retrieve_descriptors(usb_dev);
[6ee6e6f]384 if (rc != EOK) {
[3f2af64]385 *errstr_ptr = "descriptor retrieval";
[c0fdc0e]386 usb_hc_connection_close(&usb_dev->hc_conn);
[6ee6e6f]387 return rc;
388 }
389
[7fc260ff]390 /* Create alternate interfaces. We will silently ignore failure.
391 * We might either control one interface or an entire device,
392 * it makes no sense to speak about alternate interfaces when
393 * controlling a device. */
[ab27e01]394 rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
[904dcc6]395 usb_dev->descriptors.configuration,
396 usb_dev->descriptors.configuration_size, usb_dev->interface_no);
[6ee6e6f]397
[66ee26a]398 /* Create and register other pipes than default control (EP 0) */
[b06d35a]399 rc = usb_device_create_pipes(usb_dev, endpoints);
[6ee6e6f]400 if (rc != EOK) {
[c0fdc0e]401 usb_hc_connection_close(&usb_dev->hc_conn);
[7d9cd62]402 /* Full configuration descriptor is allocated. */
[945d66c]403 usb_device_release_descriptors(usb_dev);
[7d9cd62]404 /* Alternate interfaces may be allocated */
[904dcc6]405 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
[6ee6e6f]406 *errstr_ptr = "pipes initialization";
407 return rc;
408 }
409
[c0fdc0e]410 usb_hc_connection_close(&usb_dev->hc_conn);
[6ee6e6f]411 return EOK;
412}
413
[7d9cd62]414/** Clean instance of a USB device.
[70452dd4]415 *
[9c5fd7a]416 * @param dev Device to be de-initialized.
417 *
418 * Does not free/destroy supplied pointer.
[70452dd4]419 */
[9c5fd7a]420void usb_device_deinit(usb_device_t *dev)
[70452dd4]421{
[7d9cd62]422 if (dev) {
[c8c758d]423 usb_dev_session_close(dev->bus_session);
[8e3742f9]424 /* Destroy existing pipes. */
[2dc5a9f]425 usb_device_destroy_pipes(dev);
[8e3742f9]426 /* Ignore errors and hope for the best. */
427 usb_hc_connection_deinitialize(&dev->hc_conn);
[904dcc6]428 usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
[945d66c]429 usb_device_release_descriptors(dev);
[7d9cd62]430 free(dev->driver_data);
[8e3742f9]431 dev->driver_data = NULL;
[70452dd4]432 }
[065064e6]433}
434
[f6b2a76b]435const char *usb_device_get_name(usb_device_t *usb_dev)
436{
437 assert(usb_dev);
438 assert(usb_dev->ddf_dev);
439 //TODO Handle case without ddf_dev
440 return ddf_dev_get_name(usb_dev->ddf_dev);
441}
442
[bb512b2d]443ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
444 const char* name)
445{
446 assert(usb_dev);
447 if (usb_dev->ddf_dev)
448 return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
449 return NULL;
450}
451
[0f4bff8]452async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
453{
454 assert(usb_dev);
455 return async_exchange_begin(usb_dev->bus_session);
456}
457
458void usb_device_bus_exchange_end(async_exch_t *exch)
459{
460 async_exchange_end(exch);
461}
462
[6e3c005]463/** Allocate driver specific data.
464 * @param usb_dev usb_device structure.
465 * @param size requested data size.
466 * @return Pointer to the newly allocated space, NULL on failure.
467 */
[065064e6]468void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
469{
470 assert(usb_dev);
471 assert(usb_dev->driver_data == NULL);
472 return usb_dev->driver_data = calloc(1, size);
[70452dd4]473
474}
475
[0f4bff8]476void * usb_device_data_get(usb_device_t *usb_dev)
477{
478 assert(usb_dev);
479 return usb_dev->driver_data;
480}
[6105fc0]481/**
482 * @}
483 */
Note: See TracBrowser for help on using the repository browser.