CDCEther.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:47k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. // Portions of this file taken from
  2. // Petko Manolov - Petkan (petkan@dce.bg)
  3. // from his driver pegasus.c
  4. /*
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18.  */
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/module.h>
  26. #include <linux/ethtool.h>
  27. #include <asm/uaccess.h>
  28. #define DEBUG
  29. #include <linux/usb.h>
  30. #include "CDCEther.h"
  31. #define SHORT_DRIVER_DESC "CDC Ethernet Class"
  32. #define DRIVER_VERSION "0.98.6"
  33. static const char *version = __FILE__ ": " DRIVER_VERSION " 7 Jan 2002 Brad Hards and another";
  34. // We only try to claim CDC Ethernet model devices */
  35. static struct usb_device_id CDCEther_ids[] = {
  36. { USB_INTERFACE_INFO(USB_CLASS_COMM, 6, 0) },
  37. { }
  38. };
  39. /*
  40.  * module parameter that provides an alternate upper limit on the
  41.  * number of multicast filters we use, with a default to use all
  42.  * the filters available to us. Note that the actual number used
  43.  * is the lesser of this parameter and the number returned in the
  44.  * descriptor for the particular device. See Table 41 of the CDC
  45.  * spec for more info on the descriptor limit.
  46.  */
  47. static int multicast_filter_limit = 32767;
  48. //////////////////////////////////////////////////////////////////////////////
  49. // Callback routines from USB device /////////////////////////////////////////
  50. //////////////////////////////////////////////////////////////////////////////
  51. static void read_bulk_callback( struct urb *urb )
  52. {
  53. ether_dev_t *ether_dev = urb->context;
  54. struct net_device *net;
  55. int count = urb->actual_length, res;
  56. struct sk_buff *skb;
  57. switch ( urb->status ) {
  58. case USB_ST_NOERROR:
  59. break;
  60. case USB_ST_URB_KILLED:
  61. return;
  62. default:
  63. dbg("rx status %d", urb->status);
  64. }
  65. // Sanity check
  66. if ( !ether_dev || !(ether_dev->flags & CDC_ETHER_RUNNING) ) {
  67. dbg("BULK IN callback but driver is not active!");
  68. return;
  69. }
  70. net = ether_dev->net;
  71. if ( !netif_device_present(net) ) {
  72. // Somebody killed our network interface...
  73. return;
  74. }
  75. if ( ether_dev->flags & CDC_ETHER_RX_BUSY ) {
  76. // Are we already trying to receive a frame???
  77. ether_dev->stats.rx_errors++;
  78. dbg("ether_dev Rx busy");
  79. return;
  80. }
  81. // We are busy, leave us alone!
  82. ether_dev->flags |= CDC_ETHER_RX_BUSY;
  83. switch ( urb->status ) {
  84. case USB_ST_NOERROR:
  85. break;
  86. case USB_ST_NORESPONSE:
  87. dbg( "no repsonse in BULK IN" );
  88. ether_dev->flags &= ~CDC_ETHER_RX_BUSY;
  89. break;
  90. default:
  91. dbg( "%s: RX status %d", net->name, urb->status );
  92. goto goon;
  93. }
  94. // Check to make sure we got some data...
  95. if ( !count ) {
  96. // We got no data!!!
  97. goto goon;
  98. }
  99. // Tell the kernel we want some memory
  100. if ( !(skb = dev_alloc_skb(count)) ) {
  101. // We got no receive buffer.
  102. goto goon;
  103. }
  104. // Here's where it came from
  105. skb->dev = net;
  106. // Now we copy it over
  107. eth_copy_and_sum(skb, ether_dev->rx_buff, count, 0);
  108. // Not sure
  109. skb_put(skb, count);
  110. // Not sure here either
  111. skb->protocol = eth_type_trans(skb, net);
  112. // Ship it off to the kernel
  113. netif_rx(skb);
  114. // update out statistics
  115. ether_dev->stats.rx_packets++;
  116. ether_dev->stats.rx_bytes += count;
  117. goon:
  118. // Prep the USB to wait for another frame
  119. FILL_BULK_URB( &ether_dev->rx_urb, ether_dev->usb,
  120. usb_rcvbulkpipe(ether_dev->usb, ether_dev->data_ep_in),
  121. ether_dev->rx_buff, ether_dev->wMaxSegmentSize, 
  122. read_bulk_callback, ether_dev );
  123. // Give this to the USB subsystem so it can tell us 
  124. // when more data arrives.
  125. if ( (res = usb_submit_urb(&ether_dev->rx_urb)) ) {
  126. warn("%s failed submit rx_urb %d", __FUNCTION__, res);
  127. }
  128. // We are no longer busy, show us the frames!!!
  129. ether_dev->flags &= ~CDC_ETHER_RX_BUSY;
  130. }
  131. static void write_bulk_callback( struct urb *urb )
  132. {
  133. ether_dev_t *ether_dev = urb->context;
  134. // Sanity check
  135. if ( !ether_dev || !(ether_dev->flags & CDC_ETHER_RUNNING) ) {
  136. // We are insane!!!
  137. err( "write_bulk_callback: device not running" );
  138. return;
  139. }
  140. // Do we still have a valid kernel network device?
  141. if ( !netif_device_present(ether_dev->net) ) {
  142. // Someone killed our network interface.
  143. err( "write_bulk_callback: net device not present" );
  144. return;
  145. }
  146. // Hmm...  What on Earth could have happened???
  147. if ( urb->status ) {
  148. dbg("%s: TX status %d", ether_dev->net->name, urb->status);
  149. }
  150. // Update the network interface and tell it we are
  151. // ready for another frame
  152. ether_dev->net->trans_start = jiffies;
  153. netif_wake_queue( ether_dev->net );
  154. }
  155. #if 0
  156. static void setpktfilter_done( struct urb *urb )
  157. {
  158. ether_dev_t *ether_dev = urb->context;
  159. struct net_device *net;
  160. if ( !ether_dev )
  161. return;
  162. dbg("got ctrl callback for setting packet filter");
  163. switch ( urb->status ) {
  164. case USB_ST_NOERROR:
  165. break;
  166. case USB_ST_URB_KILLED:
  167. return;
  168. default:
  169. dbg("intr status %d", urb->status);
  170. }
  171. }
  172. #endif 
  173. static void intr_callback( struct urb *urb )
  174. {
  175. ether_dev_t *ether_dev = urb->context;
  176. struct net_device *net;
  177. __u8 *d;
  178. if ( !ether_dev )
  179. return;
  180. dbg("got intr callback");
  181. switch ( urb->status ) {
  182. case USB_ST_NOERROR:
  183. break;
  184. case USB_ST_URB_KILLED:
  185. return;
  186. default:
  187. dbg("intr status %d", urb->status);
  188. }
  189. d = urb->transfer_buffer;
  190. dbg("d: %x", d[0]);
  191. net = ether_dev->net;
  192. if ( d[0] & 0xfc ) {
  193. ether_dev->stats.tx_errors++;
  194. if ( d[0] & TX_UNDERRUN )
  195. ether_dev->stats.tx_fifo_errors++;
  196. if ( d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT) )
  197. ether_dev->stats.tx_aborted_errors++;
  198. if ( d[0] & LATE_COL )
  199. ether_dev->stats.tx_window_errors++;
  200. if ( d[0] & (NO_CARRIER | LOSS_CARRIER) )
  201. netif_carrier_off(net);
  202. }
  203. }
  204. //////////////////////////////////////////////////////////////////////////////
  205. // Routines for turning net traffic on and off on the USB side ///////////////
  206. //////////////////////////////////////////////////////////////////////////////
  207. static inline int enable_net_traffic( ether_dev_t *ether_dev )
  208. {
  209. struct usb_device *usb = ether_dev->usb;
  210. // Here would be the time to set the data interface to the configuration where
  211. // it has two endpoints that use a protocol we can understand.
  212. if (usb_set_interface( usb,
  213.                         ether_dev->data_bInterfaceNumber,
  214.                         ether_dev->data_bAlternateSetting_with_traffic ) )  {
  215. err("usb_set_interface() failed" );
  216. err("Attempted to set interface %d", ether_dev->data_bInterfaceNumber);
  217. err("To alternate setting       %d", ether_dev->data_bAlternateSetting_with_traffic);
  218. return -1;
  219. }
  220. return 0;
  221. }
  222. static inline void disable_net_traffic( ether_dev_t *ether_dev )
  223. {
  224. // The thing to do is to set the data interface to the alternate setting that has
  225. // no endpoints.  This is what the spec suggests.
  226. if (ether_dev->data_interface_altset_num_without_traffic >= 0 ) {
  227. if (usb_set_interface( ether_dev->usb,
  228.                         ether_dev->data_bInterfaceNumber,
  229.                         ether_dev->data_bAlternateSetting_without_traffic ) )  {
  230. err("usb_set_interface() failed");
  231. }
  232. } else {
  233. // Some devices just may not support this...
  234. warn("No way to disable net traffic");
  235. }
  236. }
  237. //////////////////////////////////////////////////////////////////////////////
  238. // Callback routines for kernel Ethernet Device //////////////////////////////
  239. //////////////////////////////////////////////////////////////////////////////
  240. static void CDCEther_tx_timeout( struct net_device *net )
  241. {
  242. ether_dev_t *ether_dev = net->priv;
  243. // Sanity check
  244. if ( !ether_dev ) {
  245. // Seems to be a case of insanity here
  246. return;
  247. }
  248. // Tell syslog we are hosed.
  249. warn("%s: Tx timed out.", net->name);
  250. // Tear the waiting frame off the list
  251. ether_dev->tx_urb.transfer_flags |= USB_ASYNC_UNLINK;
  252. usb_unlink_urb( &ether_dev->tx_urb );
  253. // Update statistics
  254. ether_dev->stats.tx_errors++;
  255. }
  256. static int CDCEther_start_xmit( struct sk_buff *skb, struct net_device *net )
  257. {
  258. ether_dev_t *ether_dev = net->priv;
  259. int  count;
  260. int  res;
  261. // If we are told to transmit an ethernet frame that fits EXACTLY 
  262. // into an integer number of USB packets, we force it to send one 
  263. // more byte so the device will get a runt USB packet signalling the 
  264. // end of the ethernet frame
  265. if ( (skb->len) ^ (ether_dev->data_ep_out_size) ) {
  266. // It was not an exact multiple
  267. // no need to add anything extra
  268. count = skb->len;
  269. } else {
  270. // Add one to make it NOT an exact multiple
  271. count = skb->len + 1;
  272. }
  273. // Tell the kernel, "No more frames 'til we are done
  274. // with this one.'
  275. netif_stop_queue( net );
  276. // Copy it from kernel memory to OUR memory
  277. memcpy(ether_dev->tx_buff, skb->data, skb->len);
  278. // Fill in the URB for shipping it out.
  279. FILL_BULK_URB( &ether_dev->tx_urb, ether_dev->usb,
  280. usb_sndbulkpipe(ether_dev->usb, ether_dev->data_ep_out),
  281. ether_dev->tx_buff, ether_dev->wMaxSegmentSize, 
  282. write_bulk_callback, ether_dev );
  283. // Tell the URB how much it will be transporting today
  284. ether_dev->tx_urb.transfer_buffer_length = count;
  285. // Send the URB on its merry way.
  286. if ((res = usb_submit_urb(&ether_dev->tx_urb)))  {
  287. // Hmm...  It didn't go. Tell someone...
  288. warn("failed tx_urb %d", res);
  289. // update some stats...
  290. ether_dev->stats.tx_errors++;
  291. // and tell the kernel to give us another.
  292. // Maybe we'll get it right next time.
  293. netif_start_queue( net );
  294. } else {
  295. // Okay, it went out.
  296. // Update statistics
  297. ether_dev->stats.tx_packets++;
  298. ether_dev->stats.tx_bytes += skb->len;
  299. // And tell the kernel when the last transmit occurred.
  300. net->trans_start = jiffies;
  301. }
  302. // We are done with the kernel's memory
  303. dev_kfree_skb(skb);
  304. // We are done here.
  305. return 0;
  306. }
  307. //////////////////////////////////////////////////////////////////////////////
  308. // Standard routines for kernel Ethernet Device //////////////////////////////
  309. //////////////////////////////////////////////////////////////////////////////
  310. static struct net_device_stats *CDCEther_netdev_stats( struct net_device *net )
  311. {
  312. // Easy enough!
  313. return &((ether_dev_t *)net->priv)->stats;
  314. }
  315. static int CDCEther_open(struct net_device *net)
  316. {
  317. ether_dev_t *ether_dev = (ether_dev_t *)net->priv;
  318. int res;
  319. // Turn on the USB and let the packets flow!!!
  320. if ( (res = enable_net_traffic( ether_dev )) ) {
  321. err("%s can't enable_net_traffic() - %d", __FUNCTION__, res );
  322. return -EIO;
  323. }
  324. /* Prep a receive URB */
  325. FILL_BULK_URB( &ether_dev->rx_urb, ether_dev->usb,
  326. usb_rcvbulkpipe(ether_dev->usb, ether_dev->data_ep_in),
  327. ether_dev->rx_buff, ether_dev->wMaxSegmentSize,
  328. read_bulk_callback, ether_dev );
  329. /* Put it out there so the device can send us stuff */
  330. if ( (res = usb_submit_urb(&ether_dev->rx_urb)) ) {
  331. /* Hmm...  Okay... */
  332. warn( "%s failed rx_urb %d", __FUNCTION__, res );
  333. }
  334. if (ether_dev->properties & HAVE_NOTIFICATION_ELEMENT) {
  335. /* Arm and submit the interrupt URB */
  336. FILL_INT_URB( &ether_dev->intr_urb,
  337. ether_dev->usb,
  338. usb_rcvintpipe(ether_dev->usb, ether_dev->comm_ep_in),
  339. ether_dev->intr_buff,
  340. 8, /* Transfer buffer length */
  341. intr_callback,
  342. ether_dev,
  343. ether_dev->intr_interval);
  344. if ( (res = usb_submit_urb(&ether_dev->intr_urb)) ) {
  345. warn("%s failed intr_urb %d", __FUNCTION__, res );
  346. }
  347. }
  348. // Tell the kernel we are ready to start receiving from it
  349. netif_start_queue( net );
  350. // We are up and running.
  351. ether_dev->flags |= CDC_ETHER_RUNNING;
  352. // Let's get ready to move frames!!!
  353. return 0;
  354. }
  355. static int CDCEther_close( struct net_device *net )
  356. {
  357. ether_dev_t *ether_dev = net->priv;
  358. // We are no longer running.
  359. ether_dev->flags &= ~CDC_ETHER_RUNNING;
  360. // Tell the kernel to stop sending us stuff
  361. netif_stop_queue( net );
  362. // If we are not already unplugged, turn off USB
  363. // traffic
  364. if ( !(ether_dev->flags & CDC_ETHER_UNPLUG) ) {
  365. disable_net_traffic( ether_dev );
  366. }
  367. // We don't need the URBs anymore.
  368. usb_unlink_urb( &ether_dev->rx_urb );
  369. usb_unlink_urb( &ether_dev->tx_urb );
  370. usb_unlink_urb( &ether_dev->intr_urb );
  371. usb_unlink_urb( &ether_dev->ctrl_urb );
  372. // That's it.  I'm done.
  373. return 0;
  374. }
  375. static int netdev_ethtool_ioctl(struct net_device *netdev, void *useraddr)
  376. {
  377. ether_dev_t *ether_dev = netdev->priv;
  378. u32 cmd;
  379. char tmp[40];
  380. if (get_user(cmd, (u32 *)useraddr))
  381. return -EFAULT;
  382. switch (cmd) {
  383. /* get driver info */
  384. case ETHTOOL_GDRVINFO: {
  385. struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
  386. strncpy(info.driver, SHORT_DRIVER_DESC, ETHTOOL_BUSINFO_LEN);
  387. strncpy(info.version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  388. sprintf(tmp, "usb%d:%d", ether_dev->usb->bus->busnum, ether_dev->usb->devnum);
  389. strncpy(info.bus_info, tmp, ETHTOOL_BUSINFO_LEN);
  390. sprintf(tmp, "CDC %x.%x", ((ether_dev->bcdCDC & 0xff00)>>8), (ether_dev->bcdCDC & 0x00ff) );
  391. strncpy(info.fw_version, tmp, ETHTOOL_BUSINFO_LEN);
  392. if (copy_to_user(useraddr, &info, sizeof(info)))
  393. return -EFAULT;
  394. return 0;
  395. }
  396. /* get link status */
  397. case ETHTOOL_GLINK: {
  398. struct ethtool_value edata = {ETHTOOL_GLINK};
  399. edata.data = netif_carrier_ok(netdev);
  400. if (copy_to_user(useraddr, &edata, sizeof(edata)))
  401. return -EFAULT;
  402. return 0;
  403. }
  404. }
  405. dbg("Got unsupported ioctl: %x", cmd);
  406. return -EOPNOTSUPP; /* the ethtool user space tool relies on this */
  407. }
  408. static int CDCEther_ioctl( struct net_device *net, struct ifreq *rq, int cmd )
  409. {
  410. switch(cmd) {
  411. case SIOCETHTOOL:
  412. return netdev_ethtool_ioctl(net, (void *) rq->ifr_data);
  413. default:
  414. return -ENOTTY; /* per ioctl man page */
  415. }
  416. }
  417. /* Multicast routines */
  418. static void CDC_SetEthernetPacketFilter (ether_dev_t *ether_dev)
  419. {
  420. #if 0
  421. struct usb_ctrlrequest *dr = &ether_dev->ctrl_dr;
  422. int res;
  423. dr->bRequestType = USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE;
  424. dr->bRequest = SET_ETHERNET_PACKET_FILTER;
  425. dr->wValue = cpu_to_le16(ether_dev->mode_flags);
  426. dr->wIndex = cpu_to_le16((u16)ether_dev->comm_interface);
  427. dr->wLength = 0;
  428. FILL_CONTROL_URB(&ether_dev->ctrl_urb,
  429. ether_dev->usb,
  430. usb_sndctrlpipe(ether_dev->usb, 0),
  431. dr,
  432. NULL,
  433. NULL,
  434. setpktfilter_done,
  435. ether_dev);
  436. if ( (res = usb_submit_urb(&ether_dev->ctrl_urb)) ) {
  437. warn("%s failed submit ctrl_urb %d", __FUNCTION__, res);
  438. }
  439. #endif
  440. }
  441. static void CDCEther_set_multicast( struct net_device *net )
  442. {
  443. ether_dev_t *ether_dev = net->priv;
  444. int i;
  445. __u8 *buff;
  446. // Tell the kernel to stop sending us frames while we get this
  447. // all set up.
  448. netif_stop_queue(net);
  449. /* Note: do not reorder, GCC is clever about common statements. */
  450. if (net->flags & IFF_PROMISC) {
  451. /* Unconditionally log net taps. */
  452. dbg( "%s: Promiscuous mode enabled", net->name);
  453. ether_dev->mode_flags = MODE_FLAG_PROMISCUOUS |
  454. MODE_FLAG_ALL_MULTICAST |
  455. MODE_FLAG_DIRECTED |
  456. MODE_FLAG_BROADCAST |
  457. MODE_FLAG_MULTICAST;
  458. } else if (net->mc_count > ether_dev->wNumberMCFilters) {
  459. /* Too many to filter perfectly -- accept all multicasts. */
  460. dbg("%s: too many MC filters for hardware, using allmulti", net->name);
  461. ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
  462. MODE_FLAG_DIRECTED |
  463. MODE_FLAG_BROADCAST |
  464. MODE_FLAG_MULTICAST;
  465. } else if (net->flags & IFF_ALLMULTI) {
  466. /* Filter in software */
  467. dbg("%s: using allmulti", net->name);
  468. ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
  469. MODE_FLAG_DIRECTED |
  470. MODE_FLAG_BROADCAST |
  471. MODE_FLAG_MULTICAST;
  472. } else {
  473. /* do multicast filtering in hardware */
  474. struct dev_mc_list *mclist;
  475. dbg("%s: set multicast filters", net->name);
  476. ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
  477. MODE_FLAG_DIRECTED |
  478. MODE_FLAG_BROADCAST |
  479. MODE_FLAG_MULTICAST;
  480. buff = kmalloc(6 * net->mc_count, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  481. for (i = 0, mclist = net->mc_list;
  482. mclist && i < net->mc_count;
  483. i++, mclist = mclist->next) {
  484. memcpy(&mclist->dmi_addr, &buff[i * 6], 6);
  485. }
  486. #if 0
  487. usb_control_msg(ether_dev->usb,
  488. usb_sndctrlpipe(ether_dev->usb, 0),
  489. SET_ETHERNET_MULTICAST_FILTER, /* request */
  490. USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE, /* request type */
  491. cpu_to_le16(net->mc_count), /* value */
  492. cpu_to_le16((u16)ether_dev->comm_interface), /* index */
  493. buff,
  494. (6* net->mc_count), /* size */
  495. HZ); /* timeout */
  496. #endif
  497. kfree(buff);
  498. }
  499. CDC_SetEthernetPacketFilter(ether_dev);
  500. /* Tell the kernel to start giving frames to us again. */
  501. netif_wake_queue(net);
  502. }
  503. //////////////////////////////////////////////////////////////////////////////
  504. // Routines used to parse out the Functional Descriptors /////////////////////
  505. //////////////////////////////////////////////////////////////////////////////
  506. /* Header Descriptor - CDC Spec 5.2.3.1, Table 26 */
  507. static int parse_header_functional_descriptor( int *bFunctionLength,
  508.                                                int bDescriptorType,
  509.                                                int bDescriptorSubtype,
  510.                                                unsigned char *data,
  511.                                                ether_dev_t *ether_dev,
  512.                                                int *requirements )
  513. {
  514. /* Check to make sure we haven't seen one of these already. */
  515. if ( (~*requirements) & REQ_HDR_FUNC_DESCR ) {
  516. err( "Multiple Header Functional Descriptors found." );
  517. return -1;
  518. }
  519. /* Check for appropriate length */
  520. if (*bFunctionLength != HEADER_FUNC_DESC_LEN) {
  521. dbg( "Invalid length in Header Functional Descriptor, working around it." );
  522. /* This is a hack to get around a particular device (NO NAMES)
  523.  * It has this function length set to the length of the
  524.  * whole class-specific descriptor */
  525. *bFunctionLength = HEADER_FUNC_DESC_LEN;
  526. }
  527. /* Nothing extremely useful here */
  528. /* We'll keep it for posterity */
  529. ether_dev->bcdCDC = data[0] + (data[1] << 8);
  530. dbg( "Found Header descriptor, CDC version %x.", ether_dev->bcdCDC);
  531. /* We've seen one of these */
  532. *requirements &= ~REQ_HDR_FUNC_DESCR;
  533. /* Success */
  534. return 0;
  535. }
  536. /* Union Descriptor - CDC Spec 5.2.3.8, Table 33 */
  537. static int parse_union_functional_descriptor( int *bFunctionLength, 
  538.                                               int bDescriptorType, 
  539.                                               int bDescriptorSubtype,
  540.                                               unsigned char *data,
  541.                                               ether_dev_t *ether_dev,
  542.                                               int *requirements )
  543. {
  544. /* Check to make sure we haven't seen one of these already. */
  545. if ( (~*requirements) & REQ_UNION_FUNC_DESCR ) {
  546. err( "Multiple Union Functional Descriptors found." );
  547. return -1;
  548. }
  549. /* Check for appropriate length */
  550. if (*bFunctionLength != UNION_FUNC_DESC_LEN) {
  551. // It is NOT the size we expected.
  552. err( "Invalid length in Union Functional Descriptor." );
  553. return -1;
  554. }
  555. /* Sanity check of sorts */
  556. if (ether_dev->comm_interface != data[0]) {
  557. /* This tells us that we are chasing the wrong comm
  558.  * interface or we are crazy or something else weird. */
  559. if (ether_dev->comm_interface == data[1]) {
  560. dbg( "Probably broken Union descriptor, fudging data interface." );
  561. /* We'll need this in a few microseconds,
  562.  * so if the comm interface was the first slave,
  563.  * then probably the master interface is the data one
  564.  * Just hope for the best */
  565. ether_dev->data_interface = data[0];
  566. } else {
  567. err( "Union Functional Descriptor is broken beyond repair." );
  568. return -1;
  569. }
  570. } else{ /* Descriptor is OK */
  571. ether_dev->data_interface = data[1];
  572. }
  573. /* We've seen one of these */
  574. *requirements &= ~REQ_UNION_FUNC_DESCR;
  575. /* Success */
  576. return 0;
  577. }
  578. /* Ethernet Descriptor - CDC Spec 5.2.3.16, Table 41 */
  579. static int parse_ethernet_functional_descriptor( int *bFunctionLength,
  580.                                                  int bDescriptorType, 
  581.                                                  int bDescriptorSubtype,
  582.                                                  unsigned char *data,
  583.                                                  ether_dev_t *ether_dev,
  584.                                                  int *requirements )
  585. {
  586. //* Check to make sure we haven't seen one of these already. */
  587. if ( (~*requirements) & REQ_ETH_FUNC_DESCR ) {
  588. err( "Multiple Ethernet Functional Descriptors found." );
  589. return -1;
  590. }
  591. /* Check for appropriate length */
  592. if (*bFunctionLength != ETHERNET_FUNC_DESC_LEN) {
  593. err( "Invalid length in Ethernet Networking Functional Descriptor." );
  594. return -1;
  595. }
  596. /* Lots of goodies from this one.  They are all important. */
  597. ether_dev->iMACAddress = data[0];
  598. ether_dev->bmEthernetStatistics = data[1] + (data[2] << 8) + (data[3] << 16) + (data[4] << 24);
  599. ether_dev->wMaxSegmentSize = data[5] + (data[6] << 8);
  600. ether_dev->wNumberMCFilters = (data[7] + (data[8] << 8));
  601. if (ether_dev->wNumberMCFilters & (1 << 15)) {
  602. ether_dev->properties |= PERFECT_FILTERING;
  603. dbg("Perfect filtering support");
  604. } else {
  605. dbg("Imperfect filtering support - need sw hashing");
  606. }
  607. if (0 == (ether_dev->wNumberMCFilters & (0x7f))) {
  608. ether_dev->properties |= NO_SET_MULTICAST;
  609. dbg("Can't use SetEthernetMulticastFilters request");
  610. }
  611. if (ether_dev->wNumberMCFilters > multicast_filter_limit) {
  612. ether_dev->wNumberMCFilters = multicast_filter_limit;
  613. }
  614. ether_dev->bNumberPowerFilters = data[9];
  615. /* We've seen one of these */
  616. *requirements &= ~REQ_ETH_FUNC_DESCR;
  617. /* Success */
  618. return 0;
  619. }
  620. static int parse_protocol_unit_functional_descriptor( int *bFunctionLength, 
  621.                                                       int bDescriptorType, 
  622.                                                       int bDescriptorSubtype,
  623.                                                       unsigned char *data,
  624.                                                       ether_dev_t *ether_dev,
  625.                                                       int *requirements )
  626. {
  627. /* There should only be one type if we are sane */
  628. if (bDescriptorType != CS_INTERFACE) {
  629. err( "Invalid bDescriptorType found." );
  630. return -1;
  631. }
  632. /* The Subtype tells the tale - CDC spec Table 25 */
  633. switch (bDescriptorSubtype) {
  634. case 0x00: /* Header Functional Descriptor */
  635. return parse_header_functional_descriptor( bFunctionLength,
  636.                                            bDescriptorType,
  637.                                            bDescriptorSubtype,
  638.                                            data,
  639.                                            ether_dev,
  640.                                            requirements );
  641. break;
  642. case 0x06: /* Union Functional Descriptor */
  643. return parse_union_functional_descriptor( bFunctionLength,
  644.                                           bDescriptorType,
  645.                                           bDescriptorSubtype,
  646.                                           data,
  647.                                           ether_dev,
  648.                                           requirements );
  649. break;
  650. case 0x0F: /* Ethernet Networking Functional Descriptor */
  651. return parse_ethernet_functional_descriptor( bFunctionLength,
  652.                                              bDescriptorType,
  653.                                              bDescriptorSubtype,
  654.                                              data,
  655.                                              ether_dev,
  656.                                              requirements );
  657. break;
  658. default: /* We don't support this at this time... */
  659. /* However that doesn't necessarily indicate an error. */
  660. dbg( "Unexpected header type %x.", bDescriptorSubtype );
  661. return 0;
  662. }
  663. /* How did we get here? */
  664. return -1;
  665. }
  666. static int parse_ethernet_class_information( unsigned char *data, int length, ether_dev_t *ether_dev )
  667. {
  668. int loc = 0;
  669. int rc;
  670. int bFunctionLength;
  671. int bDescriptorType;
  672. int bDescriptorSubtype;
  673. int requirements = REQUIREMENTS_TOTAL; /* We init to our needs, and then clear
  674. * bits as we find the descriptors */
  675. /* As long as there is something here, we will try to parse it */
  676. /* All of the functional descriptors start with the same 3 byte pattern */
  677. while (loc < length) {
  678. /* Length */
  679. bFunctionLength = data[loc];
  680. loc++;
  681. /* Type */
  682. bDescriptorType = data[loc];
  683. loc++;
  684. /* Subtype */
  685. bDescriptorSubtype = data[loc];
  686. loc++;
  687. /* ship this off to be processed */
  688. rc = parse_protocol_unit_functional_descriptor( &bFunctionLength, 
  689.                                                 bDescriptorType, 
  690.                                                 bDescriptorSubtype, 
  691.                                                 &data[loc],
  692.                                                 ether_dev,
  693.                                                 &requirements );
  694. /* Did it process okay? */
  695. if (rc) {
  696. /* Something was hosed somewhere. */
  697. /*  No need to continue */
  698. err("Bad descriptor parsing: %x", rc );
  699. return -1;
  700. }
  701. /* We move the loc pointer along, remembering
  702.  * that we have already taken three bytes */
  703. loc += (bFunctionLength - 3);
  704. }
  705. /* Check to see if we got everything we need. */
  706. if (requirements) {
  707. // We missed some of the requirements...
  708. err( "Not all required functional descriptors present 0x%08X.", requirements );
  709. return -1;
  710. }
  711. /* We got everything */
  712. return 0;
  713. }
  714. //////////////////////////////////////////////////////////////////////////////
  715. // Routine to check for the existence of the Functional Descriptors //////////
  716. //////////////////////////////////////////////////////////////////////////////
  717. static int find_and_parse_ethernet_class_information( struct usb_device *device, ether_dev_t *ether_dev )
  718. {
  719. struct usb_config_descriptor *conf = NULL;
  720. struct usb_interface *comm_intf_group = NULL;
  721. struct usb_interface_descriptor *comm_intf = NULL;
  722. int rc = -1;
  723. /* The assumption here is that find_ethernet_comm_interface
  724.  * and find_valid_configuration
  725.  * have already filled in the information about where to find
  726.  * the a valid commication interface. */
  727. conf = &( device->config[ether_dev->configuration_num] );
  728. comm_intf_group = &( conf->interface[ether_dev->comm_interface] );
  729. comm_intf = &( comm_intf_group->altsetting[ether_dev->comm_interface_altset_num] );
  730. /* Let's check and see if it has the extra information we need */
  731. if (comm_intf->extralen > 0) {
  732. /* This is where the information is SUPPOSED to be */
  733. rc = parse_ethernet_class_information( comm_intf->extra, comm_intf->extralen, ether_dev );
  734. } else if (conf->extralen > 0) {
  735. /* This is a hack.  The spec says it should be at the interface
  736.  * location checked above.  However I have seen it here also.
  737.  * This is the same device that requires the functional descriptor hack above */
  738. dbg( "Ethernet information found at device configuration.  Trying to use it anyway." );
  739. rc = parse_ethernet_class_information( conf->extra, conf->extralen, ether_dev );
  740. } else  {
  741. /* I don't know where else to look */
  742. err( "No ethernet information found." );
  743. rc = -1;
  744. }
  745. return rc;
  746. }
  747. //////////////////////////////////////////////////////////////////////////////
  748. // Routines to verify the data interface /////////////////////////////////////
  749. //////////////////////////////////////////////////////////////////////////////
  750. static int get_data_interface_endpoints( struct usb_device *device, ether_dev_t *ether_dev )
  751. {
  752. struct usb_config_descriptor *conf = NULL;
  753. struct usb_interface *data_intf_group = NULL;
  754. struct usb_interface_descriptor *data_intf = NULL;
  755. /* Walk through and get to the data interface we are checking. */
  756. conf = &( device->config[ether_dev->configuration_num] );
  757. data_intf_group = &( conf->interface[ether_dev->data_interface] );
  758. data_intf = &( data_intf_group->altsetting[ether_dev->data_interface_altset_num_with_traffic] );
  759. /* Start out assuming we won't find anything we can use */
  760. ether_dev->data_ep_in = 0;
  761. ether_dev->data_ep_out = 0;
  762. /* If these are not BULK endpoints, we don't want them */
  763. if ( data_intf->endpoint[0].bmAttributes != USB_ENDPOINT_XFER_BULK ) {
  764. return -1;
  765. }
  766. if ( data_intf->endpoint[1].bmAttributes != USB_ENDPOINT_XFER_BULK ) {
  767. return -1;
  768. }
  769. /* Check the first endpoint to see if it is IN or OUT */
  770. if ( data_intf->endpoint[0].bEndpointAddress & USB_DIR_IN ) {
  771. ether_dev->data_ep_in = data_intf->endpoint[0].bEndpointAddress & 0x7F;
  772. } else {
  773. ether_dev->data_ep_out = data_intf->endpoint[0].bEndpointAddress;
  774. ether_dev->data_ep_out_size = data_intf->endpoint[0].wMaxPacketSize;
  775. }
  776. /* Check the second endpoint to see if it is IN or OUT */
  777. if ( data_intf->endpoint[1].bEndpointAddress & USB_DIR_IN ) {
  778. ether_dev->data_ep_in = data_intf->endpoint[1].bEndpointAddress & 0x7F;
  779. } else {
  780. ether_dev->data_ep_out = data_intf->endpoint[1].bEndpointAddress;
  781. ether_dev->data_ep_out_size = data_intf->endpoint[1].wMaxPacketSize;
  782. }
  783. /* Now make sure we got both an IN and an OUT */
  784. if (ether_dev->data_ep_in && ether_dev->data_ep_out) {
  785. dbg( "detected BULK OUT packets of size %d", ether_dev->data_ep_out_size );
  786. return 0;
  787. }
  788. return -1;
  789. }
  790. static int verify_ethernet_data_interface( struct usb_device *device, ether_dev_t *ether_dev )
  791. {
  792. struct usb_config_descriptor *conf = NULL;
  793. struct usb_interface *data_intf_group = NULL;
  794. struct usb_interface_descriptor *data_intf = NULL;
  795. int rc = -1;
  796. int status;
  797. int altset_num;
  798. // The assumption here is that parse_ethernet_class_information()
  799. // and find_valid_configuration() 
  800. // have already filled in the information about where to find
  801. // a data interface
  802. conf = &( device->config[ether_dev->configuration_num] );
  803. data_intf_group = &( conf->interface[ether_dev->data_interface] );
  804. // start out assuming we won't find what we are looking for.
  805. ether_dev->data_interface_altset_num_with_traffic = -1;
  806. ether_dev->data_bAlternateSetting_with_traffic = -1;
  807. ether_dev->data_interface_altset_num_without_traffic = -1;
  808. ether_dev->data_bAlternateSetting_without_traffic = -1;
  809. // Walk through every possible setting for this interface until
  810. // we find what makes us happy.
  811. for ( altset_num = 0; altset_num < data_intf_group->num_altsetting; altset_num++ ) {
  812. data_intf = &( data_intf_group->altsetting[altset_num] );
  813. // Is this a data interface we like?
  814. if ( ( data_intf->bInterfaceClass == 0x0A )
  815.    && ( data_intf->bInterfaceSubClass == 0x00 )
  816.    && ( data_intf->bInterfaceProtocol == 0x00 ) ) {
  817. if ( data_intf->bNumEndpoints == 2 ) {
  818. // We are required to have one of these.
  819. // An interface with 2 endpoints to send Ethernet traffic back and forth
  820. // It actually may be possible that the device might only
  821. // communicate in a vendor specific manner.
  822. // That would not be very nice.
  823. // We can add that one later.
  824. ether_dev->data_bInterfaceNumber = data_intf->bInterfaceNumber;
  825. ether_dev->data_interface_altset_num_with_traffic = altset_num;
  826. ether_dev->data_bAlternateSetting_with_traffic = data_intf->bAlternateSetting;
  827. status = get_data_interface_endpoints( device, ether_dev );
  828. if (!status) {
  829. rc = 0;
  830. }
  831. }
  832. if ( data_intf->bNumEndpoints == 0 ) {
  833. // According to the spec we are SUPPOSED to have one of these
  834. // In fact the device is supposed to come up in this state.
  835. // However, I have seen a device that did not have such an interface.
  836. // So it must be just optional for our driver...
  837. ether_dev->data_bInterfaceNumber = data_intf->bInterfaceNumber;
  838. ether_dev->data_interface_altset_num_without_traffic = altset_num;
  839. ether_dev->data_bAlternateSetting_without_traffic = data_intf->bAlternateSetting;
  840. }
  841. }
  842. }
  843. return rc;
  844. }
  845. //////////////////////////////////////////////////////////////////////////////
  846. // Routine to find a communication interface /////////////////////////////////
  847. //////////////////////////////////////////////////////////////////////////////
  848. static int find_ethernet_comm_interface( struct usb_device *device, ether_dev_t *ether_dev )
  849. {
  850. struct usb_config_descriptor *conf = NULL;
  851. struct usb_interface *comm_intf_group = NULL;
  852. struct usb_interface_descriptor *comm_intf = NULL;
  853. int intf_num;
  854. int altset_num;
  855. int rc;
  856. conf = &( device->config[ether_dev->configuration_num] );
  857. // We need to check and see if any of these interfaces are something we want.
  858. // Walk through each interface one at a time
  859. for ( intf_num = 0; intf_num < conf->bNumInterfaces; intf_num++ ) {
  860. comm_intf_group = &( conf->interface[intf_num] );
  861. // Now for each of those interfaces, check every possible
  862. // alternate setting.
  863. for ( altset_num = 0; altset_num < comm_intf_group->num_altsetting; altset_num++ ) {
  864. comm_intf = &( comm_intf_group->altsetting[altset_num] );
  865. /* Good, we found one, we will try this one */
  866. /* Fill in the structure */
  867. ether_dev->comm_interface = intf_num;
  868. ether_dev->comm_bInterfaceNumber = comm_intf->bInterfaceNumber;
  869. ether_dev->comm_interface_altset_num = altset_num;
  870. ether_dev->comm_bAlternateSetting = comm_intf->bAlternateSetting;
  871. // Look for the Ethernet Functional Descriptors
  872. rc = find_and_parse_ethernet_class_information( device, ether_dev );
  873. if (rc) {
  874. // Nope this was no good after all.
  875. continue;
  876. }
  877. /* Check that we really can talk to the data interface
  878.  * This includes # of endpoints, protocols, etc. */
  879. rc = verify_ethernet_data_interface( device, ether_dev );
  880. if (rc) {
  881. /* We got something we didn't like */
  882. continue;
  883. }
  884. /* It is a bit ambiguous whether the Ethernet model really requires
  885.  * the notification element (usually an interrupt endpoint) or not
  886.  * And some products (eg Sharp Zaurus) don't support it, so we
  887.  * only use the notification element if present */
  888. /* We check for a sane endpoint before using it */
  889. if ( (comm_intf->bNumEndpoints == 1) &&
  890. (comm_intf->endpoint[0].bEndpointAddress & USB_DIR_IN) &&
  891. (comm_intf->endpoint[0].bmAttributes == USB_ENDPOINT_XFER_INT)) {
  892. ether_dev->properties |= HAVE_NOTIFICATION_ELEMENT;
  893. ether_dev->comm_ep_in = (comm_intf->endpoint[0].bEndpointAddress & 0x7F);
  894. dbg("interrupt address: %x",ether_dev->comm_ep_in);
  895. ether_dev->intr_interval = (comm_intf->endpoint[0].bInterval);
  896. dbg("interrupt interval: %d",ether_dev->intr_interval);
  897. }
  898. // This communication interface seems to give us everything
  899. // we require.  We have all the ethernet info we need.
  900. return 0;
  901. } // end for altset_num
  902. } // end for intf_num
  903. return -1;
  904. }
  905. //////////////////////////////////////////////////////////////////////////////
  906. // Routine to go through all configurations and find one that ////////////////
  907. // is an Ethernet Networking Device //////////////////////////////////////////
  908. //////////////////////////////////////////////////////////////////////////////
  909. static int find_valid_configuration( struct usb_device *device, ether_dev_t *ether_dev )
  910. {
  911. struct usb_config_descriptor *conf = NULL;
  912. int conf_num;
  913. int rc;
  914. // We will try each and every possible configuration
  915. for ( conf_num = 0; conf_num < device->descriptor.bNumConfigurations; conf_num++ ) {
  916. conf = &( device->config[conf_num] );
  917. // Our first requirement : 2 interfaces
  918. if ( conf->bNumInterfaces != 2 ) {
  919. // I currently don't know how to handle devices with any number of interfaces
  920. // other than 2.
  921. continue;
  922. }
  923. // This one passed our first check, fill in some 
  924. // useful data
  925. ether_dev->configuration_num = conf_num;
  926. ether_dev->bConfigurationValue = conf->bConfigurationValue;
  927. // Now run it through the ringers and see what comes
  928. // out the other side.
  929. rc = find_ethernet_comm_interface( device, ether_dev );
  930. // Check if we found an ethernet Communcation Device
  931. if ( !rc ) {
  932. // We found one.
  933. return 0;
  934. }
  935. }
  936. // None of the configurations suited us.
  937. return -1;
  938. }
  939. //////////////////////////////////////////////////////////////////////////////
  940. // Routine that checks a given configuration to see if any driver ////////////
  941. // has claimed any of the devices interfaces /////////////////////////////////
  942. //////////////////////////////////////////////////////////////////////////////
  943. static int check_for_claimed_interfaces( struct usb_config_descriptor *config )
  944. {
  945. struct usb_interface *comm_intf_group;
  946. int intf_num;
  947. // Go through all the interfaces and make sure none are 
  948. // claimed by anybody else.
  949. for ( intf_num = 0; intf_num < config->bNumInterfaces; intf_num++ ) {
  950. comm_intf_group = &( config->interface[intf_num] );
  951. if ( usb_interface_claimed( comm_intf_group ) ) {
  952. // Somebody has beat us to this guy.
  953. // We can't change the configuration out from underneath of whoever
  954. // is using this device, so we will go ahead and give up.
  955. return -1;
  956. }
  957. }
  958. // We made it all the way through.
  959. // I guess no one has claimed any of these interfaces.
  960. return 0;
  961. }
  962. //////////////////////////////////////////////////////////////////////////////
  963. // Routines to ask for and set the kernel network interface's MAC address ////
  964. // Used by driver's probe routine ////////////////////////////////////////////
  965. //////////////////////////////////////////////////////////////////////////////
  966. static inline unsigned char hex2dec( unsigned char digit )
  967. {
  968. /* Is there a standard way to do this??? */
  969. /* I have written this code TOO MANY times. */
  970. if ( (digit >= '0') && (digit <= '9') ) {
  971. return (digit - '0');
  972. }
  973. if ( (digit >= 'a') && (digit <= 'f') ) {
  974. return (digit - 'a' + 10);
  975. }
  976. if ( (digit >= 'A') && (digit <= 'F') ) {
  977. return (digit - 'A' + 10);
  978. }
  979. return 16;
  980. }
  981. /* CDC Ethernet devices provide the MAC address as a string */
  982. /* We get an index to the string in the Ethernet functional header */
  983. /* This routine retrieves the string, sanity checks it, and sets the */
  984. /* MAC address in the network device */
  985. /* The encoding is a bit wacky - see CDC Spec Table 41 for details */
  986. static void set_ethernet_addr( ether_dev_t *ether_dev )
  987. {
  988. unsigned char mac_addr[6];
  989. int i;
  990. int  len;
  991. unsigned char buffer[13];
  992. /* Let's assume we don't get anything */
  993. mac_addr[0] = 0x00;
  994. mac_addr[1] = 0x00;
  995. mac_addr[2] = 0x00;
  996. mac_addr[3] = 0x00;
  997. mac_addr[4] = 0x00;
  998. mac_addr[5] = 0x00;
  999. /* Let's ask the device */
  1000. if (0 > (len = usb_string(ether_dev->usb, ether_dev->iMACAddress, buffer, 13))) {
  1001. err("Attempting to get MAC address failed: %d", -1*len);
  1002. return;
  1003. }
  1004. /* Sanity check */
  1005. if (len != 12) {
  1006. /* You gotta love failing sanity checks */
  1007. err("Attempting to get MAC address returned %d bytes", len);
  1008. return;
  1009. }
  1010. /* Fill in the mac_addr */
  1011. for (i = 0; i < 6; i++) {
  1012. if ((16 == buffer[2 * i]) || (16 == buffer[2 * i + 1])) {
  1013. err("Bad value in MAC address");
  1014. }
  1015. else {
  1016. mac_addr[i] = ( hex2dec( buffer[2 * i] ) << 4 ) + hex2dec( buffer[2 * i + 1] );
  1017. }
  1018. }
  1019. /* Now copy it over to our network device structure */
  1020. memcpy( ether_dev->net->dev_addr, mac_addr, sizeof(mac_addr) );
  1021. }
  1022. //////////////////////////////////////////////////////////////////////////////
  1023. // Routine to print to syslog information about the driver ///////////////////
  1024. // Used by driver's probe routine ////////////////////////////////////////////
  1025. //////////////////////////////////////////////////////////////////////////////
  1026. void log_device_info(ether_dev_t *ether_dev)
  1027. {
  1028. int len;
  1029. int string_num;
  1030. unsigned char manu[256];
  1031. unsigned char prod[256];
  1032. unsigned char sern[256];
  1033. unsigned char *mac_addr;
  1034. /* Default empty strings in case we don't find a real one */
  1035. manu[0] = 0x00;
  1036. prod[0] = 0x00;
  1037. sern[0] = 0x00;
  1038. /*  Try to get the device Manufacturer */
  1039. string_num = ether_dev->usb->descriptor.iManufacturer;
  1040. if (string_num) {
  1041. // Put it into its buffer
  1042. len = usb_string(ether_dev->usb, string_num, manu, 255);
  1043. // Just to be safe
  1044. manu[len] = 0x00;
  1045. }
  1046. /* Try to get the device Product Name */
  1047. string_num = ether_dev->usb->descriptor.iProduct;
  1048. if (string_num) {
  1049. // Put it into its buffer
  1050. len = usb_string(ether_dev->usb, string_num, prod, 255);
  1051. // Just to be safe
  1052. prod[len] = 0x00;
  1053. }
  1054. /* Try to get the device Serial Number */
  1055. string_num = ether_dev->usb->descriptor.iSerialNumber;
  1056. if (string_num) {
  1057. // Put it into its buffer
  1058. len = usb_string(ether_dev->usb, string_num, sern, 255);
  1059. // Just to be safe
  1060. sern[len] = 0x00;
  1061. }
  1062. /* This makes it easier for us to print */
  1063. mac_addr = ether_dev->net->dev_addr;
  1064. /* Now send everything we found to the syslog */
  1065. info( "%s: %s %s %s", ether_dev->net->name, manu, prod, sern);
  1066. dbg( "%s: %02X:%02X:%02X:%02X:%02X:%02X",
  1067. ether_dev->net->name,
  1068. mac_addr[0],
  1069. mac_addr[1],
  1070. mac_addr[2],
  1071. mac_addr[3],
  1072. mac_addr[4],
  1073. mac_addr[5] );
  1074. }
  1075. /* Forward declaration */
  1076. static struct usb_driver CDCEther_driver ;
  1077. //////////////////////////////////////////////////////////////////////////////
  1078. // Module's probe routine ////////////////////////////////////////////////////
  1079. // claims interfaces if they are for an Ethernet CDC /////////////////////////
  1080. //////////////////////////////////////////////////////////////////////////////
  1081. static void * CDCEther_probe( struct usb_device *usb, unsigned int ifnum,
  1082.      const struct usb_device_id *id)
  1083. {
  1084. struct net_device *net;
  1085. ether_dev_t *ether_dev;
  1086. int  rc;
  1087. // First we should check the active configuration to see if 
  1088. // any other driver has claimed any of the interfaces.
  1089. if ( check_for_claimed_interfaces( usb->actconfig ) ) {
  1090. // Someone has already put there grubby paws on this device.
  1091. // We don't want it now...
  1092. return NULL;
  1093. }
  1094. // We might be finding a device we can use.
  1095. // We all go ahead and allocate our storage space.
  1096. // We need to because we have to start filling in the data that
  1097. // we are going to need later.
  1098. if(!(ether_dev = kmalloc(sizeof(ether_dev_t), GFP_KERNEL))) {
  1099. err("out of memory allocating device structure");
  1100. return NULL;
  1101. }
  1102. // Zero everything out.
  1103. memset(ether_dev, 0, sizeof(ether_dev_t));
  1104. // Let's see if we can find a configuration we can use.
  1105. rc = find_valid_configuration( usb, ether_dev );
  1106. if (rc) {
  1107. // Nope we couldn't find one we liked.
  1108. // This device was not meant for us to control.
  1109. kfree( ether_dev );
  1110. return NULL;
  1111. }
  1112. // Now that we FOUND a configuration. let's try to make the 
  1113. // device go into it.
  1114. if ( usb_set_configuration( usb, ether_dev->bConfigurationValue ) ) {
  1115. err("usb_set_configuration() failed");
  1116. kfree( ether_dev );
  1117. return NULL;
  1118. }
  1119. // Now set the communication interface up as required.
  1120. if (usb_set_interface(usb, ether_dev->comm_bInterfaceNumber, ether_dev->comm_bAlternateSetting)) {
  1121. err("usb_set_interface() failed");
  1122. kfree( ether_dev );
  1123. return NULL;
  1124. }
  1125. // Only turn traffic on right now if we must...
  1126. if (ether_dev->data_interface_altset_num_without_traffic >= 0) {
  1127. // We found an alternate setting for the data
  1128. // interface that allows us to turn off traffic.
  1129. // We should use it.
  1130. if (usb_set_interface( usb, 
  1131.                        ether_dev->data_bInterfaceNumber, 
  1132.                        ether_dev->data_bAlternateSetting_without_traffic)) {
  1133. err("usb_set_interface() failed");
  1134. kfree( ether_dev );
  1135. return NULL;
  1136. }
  1137. } else {
  1138. // We didn't find an alternate setting for the data
  1139. // interface that would let us turn off traffic.
  1140. // Oh well, let's go ahead and do what we must...
  1141. if (usb_set_interface( usb, 
  1142.                        ether_dev->data_bInterfaceNumber, 
  1143.                        ether_dev->data_bAlternateSetting_with_traffic)) {
  1144. err("usb_set_interface() failed");
  1145. kfree( ether_dev );
  1146. return NULL;
  1147. }
  1148. }
  1149. // Now we need to get a kernel Ethernet interface.
  1150. net = init_etherdev( NULL, 0 );
  1151. if ( !net ) {
  1152. // Hmm...  The kernel is not sharing today...
  1153. // Fine, we didn't want it anyway...
  1154. err( "Unable to initialize ethernet device" );
  1155. kfree( ether_dev );
  1156. return NULL;
  1157. }
  1158. // Now that we have an ethernet device, let's set it up
  1159. // (And I don't mean "set [it] up the bomb".)
  1160. net->priv = ether_dev;
  1161. SET_MODULE_OWNER(net);
  1162. net->open = CDCEther_open;
  1163. net->stop = CDCEther_close;
  1164. net->watchdog_timeo = CDC_ETHER_TX_TIMEOUT;
  1165. net->tx_timeout = CDCEther_tx_timeout;   // TX timeout function
  1166. net->do_ioctl = CDCEther_ioctl;
  1167. net->hard_start_xmit = CDCEther_start_xmit;
  1168. net->set_multicast_list = CDCEther_set_multicast;
  1169. net->get_stats = CDCEther_netdev_stats;
  1170. net->mtu = ether_dev->wMaxSegmentSize - 14;
  1171. // We'll keep track of this information for later...
  1172. ether_dev->usb = usb;
  1173. ether_dev->net = net;
  1174. // and don't forget the MAC address.
  1175. set_ethernet_addr( ether_dev );
  1176. // Send a message to syslog about what we are handling
  1177. log_device_info( ether_dev );
  1178. /* We need to manually claim the data interface, while the comm interface gets claimed in the return */
  1179. usb_driver_claim_interface( &CDCEther_driver, 
  1180.                             &(usb->config[ether_dev->configuration_num].interface[ether_dev->data_interface]), 
  1181.                             ether_dev );
  1182. // Does this REALLY do anything???
  1183. usb_inc_dev_use( usb );
  1184. // Okay, we are finally done...
  1185. return ether_dev;
  1186. }
  1187. //////////////////////////////////////////////////////////////////////////////
  1188. // Module's disconnect routine ///////////////////////////////////////////////
  1189. // Called when the driver is unloaded or the device is unplugged /////////////
  1190. // (Whichever happens first assuming the driver suceeded at its probe) ///////
  1191. //////////////////////////////////////////////////////////////////////////////
  1192. static void CDCEther_disconnect( struct usb_device *usb, void *ptr )
  1193. {
  1194. ether_dev_t *ether_dev = ptr;
  1195. // Sanity check!!!
  1196. if ( !ether_dev || !ether_dev->usb ) {
  1197. // We failed.  We are insane!!!
  1198. warn("unregistering non-existant device");
  1199. return;
  1200. }
  1201. // Make sure we fail the sanity check if we try this again.
  1202. ether_dev->usb = NULL;
  1203. // It is possible that this function is called before
  1204. // the "close" function.
  1205. // This tells the close function we are already disconnected
  1206. ether_dev->flags |= CDC_ETHER_UNPLUG;
  1207. // We don't need the network device any more
  1208. unregister_netdev( ether_dev->net );
  1209. // For sanity checks
  1210. ether_dev->net = NULL;
  1211. // I ask again, does this do anything???
  1212. usb_dec_dev_use( usb );
  1213. // We are done with this interface
  1214. usb_driver_release_interface( &CDCEther_driver, 
  1215.                               &(usb->config[ether_dev->configuration_num].interface[ether_dev->comm_interface]) );
  1216. // We are done with this interface too
  1217. usb_driver_release_interface( &CDCEther_driver, 
  1218.                               &(usb->config[ether_dev->configuration_num].interface[ether_dev->data_interface]) );
  1219. // No more tied up kernel memory
  1220. kfree( ether_dev );
  1221. // This does no good, but it looks nice!
  1222. ether_dev = NULL;
  1223. }
  1224. //////////////////////////////////////////////////////////////////////////////
  1225. // Driver info ///////////////////////////////////////////////////////////////
  1226. //////////////////////////////////////////////////////////////////////////////
  1227. static struct usb_driver CDCEther_driver = {
  1228. name: "CDCEther",
  1229. probe: CDCEther_probe,
  1230. disconnect: CDCEther_disconnect,
  1231. id_table: CDCEther_ids,
  1232. };
  1233. //////////////////////////////////////////////////////////////////////////////
  1234. // init and exit routines called when driver is installed and uninstalled ////
  1235. //////////////////////////////////////////////////////////////////////////////
  1236. int __init CDCEther_init(void)
  1237. {
  1238. dbg( "%s", version );
  1239. return usb_register( &CDCEther_driver );
  1240. }
  1241. void __exit CDCEther_exit(void)
  1242. {
  1243. usb_deregister( &CDCEther_driver );
  1244. }
  1245. //////////////////////////////////////////////////////////////////////////////
  1246. // Module info ///////////////////////////////////////////////////////////////
  1247. //////////////////////////////////////////////////////////////////////////////
  1248. module_init( CDCEther_init );
  1249. module_exit( CDCEther_exit );
  1250. MODULE_AUTHOR("Brad Hards and another");
  1251. MODULE_DESCRIPTION("USB CDC Ethernet driver");
  1252. MODULE_LICENSE("GPL");
  1253. MODULE_DEVICE_TABLE (usb, CDCEther_ids);
  1254. MODULE_PARM (multicast_filter_limit, "i");
  1255. MODULE_PARM_DESC (multicast_filter_limit, "CDCEther maximum number of filtered multicast addresses");
  1256. //////////////////////////////////////////////////////////////////////////////
  1257. // End of file ///////////////////////////////////////////////////////////////
  1258. //////////////////////////////////////////////////////////////////////////////