📄 usbnet.c
字号:
.description = "Sharp Zaurus SL-A300", .flags = FLAG_FRAMING_Z, .check_connect = always_connected, .tx_fixup = zaurus_tx_fixup, .in = 1, .out = 2, .epsize = 64,};static const struct driver_info zaurus_slb500_info = { /* Japanese B500 ~= US SL-5600 */ .description = "Sharp Zaurus SL-B500", .flags = FLAG_FRAMING_Z, .check_connect = always_connected, .tx_fixup = zaurus_tx_fixup, .in = 1, .out = 2, .epsize = 64,};// SL-5600 and C-700 are PXA based; should resemble A300#endif/*------------------------------------------------------------------------- * * Network Device Driver (peer link to "Host Device", from USB host) * *-------------------------------------------------------------------------*/static int usbnet_change_mtu (struct net_device *net, int new_mtu){ struct usbnet *dev = (struct usbnet *) net->priv; if (new_mtu <= MIN_PACKET || new_mtu > MAX_PACKET) return -EINVAL;#ifdef CONFIG_USB_NET1080 if (((dev->driver_info->flags) & FLAG_FRAMING_NC)) { if (FRAMED_SIZE (new_mtu) > MAX_PACKET) return -EINVAL; }#endif#ifdef CONFIG_USB_GENESYS if (((dev->driver_info->flags) & FLAG_FRAMING_GL) && new_mtu > GL_MAX_PACKET_LEN) return -EINVAL;#endif // no second zero-length packet read wanted after mtu-sized packets if (((new_mtu + sizeof (struct ethhdr)) % EP_SIZE (dev)) == 0) return -EDOM; net->mtu = new_mtu; return 0;}/*-------------------------------------------------------------------------*/static struct net_device_stats *usbnet_get_stats (struct net_device *net){ return &((struct usbnet *) net->priv)->stats;}/*-------------------------------------------------------------------------*//* urb completions may be in_irq; avoid doing real work then. */static void defer_bh (struct usbnet *dev, struct sk_buff *skb){ struct sk_buff_head *list = skb->list; unsigned long flags; spin_lock_irqsave (&list->lock, flags); __skb_unlink (skb, list); spin_unlock (&list->lock); spin_lock (&dev->done.lock); __skb_queue_tail (&dev->done, skb); if (dev->done.qlen == 1) tasklet_schedule (&dev->bh); spin_unlock_irqrestore (&dev->done.lock, flags);}/* some work can't be done in tasklets, so we use keventd * * NOTE: annoying asymmetry: if it's active, schedule_task() fails, * but tasklet_schedule() doesn't. hope the failure is rare. */static void defer_kevent (struct usbnet *dev, int work){ set_bit (work, &dev->flags); if (!schedule_task (&dev->kevent)) err ("%s: kevent %d may have been dropped", dev->net.name, work); else dbg ("%s: kevent %d scheduled", dev->net.name, work);}/*-------------------------------------------------------------------------*/static void rx_complete (struct urb *urb);static void rx_submit (struct usbnet *dev, struct urb *urb, int flags){ struct sk_buff *skb; struct skb_data *entry; int retval = 0; unsigned long lockflags; size_t size;#ifdef CONFIG_USB_NET1080 if (dev->driver_info->flags & FLAG_FRAMING_NC) size = FRAMED_SIZE (dev->net.mtu); else#endif#ifdef CONFIG_USB_GENESYS if (dev->driver_info->flags & FLAG_FRAMING_GL) size = GL_RCV_BUF_SIZE; else#endif#ifdef CONFIG_USB_ZAURUS if (dev->driver_info->flags & FLAG_FRAMING_Z) size = 6 + (sizeof (struct ethhdr) + dev->net.mtu); else#endif size = (sizeof (struct ethhdr) + dev->net.mtu); if ((skb = alloc_skb (size, flags)) == 0) { dbg ("no rx skb"); defer_kevent (dev, EVENT_RX_MEMORY); usb_free_urb (urb); return; } entry = (struct skb_data *) skb->cb; entry->urb = urb; entry->dev = dev; entry->state = rx_start; entry->length = 0; usb_fill_bulk_urb (urb, dev->udev, usb_rcvbulkpipe (dev->udev, dev->driver_info->in), skb->data, size, rx_complete, skb); urb->transfer_flags |= USB_ASYNC_UNLINK; spin_lock_irqsave (&dev->rxq.lock, lockflags); if (netif_running (&dev->net) && !test_bit (EVENT_RX_HALT, &dev->flags)) { switch (retval = SUBMIT_URB (urb, GFP_ATOMIC)){ case -EPIPE: defer_kevent (dev, EVENT_RX_HALT); break; case -ENOMEM: defer_kevent (dev, EVENT_RX_MEMORY); break; default: dbg ("%s rx submit, %d", dev->net.name, retval); tasklet_schedule (&dev->bh); break; case 0: __skb_queue_tail (&dev->rxq, skb); } } else { dbg ("rx: stopped"); retval = -ENOLINK; } spin_unlock_irqrestore (&dev->rxq.lock, lockflags); if (retval) { dev_kfree_skb_any (skb); usb_free_urb (urb); }}/*-------------------------------------------------------------------------*/static inline void rx_process (struct usbnet *dev, struct sk_buff *skb){ if (dev->driver_info->rx_fixup && !dev->driver_info->rx_fixup (dev, skb)) goto error; // else network stack removes extra byte if we forced a short packet if (skb->len) { int status; skb->dev = &dev->net; skb->protocol = eth_type_trans (skb, &dev->net); dev->stats.rx_packets++; dev->stats.rx_bytes += skb->len;#ifdef VERBOSE devdbg (dev, "< rx, len %d, type 0x%x", skb->len + sizeof (struct ethhdr), skb->protocol);#endif memset (skb->cb, 0, sizeof (struct skb_data)); status = netif_rx (skb); if (status != NET_RX_SUCCESS) devdbg (dev, "netif_rx status %d", status); } else { dbg ("drop");error: dev->stats.rx_errors++; skb_queue_tail (&dev->done, skb); }}/*-------------------------------------------------------------------------*/static void rx_complete (struct urb *urb){ struct sk_buff *skb = (struct sk_buff *) urb->context; struct skb_data *entry = (struct skb_data *) skb->cb; struct usbnet *dev = entry->dev; int urb_status = urb->status; skb_put (skb, urb->actual_length); entry->state = rx_done; entry->urb = 0; switch (urb_status) { // success case 0: if (MIN_PACKET > skb->len || skb->len > MAX_PACKET) { entry->state = rx_cleanup; dev->stats.rx_errors++; dev->stats.rx_length_errors++; dbg ("rx length %d", skb->len); } break; // stalls need manual reset. this is rare ... except that // when going through USB 2.0 TTs, unplug appears this way. // we avoid the highspeed version of the ETIMEOUT/EILSEQ // storm, recovering as needed. case -EPIPE: defer_kevent (dev, EVENT_RX_HALT); // FALLTHROUGH // software-driven interface shutdown case -ECONNRESET: // according to API spec case -ECONNABORTED: // some (now fixed?) UHCI bugs dbg ("%s rx shutdown, code %d", dev->net.name, urb_status); entry->state = rx_cleanup; // do urb frees only in the tasklet (UHCI has oopsed ...) entry->urb = urb; urb = 0; break; // data overrun ... flush fifo? case -EOVERFLOW: dev->stats.rx_over_errors++; // FALLTHROUGH default: // on unplug we get ETIMEDOUT (ohci) or EILSEQ (uhci) // until khubd sees its interrupt and disconnects us. // that can easily be hundreds of passes through here. entry->state = rx_cleanup; dev->stats.rx_errors++; dbg ("%s rx: status %d", dev->net.name, urb_status); break; } defer_bh (dev, skb); if (urb) { if (netif_running (&dev->net) && !test_bit (EVENT_RX_HALT, &dev->flags)) { rx_submit (dev, urb, GFP_ATOMIC); return; } usb_free_urb (urb); }#ifdef VERBOSE dbg ("no read resubmitted");#endif /* VERBOSE */}/*-------------------------------------------------------------------------*/// unlink pending rx/tx; completion handlers do all other cleanupstatic int unlink_urbs (struct sk_buff_head *q){ unsigned long flags; struct sk_buff *skb, *skbnext; int count = 0; spin_lock_irqsave (&q->lock, flags); for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) { struct skb_data *entry; struct urb *urb; int retval; entry = (struct skb_data *) skb->cb; urb = entry->urb; skbnext = skb->next; // during some PM-driven resume scenarios, // these (async) unlinks complete immediately retval = usb_unlink_urb (urb); if (retval != -EINPROGRESS && retval != 0) dbg ("unlink urb err, %d", retval); else count++; } spin_unlock_irqrestore (&q->lock, flags); return count;}/*-------------------------------------------------------------------------*/// precondition: never called in_interruptstatic int usbnet_stop (struct net_device *net){ struct usbnet *dev = (struct usbnet *) net->priv; int temp; DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); DECLARE_WAITQUEUE (wait, current); mutex_lock (&dev->mutex); netif_stop_queue (net); if (dev->msg_level >= 2) devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld", dev->stats.rx_packets, dev->stats.tx_packets, dev->stats.rx_errors, dev->stats.tx_errors ); // ensure there are no more active urbs add_wait_queue (&unlink_wakeup, &wait); dev->wait = &unlink_wakeup; temp = unlink_urbs (&dev->txq) + unlink_urbs (&dev->rxq); // maybe wait for deletions to finish. while (skb_queue_len (&dev->rxq) && skb_queue_len (&dev->txq) && skb_queue_len (&dev->done)) { set_current_state (TASK_UNINTERRUPTIBLE); schedule_timeout (UNLINK_TIMEOUT_JIFFIES); dbg ("waited for %d urb completions", temp); } dev->wait = 0; remove_wait_queue (&unlink_wakeup, &wait); mutex_unlock (&dev->mutex); return 0;}/*-------------------------------------------------------------------------*/// posts reads, and enables write queing// precondition: never called in_interruptstatic int usbnet_open (struct net_device *net){ struct usbnet *dev = (struct usbnet *) net->priv; int retval = 0; struct driver_info *info = dev->driver_info; mutex_lock (&dev->mutex); // put into "known safe" state if (info->reset && (retval = info->reset (dev)) < 0) { devinfo (dev, "open reset fail (%d) usbnet usb-%s-%s, %s", retval, dev->udev->bus->bus_name, dev->udev->devpath, info->description); goto done; } // insist peer be connected if (info->check_connect && (retval = info->check_connect (dev)) < 0) { devdbg (dev, "can't open; %d", retval); goto done; } netif_start_queue (net); if (dev->msg_level >= 2) devinfo (dev, "open: enable queueing " "(rx %d, tx %d) mtu %d %s framing", RX_QLEN, TX_QLEN, dev->net.mtu, (info->flags & (FLAG_FRAMING_NC | FLAG_FRAMING_GL)) ? ((info->flags & FLAG_FRAMING_NC) ? "NetChip" : "GeneSys") : "raw" ); // delay posting reads until we're fully open tasklet_schedule (&dev->bh);done: mutex_unlock (&dev->mutex); return retval;}/*-------------------------------------------------------------------------*/static int usbnet_ethtool_ioctl (struct net_device *net, void *useraddr){ struct usbnet *dev = (struct usbnet *) net->priv; u32 cmd; if (get_user (cmd, (u32 *)useraddr)) return -EFAULT; switch (cmd) { case ETHTOOL_GDRVINFO: { /* get driver info */ struct ethtool_drvinfo info; memset (&info, 0, sizeof info); info.cmd = ETHTOOL_GDRVINFO; strncpy (info.driver, driver_name, sizeof info.driver); strncpy (info.version, DRIVER_VERSION, sizeof info.version); strncpy (info.fw_version, dev->driver_info->description, sizeof info.fw_version); usb_make_path (dev->udev, info.bus_info, sizeof info.bus_info); if (copy_to_user (useraddr, &info, sizeof (info))) return -EFAULT; return 0; } case ETHTOOL_GLINK: /* get link status */ if (dev->driver_info->check_connect) { struct ethtool_value edata = { ETHTOOL_GLINK }; edata.data = dev->driver_info->check_connect (dev) == 0; if (copy_to_user (useraddr, &edata, sizeof (edata))) return -EFAULT; return 0; } break; case ETHTOOL_GMSGLVL: { /* get message-level */ struct ethtool_value edata = {ETHTOOL_GMSGLVL}; edata.data = dev->msg_level; if (copy_to_user (useraddr, &edata, sizeof (edata))) return -EFAULT; return 0; } case ETHTOOL_SMSGLVL: { /* set message-level */ struct ethtool_value edata; if (copy_from_user (&edata, useraddr, sizeof (edata))) return -EFAULT; dev->msg_level = edata.data; return 0; } /* could also map RINGPARAM to RX/TX QLEN */ } /* Note that the ethtool user space code requires EOPNOTSUPP */ return -EOPNOTSUPP;}static int usbnet_ioctl (struct net_device *net, struct ifreq *rq, int cmd){ switch (cmd) { case SIOCETHTOOL: return usbnet_ethtool_ioctl (net, (void *)rq->ifr_data); default: return -EOPNOTSUPP; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -