^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) * CAIF USB handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) ST-Ericsson AB 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author: Sjur Brendeland
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mii.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/usbnet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/netns/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/caif/caif_dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <net/caif/caif_layer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <net/caif/cfpkt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <net/caif/cfcnfg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define CFUSB_PAD_DESCR_SZ 1 /* Alignment descriptor length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define CFUSB_ALIGNMENT 4 /* Number of bytes to align. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define CFUSB_MAX_HEADLEN (CFUSB_PAD_DESCR_SZ + CFUSB_ALIGNMENT-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define STE_USB_VID 0x04cc /* USB Product ID for ST-Ericsson */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define STE_USB_PID_CAIF 0x230f /* Product id for CAIF Modems */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct cfusbl {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct cflayer layer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) u8 tx_eth_hdr[ETH_HLEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static bool pack_added;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int cfusbl_receive(struct cflayer *layr, struct cfpkt *pkt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) u8 hpad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Remove padding. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) cfpkt_extr_head(pkt, &hpad, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) cfpkt_extr_head(pkt, NULL, hpad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return layr->up->receive(layr->up, pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int cfusbl_transmit(struct cflayer *layr, struct cfpkt *pkt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct caif_payload_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) u8 hpad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) u8 zeros[CFUSB_ALIGNMENT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct cfusbl *usbl = container_of(layr, struct cfusbl, layer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) skb = cfpkt_tonative(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) skb->protocol = htons(ETH_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) info = cfpkt_info(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) hpad = (info->hdr_len + CFUSB_PAD_DESCR_SZ) & (CFUSB_ALIGNMENT - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (skb_headroom(skb) < ETH_HLEN + CFUSB_PAD_DESCR_SZ + hpad) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_warn("Headroom too small\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) memset(zeros, 0, hpad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) cfpkt_add_head(pkt, zeros, hpad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) cfpkt_add_head(pkt, &hpad, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) cfpkt_add_head(pkt, usbl->tx_eth_hdr, sizeof(usbl->tx_eth_hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return layr->dn->transmit(layr->dn, pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static void cfusbl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int phyid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (layr->up && layr->up->ctrlcmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) layr->up->ctrlcmd(layr->up, ctrl, layr->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static struct cflayer *cfusbl_create(int phyid, u8 ethaddr[ETH_ALEN],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) u8 braddr[ETH_ALEN])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct cfusbl *this = kmalloc(sizeof(struct cfusbl), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!this)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) caif_assert(offsetof(struct cfusbl, layer) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) memset(&this->layer, 0, sizeof(this->layer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) this->layer.receive = cfusbl_receive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) this->layer.transmit = cfusbl_transmit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) this->layer.ctrlcmd = cfusbl_ctrlcmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "usb%d", phyid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) this->layer.id = phyid;
^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) * Construct TX ethernet header:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * 0-5 destination address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * 5-11 source address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * 12-13 protocol type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], braddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ether_addr_copy(&this->tx_eth_hdr[ETH_ALEN], ethaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) this->tx_eth_hdr[12] = cpu_to_be16(ETH_P_802_EX1) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) this->tx_eth_hdr[13] = (cpu_to_be16(ETH_P_802_EX1) >> 8) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) pr_debug("caif ethernet TX-header dst:%pM src:%pM type:%02x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) this->tx_eth_hdr, this->tx_eth_hdr + ETH_ALEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) this->tx_eth_hdr[12], this->tx_eth_hdr[13]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return (struct cflayer *) this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void cfusbl_release(struct cflayer *layer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) kfree(layer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static struct packet_type caif_usb_type __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .type = cpu_to_be16(ETH_P_802_EX1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int cfusbl_device_notify(struct notifier_block *me, unsigned long what,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct net_device *dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct caif_dev_common common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct cflayer *layer, *link_support;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct usbnet *usbnet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct usb_device *usbdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* Check whether we have a NCM device, and find its VID/PID. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!(dev->dev.parent && dev->dev.parent->driver &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) strcmp(dev->dev.parent->driver->name, "cdc_ncm") == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) usbnet = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) usbdev = usbnet->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) pr_debug("USB CDC NCM device VID:0x%4x PID:0x%4x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) le16_to_cpu(usbdev->descriptor.idVendor),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) le16_to_cpu(usbdev->descriptor.idProduct));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* Check for VID/PID that supports CAIF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (!(le16_to_cpu(usbdev->descriptor.idVendor) == STE_USB_VID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) le16_to_cpu(usbdev->descriptor.idProduct) == STE_USB_PID_CAIF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (what == NETDEV_UNREGISTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (what != NETDEV_REGISTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) memset(&common, 0, sizeof(common));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) common.use_frag = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) common.use_fcs = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) common.use_stx = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) common.link_select = CAIF_LINK_HIGH_BANDW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) common.flowctrl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) link_support = cfusbl_create(dev->ifindex, dev->dev_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) dev->broadcast);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (!link_support)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (dev->num_tx_queues > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) pr_warn("USB device uses more than one tx queue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) res = caif_enroll_dev(dev, &common, link_support, CFUSB_MAX_HEADLEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) &layer, &caif_usb_type.func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!pack_added)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) dev_add_pack(&caif_usb_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) pack_added = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) strlcpy(layer->name, dev->name, sizeof(layer->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) cfusbl_release(link_support);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static struct notifier_block caif_device_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .notifier_call = cfusbl_device_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .priority = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int __init cfusbl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return register_netdevice_notifier(&caif_device_notifier);
^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) static void __exit cfusbl_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unregister_netdevice_notifier(&caif_device_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) dev_remove_pack(&caif_usb_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) module_init(cfusbl_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) module_exit(cfusbl_exit);