Deleted Added
full compact
usb_device.c (302408) usb_device.c (305733)
1/* $FreeBSD: stable/11/sys/dev/usb/usb_device.c 300667 2016-05-25 07:48:36Z hselasky $ */
1/* $FreeBSD: stable/11/sys/dev/usb/usb_device.c 305733 2016-09-12 10:14:30Z hselasky $ */
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.

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

1580 }
1581 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1582 if (udev == NULL) {
1583 return (NULL);
1584 }
1585 /* initialise our SX-lock */
1586 sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1587 sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
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.

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

1580 }
1581 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1582 if (udev == NULL) {
1583 return (NULL);
1584 }
1585 /* initialise our SX-lock */
1586 sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1587 sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
1588 sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK);
1588
1589 cv_init(&udev->ctrlreq_cv, "WCTRL");
1590 cv_init(&udev->ref_cv, "UGONE");
1591
1592 /* initialise our mutex */
1593 mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1594
1595 /* initialise generic clear stall */

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

1765 * descriptors.
1766 *
1767 * Try to detect such chips by checking the strings in the USB
1768 * device descriptor. If no strings are present there we
1769 * simply disable all USB strings.
1770 */
1771
1772 /* Protect scratch area */
1589
1590 cv_init(&udev->ctrlreq_cv, "WCTRL");
1591 cv_init(&udev->ref_cv, "UGONE");
1592
1593 /* initialise our mutex */
1594 mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1595
1596 /* initialise generic clear stall */

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

1766 * descriptors.
1767 *
1768 * Try to detect such chips by checking the strings in the USB
1769 * device descriptor. If no strings are present there we
1770 * simply disable all USB strings.
1771 */
1772
1773 /* Protect scratch area */
1773 do_unlock = usbd_enum_lock(udev);
1774 do_unlock = usbd_ctrl_lock(udev);
1774
1775 scratch_ptr = udev->scratch.data;
1776
1777 if (udev->flags.no_strings) {
1778 err = USB_ERR_INVAL;
1779 } else if (udev->ddesc.iManufacturer ||
1780 udev->ddesc.iProduct ||
1781 udev->ddesc.iSerialNumber) {

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

1816 langid = UGETW(scratch_ptr + 2);
1817 }
1818
1819 DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1820 udev->langid = langid;
1821 }
1822
1823 if (do_unlock)
1775
1776 scratch_ptr = udev->scratch.data;
1777
1778 if (udev->flags.no_strings) {
1779 err = USB_ERR_INVAL;
1780 } else if (udev->ddesc.iManufacturer ||
1781 udev->ddesc.iProduct ||
1782 udev->ddesc.iSerialNumber) {

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

1817 langid = UGETW(scratch_ptr + 2);
1818 }
1819
1820 DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1821 udev->langid = langid;
1822 }
1823
1824 if (do_unlock)
1824 usbd_enum_unlock(udev);
1825 usbd_ctrl_unlock(udev);
1825
1826 /* assume 100mA bus powered for now. Changed when configured. */
1827 udev->power = USB_MIN_POWER;
1828 /* fetch the vendor and product strings from the device */
1829 usbd_set_device_strings(udev);
1830
1831 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1832 /* USB device mode setup is complete */

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

2190 &udev->cs_msg[0], &udev->cs_msg[1]);
2191 USB_BUS_UNLOCK(udev->bus);
2192
2193 /* wait for all references to go away */
2194 usb_wait_pending_refs(udev);
2195
2196 sx_destroy(&udev->enum_sx);
2197 sx_destroy(&udev->sr_sx);
1826
1827 /* assume 100mA bus powered for now. Changed when configured. */
1828 udev->power = USB_MIN_POWER;
1829 /* fetch the vendor and product strings from the device */
1830 usbd_set_device_strings(udev);
1831
1832 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1833 /* USB device mode setup is complete */

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

2191 &udev->cs_msg[0], &udev->cs_msg[1]);
2192 USB_BUS_UNLOCK(udev->bus);
2193
2194 /* wait for all references to go away */
2195 usb_wait_pending_refs(udev);
2196
2197 sx_destroy(&udev->enum_sx);
2198 sx_destroy(&udev->sr_sx);
2199 sx_destroy(&udev->ctrl_sx);
2198
2199 cv_destroy(&udev->ctrlreq_cv);
2200 cv_destroy(&udev->ref_cv);
2201
2202 mtx_destroy(&udev->device_mtx);
2203#if USB_HAVE_UGEN
2204 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2205#endif

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

2353#endif
2354 char *temp_ptr;
2355 size_t temp_size;
2356 uint16_t vendor_id;
2357 uint16_t product_id;
2358 uint8_t do_unlock;
2359
2360 /* Protect scratch area */
2200
2201 cv_destroy(&udev->ctrlreq_cv);
2202 cv_destroy(&udev->ref_cv);
2203
2204 mtx_destroy(&udev->device_mtx);
2205#if USB_HAVE_UGEN
2206 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2207#endif

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

2355#endif
2356 char *temp_ptr;
2357 size_t temp_size;
2358 uint16_t vendor_id;
2359 uint16_t product_id;
2360 uint8_t do_unlock;
2361
2362 /* Protect scratch area */
2361 do_unlock = usbd_enum_lock(udev);
2363 do_unlock = usbd_ctrl_lock(udev);
2362
2363 temp_ptr = (char *)udev->scratch.data;
2364 temp_size = sizeof(udev->scratch.data);
2365
2366 vendor_id = UGETW(udd->idVendor);
2367 product_id = UGETW(udd->idProduct);
2368
2369 /* get serial number string */

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

2413 udev->manufacturer = strdup(temp_ptr, M_USB);
2414 }
2415 if (udev->product == NULL) {
2416 snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
2417 udev->product = strdup(temp_ptr, M_USB);
2418 }
2419
2420 if (do_unlock)
2364
2365 temp_ptr = (char *)udev->scratch.data;
2366 temp_size = sizeof(udev->scratch.data);
2367
2368 vendor_id = UGETW(udd->idVendor);
2369 product_id = UGETW(udd->idProduct);
2370
2371 /* get serial number string */

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

2415 udev->manufacturer = strdup(temp_ptr, M_USB);
2416 }
2417 if (udev->product == NULL) {
2418 snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
2419 udev->product = strdup(temp_ptr, M_USB);
2420 }
2421
2422 if (do_unlock)
2421 usbd_enum_unlock(udev);
2423 usbd_ctrl_unlock(udev);
2422}
2423
2424/*
2425 * Returns:
2426 * See: USB_MODE_XXX
2427 */
2428enum usb_hc_mode
2429usbd_get_mode(struct usb_device *udev)

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

2820
2821uint8_t
2822usbd_enum_is_locked(struct usb_device *udev)
2823{
2824 return (sx_xlocked(&udev->enum_sx));
2825}
2826
2827/*
2424}
2425
2426/*
2427 * Returns:
2428 * See: USB_MODE_XXX
2429 */
2430enum usb_hc_mode
2431usbd_get_mode(struct usb_device *udev)

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

2822
2823uint8_t
2824usbd_enum_is_locked(struct usb_device *udev)
2825{
2826 return (sx_xlocked(&udev->enum_sx));
2827}
2828
2829/*
2830 * The following function is used to serialize access to USB control
2831 * transfers and the USB scratch area. If the lock is already grabbed
2832 * this function returns zero. Else a value of one is returned.
2833 */
2834uint8_t
2835usbd_ctrl_lock(struct usb_device *udev)
2836{
2837 if (sx_xlocked(&udev->ctrl_sx))
2838 return (0);
2839 sx_xlock(&udev->ctrl_sx);
2840
2841 /*
2842 * We need to allow suspend and resume at this point, else the
2843 * control transfer will timeout if the device is suspended!
2844 */
2845 if (usbd_enum_is_locked(udev))
2846 usbd_sr_unlock(udev);
2847 return (1);
2848}
2849
2850void
2851usbd_ctrl_unlock(struct usb_device *udev)
2852{
2853 sx_xunlock(&udev->ctrl_sx);
2854
2855 /*
2856 * Restore the suspend and resume lock after we have unlocked
2857 * the USB control transfer lock to avoid LOR:
2858 */
2859 if (usbd_enum_is_locked(udev))
2860 usbd_sr_lock(udev);
2861}
2862
2863/*
2828 * The following function is used to set the per-interface specific
2829 * plug and play information. The string referred to by the pnpinfo
2830 * argument can safely be freed after calling this function. The
2831 * pnpinfo of an interface will be reset at device detach or when
2832 * passing a NULL argument to this function. This function
2833 * returns zero on success, else a USB_ERR_XXX failure code.
2834 */
2835

--- 76 unchanged lines hidden ---
2864 * The following function is used to set the per-interface specific
2865 * plug and play information. The string referred to by the pnpinfo
2866 * argument can safely be freed after calling this function. The
2867 * pnpinfo of an interface will be reset at device detach or when
2868 * passing a NULL argument to this function. This function
2869 * returns zero on success, else a USB_ERR_XXX failure code.
2870 */
2871

--- 76 unchanged lines hidden ---