^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * ipheth.c - Apple iPhone USB Ethernet driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2009 Diego Giagio <diego@giagio.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * modification, are permitted provided that the following conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * 1. Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * notice, this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * 2. Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * notice, this list of conditions and the following disclaimer in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * documentation and/or other materials provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * 3. Neither the name of GIAGIO.COM nor the names of its contributors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * may be used to endorse or promote products derived from this software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * without specific prior written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Alternatively, provided that this notice is retained in full, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * software may be distributed under the terms of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Public License ("GPL") version 2, in which case the provisions of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * GPL apply INSTEAD OF those given above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * The provided data structures and external interfaces from this code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * are not restricted to be used by modules with a GPL compatible license.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * Attention: iPhone device must be paired, otherwise it won't respond to our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * driver. For more info: http://giagio.com/wiki/moin.cgi/iPhoneEthernetDriver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/ethtool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define USB_VENDOR_APPLE 0x05ac
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define IPHETH_USBINTF_CLASS 255
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define IPHETH_USBINTF_SUBCLASS 253
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define IPHETH_USBINTF_PROTO 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define IPHETH_BUF_SIZE 1514
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define IPHETH_IP_ALIGN 2 /* padding at front of URB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define IPHETH_TX_TIMEOUT (5 * HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define IPHETH_INTFNUM 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define IPHETH_ALT_INTFNUM 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #define IPHETH_CTRL_ENDP 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define IPHETH_CTRL_BUF_SIZE 0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define IPHETH_CTRL_TIMEOUT (5 * HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define IPHETH_CMD_GET_MACADDR 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define IPHETH_CMD_CARRIER_CHECK 0x45
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define IPHETH_CARRIER_CHECK_TIMEOUT round_jiffies_relative(1 * HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define IPHETH_CARRIER_ON 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static const struct usb_device_id ipheth_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) { USB_VENDOR_AND_INTERFACE_INFO(USB_VENDOR_APPLE, IPHETH_USBINTF_CLASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) IPHETH_USBINTF_SUBCLASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) IPHETH_USBINTF_PROTO) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) MODULE_DEVICE_TABLE(usb, ipheth_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct ipheth_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct usb_interface *intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct net_device *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct urb *tx_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct urb *rx_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned char *tx_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned char *rx_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) unsigned char *ctrl_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) u8 bulk_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) u8 bulk_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct delayed_work carrier_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) bool confirmed_pairing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int ipheth_alloc_urbs(struct ipheth_device *iphone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct urb *tx_urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct urb *rx_urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) u8 *tx_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u8 *rx_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) tx_urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (tx_urb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) goto error_nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) rx_urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (rx_urb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) goto free_tx_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) tx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) GFP_KERNEL, &tx_urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (tx_buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto free_rx_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) rx_buf = usb_alloc_coherent(iphone->udev, IPHETH_BUF_SIZE + IPHETH_IP_ALIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) GFP_KERNEL, &rx_urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (rx_buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto free_tx_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) iphone->tx_urb = tx_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) iphone->rx_urb = rx_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) iphone->tx_buf = tx_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) iphone->rx_buf = rx_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) free_tx_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, tx_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) tx_urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) free_rx_urb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) usb_free_urb(rx_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) free_tx_urb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) usb_free_urb(tx_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) error_nomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static void ipheth_free_urbs(struct ipheth_device *iphone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE + IPHETH_IP_ALIGN, iphone->rx_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) iphone->rx_urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) usb_free_coherent(iphone->udev, IPHETH_BUF_SIZE, iphone->tx_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) iphone->tx_urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) usb_free_urb(iphone->rx_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) usb_free_urb(iphone->tx_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void ipheth_kill_urbs(struct ipheth_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) usb_kill_urb(dev->tx_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) usb_kill_urb(dev->rx_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static void ipheth_rcvbulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct ipheth_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) dev = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) case -EPROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) dev_err(&dev->intf->dev, "%s: urb status: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (urb->actual_length <= IPHETH_IP_ALIGN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) dev->net->stats.rx_length_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) len = urb->actual_length - IPHETH_IP_ALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) buf = urb->transfer_buffer + IPHETH_IP_ALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) skb = dev_alloc_skb(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) dev_err(&dev->intf->dev, "%s: dev_alloc_skb: -ENOMEM\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) dev->net->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) skb_put_data(skb, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) skb->dev = dev->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) skb->protocol = eth_type_trans(skb, dev->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) dev->net->stats.rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) dev->net->stats.rx_bytes += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) dev->confirmed_pairing = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ipheth_rx_submit(dev, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static void ipheth_sndbulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) struct ipheth_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) dev = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (dev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (status != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) status != -ENOENT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) status != -ECONNRESET &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) status != -ESHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) dev_err(&dev->intf->dev, "%s: urb status: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (status == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) netif_wake_queue(dev->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) // on URB error, trigger immediate poll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) schedule_delayed_work(&dev->carrier_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static int ipheth_carrier_set(struct ipheth_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (!dev->confirmed_pairing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) udev = dev->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) retval = usb_control_msg(udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) IPHETH_CMD_CARRIER_CHECK, /* request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 0xc0, /* request type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 0x00, /* value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 0x02, /* index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) dev->ctrl_buf, IPHETH_CTRL_BUF_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) IPHETH_CTRL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (dev->ctrl_buf[0] == IPHETH_CARRIER_ON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) netif_carrier_on(dev->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (dev->tx_urb->status != -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) netif_wake_queue(dev->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) netif_carrier_off(dev->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) netif_stop_queue(dev->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static void ipheth_carrier_check_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct ipheth_device *dev = container_of(work, struct ipheth_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) carrier_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ipheth_carrier_set(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int ipheth_get_macaddr(struct ipheth_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct usb_device *udev = dev->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct net_device *net = dev->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) retval = usb_control_msg(udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) usb_rcvctrlpipe(udev, IPHETH_CTRL_ENDP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) IPHETH_CMD_GET_MACADDR, /* request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 0xc0, /* request type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 0x00, /* value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 0x02, /* index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) dev->ctrl_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) IPHETH_CTRL_BUF_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) IPHETH_CTRL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) dev_err(&dev->intf->dev, "%s: usb_control_msg: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) } else if (retval < ETH_ALEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) dev_err(&dev->intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) "%s: usb_control_msg: short packet: %d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) memcpy(net->dev_addr, dev->ctrl_buf, ETH_ALEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static int ipheth_rx_submit(struct ipheth_device *dev, gfp_t mem_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct usb_device *udev = dev->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) usb_fill_bulk_urb(dev->rx_urb, udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) usb_rcvbulkpipe(udev, dev->bulk_in),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) dev->rx_buf, IPHETH_BUF_SIZE + IPHETH_IP_ALIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) ipheth_rcvbulk_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) dev->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) retval = usb_submit_urb(dev->rx_urb, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static int ipheth_open(struct net_device *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct ipheth_device *dev = netdev_priv(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct usb_device *udev = dev->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) usb_set_interface(udev, IPHETH_INTFNUM, IPHETH_ALT_INTFNUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) retval = ipheth_carrier_set(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) retval = ipheth_rx_submit(dev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) schedule_delayed_work(&dev->carrier_work, IPHETH_CARRIER_CHECK_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static int ipheth_close(struct net_device *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct ipheth_device *dev = netdev_priv(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) cancel_delayed_work_sync(&dev->carrier_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) netif_stop_queue(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static netdev_tx_t ipheth_tx(struct sk_buff *skb, struct net_device *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct ipheth_device *dev = netdev_priv(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct usb_device *udev = dev->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /* Paranoid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) if (skb->len > IPHETH_BUF_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) WARN(1, "%s: skb too large: %d bytes\n", __func__, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) dev->net->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) memcpy(dev->tx_buf, skb->data, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (skb->len < IPHETH_BUF_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) memset(dev->tx_buf + skb->len, 0, IPHETH_BUF_SIZE - skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) usb_fill_bulk_urb(dev->tx_urb, udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) usb_sndbulkpipe(udev, dev->bulk_out),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) dev->tx_buf, IPHETH_BUF_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) ipheth_sndbulk_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) dev->tx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) netif_stop_queue(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) retval = usb_submit_urb(dev->tx_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) dev_err(&dev->intf->dev, "%s: usb_submit_urb: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) dev->net->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) netif_wake_queue(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) dev->net->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) dev->net->stats.tx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) dev_consume_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static void ipheth_tx_timeout(struct net_device *net, unsigned int txqueue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) struct ipheth_device *dev = netdev_priv(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) dev_err(&dev->intf->dev, "%s: TX timeout\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) dev->net->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) usb_unlink_urb(dev->tx_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static u32 ipheth_ethtool_op_get_link(struct net_device *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct ipheth_device *dev = netdev_priv(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return netif_carrier_ok(dev->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static const struct ethtool_ops ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) .get_link = ipheth_ethtool_op_get_link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static const struct net_device_ops ipheth_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) .ndo_open = ipheth_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) .ndo_stop = ipheth_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) .ndo_start_xmit = ipheth_tx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) .ndo_tx_timeout = ipheth_tx_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static int ipheth_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct usb_device *udev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct usb_host_interface *hintf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) struct usb_endpoint_descriptor *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) struct ipheth_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct net_device *netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) netdev = alloc_etherdev(sizeof(struct ipheth_device));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (!netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) netdev->netdev_ops = &ipheth_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) netdev->watchdog_timeo = IPHETH_TX_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) strcpy(netdev->name, "eth%d");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) dev = netdev_priv(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) dev->udev = udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) dev->net = netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) dev->intf = intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) dev->confirmed_pairing = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /* Set up endpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) hintf = usb_altnum_to_altsetting(intf, IPHETH_ALT_INTFNUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (hintf == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) dev_err(&intf->dev, "Unable to find alternate settings interface\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) goto err_endpoints;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) for (i = 0; i < hintf->desc.bNumEndpoints; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) endp = &hintf->endpoint[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (usb_endpoint_is_bulk_in(endp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) dev->bulk_in = endp->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) else if (usb_endpoint_is_bulk_out(endp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) dev->bulk_out = endp->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (!(dev->bulk_in && dev->bulk_out)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) dev_err(&intf->dev, "Unable to find endpoints\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) goto err_endpoints;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) dev->ctrl_buf = kmalloc(IPHETH_CTRL_BUF_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (dev->ctrl_buf == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) goto err_alloc_ctrl_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) retval = ipheth_get_macaddr(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) goto err_get_macaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) INIT_DELAYED_WORK(&dev->carrier_work, ipheth_carrier_check_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) retval = ipheth_alloc_urbs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) dev_err(&intf->dev, "error allocating urbs: %d\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) goto err_alloc_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) usb_set_intfdata(intf, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) SET_NETDEV_DEV(netdev, &intf->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) netdev->ethtool_ops = &ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) retval = register_netdev(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) dev_err(&intf->dev, "error registering netdev: %d\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) goto err_register_netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) // carrier down and transmit queues stopped until packet from device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) netif_carrier_off(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) netif_tx_stop_all_queues(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) dev_info(&intf->dev, "Apple iPhone USB Ethernet device attached\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) err_register_netdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) ipheth_free_urbs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) err_alloc_urbs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) err_get_macaddr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) err_alloc_ctrl_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) kfree(dev->ctrl_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) err_endpoints:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) free_netdev(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static void ipheth_disconnect(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct ipheth_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) dev = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (dev != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) unregister_netdev(dev->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) ipheth_kill_urbs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) ipheth_free_urbs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) kfree(dev->ctrl_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) free_netdev(dev->net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) usb_set_intfdata(intf, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) dev_info(&intf->dev, "Apple iPhone USB Ethernet now disconnected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) static struct usb_driver ipheth_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) .name = "ipheth",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) .probe = ipheth_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) .disconnect = ipheth_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) .id_table = ipheth_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) .disable_hub_initiated_lpm = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) module_usb_driver(ipheth_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) MODULE_AUTHOR("Diego Giagio <diego@giagio.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) MODULE_DESCRIPTION("Apple iPhone USB Ethernet driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) MODULE_LICENSE("Dual BSD/GPL");