source: mainline/uspace/lib/usb/src/hcdhubd.c@ da55d5b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since da55d5b was da55d5b, checked in by smekideki@…>, 15 years ago

loading keyboard hardwired

  • Property mode set to 100644
File size: 11.3 KB
RevLine 
[c7137738]1/*
2 * Copyright (c) 2010 Vojtech Horky
3 * 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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libusb usb
30 * @{
31 */
32/** @file
33 * @brief HC driver and hub driver (implementation).
34 */
[4b4c797]35#include <usb/hcdhubd.h>
[8f62b0f]36#include <usb/devreq.h>
[cb59f787]37#include <usbhc_iface.h>
[da55d5b]38#include <usb/descriptor.h>
[c7137738]39#include <driver.h>
40#include <bool.h>
41#include <errno.h>
[03171de]42#include <usb/classes/hub.h>
[c7137738]43
[0ee648a]44#define USB_HUB_DEVICE_NAME "usbhub"
45
[da55d5b]46#define USB_KBD_DEVICE_NAME "hid"
47
48
49
50
[c7137738]51/** List of handled host controllers. */
52static LIST_INITIALIZE(hc_list);
53
54/** Our HC driver. */
55static usb_hc_driver_t *hc_driver = NULL;
56
[cb59f787]57static usbhc_iface_t usb_interface = {
[7034be15]58 .interrupt_out = NULL,
59 .interrupt_in = NULL
60};
61
62static device_ops_t usb_device_ops = {
[cb59f787]63 .interfaces[USBHC_DEV_IFACE] = &usb_interface
[7034be15]64};
65
[03171de]66size_t USB_HUB_MAX_DESCRIPTOR_SIZE = 71;
67
68uint8_t USB_HUB_DESCRIPTOR_TYPE = 0x29;
69
70//*********************************************
71//
72// various utils
73//
74//*********************************************
75
[da55d5b]76void * usb_serialize_hub_descriptor(usb_hub_descriptor_t * descriptor) {
[03171de]77 //base size
78 size_t size = 7;
79 //variable size according to port count
[da55d5b]80 size_t var_size = descriptor->ports_count / 8 + ((descriptor->ports_count % 8 > 0) ? 1 : 0);
[03171de]81 size += 2 * var_size;
82 uint8_t * result = (uint8_t*) malloc(size);
83 //size
[da55d5b]84 result[0] = size;
[03171de]85 //descriptor type
[da55d5b]86 result[1] = USB_DESCTYPE_HUB;
87 result[2] = descriptor->ports_count;
[03171de]88 /// @fixme handling of endianness??
[da55d5b]89 result[3] = descriptor->hub_characteristics / 256;
90 result[4] = descriptor->hub_characteristics % 256;
91 result[5] = descriptor->pwr_on_2_good_time;
92 result[6] = descriptor->current_requirement;
93
94 size_t i;
95 for (i = 0; i < var_size; ++i) {
96 result[7 + i] = descriptor->devices_removable[i];
[03171de]97 }
[da55d5b]98 for (i = 0; i < var_size; ++i) {
99 result[7 + var_size + i] = 255;
[03171de]100 }
101 return result;
102}
103
[da55d5b]104usb_hub_descriptor_t * usb_deserialize_hub_desriptor(void * serialized_descriptor) {
105 uint8_t * sdescriptor = (uint8_t*) serialized_descriptor;
106 if (sdescriptor[1] != USB_DESCTYPE_HUB) return NULL;
107 usb_hub_descriptor_t * result = (usb_hub_descriptor_t*) malloc(sizeof (usb_hub_descriptor_t));
[03171de]108 //uint8_t size = sdescriptor[0];
109 result->ports_count = sdescriptor[2];
110 /// @fixme handling of endianness??
111 result->hub_characteristics = sdescriptor[4] + 256 * sdescriptor[3];
[da55d5b]112 result->pwr_on_2_good_time = sdescriptor[5];
113 result->current_requirement = sdescriptor[6];
114 size_t var_size = result->ports_count / 8 + ((result->ports_count % 8 > 0) ? 1 : 0);
115 result->devices_removable = (uint8_t*) malloc(var_size);
116
117 size_t i;
118 for (i = 0; i < var_size; ++i) {
119 result->devices_removable[i] = sdescriptor[7 + i];
[03171de]120 }
121 return result;
122}
123
124
125//*********************************************
126//
127// hub driver code
128//
129//*********************************************
130
[da55d5b]131static void set_hub_address(usb_hc_device_t *hc, usb_address_t address);
[03171de]132
[da55d5b]133usb_hcd_hub_info_t * usb_create_hub_info(device_t * device) {
134 usb_hcd_hub_info_t* result = (usb_hcd_hub_info_t*) malloc(sizeof (usb_hcd_hub_info_t));
135 //get parent device
136 /// @TODO this code is not correct
137 device_t * my_hcd = device;
138 while (my_hcd->parent)
139 my_hcd = my_hcd->parent;
140 //dev->
141 printf("%s: owner hcd found: %s\n", hc_driver->name, my_hcd->name);
142 //we add the hub into the first hc
143 //link_t *link_hc = hc_list.next;
144 //usb_hc_device_t *hc = list_get_instance(link_hc,
145 // usb_hc_device_t, link);
146 //must get generic device info
[03171de]147
[da55d5b]148
149 return result;
150}
[8f62b0f]151
[c7137738]152/** Callback when new device is detected and must be handled by this driver.
153 *
154 * @param dev New device.
[03171de]155 * @return Error code.hub added, hurrah!\n"
[c7137738]156 */
[da55d5b]157static int add_device(device_t *dev) {
[c7137738]158 /*
159 * FIXME: use some magic to determine whether hub or another HC
160 * was connected.
161 */
[0ee648a]162 bool is_hc = str_cmp(dev->name, USB_HUB_DEVICE_NAME) != 0;
163 printf("%s: add_device(name=\"%s\")\n", hc_driver->name, dev->name);
[c7137738]164
165 if (is_hc) {
166 /*
167 * We are the HC itself.
168 */
[da55d5b]169 usb_hc_device_t *hc_dev = malloc(sizeof (usb_hc_device_t));
[c7137738]170 list_initialize(&hc_dev->link);
[7034be15]171 hc_dev->transfer_ops = NULL;
[c7137738]172
173 hc_dev->generic = dev;
[e27595b]174 dev->ops = &usb_device_ops;
175 hc_dev->generic->driver_data = hc_dev;
176
[c7137738]177 int rc = hc_driver->add_hc(hc_dev);
178 if (rc != EOK) {
179 free(hc_dev);
180 return rc;
181 }
182
[7034be15]183 /*
184 * FIXME: The following line causes devman to hang.
185 * Will investigate later why.
186 */
187 // add_device_to_class(dev, "usbhc");
[c7137738]188
189 list_append(&hc_dev->link, &hc_list);
190
[da55d5b]191 //add keyboard
192 /// @TODO this is not correct code
193
194 /*
195 * Announce presence of child device.
196 */
197 device_t *kbd = NULL;
198 match_id_t *match_id = NULL;
199
200 kbd = create_device();
201 if (kbd == NULL) {
202 printf("ERROR: enomem\n");
203 }
204 kbd->name = USB_KBD_DEVICE_NAME;
205
206 match_id = create_match_id();
207 if (match_id == NULL) {
208 printf("ERROR: enomem\n");
209 }
210
211 char *id;
212 rc = asprintf(&id, USB_KBD_DEVICE_NAME);
213 if (rc <= 0) {
214 printf("ERROR: enomem\n");
215 return rc;
216 }
217
218 match_id->id = id;
219 match_id->score = 30;
220
221 add_match_id(&kbd->match_ids, match_id);
222
223 rc = child_device_register(kbd, dev);
224 if (rc != EOK) {
225 printf("ERROR: cannot register kbd\n");
226 return rc;
227 }
228
229 printf("%s: registered root hub\n", dev->name);
[c7137738]230 return EOK;
[da55d5b]231
232
233
[c7137738]234 } else {
[8f62b0f]235 usb_hc_device_t *hc = list_get_instance(hc_list.next, usb_hc_device_t, link);
236 set_hub_address(hc, 5);
237
[c7137738]238 /*
239 * We are some (probably deeply nested) hub.
240 * Thus, assign our own operations and explore already
241 * connected devices.
242 */
[da55d5b]243 //insert hub into list
244 //find owner hcd
245 device_t * my_hcd = dev;
246 while (my_hcd->parent)
247 my_hcd = my_hcd->parent;
248 //dev->
249 printf("%s: owner hcd found: %s\n", hc_driver->name, my_hcd->name);
250 my_hcd = dev;
251 while (my_hcd->parent)
252 my_hcd = my_hcd->parent;
253 //dev->
254
255 printf("%s: owner hcd found: %s\n", hc_driver->name, my_hcd->name);
256
257 //create the hub structure
258 usb_hcd_hub_info_t * hub_info = usb_create_hub_info(dev);
[03171de]259
[8f62b0f]260
[da55d5b]261 //append into the list
262 //we add the hub into the first hc
263 list_append(&hub_info->link, &hc->hubs);
264
265
266
267 return EOK;
268 //return ENOTSUP;
[c7137738]269 }
270}
271
[8f62b0f]272/** Sample usage of usb_hc_async functions.
273 * This function sets hub address using standard SET_ADDRESS request.
274 *
275 * @warning This function shall be removed once you are familiar with
276 * the usb_hc_ API.
277 *
278 * @param hc Host controller the hub belongs to.
279 * @param address New hub address.
280 */
[da55d5b]281static void set_hub_address(usb_hc_device_t *hc, usb_address_t address) {
[8f62b0f]282 printf("%s: setting hub address to %d\n", hc->generic->name, address);
283 usb_target_t target = {0, 0};
284 usb_handle_t handle;
285 int rc;
286
287 usb_device_request_setup_packet_t setup_packet = {
288 .request_type = 0,
289 .request = USB_DEVREQ_SET_ADDRESS,
290 .index = 0,
291 .length = 0,
292 };
293 setup_packet.value = address;
294
295 rc = usb_hc_async_control_write_setup(hc, target,
[da55d5b]296 &setup_packet, sizeof (setup_packet), &handle);
[8f62b0f]297 if (rc != EOK) {
298 return;
299 }
300
301 rc = usb_hc_async_wait_for(handle);
302 if (rc != EOK) {
303 return;
304 }
305
306 rc = usb_hc_async_control_write_status(hc, target, &handle);
307 if (rc != EOK) {
308 return;
309 }
310
311 rc = usb_hc_async_wait_for(handle);
312 if (rc != EOK) {
313 return;
314 }
315
316 printf("%s: hub address changed\n", hc->generic->name);
317}
318
[c7137738]319/** Check changes on all known hubs.
320 */
[da55d5b]321static void check_hub_changes(void) {
[c7137738]322 /*
323 * Iterate through all HCs.
324 */
325 link_t *link_hc;
326 for (link_hc = hc_list.next;
[da55d5b]327 link_hc != &hc_list;
328 link_hc = link_hc->next) {
[c7137738]329 usb_hc_device_t *hc = list_get_instance(link_hc,
[da55d5b]330 usb_hc_device_t, link);
[c7137738]331 /*
332 * Iterate through all their hubs.
333 */
334 link_t *link_hub;
335 for (link_hub = hc->hubs.next;
[da55d5b]336 link_hub != &hc->hubs;
337 link_hub = link_hub->next) {
[c7137738]338 usb_hcd_hub_info_t *hub = list_get_instance(link_hub,
[da55d5b]339 usb_hcd_hub_info_t, link);
[c7137738]340
341 /*
342 * Check status change pipe of this hub.
343 */
344 usb_target_t target = {
345 .address = hub->device->address,
346 .endpoint = 1
347 };
348
349 // FIXME: count properly
350 size_t byte_length = (hub->port_count / 8) + 1;
351
352 void *change_bitmap = malloc(byte_length);
353 size_t actual_size;
354 usb_handle_t handle;
355
356 /*
357 * Send the request.
358 * FIXME: check returned value for possible errors
359 */
[7034be15]360 usb_hc_async_interrupt_in(hc, target,
[da55d5b]361 change_bitmap, byte_length, &actual_size,
362 &handle);
[c7137738]363
[7034be15]364 usb_hc_async_wait_for(handle);
[c7137738]365
366 /*
367 * TODO: handle the changes.
368 */
369 }
370 }
371}
372
373/** Operations for combined HC and HUB driver. */
374static driver_ops_t hc_driver_generic_ops = {
375 .add_device = add_device
376};
377
378/** Combined HC and HUB driver. */
379static driver_t hc_driver_generic = {
380 .driver_ops = &hc_driver_generic_ops
381};
382
383/** Main USB host controller driver routine.
384 *
385 * @see driver_main
386 *
387 * @param hc Host controller driver.
388 * @return Error code.
389 */
[da55d5b]390int usb_hcd_main(usb_hc_driver_t *hc) {
[c7137738]391 hc_driver = hc;
392 hc_driver_generic.name = hc->name;
393
394 /*
395 * Launch here fibril that will periodically check all
396 * attached hubs for status change.
397 * WARN: This call will effectively do nothing.
398 */
399 check_hub_changes();
400
401 /*
402 * Run the device driver framework.
403 */
404 return driver_main(&hc_driver_generic);
405}
406
407/** Add a root hub for given host controller.
408 * This function shall be called only once for each host controller driven
409 * by this driver.
410 * It takes care of creating child device - hub - that will be driven by
411 * this task.
412 *
413 * @param dev Host controller device.
414 * @return Error code.
415 */
[da55d5b]416int usb_hcd_add_root_hub(usb_hc_device_t *dev) {
[7034be15]417 int rc;
418
[c7137738]419 /*
420 * Announce presence of child device.
421 */
422 device_t *hub = NULL;
423 match_id_t *match_id = NULL;
424
425 hub = create_device();
426 if (hub == NULL) {
427 rc = ENOMEM;
428 goto failure;
429 }
[0ee648a]430 hub->name = USB_HUB_DEVICE_NAME;
[c7137738]431
432 match_id = create_match_id();
433 if (match_id == NULL) {
434 rc = ENOMEM;
435 goto failure;
436 }
437
438 char *id;
439 rc = asprintf(&id, "usb&hc=%s&hub", dev->generic->name);
440 if (rc <= 0) {
441 rc = ENOMEM;
442 goto failure;
443 }
444
445 match_id->id = id;
[7034be15]446 match_id->score = 30;
[c7137738]447
448 add_match_id(&hub->match_ids, match_id);
449
450 rc = child_device_register(hub, dev->generic);
451 if (rc != EOK) {
452 goto failure;
453 }
454
[7034be15]455 printf("%s: registered root hub\n", dev->generic->name);
[c7137738]456 return EOK;
457
458failure:
459 if (hub != NULL) {
460 hub->name = NULL;
461 delete_device(hub);
462 }
463 delete_match_id(match_id);
464
465 return rc;
466}
467
468/**
469 * @}
470 */
Note: See TracBrowser for help on using the repository browser.