^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * phonet.c -- USB CDC Phonet host driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2008-2009 Nokia Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Rémi Denis-Courmont
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/usb/cdc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/if_phonet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/phonet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define PN_MEDIA_USB 0x1B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static const unsigned rxq_size = 17;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct usbpn_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct usb_interface *intf, *data_intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct usb_device *usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned int tx_pipe, rx_pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u8 active_setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) u8 disconnected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned tx_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) spinlock_t tx_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) spinlock_t rx_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct sk_buff *rx_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct urb *urbs[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static void tx_complete(struct urb *req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void rx_complete(struct urb *req);
^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) * Network device callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static netdev_tx_t usbpn_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct usbpn_dev *pnd = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct urb *req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (skb->protocol != htons(ETH_P_PHONET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) req = usb_alloc_urb(0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) usb_fill_bulk_urb(req, pnd->usb, pnd->tx_pipe, skb->data, skb->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) tx_complete, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) req->transfer_flags = URB_ZERO_PACKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) err = usb_submit_urb(req, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) usb_free_urb(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) spin_lock_irqsave(&pnd->tx_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pnd->tx_queue++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (pnd->tx_queue >= dev->tx_queue_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) netif_stop_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) spin_unlock_irqrestore(&pnd->tx_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static void tx_complete(struct urb *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct sk_buff *skb = req->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct net_device *dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct usbpn_dev *pnd = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int status = req->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) dev->stats.tx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) dev->stats.tx_aborted_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) dev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) dev_dbg(&dev->dev, "TX error (%d)\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) dev->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) spin_lock_irqsave(&pnd->tx_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pnd->tx_queue--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) netif_wake_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) spin_unlock_irqrestore(&pnd->tx_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) dev_kfree_skb_any(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) usb_free_urb(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int rx_submit(struct usbpn_dev *pnd, struct urb *req, gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct net_device *dev = pnd->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) page = __dev_alloc_page(gfp_flags | __GFP_NOMEMALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) usb_fill_bulk_urb(req, pnd->usb, pnd->rx_pipe, page_address(page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) PAGE_SIZE, rx_complete, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) req->transfer_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) err = usb_submit_urb(req, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dev_dbg(&dev->dev, "RX submit error (%d)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static void rx_complete(struct urb *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct net_device *dev = req->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct usbpn_dev *pnd = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct page *page = virt_to_page(req->transfer_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int status = req->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) spin_lock_irqsave(&pnd->rx_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) skb = pnd->rx_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) skb = pnd->rx_skb = netdev_alloc_skb(dev, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (likely(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Can't use pskb_pull() on page in IRQ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) skb_put_data(skb, page_address(page), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) page, 1, req->actual_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) page, 0, req->actual_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (req->actual_length < PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) pnd->rx_skb = NULL; /* Last fragment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) spin_unlock_irqrestore(&pnd->rx_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) skb->protocol = htons(ETH_P_PHONET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) skb_reset_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) __skb_pull(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) dev->stats.rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) dev->stats.rx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) netif_rx(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto resubmit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) case -EOVERFLOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) dev->stats.rx_over_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) dev_dbg(&dev->dev, "RX overflow\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) case -EILSEQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) dev->stats.rx_crc_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) dev->stats.rx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) resubmit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) rx_submit(pnd, req, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static int usbpn_close(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int usbpn_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct usbpn_dev *pnd = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) err = usb_set_interface(pnd->usb, num, pnd->active_setting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) for (i = 0; i < rxq_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct urb *req = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (!req || rx_submit(pnd, req, GFP_KERNEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) usb_free_urb(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) usbpn_close(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) pnd->urbs[i] = req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) netif_wake_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static int usbpn_close(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct usbpn_dev *pnd = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) netif_stop_queue(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) for (i = 0; i < rxq_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct urb *req = pnd->urbs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) usb_kill_urb(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) usb_free_urb(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) pnd->urbs[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return usb_set_interface(pnd->usb, num, !pnd->active_setting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static int usbpn_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct if_phonet_req *req = (struct if_phonet_req *)ifr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) case SIOCPNGAUTOCONF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) req->ifr_phonet_autoconf.device = PN_DEV_PC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static const struct net_device_ops usbpn_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) .ndo_open = usbpn_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) .ndo_stop = usbpn_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) .ndo_start_xmit = usbpn_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) .ndo_do_ioctl = usbpn_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static void usbpn_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) dev->features = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) dev->netdev_ops = &usbpn_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) dev->header_ops = &phonet_header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) dev->type = ARPHRD_PHONET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) dev->flags = IFF_POINTOPOINT | IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) dev->mtu = PHONET_MAX_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) dev->min_mtu = PHONET_MIN_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) dev->max_mtu = PHONET_MAX_MTU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) dev->hard_header_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) dev->dev_addr[0] = PN_MEDIA_USB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) dev->addr_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) dev->tx_queue_len = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * USB driver callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static const struct usb_device_id usbpn_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) .match_flags = USB_DEVICE_ID_MATCH_VENDOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) | USB_DEVICE_ID_MATCH_INT_CLASS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) | USB_DEVICE_ID_MATCH_INT_SUBCLASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) .idVendor = 0x0421, /* Nokia */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) .bInterfaceClass = USB_CLASS_COMM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) .bInterfaceSubClass = 0xFE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) MODULE_DEVICE_TABLE(usb, usbpn_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static struct usb_driver usbpn_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static int usbpn_probe(struct usb_interface *intf, const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static const char ifname[] = "usbpn%d";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) const struct usb_cdc_union_desc *union_header = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) const struct usb_host_interface *data_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct usb_interface *data_intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct usb_device *usbdev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct usbpn_dev *pnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) int phonet = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) int len, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct usb_cdc_parsed_header hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) data = intf->altsetting->extra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) len = intf->altsetting->extralen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) cdc_parse_cdc_header(&hdr, intf, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) union_header = hdr.usb_cdc_union_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) phonet = hdr.phonet_magic_present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (!union_header || !phonet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) data_intf = usb_ifnum_to_if(usbdev, union_header->bSlaveInterface0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (data_intf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) /* Data interface has one inactive and one active setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (data_intf->num_altsetting != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (data_intf->altsetting[0].desc.bNumEndpoints == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) data_intf->altsetting[1].desc.bNumEndpoints == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) data_desc = data_intf->altsetting + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (data_intf->altsetting[0].desc.bNumEndpoints == 2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) data_intf->altsetting[1].desc.bNumEndpoints == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) data_desc = data_intf->altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) dev = alloc_netdev(struct_size(pnd, urbs, rxq_size), ifname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) NET_NAME_UNKNOWN, usbpn_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) pnd = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) SET_NETDEV_DEV(dev, &intf->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) pnd->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) pnd->usb = usbdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) pnd->intf = intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) pnd->data_intf = data_intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) spin_lock_init(&pnd->tx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) spin_lock_init(&pnd->rx_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* Endpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (usb_pipein(data_desc->endpoint[0].desc.bEndpointAddress)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) data_desc->endpoint[0].desc.bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) pnd->tx_pipe = usb_sndbulkpipe(usbdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) data_desc->endpoint[1].desc.bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) data_desc->endpoint[1].desc.bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) pnd->tx_pipe = usb_sndbulkpipe(usbdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) data_desc->endpoint[0].desc.bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) pnd->active_setting = data_desc - data_intf->altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) err = usb_driver_claim_interface(&usbpn_driver, data_intf, pnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) /* Force inactive mode until the network device is brought UP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) usb_set_interface(usbdev, union_header->bSlaveInterface0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) !pnd->active_setting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) usb_set_intfdata(intf, pnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) err = register_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) /* Set disconnected flag so that disconnect() returns early. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) pnd->disconnected = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) usb_driver_release_interface(&usbpn_driver, data_intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) dev_dbg(&dev->dev, "USB CDC Phonet device found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) usb_set_intfdata(intf, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) free_netdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static void usbpn_disconnect(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct usbpn_dev *pnd = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (pnd->disconnected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) pnd->disconnected = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) usb_driver_release_interface(&usbpn_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) (pnd->intf == intf) ? pnd->data_intf : pnd->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) unregister_netdev(pnd->dev);
^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 struct usb_driver usbpn_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) .name = "cdc_phonet",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) .probe = usbpn_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) .disconnect = usbpn_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) .id_table = usbpn_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) .disable_hub_initiated_lpm = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) module_usb_driver(usbpn_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) MODULE_AUTHOR("Remi Denis-Courmont");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) MODULE_DESCRIPTION("USB CDC Phonet host interface");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) MODULE_LICENSE("GPL");