📄 kaweth.c
字号:
return &((struct kaweth_device *)dev->priv)->stats;}/**************************************************************** * kaweth_tx_timeout ****************************************************************/static void kaweth_tx_timeout(struct net_device *net){ struct kaweth_device *kaweth = net->priv; kaweth_warn("%s: Tx timed out. Resetting.", net->name); kaweth->stats.tx_errors++; net->trans_start = jiffies; usb_unlink_urb(kaweth->tx_urb);}/**************************************************************** * kaweth_probe ****************************************************************/static int kaweth_probe( struct usb_interface *intf, const struct usb_device_id *id /* from id_table */ ){ struct usb_device *dev = interface_to_usbdev(intf); struct kaweth_device *kaweth; struct net_device *netdev; const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; int result = 0; kaweth_dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x", dev->devnum, (int)dev->descriptor.idVendor, (int)dev->descriptor.idProduct, (int)dev->descriptor.bcdDevice); kaweth_dbg("Device at %p", dev); kaweth_dbg("Descriptor length: %x type: %x", (int)dev->descriptor.bLength, (int)dev->descriptor.bDescriptorType); if(!(kaweth = kmalloc(sizeof(struct kaweth_device), GFP_KERNEL))) return -ENOMEM; memset(kaweth, 0, sizeof(struct kaweth_device)); kaweth->dev = dev; spin_lock_init(&kaweth->device_lock); init_waitqueue_head(&kaweth->term_wait); kaweth_dbg("Resetting."); kaweth_reset(kaweth); /* * If high byte of bcdDevice is nonzero, firmware is already * downloaded. Don't try to do it again, or we'll hang the device. */ if (dev->descriptor.bcdDevice >> 8) { kaweth_info("Firmware present in device."); } else { /* Download the firmware */ kaweth_info("Downloading firmware..."); kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL); if ((result = kaweth_download_firmware(kaweth, kaweth_new_code, len_kaweth_new_code, 100, 2)) < 0) { kaweth_err("Error downloading firmware (%d)", result); free_page((unsigned long)kaweth->firmware_buf); kfree(kaweth); return -EIO; } if ((result = kaweth_download_firmware(kaweth, kaweth_new_code_fix, len_kaweth_new_code_fix, 100, 3)) < 0) { kaweth_err("Error downloading firmware fix (%d)", result); free_page((unsigned long)kaweth->firmware_buf); kfree(kaweth); return -EIO; } if ((result = kaweth_download_firmware(kaweth, kaweth_trigger_code, len_kaweth_trigger_code, 126, 2)) < 0) { kaweth_err("Error downloading trigger code (%d)", result); free_page((unsigned long)kaweth->firmware_buf); kfree(kaweth); return -EIO; } if ((result = kaweth_download_firmware(kaweth, kaweth_trigger_code_fix, len_kaweth_trigger_code_fix, 126, 3)) < 0) { kaweth_err("Error downloading trigger code fix (%d)", result); free_page((unsigned long)kaweth->firmware_buf); kfree(kaweth); return -EIO; } if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) { kaweth_err("Error triggering firmware (%d)", result); free_page((unsigned long)kaweth->firmware_buf); kfree(kaweth); return -EIO; } /* Device will now disappear for a moment... */ kaweth_info("Firmware loaded. I'll be back..."); free_page((unsigned long)kaweth->firmware_buf); kfree(kaweth); return -EIO; } result = kaweth_read_configuration(kaweth); if(result < 0) { kaweth_err("Error reading configuration (%d), no net device created", result); kfree(kaweth); return -EIO; } kaweth_info("Statistics collection: %x", kaweth->configuration.statistics_mask); kaweth_info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1)); kaweth_info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size)); kaweth_info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", (int)kaweth->configuration.hw_addr[0], (int)kaweth->configuration.hw_addr[1], (int)kaweth->configuration.hw_addr[2], (int)kaweth->configuration.hw_addr[3], (int)kaweth->configuration.hw_addr[4], (int)kaweth->configuration.hw_addr[5]); if(!memcmp(&kaweth->configuration.hw_addr, &bcast_addr, sizeof(bcast_addr))) { kaweth_err("Firmware not functioning properly, no net device created"); kfree(kaweth); return -EIO; } if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) { kaweth_dbg("Error setting URB size"); goto err_no_netdev; } if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) { kaweth_err("Error setting SOFS wait"); goto err_no_netdev; } result = kaweth_set_receive_filter(kaweth, KAWETH_PACKET_FILTER_DIRECTED | KAWETH_PACKET_FILTER_BROADCAST | KAWETH_PACKET_FILTER_MULTICAST); if(result < 0) { kaweth_err("Error setting receive filter"); kfree(kaweth); return -EIO; } kaweth_dbg("Initializing net device."); if (!(netdev = alloc_etherdev(0))) { kfree(kaweth); return -ENOMEM; } kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL); if (!kaweth->tx_urb) goto err_no_urb; kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL); if (!kaweth->rx_urb) goto err_only_tx; kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL); if (!kaweth->irq_urb) goto err_tx_and_rx; kaweth->intbuffer = usb_buffer_alloc( kaweth->dev, INTBUFFERSIZE, GFP_KERNEL, &kaweth->intbufferhandle); if (!kaweth->intbuffer) goto err_tx_and_rx_and_irq; kaweth->rx_buf = usb_buffer_alloc( kaweth->dev, KAWETH_BUF_SIZE, GFP_KERNEL, &kaweth->rxbufferhandle); if (!kaweth->rx_buf) goto err_all_but_rxbuf; kaweth->net = netdev; memcpy(kaweth->net->broadcast, &bcast_addr, sizeof(bcast_addr)); memcpy(kaweth->net->dev_addr, &kaweth->configuration.hw_addr, sizeof(kaweth->configuration.hw_addr)); kaweth->net->priv = kaweth; kaweth->net->open = kaweth_open; kaweth->net->stop = kaweth_close; kaweth->net->watchdog_timeo = KAWETH_TX_TIMEOUT; kaweth->net->tx_timeout = kaweth_tx_timeout; kaweth->net->do_ioctl = kaweth_ioctl; kaweth->net->hard_start_xmit = kaweth_start_xmit; kaweth->net->set_multicast_list = kaweth_set_rx_mode; kaweth->net->get_stats = kaweth_netdev_stats; kaweth->net->mtu = le16_to_cpu(kaweth->configuration.segment_size); memset(&kaweth->stats, 0, sizeof(kaweth->stats)); INIT_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl, (void *)kaweth); SET_MODULE_OWNER(netdev); usb_set_intfdata(intf, kaweth);#if 0// dma_supported() is deeply broken on almost all architectures if (dma_supported (&intf->dev, 0xffffffffffffffffULL)) kaweth->net->features |= NETIF_F_HIGHDMA;#endif SET_NETDEV_DEV(netdev, &intf->dev); if (register_netdev(netdev) != 0) { kaweth_err("Error registering netdev."); goto err_intfdata; } kaweth_info("kaweth interface created at %s", kaweth->net->name); kaweth_dbg("Kaweth probe returning."); return 0;err_intfdata: usb_set_intfdata(intf, NULL); usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);err_all_but_rxbuf: usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);err_tx_and_rx_and_irq: usb_free_urb(kaweth->irq_urb);err_tx_and_rx: usb_free_urb(kaweth->rx_urb);err_only_tx: usb_free_urb(kaweth->tx_urb);err_no_urb: free_netdev(netdev);err_no_netdev: kfree(kaweth); return -EIO;}/**************************************************************** * kaweth_disconnect ****************************************************************/static void kaweth_disconnect(struct usb_interface *intf){ struct kaweth_device *kaweth = usb_get_intfdata(intf); kaweth_info("Unregistering"); usb_set_intfdata(intf, NULL); if (!kaweth) { kaweth_warn("unregistering non-existant device"); return; } kaweth->removed = 1; usb_unlink_urb(kaweth->irq_urb); usb_unlink_urb(kaweth->rx_urb); /* we need to wait for the urb to be cancelled, if it is active */ spin_lock(&kaweth->device_lock); if (usb_unlink_urb(kaweth->tx_urb) == -EINPROGRESS) { spin_unlock(&kaweth->device_lock); wait_event(kaweth->term_wait, kaweth->end); } else { spin_unlock(&kaweth->device_lock); } if(kaweth->net) { if(kaweth->net->flags & IFF_UP) { kaweth_dbg("Closing net device"); dev_close(kaweth->net); } kaweth_dbg("Unregistering net device"); unregister_netdev(kaweth->net); free_netdev(kaweth->net); } usb_free_urb(kaweth->rx_urb); usb_free_urb(kaweth->tx_urb); usb_free_urb(kaweth->irq_urb); usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle); usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle); kfree(kaweth);}// FIXME this completion stuff is a modified clone of// an OLD version of some stuff in usb.c ...struct usb_api_data { wait_queue_head_t wqh; int done;};/*-------------------------------------------------------------------* * completion handler for compatibility wrappers (sync control/bulk) * *-------------------------------------------------------------------*/static void usb_api_blocking_completion(struct urb *urb, struct pt_regs *regs){ struct usb_api_data *awd = (struct usb_api_data *)urb->context; awd->done=1; wake_up(&awd->wqh);}/*-------------------------------------------------------------------* * COMPATIBILITY STUFF * *-------------------------------------------------------------------*/// Starts urb and waits for completion or timeoutstatic int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length){ DECLARE_WAITQUEUE(wait, current); struct usb_api_data awd; int status; init_waitqueue_head(&awd.wqh); awd.done = 0; add_wait_queue(&awd.wqh, &wait); urb->context = &awd; status = usb_submit_urb(urb, GFP_NOIO); if (status) { // something went wrong usb_free_urb(urb); remove_wait_queue(&awd.wqh, &wait); return status; } set_current_state(TASK_UNINTERRUPTIBLE); while (timeout && !awd.done) { timeout = schedule_timeout(timeout); set_current_state(TASK_UNINTERRUPTIBLE); } set_current_state(TASK_RUNNING); remove_wait_queue(&awd.wqh, &wait); if (!timeout) { // timeout kaweth_warn("usb_control/bulk_msg: timeout"); usb_unlink_urb(urb); // remove urb safely status = -ETIMEDOUT; } else { status = urb->status; } if (actual_length) { *actual_length = urb->actual_length; } usb_free_urb(urb); return status;}/*-------------------------------------------------------------------*/// returns status (negative) or length (positive)static int kaweth_internal_control_msg(struct usb_device *usb_dev, unsigned int pipe, struct usb_ctrlrequest *cmd, void *data, int len, int timeout){ struct urb *urb; int retv; int length; urb = usb_alloc_urb(0, GFP_NOIO); if (!urb) return -ENOMEM; usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data, len, usb_api_blocking_completion, NULL); retv = usb_start_wait_urb(urb, timeout, &length); if (retv < 0) { return retv; } else { return length; }}/**************************************************************** * kaweth_init ****************************************************************/static int __init kaweth_init(void){ kaweth_dbg("Driver loading"); return usb_register(&kaweth_driver);}/**************************************************************** * kaweth_exit ****************************************************************/static void __exit kaweth_exit(void){ usb_deregister(&kaweth_driver);}module_init(kaweth_init);module_exit(kaweth_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -