📄 rtmp_main.c
字号:
* complete_and_exit() goes even further than this -- it is safe in * the case that the thread of the caller is going away (not just * the structure) -- this is necessary for the module-remove case. * This is important in preemption kernels, which transfer the flow * of execution immediately upon a complete(). */ complete_and_exit (&pAd->CmdThreadNotify, 0); DBGPRINT(RT_DEBUG_TRACE, "<---RTUSBCmdThread\n");}#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)static void *usb_rtusb_probe(struct usb_device *dev, UINT interface, const struct usb_device_id *id_table){ PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL; int i; struct net_device *netdev; int res = -ENOMEM; DBGPRINT(RT_DEBUG_TRACE, "%s-->\n", __FUNCTION__); for (i = 0; i < rtusb_usb_id_len; i++) { if (dev->descriptor.idVendor == rtusb_usb_id[i].idVendor && dev->descriptor.idProduct == rtusb_usb_id[i].idProduct) { printk("idVendor = 0x%x, idProduct = 0x%x \n",dev->descriptor.idVendor, dev->descriptor.idProduct); break; } } if (i == rtusb_usb_id_len) { printk("Device Descriptor not matching\n"); return NULL; } netdev = alloc_etherdev(sizeof(PRTMP_ADAPTER)); if(!netdev) { printk("alloc_etherdev failed\n"); MOD_DEC_USE_COUNT; usb_dec_dev_use(dev); return NULL; } netdev->priv = (PVOID)vmalloc(sizeof(RTMP_ADAPTER)); pAd = netdev->priv; if(pAd == NULL) { kfree(pAd->net_dev); printk("vmalloc failed\n"); return NULL; } NdisZeroMemory(pAd, sizeof(RTMP_ADAPTER)); pAd->net_dev = netdev; netif_stop_queue(netdev); pAd->config = dev->config; pAd->pUsb_Dev= dev; SET_MODULE_OWNER(pAd->net_dev); ether_setup(pAd->net_dev); netdev->priv_flags = INT_MAIN; netdev->open = usb_rtusb_open; netdev->hard_start_xmit = usb_rtusb_sendpkt; netdev->stop = usb_rtusb_close; netdev->priv = pAd; netdev->get_stats = rt73_get_ether_stats;#if (WIRELESS_EXT >= 12)#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) netdev->get_wireless_stats = rt73_get_wireless_stats;#endif netdev->wireless_handlers = (struct iw_handler_def *) &rt73_iw_handler_def;#endif netdev->do_ioctl = rt73_ioctl; pAd->net_dev->hard_header_len = 14; pAd->net_dev->mtu = 1500; pAd->net_dev->addr_len = 6; pAd->net_dev->weight = 64; OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED); {// find available int i=0; char slot_name[IFNAMSIZ]; struct net_device *device; struct usb_interface *ifp= &dev->actconfig->interface[interface]; // get interface from system struct usb_interface_descriptor *as; struct usb_endpoint_descriptor *ep; for (i = 0; i < 8; i++) { sprintf(slot_name, "rausb%d", i); read_lock_bh(&dev_base_lock); // avoid multiple init//Benson modify -->#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,21) for (device = dev_base; device != NULL; device = device->next)#else for (device = first_net_device(); device != NULL; device = next_net_device(device))#endif//<-- Benson modify { if (strncmp(device->name, slot_name, 4) == 0) { break; } } read_unlock_bh(&dev_base_lock); if(device == NULL) break; } if(i == 8) { DBGPRINT(RT_DEBUG_ERROR, "No available slot name\n"); return NULL; } sprintf(pAd->net_dev->name, "rausb%d", i); DBGPRINT(RT_DEBUG_ERROR, "usb device name %s\n",pAd->net_dev->name); /* get Max Packet Size from usb_dev endpoint */// ifp = dev->actconfig->interface + i; as = ifp->altsetting + ifp->act_altsetting; ep = as->endpoint; pAd->BulkOutMaxPacketSize = (USHORT)ep[i].wMaxPacketSize; DBGPRINT(RT_DEBUG_TRACE, "BulkOutMaxPacketSize %d\n", pAd->BulkOutMaxPacketSize); } res = register_netdev(pAd->net_dev); if (res) goto out; RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST); if (usb_rtusb_init_device(pAd->net_dev)!=NDIS_STATUS_SUCCESS) goto out; DBGPRINT(RT_DEBUG_TRACE, "%s<--\n", __FUNCTION__); return pAd; out: printk("register_netdev failed err=%d\n",res); return NULL;}//Disconnect function is called within exit routinestatic void usb_rtusb_disconnect(struct usb_device *dev, void *ptr){ PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) ptr; struct net_device *net_dev = NULL; DBGPRINT(RT_DEBUG_TRACE, "%s-->\n", __FUNCTION__); RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST); if (!pAd) return; if (pAd) usb_rtusb_close_device(pAd); // for debug, wait to show some messages to /proc system udelay(1); //After Add Thread implementation, Upon exec there, pAd->net_dev seems becomes NULL, //need to check why??? //assert(pAd->net_dev != NULL) net_dev = pAd->net_dev; if(pAd->net_dev != NULL) { printk("unregister_netdev()\n"); unregister_netdev (pAd->net_dev); } udelay(1); udelay(1); // free adapter vfree(pAd); if (net_dev != NULL) kfree(net_dev); while (MOD_IN_USE > 0) { MOD_DEC_USE_COUNT; } udelay(1); DBGPRINT(RT_DEBUG_TRACE, "%s<--\n", __FUNCTION__);}#elsestatic int usb_rtusb_probe (struct usb_interface *intf, const struct usb_device_id *id){ struct usb_device *dev = interface_to_usbdev(intf); PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL; int i; struct net_device *netdev; int res = -ENOMEM; DBGPRINT(RT_DEBUG_TRACE, "%s-->\n", __FUNCTION__); usb_get_dev(dev); for (i = 0; i < rtusb_usb_id_len; i++) { if (dev->descriptor.idVendor == rtusb_usb_id[i].idVendor && dev->descriptor.idProduct == rtusb_usb_id[i].idProduct) { printk("idVendor = 0x%x, idProduct = 0x%x \n",dev->descriptor.idVendor, dev->descriptor.idProduct); break; } } if (i == rtusb_usb_id_len) { printk("Device Descriptor not matching\n"); return res; } netdev = alloc_etherdev(sizeof(PRTMP_ADAPTER)); if(!netdev) { printk("alloc_etherdev failed\n"); return res; } netdev->priv = (PVOID)vmalloc(sizeof(RTMP_ADAPTER)); pAd = netdev->priv; if(!pAd) { free_netdev(netdev); printk("vmalloc failed\n"); return res; } NdisZeroMemory(pAd, sizeof(RTMP_ADAPTER)); pAd->net_dev = netdev; netif_stop_queue(netdev); pAd->config = &dev->config->desc; pAd->pUsb_Dev = dev; SET_MODULE_OWNER(pAd->net_dev); ether_setup(pAd->net_dev); netdev->priv_flags = INT_MAIN; netdev->open = usb_rtusb_open; netdev->stop = usb_rtusb_close; netdev->priv = pAd; netdev->hard_start_xmit = usb_rtusb_sendpkt; netdev->get_stats = rt73_get_ether_stats;#if (WIRELESS_EXT >= 12)#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) netdev->get_wireless_stats = rt73_get_wireless_stats;#endif netdev->wireless_handlers = (struct iw_handler_def *) &rt73_iw_handler_def;#endif netdev->do_ioctl = rt73_ioctl; pAd->net_dev->hard_header_len = 14; pAd->net_dev->mtu = 1500; pAd->net_dev->addr_len = 6; pAd->net_dev->weight = 64; OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED); pAd->MLMEThr_pid= -1; pAd->RTUSBCmdThr_pid= -1;#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) SET_ETHTOOL_OPS(pAd->net_dev, &rt73_ethtool_ops);#endif#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) SET_NETDEV_DEV(pAd->net_dev, &(intf->dev));#endif {// find available int i=0; char slot_name[IFNAMSIZ]; struct usb_host_interface *iface_desc; struct usb_endpoint_descriptor *endpoint; for (i = 0; i < 8; i++) { sprintf(slot_name, "rausb%d", i);#if 1 if(dev_get_by_name(slot_name)==NULL) break;#else read_lock_bh(&dev_base_lock); // avoid multiple init//Benson modify -->#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,21) for (device = dev_base; device != NULL; device = device->next)#else for (device = first_net_device(); device != NULL; device = next_net_device(device))#endif//<-- Benson modify { if (strncmp(device->name, slot_name, 4) == 0) { break; } } read_unlock_bh(&dev_base_lock); if(device == NULL) break;#endif } if(i == 8) { DBGPRINT(RT_DEBUG_ERROR, "No available slot name\n"); return res; } sprintf(pAd->net_dev->name, "rausb%d", i); DBGPRINT(RT_DEBUG_ERROR, "usb device name %s\n", pAd->net_dev->name); /* get the active interface descriptor */ iface_desc = intf->cur_altsetting; /* check out the endpoint: it has to be Interrupt & IN */ endpoint = &iface_desc->endpoint[i].desc; /* get Max Packet Size from endpoint */ pAd->BulkOutMaxPacketSize = (USHORT)endpoint->wMaxPacketSize; DBGPRINT(RT_DEBUG_TRACE, "BulkOutMaxPacketSize %d\n", pAd->BulkOutMaxPacketSize); } res = register_netdev(pAd->net_dev); if (res) goto out; usb_set_intfdata(intf, pAd); RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST); if (usb_rtusb_init_device(pAd->net_dev)!=NDIS_STATUS_SUCCESS) goto out; pAd->ProbeFinish = TRUE; //RTUSBBulkReceive(pAd); DBGPRINT(RT_DEBUG_TRACE, "%s<--\n", __FUNCTION__); return 0; out: printk("register_netdev failed err=%d\n",res); free_netdev(netdev); return -1;}static void usb_rtusb_disconnect(struct usb_interface *intf){ struct net_device *net_dev = NULL; struct usb_device *dev = interface_to_usbdev(intf); PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL; pAd = usb_get_intfdata(intf); DBGPRINT(RT_DEBUG_TRACE, "%s-->\n", __FUNCTION__); usb_set_intfdata(intf, NULL); RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST); DBGPRINT(RT_DEBUG_ERROR, "unregister usbnet usb-%s-%s\n", dev->bus->bus_name, dev->devpath); if (!pAd) return; if (pAd) usb_rtusb_close_device(pAd); // for debug, wait to show some messages to /proc system udelay(1); //After Add Thread implementation, Upon exec there, pAd->net_dev seems becomes NULL, //need to check why??? //assert(pAd->net_dev != NULL) net_dev = pAd->net_dev; if(pAd->net_dev!= NULL) { printk("unregister_netdev()\n"); unregister_netdev (pAd->net_dev); } udelay(1); flush_scheduled_work (); udelay(1); // free adapter vfree(pAd); if (net_dev != NULL) free_netdev(net_dev); usb_put_dev(dev); udelay(1); DBGPRINT(RT_DEBUG_TRACE, "%s<--\n", __FUNCTION__);}#endif//// Driver module load function//INT __init usb_rtusb_init(void){ DBGPRINT(RT_DEBUG_TRACE, "%s-->\n", __FUNCTION__); return usb_register(&rtusb_driver);}//// Driver module unload function//VOID __exit usb_rtusb_exit(void){ udelay(1); udelay(1); usb_deregister(&rtusb_driver); DBGPRINT(RT_DEBUG_TRACE, "%s<--\n", __FUNCTION__);}/**************************************/module_init(usb_rtusb_init);module_exit(usb_rtusb_exit);/**************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -