usbnet.c (6f0a0986e328dd61610d898a09c9f4aa960ae64a) usbnet.c (9532021da6da48351ce48a079c673df8e3930b4a)
1/*
2 * USB Network driver infrastructure
3 * Copyright (C) 2000-2005 by David Brownell
4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or

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

354/* some work can't be done in tasklets, so we use keventd
355 *
356 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
357 * but tasklet_schedule() doesn't. hope the failure is rare.
358 */
359void usbnet_defer_kevent (struct usbnet *dev, int work)
360{
361 set_bit (work, &dev->flags);
1/*
2 * USB Network driver infrastructure
3 * Copyright (C) 2000-2005 by David Brownell
4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or

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

354/* some work can't be done in tasklets, so we use keventd
355 *
356 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
357 * but tasklet_schedule() doesn't. hope the failure is rare.
358 */
359void usbnet_defer_kevent (struct usbnet *dev, int work)
360{
361 set_bit (work, &dev->flags);
362 if (!schedule_work (&dev->kevent))
363 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
364 else
362 if (!schedule_work (&dev->kevent)) {
363 if (net_ratelimit())
364 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
365 } else {
365 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
366 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
367 }
366}
367EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
368
369/*-------------------------------------------------------------------------*/
370
371static void rx_complete (struct urb *urb);
372
373static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)

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

1153
1154#ifdef CONFIG_PM
1155 /* if this triggers the device is still a sleep */
1156 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1157 /* transmission will be done in resume */
1158 usb_anchor_urb(urb, &dev->deferred);
1159 /* no use to process more packets */
1160 netif_stop_queue(net);
368}
369EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
370
371/*-------------------------------------------------------------------------*/
372
373static void rx_complete (struct urb *urb);
374
375static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)

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

1155
1156#ifdef CONFIG_PM
1157 /* if this triggers the device is still a sleep */
1158 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1159 /* transmission will be done in resume */
1160 usb_anchor_urb(urb, &dev->deferred);
1161 /* no use to process more packets */
1162 netif_stop_queue(net);
1163 usb_put_urb(urb);
1161 spin_unlock_irqrestore(&dev->txq.lock, flags);
1162 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
1163 goto deferred;
1164 }
1165#endif
1166
1167 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1168 case -EPIPE:

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

1305 xdev->bus->bus_name, xdev->devpath,
1306 dev->driver_info->description);
1307
1308 net = dev->net;
1309 unregister_netdev (net);
1310
1311 cancel_work_sync(&dev->kevent);
1312
1164 spin_unlock_irqrestore(&dev->txq.lock, flags);
1165 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
1166 goto deferred;
1167 }
1168#endif
1169
1170 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1171 case -EPIPE:

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

1308 xdev->bus->bus_name, xdev->devpath,
1309 dev->driver_info->description);
1310
1311 net = dev->net;
1312 unregister_netdev (net);
1313
1314 cancel_work_sync(&dev->kevent);
1315
1316 usb_scuttle_anchored_urbs(&dev->deferred);
1317
1313 if (dev->driver_info->unbind)
1314 dev->driver_info->unbind (dev, intf);
1315
1316 usb_kill_urb(dev->interrupt);
1317 usb_free_urb(dev->interrupt);
1318
1319 free_netdev(net);
1320}

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

1606 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1607 dev->intf->needs_remote_wakeup = 1;
1608 usb_autopm_put_interface_async(dev->intf);
1609 }
1610}
1611EXPORT_SYMBOL(usbnet_device_suggests_idle);
1612
1613/*-------------------------------------------------------------------------*/
1318 if (dev->driver_info->unbind)
1319 dev->driver_info->unbind (dev, intf);
1320
1321 usb_kill_urb(dev->interrupt);
1322 usb_free_urb(dev->interrupt);
1323
1324 free_netdev(net);
1325}

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

1611 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1612 dev->intf->needs_remote_wakeup = 1;
1613 usb_autopm_put_interface_async(dev->intf);
1614 }
1615}
1616EXPORT_SYMBOL(usbnet_device_suggests_idle);
1617
1618/*-------------------------------------------------------------------------*/
1614static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1615 u16 value, u16 index, void *data, u16 size)
1616{
1617 void *buf = NULL;
1618 int err = -ENOMEM;
1619
1619
1620 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1621 " value=0x%04x index=0x%04x size=%d\n",
1622 cmd, reqtype, value, index, size);
1623
1624 if (data) {
1625 buf = kmalloc(size, GFP_KERNEL);
1626 if (!buf)
1627 goto out;
1628 }
1629
1630 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1631 cmd, reqtype, value, index, buf, size,
1632 USB_CTRL_GET_TIMEOUT);
1633 if (err > 0 && err <= size)
1634 memcpy(data, buf, err);
1635 kfree(buf);
1636out:
1637 return err;
1638}
1639
1640static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1641 u16 value, u16 index, const void *data,
1642 u16 size)
1643{
1644 void *buf = NULL;
1645 int err = -ENOMEM;
1646
1647 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1648 " value=0x%04x index=0x%04x size=%d\n",
1649 cmd, reqtype, value, index, size);
1650
1651 if (data) {
1652 buf = kmemdup(data, size, GFP_KERNEL);
1653 if (!buf)
1654 goto out;
1655 }
1656
1657 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1658 cmd, reqtype, value, index, buf, size,
1659 USB_CTRL_SET_TIMEOUT);
1660 kfree(buf);
1661
1662out:
1663 return err;
1664}
1665
1666/*
1667 * The function can't be called inside suspend/resume callback,
1668 * otherwise deadlock will be caused.
1669 */
1670int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1671 u16 value, u16 index, void *data, u16 size)
1672{
1673 int ret;
1674
1675 if (usb_autopm_get_interface(dev->intf) < 0)
1676 return -ENODEV;
1677 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
1678 data, size);
1679 usb_autopm_put_interface(dev->intf);
1680 return ret;
1681}
1682EXPORT_SYMBOL_GPL(usbnet_read_cmd);
1683
1684/*
1685 * The function can't be called inside suspend/resume callback,
1686 * otherwise deadlock will be caused.
1687 */
1688int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1689 u16 value, u16 index, const void *data, u16 size)
1690{
1691 int ret;
1692
1693 if (usb_autopm_get_interface(dev->intf) < 0)
1694 return -ENODEV;
1695 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
1696 data, size);
1697 usb_autopm_put_interface(dev->intf);
1698 return ret;
1699}
1700EXPORT_SYMBOL_GPL(usbnet_write_cmd);
1701
1702/*
1703 * The function can be called inside suspend/resume callback safely
1704 * and should only be called by suspend/resume callback generally.
1705 */
1706int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
1707 u16 value, u16 index, void *data, u16 size)
1708{
1709 return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
1710 data, size);
1711}
1712EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
1713
1714/*
1715 * The function can be called inside suspend/resume callback safely
1716 * and should only be called by suspend/resume callback generally.
1717 */
1718int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
1719 u16 value, u16 index, const void *data,
1720 u16 size)
1721{
1722 return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
1723 data, size);
1724}
1725EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
1726
1727static void usbnet_async_cmd_cb(struct urb *urb)
1728{
1729 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
1730 int status = urb->status;
1731
1732 if (status < 0)
1733 dev_dbg(&urb->dev->dev, "%s failed with %d",
1734 __func__, status);
1735
1736 kfree(req);
1737 usb_free_urb(urb);
1738}
1739
1740/*
1741 * The caller must make sure that device can't be put into suspend
1742 * state until the control URB completes.
1743 */
1744int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
1745 u16 value, u16 index, const void *data, u16 size)
1746{
1747 struct usb_ctrlrequest *req = NULL;
1748 struct urb *urb;
1749 int err = -ENOMEM;
1750 void *buf = NULL;
1751
1752 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1753 " value=0x%04x index=0x%04x size=%d\n",
1754 cmd, reqtype, value, index, size);
1755
1756 urb = usb_alloc_urb(0, GFP_ATOMIC);
1757 if (!urb) {
1758 netdev_err(dev->net, "Error allocating URB in"
1759 " %s!\n", __func__);
1760 goto fail;
1761 }
1762
1763 if (data) {
1764 buf = kmemdup(data, size, GFP_ATOMIC);
1765 if (!buf) {
1766 netdev_err(dev->net, "Error allocating buffer"
1767 " in %s!\n", __func__);
1768 goto fail_free;
1769 }
1770 }
1771
1772 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
1773 if (!req) {
1774 netdev_err(dev->net, "Failed to allocate memory for %s\n",
1775 __func__);
1776 goto fail_free_buf;
1777 }
1778
1779 req->bRequestType = reqtype;
1780 req->bRequest = cmd;
1781 req->wValue = cpu_to_le16(value);
1782 req->wIndex = cpu_to_le16(index);
1783 req->wLength = cpu_to_le16(size);
1784
1785 usb_fill_control_urb(urb, dev->udev,
1786 usb_sndctrlpipe(dev->udev, 0),
1787 (void *)req, buf, size,
1788 usbnet_async_cmd_cb, req);
1789 urb->transfer_flags |= URB_FREE_BUFFER;
1790
1791 err = usb_submit_urb(urb, GFP_ATOMIC);
1792 if (err < 0) {
1793 netdev_err(dev->net, "Error submitting the control"
1794 " message: status=%d\n", err);
1795 goto fail_free;
1796 }
1797 return 0;
1798
1799fail_free_buf:
1800 kfree(buf);
1801fail_free:
1802 kfree(req);
1803 usb_free_urb(urb);
1804fail:
1805 return err;
1806
1807}
1808EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
1809/*-------------------------------------------------------------------------*/
1810
1811static int __init usbnet_init(void)
1812{
1813 /* Compiler should optimize this out. */
1814 BUILD_BUG_ON(
1815 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
1816
1817 eth_random_addr(node_id);
1818 return 0;
1819}
1820module_init(usbnet_init);
1821
1822static void __exit usbnet_exit(void)
1823{
1824}
1825module_exit(usbnet_exit);
1826
1827MODULE_AUTHOR("David Brownell");
1828MODULE_DESCRIPTION("USB network driver framework");
1829MODULE_LICENSE("GPL");
1620static int __init usbnet_init(void)
1621{
1622 /* Compiler should optimize this out. */
1623 BUILD_BUG_ON(
1624 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
1625
1626 eth_random_addr(node_id);
1627 return 0;
1628}
1629module_init(usbnet_init);
1630
1631static void __exit usbnet_exit(void)
1632{
1633}
1634module_exit(usbnet_exit);
1635
1636MODULE_AUTHOR("David Brownell");
1637MODULE_DESCRIPTION("USB network driver framework");
1638MODULE_LICENSE("GPL");