Lines Matching +full:g +full:- +full:link

1 // SPDX-License-Identifier: GPL-2.0+
3 * u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
25 * This component encapsulates the Ethernet link glue needed to provide
26 * one (!) network link through the USB gadget stack, normally "usb0".
33 * Link level addressing is handled by this component using module
34 * parameters; if no such parameters are provided, random link level
35 * addresses are used. Each end of the link uses one address. The
39 * The driver which assembles each configuration using such a link is
41 * instance of is network link. (The network layer provides ways for
42 * this single "physical" link to be used by multiple virtual links.)
45 #define UETH__VERSION "29-May-2008"
88 /*-------------------------------------------------------------------------*/
94 /* for dual-speed hardware, use deeper queues at high/super speed */
97 if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH || in qlen()
98 gadget->speed >= USB_SPEED_SUPER)) in qlen()
104 /*-------------------------------------------------------------------------*/
116 printk(level "%s: " fmt , (d)->net->name , ## args)
139 /*-------------------------------------------------------------------------*/
147 strscpy(p->driver, "g_ether", sizeof(p->driver)); in eth_get_drvinfo()
148 strscpy(p->version, UETH__VERSION, sizeof(p->version)); in eth_get_drvinfo()
149 strscpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version)); in eth_get_drvinfo()
150 strscpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info)); in eth_get_drvinfo()
154 * - WOL (by tracking suspends and issuing remote wakeup)
155 * - msglevel (implies updated messaging)
156 * - ... probably more ethtool ops
166 if (test_and_set_bit(flag, &dev->todo)) in defer_kevent()
168 if (!schedule_work(&dev->work)) in defer_kevent()
179 struct usb_gadget *g = dev->gadget; in rx_submit() local
181 int retval = -ENOMEM; in rx_submit()
186 spin_lock_irqsave(&dev->lock, flags); in rx_submit()
187 if (dev->port_usb) in rx_submit()
188 out = dev->port_usb->out_ep; in rx_submit()
194 spin_unlock_irqrestore(&dev->lock, flags); in rx_submit()
195 return -ENOTCONN; in rx_submit()
202 * reads (e.g. DMA must be N*maxpacket), so for now don't trim a in rx_submit()
206 * pad to end-of-packet. That's potentially nice for speed, but in rx_submit()
210 size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA; in rx_submit()
211 size += dev->port_usb->header_len; in rx_submit()
213 if (g->quirk_ep_out_aligned_size) { in rx_submit()
214 size += out->maxpacket - 1; in rx_submit()
215 size -= size % out->maxpacket; in rx_submit()
218 if (dev->port_usb->is_fixed) in rx_submit()
219 size = max_t(size_t, size, dev->port_usb->fixed_out_len); in rx_submit()
220 spin_unlock_irqrestore(&dev->lock, flags); in rx_submit()
222 skb = __netdev_alloc_skb(dev->net, size + NET_IP_ALIGN, gfp_flags); in rx_submit()
232 if (likely(!dev->no_skb_reserve)) in rx_submit()
235 req->buf = skb->data; in rx_submit()
236 req->length = size; in rx_submit()
237 req->complete = rx_complete; in rx_submit()
238 req->context = skb; in rx_submit()
241 if (retval == -ENOMEM) in rx_submit()
245 DBG(dev, "rx submit --> %d\n", retval); in rx_submit()
248 spin_lock_irqsave(&dev->req_lock, flags); in rx_submit()
249 list_add(&req->list, &dev->rx_reqs); in rx_submit()
250 spin_unlock_irqrestore(&dev->req_lock, flags); in rx_submit()
257 struct sk_buff *skb = req->context, *skb2; in rx_complete()
258 struct eth_dev *dev = ep->driver_data; in rx_complete()
259 int status = req->status; in rx_complete()
265 skb_put(skb, req->actual); in rx_complete()
267 if (dev->unwrap) { in rx_complete()
270 spin_lock_irqsave(&dev->lock, flags); in rx_complete()
271 if (dev->port_usb) { in rx_complete()
272 status = dev->unwrap(dev->port_usb, in rx_complete()
274 &dev->rx_frames); in rx_complete()
277 status = -ENOTCONN; in rx_complete()
279 spin_unlock_irqrestore(&dev->lock, flags); in rx_complete()
281 skb_queue_tail(&dev->rx_frames, skb); in rx_complete()
285 skb2 = skb_dequeue(&dev->rx_frames); in rx_complete()
288 || ETH_HLEN > skb2->len in rx_complete()
289 || skb2->len > GETHER_MAX_ETH_FRAME_LEN) { in rx_complete()
290 dev->net->stats.rx_errors++; in rx_complete()
291 dev->net->stats.rx_length_errors++; in rx_complete()
292 DBG(dev, "rx length %d\n", skb2->len); in rx_complete()
296 skb2->protocol = eth_type_trans(skb2, dev->net); in rx_complete()
297 dev->net->stats.rx_packets++; in rx_complete()
298 dev->net->stats.rx_bytes += skb2->len; in rx_complete()
305 skb2 = skb_dequeue(&dev->rx_frames); in rx_complete()
309 /* software-driven interface shutdown */ in rx_complete()
310 case -ECONNRESET: /* unlink */ in rx_complete()
311 case -ESHUTDOWN: /* disconnect etc */ in rx_complete()
316 case -ECONNABORTED: /* endpoint reset */ in rx_complete()
317 DBG(dev, "rx %s reset\n", ep->name); in rx_complete()
324 case -EOVERFLOW: in rx_complete()
325 dev->net->stats.rx_over_errors++; in rx_complete()
329 dev->net->stats.rx_errors++; in rx_complete()
336 if (!netif_running(dev->net)) { in rx_complete()
338 spin_lock(&dev->req_lock); in rx_complete()
339 list_add(&req->list, &dev->rx_reqs); in rx_complete()
340 spin_unlock(&dev->req_lock); in rx_complete()
353 return -ENOMEM; in prealloc()
358 if (i-- == 0) in prealloc()
361 while (i--) { in prealloc()
364 return list_empty(list) ? -ENOMEM : 0; in prealloc()
365 list_add(&req->list, list); in prealloc()
374 next = req->list.next; in prealloc()
375 list_del(&req->list); in prealloc()
386 static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n) in alloc_requests() argument
390 spin_lock(&dev->req_lock); in alloc_requests()
391 status = prealloc(&dev->tx_reqs, link->in_ep, n); in alloc_requests()
394 status = prealloc(&dev->rx_reqs, link->out_ep, n); in alloc_requests()
401 spin_unlock(&dev->req_lock); in alloc_requests()
411 spin_lock_irqsave(&dev->req_lock, flags); in rx_fill()
412 while (!list_empty(&dev->rx_reqs)) { in rx_fill()
413 req = list_first_entry(&dev->rx_reqs, struct usb_request, list); in rx_fill()
414 list_del_init(&req->list); in rx_fill()
415 spin_unlock_irqrestore(&dev->req_lock, flags); in rx_fill()
422 spin_lock_irqsave(&dev->req_lock, flags); in rx_fill()
424 spin_unlock_irqrestore(&dev->req_lock, flags); in rx_fill()
431 if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) { in eth_work()
432 if (netif_running(dev->net)) in eth_work()
436 if (dev->todo) in eth_work()
437 DBG(dev, "work done, flags = 0x%lx\n", dev->todo); in eth_work()
442 struct sk_buff *skb = req->context; in tx_complete()
443 struct eth_dev *dev = ep->driver_data; in tx_complete()
445 switch (req->status) { in tx_complete()
447 dev->net->stats.tx_errors++; in tx_complete()
448 VDBG(dev, "tx err %d\n", req->status); in tx_complete()
450 case -ECONNRESET: /* unlink */ in tx_complete()
451 case -ESHUTDOWN: /* disconnect etc */ in tx_complete()
455 dev->net->stats.tx_bytes += skb->len; in tx_complete()
458 dev->net->stats.tx_packets++; in tx_complete()
460 spin_lock(&dev->req_lock); in tx_complete()
461 list_add(&req->list, &dev->tx_reqs); in tx_complete()
462 spin_unlock(&dev->req_lock); in tx_complete()
464 atomic_dec(&dev->tx_qlen); in tx_complete()
465 if (netif_carrier_ok(dev->net)) in tx_complete()
466 netif_wake_queue(dev->net); in tx_complete()
485 spin_lock_irqsave(&dev->lock, flags); in eth_start_xmit()
486 if (dev->port_usb) { in eth_start_xmit()
487 in = dev->port_usb->in_ep; in eth_start_xmit()
488 cdc_filter = dev->port_usb->cdc_filter; in eth_start_xmit()
493 spin_unlock_irqrestore(&dev->lock, flags); in eth_start_xmit()
503 u8 *dest = skb->data; in eth_start_xmit()
523 spin_lock_irqsave(&dev->req_lock, flags); in eth_start_xmit()
529 if (list_empty(&dev->tx_reqs)) { in eth_start_xmit()
530 spin_unlock_irqrestore(&dev->req_lock, flags); in eth_start_xmit()
534 req = list_first_entry(&dev->tx_reqs, struct usb_request, list); in eth_start_xmit()
535 list_del(&req->list); in eth_start_xmit()
538 if (list_empty(&dev->tx_reqs)) in eth_start_xmit()
540 spin_unlock_irqrestore(&dev->req_lock, flags); in eth_start_xmit()
546 if (dev->wrap) { in eth_start_xmit()
549 spin_lock_irqsave(&dev->lock, flags); in eth_start_xmit()
550 if (dev->port_usb) in eth_start_xmit()
551 skb = dev->wrap(dev->port_usb, skb); in eth_start_xmit()
552 spin_unlock_irqrestore(&dev->lock, flags); in eth_start_xmit()
557 if (dev->port_usb && in eth_start_xmit()
558 dev->port_usb->supports_multi_frame) in eth_start_xmit()
564 length = skb->len; in eth_start_xmit()
565 req->buf = skb->data; in eth_start_xmit()
566 req->context = skb; in eth_start_xmit()
567 req->complete = tx_complete; in eth_start_xmit()
570 if (dev->port_usb && in eth_start_xmit()
571 dev->port_usb->is_fixed && in eth_start_xmit()
572 length == dev->port_usb->fixed_in_len && in eth_start_xmit()
573 (length % in->maxpacket) == 0) in eth_start_xmit()
574 req->zero = 0; in eth_start_xmit()
576 req->zero = 1; in eth_start_xmit()
578 /* use zlp framing on tx for strict CDC-Ether conformance, in eth_start_xmit()
582 if (req->zero && !dev->zlp && (length % in->maxpacket) == 0) in eth_start_xmit()
585 req->length = length; in eth_start_xmit()
594 atomic_inc(&dev->tx_qlen); in eth_start_xmit()
600 dev->net->stats.tx_dropped++; in eth_start_xmit()
602 spin_lock_irqsave(&dev->req_lock, flags); in eth_start_xmit()
603 if (list_empty(&dev->tx_reqs)) in eth_start_xmit()
605 list_add(&req->list, &dev->tx_reqs); in eth_start_xmit()
606 spin_unlock_irqrestore(&dev->req_lock, flags); in eth_start_xmit()
611 /*-------------------------------------------------------------------------*/
621 atomic_set(&dev->tx_qlen, 0); in eth_start()
622 netif_wake_queue(dev->net); in eth_start()
628 struct gether *link; in eth_open() local
631 if (netif_carrier_ok(dev->net)) in eth_open()
634 spin_lock_irq(&dev->lock); in eth_open()
635 link = dev->port_usb; in eth_open()
636 if (link && link->open) in eth_open()
637 link->open(link); in eth_open()
638 spin_unlock_irq(&dev->lock); in eth_open()
652 dev->net->stats.rx_packets, dev->net->stats.tx_packets, in eth_stop()
653 dev->net->stats.rx_errors, dev->net->stats.tx_errors in eth_stop()
657 spin_lock_irqsave(&dev->lock, flags); in eth_stop()
658 if (dev->port_usb) { in eth_stop()
659 struct gether *link = dev->port_usb; in eth_stop() local
663 if (link->close) in eth_stop()
664 link->close(link); in eth_stop()
666 /* NOTE: we have no abort-queue primitive we could use in eth_stop()
669 * wrong, but that's a self-correcting error. in eth_stop()
675 in = link->in_ep->desc; in eth_stop()
676 out = link->out_ep->desc; in eth_stop()
677 usb_ep_disable(link->in_ep); in eth_stop()
678 usb_ep_disable(link->out_ep); in eth_stop()
681 link->in_ep->desc = in; in eth_stop()
682 link->out_ep->desc = out; in eth_stop()
683 usb_ep_enable(link->in_ep); in eth_stop()
684 usb_ep_enable(link->out_ep); in eth_stop()
687 spin_unlock_irqrestore(&dev->lock, flags); in eth_stop()
692 /*-------------------------------------------------------------------------*/
718 return -EINVAL; in get_ether_addr_str()
737 * gether_setup_name - initialize one ethernet-over-usb link
738 * @g: gadget to associated with these links
740 * host side of the link is recorded
744 * This sets up the single network link that may be exported by a
745 * gadget driver using this framework. The link layer addresses are
750 struct eth_dev *gether_setup_name(struct usb_gadget *g, in gether_setup_name() argument
761 return ERR_PTR(-ENOMEM); in gether_setup_name()
764 spin_lock_init(&dev->lock); in gether_setup_name()
765 spin_lock_init(&dev->req_lock); in gether_setup_name()
766 INIT_WORK(&dev->work, eth_work); in gether_setup_name()
767 INIT_LIST_HEAD(&dev->tx_reqs); in gether_setup_name()
768 INIT_LIST_HEAD(&dev->rx_reqs); in gether_setup_name()
770 skb_queue_head_init(&dev->rx_frames); in gether_setup_name()
773 dev->net = net; in gether_setup_name()
774 dev->qmult = qmult; in gether_setup_name()
775 snprintf(net->name, sizeof(net->name), "%s%%d", netname); in gether_setup_name()
778 net->addr_assign_type = NET_ADDR_RANDOM; in gether_setup_name()
779 dev_warn(&g->dev, in gether_setup_name()
782 net->addr_assign_type = NET_ADDR_SET; in gether_setup_name()
785 if (get_ether_addr(host_addr, dev->host_mac)) in gether_setup_name()
786 dev_warn(&g->dev, in gether_setup_name()
790 memcpy(ethaddr, dev->host_mac, ETH_ALEN); in gether_setup_name()
792 net->netdev_ops = &eth_netdev_ops; in gether_setup_name()
794 net->ethtool_ops = &ops; in gether_setup_name()
796 /* MTU range: 14 - 15412 */ in gether_setup_name()
797 net->min_mtu = ETH_HLEN; in gether_setup_name()
798 net->max_mtu = GETHER_MAX_MTU_SIZE; in gether_setup_name()
800 dev->gadget = g; in gether_setup_name()
801 SET_NETDEV_DEV(net, &g->dev); in gether_setup_name()
806 dev_dbg(&g->dev, "register_netdev failed, %d\n", status); in gether_setup_name()
810 INFO(dev, "MAC %pM\n", net->dev_addr); in gether_setup_name()
811 INFO(dev, "HOST MAC %pM\n", dev->host_mac); in gether_setup_name()
814 * two kinds of host-initiated state changes: in gether_setup_name()
815 * - iff DATA transfer is active, carrier is "on" in gether_setup_name()
816 * - tx queueing enabled if open *and* carrier is "on" in gether_setup_name()
832 return ERR_PTR(-ENOMEM); in gether_setup_name_default()
835 spin_lock_init(&dev->lock); in gether_setup_name_default()
836 spin_lock_init(&dev->req_lock); in gether_setup_name_default()
837 INIT_WORK(&dev->work, eth_work); in gether_setup_name_default()
838 INIT_LIST_HEAD(&dev->tx_reqs); in gether_setup_name_default()
839 INIT_LIST_HEAD(&dev->rx_reqs); in gether_setup_name_default()
841 skb_queue_head_init(&dev->rx_frames); in gether_setup_name_default()
844 dev->net = net; in gether_setup_name_default()
845 dev->qmult = QMULT_DEFAULT; in gether_setup_name_default()
846 snprintf(net->name, sizeof(net->name), "%s%%d", netname); in gether_setup_name_default()
848 eth_random_addr(dev->dev_mac); in gether_setup_name_default()
852 net->addr_assign_type = NET_ADDR_RANDOM; in gether_setup_name_default()
854 eth_random_addr(dev->host_mac); in gether_setup_name_default()
857 net->netdev_ops = &eth_netdev_ops; in gether_setup_name_default()
859 net->ethtool_ops = &ops; in gether_setup_name_default()
862 /* MTU range: 14 - 15412 */ in gether_setup_name_default()
863 net->min_mtu = ETH_HLEN; in gether_setup_name_default()
864 net->max_mtu = GETHER_MAX_MTU_SIZE; in gether_setup_name_default()
873 struct usb_gadget *g; in gether_register_netdev() local
876 if (!net->dev.parent) in gether_register_netdev()
877 return -EINVAL; in gether_register_netdev()
879 g = dev->gadget; in gether_register_netdev()
881 eth_hw_addr_set(net, dev->dev_mac); in gether_register_netdev()
885 dev_dbg(&g->dev, "register_netdev failed, %d\n", status); in gether_register_netdev()
888 INFO(dev, "HOST MAC %pM\n", dev->host_mac); in gether_register_netdev()
889 INFO(dev, "MAC %pM\n", dev->dev_mac); in gether_register_netdev()
891 /* two kinds of host-initiated state changes: in gether_register_netdev()
892 * - iff DATA transfer is active, carrier is "on" in gether_register_netdev()
893 * - tx queueing enabled if open *and* carrier is "on" in gether_register_netdev()
902 void gether_set_gadget(struct net_device *net, struct usb_gadget *g) in gether_set_gadget() argument
907 dev->gadget = g; in gether_set_gadget()
908 SET_NETDEV_DEV(net, &g->dev); in gether_set_gadget()
919 return -EINVAL; in gether_set_dev_addr()
920 memcpy(dev->dev_mac, new_addr, ETH_ALEN); in gether_set_dev_addr()
921 net->addr_assign_type = NET_ADDR_SET; in gether_set_dev_addr()
932 ret = get_ether_addr_str(dev->dev_mac, dev_addr, len); in gether_get_dev_addr()
949 return -EINVAL; in gether_set_host_addr()
950 memcpy(dev->host_mac, new_addr, ETH_ALEN); in gether_set_host_addr()
961 ret = get_ether_addr_str(dev->host_mac, host_addr, len); in gether_get_host_addr()
976 return -EINVAL; in gether_get_host_addr_cdc()
979 snprintf(host_addr, len, "%pm", dev->host_mac); in gether_get_host_addr_cdc()
990 memcpy(host_mac, dev->host_mac, ETH_ALEN); in gether_get_host_addr_u8()
999 dev->qmult = qmult; in gether_set_qmult()
1008 return dev->qmult; in gether_get_qmult()
1019 dev->ifname_set ? net->name : netdev_name(net)); in gether_get_ifname()
1031 if (name[len - 1] == '\n') in gether_set_ifname()
1032 len--; in gether_set_ifname()
1035 return -E2BIG; in gether_set_ifname()
1039 return -EINVAL; in gether_set_ifname()
1044 return -EINVAL; in gether_set_ifname()
1046 strncpy(net->name, tmp, sizeof(net->name)); in gether_set_ifname()
1047 dev->ifname_set = true; in gether_set_ifname()
1054 * gether_cleanup - remove Ethernet-over-USB device
1064 unregister_netdev(dev->net); in gether_cleanup()
1065 flush_work(&dev->work); in gether_cleanup()
1066 free_netdev(dev->net); in gether_cleanup()
1071 * gether_connect - notify network layer that USB link is active
1072 * @link: the USB link, set up with endpoints, descriptors matching
1083 * indicate some error code (negative errno), ep->driver_data values
1086 struct net_device *gether_connect(struct gether *link) in gether_connect() argument
1088 struct eth_dev *dev = link->ioport; in gether_connect()
1092 return ERR_PTR(-EINVAL); in gether_connect()
1094 link->in_ep->driver_data = dev; in gether_connect()
1095 result = usb_ep_enable(link->in_ep); in gether_connect()
1097 DBG(dev, "enable %s --> %d\n", in gether_connect()
1098 link->in_ep->name, result); in gether_connect()
1102 link->out_ep->driver_data = dev; in gether_connect()
1103 result = usb_ep_enable(link->out_ep); in gether_connect()
1105 DBG(dev, "enable %s --> %d\n", in gether_connect()
1106 link->out_ep->name, result); in gether_connect()
1111 result = alloc_requests(dev, link, qlen(dev->gadget, in gether_connect()
1112 dev->qmult)); in gether_connect()
1115 dev->zlp = link->is_zlp_ok; in gether_connect()
1116 dev->no_skb_reserve = gadget_avoids_skb_reserve(dev->gadget); in gether_connect()
1117 DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult)); in gether_connect()
1119 dev->header_len = link->header_len; in gether_connect()
1120 dev->unwrap = link->unwrap; in gether_connect()
1121 dev->wrap = link->wrap; in gether_connect()
1123 spin_lock(&dev->lock); in gether_connect()
1124 dev->port_usb = link; in gether_connect()
1125 if (netif_running(dev->net)) { in gether_connect()
1126 if (link->open) in gether_connect()
1127 link->open(link); in gether_connect()
1129 if (link->close) in gether_connect()
1130 link->close(link); in gether_connect()
1132 spin_unlock(&dev->lock); in gether_connect()
1134 netif_carrier_on(dev->net); in gether_connect()
1135 if (netif_running(dev->net)) in gether_connect()
1140 (void) usb_ep_disable(link->out_ep); in gether_connect()
1142 (void) usb_ep_disable(link->in_ep); in gether_connect()
1148 return dev->net; in gether_connect()
1153 * gether_disconnect - notify network layer that USB link is inactive
1154 * @link: the USB link, on which gether_connect() was called
1164 void gether_disconnect(struct gether *link) in gether_disconnect() argument
1166 struct eth_dev *dev = link->ioport; in gether_disconnect()
1175 netif_stop_queue(dev->net); in gether_disconnect()
1176 netif_carrier_off(dev->net); in gether_disconnect()
1182 usb_ep_disable(link->in_ep); in gether_disconnect()
1183 spin_lock(&dev->req_lock); in gether_disconnect()
1184 while (!list_empty(&dev->tx_reqs)) { in gether_disconnect()
1185 req = list_first_entry(&dev->tx_reqs, struct usb_request, list); in gether_disconnect()
1186 list_del(&req->list); in gether_disconnect()
1188 spin_unlock(&dev->req_lock); in gether_disconnect()
1189 usb_ep_free_request(link->in_ep, req); in gether_disconnect()
1190 spin_lock(&dev->req_lock); in gether_disconnect()
1192 spin_unlock(&dev->req_lock); in gether_disconnect()
1193 link->in_ep->desc = NULL; in gether_disconnect()
1195 usb_ep_disable(link->out_ep); in gether_disconnect()
1196 spin_lock(&dev->req_lock); in gether_disconnect()
1197 while (!list_empty(&dev->rx_reqs)) { in gether_disconnect()
1198 req = list_first_entry(&dev->rx_reqs, struct usb_request, list); in gether_disconnect()
1199 list_del(&req->list); in gether_disconnect()
1201 spin_unlock(&dev->req_lock); in gether_disconnect()
1202 usb_ep_free_request(link->out_ep, req); in gether_disconnect()
1203 spin_lock(&dev->req_lock); in gether_disconnect()
1205 spin_unlock(&dev->req_lock); in gether_disconnect()
1206 link->out_ep->desc = NULL; in gether_disconnect()
1208 /* finish forgetting about this USB link episode */ in gether_disconnect()
1209 dev->header_len = 0; in gether_disconnect()
1210 dev->unwrap = NULL; in gether_disconnect()
1211 dev->wrap = NULL; in gether_disconnect()
1213 spin_lock(&dev->lock); in gether_disconnect()
1214 dev->port_usb = NULL; in gether_disconnect()
1215 spin_unlock(&dev->lock); in gether_disconnect()