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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /****************************************************************
  2.  *
  3.  *     kaweth.c - driver for KL5KUSB101 based USB->Ethernet
  4.  *
  5.  *     (c) 2000 Interlan Communications
  6.  *     (c) 2000 Stephane Alnet
  7.  *     (C) 2001 Brad Hards
  8.  *     (C) 2002 Oliver Neukum
  9.  *
  10.  *     Original author: The Zapman <zapman@interlan.net>
  11.  *     Inspired by, and much credit goes to Michael Rothwell
  12.  *     <rothwell@interlan.net> for the test equipment, help, and patience
  13.  *     Based off of (and with thanks to) Petko Manolov's pegaus.c driver.
  14.  *     Also many thanks to Joel Silverman and Ed Surprenant at Kawasaki
  15.  *     for providing the firmware and driver resources.
  16.  *
  17.  *     This program is free software; you can redistribute it and/or
  18.  *     modify it under the terms of the GNU General Public License as
  19.  *     published by the Free Software Foundation; either version 2, or
  20.  *     (at your option) any later version.
  21.  *
  22.  *     This program is distributed in the hope that it will be useful,
  23.  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25.  *     GNU General Public License for more details.
  26.  *
  27.  *     You should have received a copy of the GNU General Public License
  28.  *     along with this program; if not, write to the Free Software Foundation,
  29.  *     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  30.  *
  31.  ****************************************************************/
  32. /* TODO:
  33.  * Fix in_interrupt() problem
  34.  * Develop test procedures for USB net interfaces
  35.  * Run test procedures
  36.  * Fix bugs from previous two steps
  37.  * Snoop other OSs for any tricks we're not doing
  38.  * SMP locking
  39.  * Reduce arbitrary timeouts
  40.  * Smart multicast support
  41.  * Temporary MAC change support
  42.  * Tunable SOFs parameter - ioctl()?
  43.  * Ethernet stats collection
  44.  * Code formatting improvements
  45.  */
  46. #include <linux/module.h>
  47. #include <linux/sched.h>
  48. #include <linux/slab.h>
  49. #include <linux/string.h>
  50. #include <linux/init.h>
  51. #include <linux/delay.h>
  52. #include <linux/netdevice.h>
  53. #include <linux/etherdevice.h>
  54. #include <linux/usb.h>
  55. #include <linux/types.h>
  56. #include <linux/ethtool.h>
  57. #include <asm/uaccess.h>
  58. #include <asm/semaphore.h>
  59. #include <asm/byteorder.h>
  60. #define DEBUG
  61. #ifdef DEBUG
  62. #define kaweth_dbg(format, arg...) printk(KERN_DEBUG __FILE__ ": " format "n" ,##arg)
  63. #else
  64. #define kaweth_dbg(format, arg...) do {} while (0)
  65. #endif
  66. #define kaweth_err(format, arg...) printk(KERN_ERR __FILE__ ": " format "n" ,##arg)
  67. #define kaweth_info(format, arg...) printk(KERN_INFO __FILE__ ": " format "n" , ##arg)
  68. #define kaweth_warn(format, arg...) printk(KERN_WARNING __FILE__ ": " format "n" , ##arg)
  69. #include "kawethfw.h"
  70. #define KAWETH_MTU 1514
  71. #define KAWETH_BUF_SIZE 1664
  72. #define KAWETH_TX_TIMEOUT (5 * HZ)
  73. #define KAWETH_SCRATCH_SIZE 32
  74. #define KAWETH_FIRMWARE_BUF_SIZE 4096
  75. #define KAWETH_CONTROL_TIMEOUT (30 * HZ)
  76. #define KAWETH_STATUS_BROKEN 0x0000001
  77. #define KAWETH_STATUS_CLOSING 0x0000002
  78. #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
  79. #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
  80. #define KAWETH_PACKET_FILTER_DIRECTED 0x04
  81. #define KAWETH_PACKET_FILTER_BROADCAST 0x08
  82. #define KAWETH_PACKET_FILTER_MULTICAST 0x10
  83. /* Table 7 */
  84. #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
  85. #define KAWETH_COMMAND_MULTICAST_FILTERS        0x01
  86. #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
  87. #define KAWETH_COMMAND_STATISTICS               0x03
  88. #define KAWETH_COMMAND_SET_TEMP_MAC      0x06
  89. #define KAWETH_COMMAND_GET_TEMP_MAC             0x07
  90. #define KAWETH_COMMAND_SET_URB_SIZE 0x08
  91. #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
  92. #define KAWETH_COMMAND_SCAN 0xFF
  93. #define KAWETH_SOFS_TO_WAIT 0x05
  94. #define INTBUFFERSIZE 4
  95. #define STATE_OFFSET 0
  96. #define STATE_MASK 0x40
  97. #define STATE_SHIFT 5
  98. MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr>, Brad Hards <bhards@bigpond.net.au> and Oliver Neukum <oliver@neukum.org>");
  99. MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
  100. MODULE_LICENSE("GPL");
  101. static void *kaweth_probe(
  102.             struct usb_device *dev,             /* the device */
  103.             unsigned ifnum,                     /* what interface */
  104.             const struct usb_device_id *id      /* from id_table */
  105. );
  106. static void kaweth_disconnect(struct usb_device *dev, void *ptr);
  107. int kaweth_internal_control_msg(struct usb_device *usb_dev, unsigned int pipe,
  108. struct usb_ctrlrequest *cmd, void *data,
  109. int len, int timeout);
  110. /****************************************************************
  111.  *     usb_device_id
  112.  ****************************************************************/
  113. static struct usb_device_id usb_klsi_table[] = {
  114. { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
  115. { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
  116. { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
  117. { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
  118. { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
  119. { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
  120. { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
  121. { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
  122. { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
  123. { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
  124. { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
  125. { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
  126. { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
  127. { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
  128. { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
  129. { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
  130. { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
  131. { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
  132. { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
  133. { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
  134. { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
  135. { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
  136. { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
  137. { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
  138. { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
  139. { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
  140. { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
  141. { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
  142. { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
  143. { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
  144. { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
  145. {} /* Null terminator */
  146. };
  147. MODULE_DEVICE_TABLE (usb, usb_klsi_table);
  148. /****************************************************************
  149.  *     kaweth_driver
  150.  ****************************************************************/
  151. static struct usb_driver kaweth_driver = {
  152. .name = "kaweth",
  153. .probe = kaweth_probe,
  154. .disconnect = kaweth_disconnect,
  155. .id_table =     usb_klsi_table,
  156. };
  157. typedef __u8 eth_addr_t[6];
  158. /****************************************************************
  159.  *     usb_eth_dev
  160.  ****************************************************************/
  161. struct usb_eth_dev {
  162. char *name;
  163. __u16 vendor;
  164. __u16 device;
  165. void *pdata;
  166. };
  167. /****************************************************************
  168.  *     kaweth_ethernet_configuration
  169.  *     Refer Table 8
  170.  ****************************************************************/
  171. struct kaweth_ethernet_configuration
  172. {
  173. __u8 size;
  174. __u8 reserved1;
  175. __u8 reserved2;
  176. eth_addr_t hw_addr;
  177. __u32 statistics_mask;
  178. __u16 segment_size;
  179. __u16 max_multicast_filters;
  180. __u8 reserved3;
  181. } __attribute__ ((packed));
  182. /****************************************************************
  183.  *     kaweth_device
  184.  ****************************************************************/
  185. struct kaweth_device
  186. {
  187. spinlock_t device_lock;
  188. __u32 status;
  189. int end;
  190. int removed;
  191. int suspend_lowmem;
  192. int linkstate;
  193. struct usb_device *dev;
  194. struct net_device *net;
  195. wait_queue_head_t term_wait;
  196. struct urb *rx_urb;
  197. struct urb *tx_urb;
  198. struct urb *irq_urb;
  199. struct sk_buff *tx_skb;
  200. __u8 *firmware_buf;
  201. __u8 scratch[KAWETH_SCRATCH_SIZE];
  202. __u8 rx_buf[KAWETH_BUF_SIZE];
  203. __u8 intbuffer[INTBUFFERSIZE];
  204. __u16 packet_filter_bitmap;
  205. struct kaweth_ethernet_configuration configuration;
  206. struct net_device_stats stats;
  207. } __attribute__ ((packed));
  208. /****************************************************************
  209.  *     kaweth_control
  210.  ****************************************************************/
  211. static int kaweth_control(struct kaweth_device *kaweth,
  212.   unsigned int pipe,
  213.   __u8 request,
  214.   __u8 requesttype,
  215.   __u16 value,
  216.   __u16 index,
  217.   void *data,
  218.   __u16 size,
  219.   int timeout)
  220. {
  221. struct usb_ctrlrequest *dr;
  222. kaweth_dbg("kaweth_control()");
  223. if(in_interrupt()) {
  224. kaweth_dbg("in_interrupt()");
  225. return -EBUSY;
  226. }
  227. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  228. if (!dr) {
  229. kaweth_dbg("kmalloc() failed");
  230. return -ENOMEM;
  231. }
  232. dr->bRequestType= requesttype;
  233. dr->bRequest = request;
  234. dr->wValue = cpu_to_le16p(&value);
  235. dr->wIndex = cpu_to_le16p(&index);
  236. dr->wLength = cpu_to_le16p(&size);
  237. return kaweth_internal_control_msg(kaweth->dev,
  238. pipe,
  239. dr,
  240. data,
  241. size,
  242. timeout);
  243. }
  244. /****************************************************************
  245.  *     kaweth_read_configuration
  246.  ****************************************************************/
  247. static int kaweth_read_configuration(struct kaweth_device *kaweth)
  248. {
  249. int retval;
  250. kaweth_dbg("Reading kaweth configuration");
  251. retval = kaweth_control(kaweth,
  252. usb_rcvctrlpipe(kaweth->dev, 0),
  253. KAWETH_COMMAND_GET_ETHERNET_DESC,
  254. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  255. 0,
  256. 0,
  257. (void *)&kaweth->configuration,
  258. sizeof(kaweth->configuration),
  259. KAWETH_CONTROL_TIMEOUT);
  260. return retval;
  261. }
  262. /****************************************************************
  263.  *     kaweth_set_urb_size
  264.  ****************************************************************/
  265. static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
  266. {
  267. int retval;
  268. kaweth_dbg("Setting URB size to %d", (unsigned)urb_size);
  269. retval = kaweth_control(kaweth,
  270. usb_sndctrlpipe(kaweth->dev, 0),
  271. KAWETH_COMMAND_SET_URB_SIZE,
  272. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  273. urb_size,
  274. 0,
  275. (void *)&kaweth->scratch,
  276. 0,
  277. KAWETH_CONTROL_TIMEOUT);
  278. return retval;
  279. }
  280. /****************************************************************
  281.  *     kaweth_set_sofs_wait
  282.  ****************************************************************/
  283. static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
  284. {
  285. int retval;
  286. kaweth_dbg("Set SOFS wait to %d", (unsigned)sofs_wait);
  287. retval = kaweth_control(kaweth,
  288. usb_sndctrlpipe(kaweth->dev, 0),
  289. KAWETH_COMMAND_SET_SOFS_WAIT,
  290. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  291. sofs_wait,
  292. 0,
  293. (void *)&kaweth->scratch,
  294. 0,
  295. KAWETH_CONTROL_TIMEOUT);
  296. return retval;
  297. }
  298. /****************************************************************
  299.  *     kaweth_set_receive_filter
  300.  ****************************************************************/
  301. static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
  302.      __u16 receive_filter)
  303. {
  304. int retval;
  305. kaweth_dbg("Set receive filter to %d", (unsigned)receive_filter);
  306. retval = kaweth_control(kaweth,
  307. usb_sndctrlpipe(kaweth->dev, 0),
  308. KAWETH_COMMAND_SET_PACKET_FILTER,
  309. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  310. receive_filter,
  311. 0,
  312. (void *)&kaweth->scratch,
  313. 0,
  314. KAWETH_CONTROL_TIMEOUT);
  315. return retval;
  316. }
  317. /****************************************************************
  318.  *     kaweth_download_firmware
  319.  ****************************************************************/
  320. static int kaweth_download_firmware(struct kaweth_device *kaweth,
  321.     __u8 *data,
  322.     __u16 data_len,
  323.     __u8 interrupt,
  324.     __u8 type)
  325. {
  326. if(data_len > KAWETH_FIRMWARE_BUF_SIZE) {
  327. kaweth_err("Firmware too big: %d", data_len);
  328. return -ENOSPC;
  329. }
  330. memcpy(kaweth->firmware_buf, data, data_len);
  331. kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
  332. kaweth->firmware_buf[3] = data_len >> 8;
  333. kaweth->firmware_buf[4] = type;
  334. kaweth->firmware_buf[5] = interrupt;
  335. kaweth_dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
  336.    kaweth->firmware_buf[2]);
  337. kaweth_dbg("Downloading firmware at %p to kaweth device at %p",
  338.     data,
  339.     kaweth);
  340. kaweth_dbg("Firmware length: %d", data_len);
  341. return kaweth_control(kaweth,
  342.               usb_sndctrlpipe(kaweth->dev, 0),
  343.       KAWETH_COMMAND_SCAN,
  344.       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  345.       0,
  346.       0,
  347.       (void *)kaweth->firmware_buf,
  348.       data_len,
  349.       KAWETH_CONTROL_TIMEOUT);
  350. }
  351. /****************************************************************
  352.  *     kaweth_trigger_firmware
  353.  ****************************************************************/
  354. static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
  355.    __u8 interrupt)
  356. {
  357. kaweth->firmware_buf[0] = 0xB6;
  358. kaweth->firmware_buf[1] = 0xC3;
  359. kaweth->firmware_buf[2] = 0x01;
  360. kaweth->firmware_buf[3] = 0x00;
  361. kaweth->firmware_buf[4] = 0x06;
  362. kaweth->firmware_buf[5] = interrupt;
  363. kaweth->firmware_buf[6] = 0x00;
  364. kaweth->firmware_buf[7] = 0x00;
  365. kaweth_dbg("Triggering firmware");
  366. return kaweth_control(kaweth,
  367.       usb_sndctrlpipe(kaweth->dev, 0),
  368.       KAWETH_COMMAND_SCAN,
  369.       USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  370.       0,
  371.       0,
  372.       (void *)kaweth->firmware_buf,
  373.       8,
  374.       KAWETH_CONTROL_TIMEOUT);
  375. }
  376. /****************************************************************
  377.  *     kaweth_reset
  378.  ****************************************************************/
  379. static int kaweth_reset(struct kaweth_device *kaweth)
  380. {
  381. int result;
  382. kaweth_dbg("kaweth_reset(%p)", kaweth);
  383. result = kaweth_control(kaweth,
  384. usb_sndctrlpipe(kaweth->dev, 0),
  385. USB_REQ_SET_CONFIGURATION,
  386. 0,
  387. kaweth->dev->config[0].bConfigurationValue,
  388. 0,
  389. NULL,
  390. 0,
  391. KAWETH_CONTROL_TIMEOUT);
  392. udelay(10000);
  393. kaweth_dbg("kaweth_reset() returns %d.",result);
  394. return result;
  395. }
  396. static void kaweth_usb_receive(struct urb *);
  397. static void kaweth_resubmit_rx_urb(struct kaweth_device *);
  398. /****************************************************************
  399. int_callback
  400. *****************************************************************/
  401. static void int_callback(struct urb *u)
  402. {
  403. struct kaweth_device *kaweth = u->context;
  404. int act_state;
  405. /* we abuse the interrupt urb for rebsubmitting under low memory saving a timer */
  406. if (kaweth->suspend_lowmem)
  407. kaweth_resubmit_rx_urb(kaweth);
  408. /* we check the link state to report changes */
  409. if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
  410. if (!act_state)
  411. netif_carrier_on(kaweth->net);
  412. else
  413. netif_carrier_off(kaweth->net);
  414. kaweth->linkstate = act_state;
  415. }
  416. }
  417. /****************************************************************
  418.  *     kaweth_resubmit_rx_urb
  419.  ****************************************************************/
  420. static void kaweth_resubmit_rx_urb(struct kaweth_device *kaweth)
  421. {
  422. int result;
  423. long flags;
  424. FILL_BULK_URB(kaweth->rx_urb,
  425.       kaweth->dev,
  426.       usb_rcvbulkpipe(kaweth->dev, 1),
  427.       kaweth->rx_buf,
  428.       KAWETH_BUF_SIZE,
  429.       kaweth_usb_receive,
  430.       kaweth);
  431. spin_lock_irqsave(&kaweth->device_lock, flags);
  432. if (!kaweth->removed) { /* no resubmit if disconnecting */
  433. if((result = usb_submit_urb(kaweth->rx_urb))) {
  434. if (result == -ENOMEM)
  435. kaweth->suspend_lowmem = 1;
  436. kaweth_err("resubmitting rx_urb %d failed", result);
  437. } else {
  438. kaweth->suspend_lowmem = 0;
  439. }
  440. }
  441. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  442. }
  443. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
  444. /****************************************************************
  445.  *     kaweth_usb_receive
  446.  ****************************************************************/
  447. static void kaweth_usb_receive(struct urb *urb)
  448. {
  449. struct kaweth_device *kaweth = urb->context;
  450. struct net_device *net = kaweth->net;
  451. int count = urb->actual_length;
  452. int count2 = urb->transfer_buffer_length;
  453. __u16 pkt_len = le16_to_cpup((u16 *)kaweth->rx_buf);
  454. struct sk_buff *skb;
  455. if(urb->status == -ECONNRESET || urb->status == -ECONNABORTED)
  456. /* we are killed - set a flag and wake the disconnect handler */
  457. {
  458. kaweth->end = 1;
  459. wake_up(&kaweth->term_wait);
  460. return;
  461. }
  462. if (kaweth->status & KAWETH_STATUS_CLOSING)
  463. return;
  464. if(urb->status && urb->status != -EREMOTEIO && count != 1) {
  465. kaweth_err("%s RX status: %d count: %d packet_len: %d",
  466.                            net->name,
  467.    urb->status,
  468.    count,
  469.    (int)pkt_len);
  470. kaweth_resubmit_rx_urb(kaweth);
  471.                 return;
  472. }
  473. if(kaweth->net && (count > 2)) {
  474. if(pkt_len > (count - 2)) {
  475. kaweth_err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
  476. kaweth_err("Packet len & 2047: %x", pkt_len & 2047);
  477. kaweth_err("Count 2: %x", count2);
  478.         kaweth_resubmit_rx_urb(kaweth);
  479.                         return;
  480.                 }
  481. if(!(skb = dev_alloc_skb(pkt_len+2))) {
  482.         kaweth_resubmit_rx_urb(kaweth);
  483.                         return;
  484. }
  485. skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
  486. skb->dev = net;
  487. eth_copy_and_sum(skb, kaweth->rx_buf + 2, pkt_len, 0);
  488. skb_put(skb, pkt_len);
  489. skb->protocol = eth_type_trans(skb, net);
  490. netif_rx(skb);
  491. kaweth->stats.rx_packets++;
  492. kaweth->stats.rx_bytes += pkt_len;
  493. }
  494. kaweth_resubmit_rx_urb(kaweth);
  495. }
  496. /****************************************************************
  497.  *     kaweth_open
  498.  ****************************************************************/
  499. static int kaweth_open(struct net_device *net)
  500. {
  501. struct kaweth_device *kaweth = (struct kaweth_device *)net->priv;
  502. kaweth_dbg("Dev usage: %d", kaweth->dev->refcnt.counter);
  503. kaweth_dbg("Opening network device.");
  504. MOD_INC_USE_COUNT;
  505. kaweth_resubmit_rx_urb(kaweth);
  506. FILL_INT_URB(
  507. kaweth->irq_urb,
  508. kaweth->dev,
  509. usb_rcvintpipe(kaweth->dev, 3),
  510. kaweth->intbuffer,
  511. INTBUFFERSIZE,
  512. int_callback,
  513. kaweth,
  514. HZ/4);
  515. usb_submit_urb(kaweth->irq_urb);
  516. netif_start_queue(net);
  517. kaweth_async_set_rx_mode(kaweth);
  518. return 0;
  519. }
  520. /****************************************************************
  521.  *     kaweth_close
  522.  ****************************************************************/
  523. static int kaweth_close(struct net_device *net)
  524. {
  525. struct kaweth_device *kaweth = net->priv;
  526. netif_stop_queue(net);
  527. spin_lock_irq(&kaweth->device_lock);
  528. kaweth->status |= KAWETH_STATUS_CLOSING;
  529. spin_unlock_irq(&kaweth->device_lock);
  530. usb_unlink_urb(kaweth->irq_urb);
  531. usb_unlink_urb(kaweth->rx_urb);
  532. kaweth->status &= ~KAWETH_STATUS_CLOSING;
  533. MOD_DEC_USE_COUNT;
  534. printk("Dev usage: %d", kaweth->dev->refcnt.counter);
  535. return 0;
  536. }
  537. static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
  538. {
  539. u32 ethcmd;
  540. if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
  541. return -EFAULT;
  542. switch (ethcmd) {
  543. case ETHTOOL_GDRVINFO: {
  544. struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
  545. strncpy(info.driver, "kaweth", sizeof(info.driver)-1);
  546. if (copy_to_user(useraddr, &info, sizeof(info)))
  547. return -EFAULT;
  548. return 0;
  549. }
  550. }
  551. return -EOPNOTSUPP;
  552. }
  553. /****************************************************************
  554.  *     kaweth_ioctl
  555.  ****************************************************************/
  556. static int kaweth_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
  557. {
  558. switch (cmd) {
  559. case SIOCETHTOOL:
  560. return netdev_ethtool_ioctl(net, (void *) rq->ifr_data);
  561. }
  562. return -EOPNOTSUPP;
  563. }
  564. /****************************************************************
  565.  *     kaweth_usb_transmit_complete
  566.  ****************************************************************/
  567. static void kaweth_usb_transmit_complete(struct urb *urb)
  568. {
  569. struct kaweth_device *kaweth = urb->context;
  570. struct sk_buff *skb = kaweth->tx_skb;
  571. if (urb->status != 0)
  572. kaweth_dbg("%s: TX status %d.", kaweth->net->name, urb->status);
  573. netif_wake_queue(kaweth->net);
  574. dev_kfree_skb(skb);
  575. }
  576. /****************************************************************
  577.  *     kaweth_start_xmit
  578.  ****************************************************************/
  579. static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
  580. {
  581. struct kaweth_device *kaweth = net->priv;
  582. char *private_header;
  583. int res;
  584. spin_lock(&kaweth->device_lock);
  585. if (kaweth->removed) {
  586. /* our device is undergoing disconnection - we bail out */
  587. spin_unlock(&kaweth->device_lock);
  588. dev_kfree_skb(skb);
  589. return 0;
  590. }
  591. kaweth_async_set_rx_mode(kaweth);
  592. netif_stop_queue(net);
  593. /* We now decide whether we can put our special header into the sk_buff */
  594. if (skb_cloned(skb) || skb_headroom(skb) < 2) {
  595. /* no such luck - we make our own */
  596. struct sk_buff *copied_skb;
  597. copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
  598. dev_kfree_skb_any(skb);
  599. skb = copied_skb;
  600. if (!copied_skb) {
  601. kaweth->stats.tx_errors++;
  602. netif_start_queue(net);
  603. spin_unlock(&kaweth->device_lock);
  604. return 0;
  605. }
  606. }
  607. private_header = __skb_push(skb, 2);
  608. *private_header = cpu_to_le16(skb->len);
  609. kaweth->tx_skb = skb;
  610. FILL_BULK_URB(kaweth->tx_urb,
  611.       kaweth->dev,
  612.       usb_sndbulkpipe(kaweth->dev, 2),
  613.       private_header,
  614.       skb->len,
  615.       kaweth_usb_transmit_complete,
  616.       kaweth);
  617. kaweth->end = 0;
  618. kaweth->tx_urb->transfer_flags |= USB_ASYNC_UNLINK;
  619. if((res = usb_submit_urb(kaweth->tx_urb)))
  620. {
  621. kaweth_warn("kaweth failed tx_urb %d", res);
  622. kaweth->stats.tx_errors++;
  623. netif_start_queue(net);
  624. dev_kfree_skb(skb);
  625. }
  626. else
  627. {
  628. kaweth->stats.tx_packets++;
  629. kaweth->stats.tx_bytes += skb->len;
  630. net->trans_start = jiffies;
  631. }
  632. spin_unlock(&kaweth->device_lock);
  633. return 0;
  634. }
  635. /****************************************************************
  636.  *     kaweth_set_rx_mode
  637.  ****************************************************************/
  638. static void kaweth_set_rx_mode(struct net_device *net)
  639. {
  640. struct kaweth_device *kaweth = net->priv;
  641. __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
  642.                                      KAWETH_PACKET_FILTER_BROADCAST |
  643.                      KAWETH_PACKET_FILTER_MULTICAST;
  644. kaweth_dbg("Setting Rx mode to %d", packet_filter_bitmap);
  645. netif_stop_queue(net);
  646. if (net->flags & IFF_PROMISC) {
  647. packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
  648. }
  649. else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
  650. packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
  651. }
  652. kaweth->packet_filter_bitmap = packet_filter_bitmap;
  653. netif_wake_queue(net);
  654. }
  655. /****************************************************************
  656.  *     kaweth_async_set_rx_mode
  657.  ****************************************************************/
  658. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
  659. {
  660. __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
  661. kaweth->packet_filter_bitmap = 0;
  662. if(packet_filter_bitmap == 0) return;
  663. {
  664. int result;
  665. result = kaweth_control(kaweth,
  666. usb_sndctrlpipe(kaweth->dev, 0),
  667. KAWETH_COMMAND_SET_PACKET_FILTER,
  668. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  669. packet_filter_bitmap,
  670. 0,
  671. (void *)&kaweth->scratch,
  672. 0,
  673. KAWETH_CONTROL_TIMEOUT);
  674. if(result < 0) {
  675. kaweth_err("Failed to set Rx mode: %d", result);
  676. }
  677. else {
  678. kaweth_dbg("Set Rx mode to %d", packet_filter_bitmap);
  679. }
  680. }
  681. }
  682. /****************************************************************
  683.  *     kaweth_netdev_stats
  684.  ****************************************************************/
  685. static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
  686. {
  687. return &((struct kaweth_device *)dev->priv)->stats;
  688. }
  689. /****************************************************************
  690.  *     kaweth_tx_timeout
  691.  ****************************************************************/
  692. static void kaweth_tx_timeout(struct net_device *net)
  693. {
  694. struct kaweth_device *kaweth = net->priv;
  695. kaweth_warn("%s: Tx timed out. Resetting.", net->name);
  696. kaweth->stats.tx_errors++;
  697. net->trans_start = jiffies;
  698. usb_unlink_urb(kaweth->tx_urb);
  699. }
  700. /****************************************************************
  701.  *     kaweth_probe
  702.  ****************************************************************/
  703. static void *kaweth_probe(
  704.             struct usb_device *dev,             /* the device */
  705.             unsigned ifnum,                      /* what interface */
  706.             const struct usb_device_id *id      /* from id_table */
  707. )
  708. {
  709. struct kaweth_device *kaweth;
  710. const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  711. int result = 0;
  712. kaweth_dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
  713.  dev->devnum,
  714.  (int)dev->descriptor.idVendor,
  715.  (int)dev->descriptor.idProduct,
  716.  (int)dev->descriptor.bcdDevice);
  717. kaweth_dbg("Device at %p", dev);
  718. kaweth_dbg("Descriptor length: %x type: %x",
  719.  (int)dev->descriptor.bLength,
  720.  (int)dev->descriptor.bDescriptorType);
  721. if(!(kaweth = kmalloc(sizeof(struct kaweth_device), GFP_KERNEL))) {
  722. kaweth_dbg("out of memory allocating device structuren");
  723. return NULL;
  724. }
  725. memset(kaweth, 0, sizeof(struct kaweth_device));
  726. kaweth->dev = dev;
  727. spin_lock_init(&kaweth->device_lock);
  728. init_waitqueue_head(&kaweth->term_wait);
  729. kaweth_dbg("Resetting.");
  730. kaweth_reset(kaweth);
  731. /*
  732.  * If high byte of bcdDevice is nonzero, firmware is already
  733.  * downloaded. Don't try to do it again, or we'll hang the device.
  734.  */
  735. if (dev->descriptor.bcdDevice >> 8) {
  736. kaweth_info("Firmware present in device.");
  737. } else {
  738. /* Download the firmware */
  739. kaweth_info("Downloading firmware...");
  740. kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
  741. if ((result = kaweth_download_firmware(kaweth,
  742.       kaweth_new_code,
  743.       len_kaweth_new_code,
  744.       100,
  745.       2)) < 0) {
  746. kaweth_err("Error downloading firmware (%d)", result);
  747. free_page((unsigned long)kaweth->firmware_buf);
  748. kfree(kaweth);
  749. return NULL;
  750. }
  751. if ((result = kaweth_download_firmware(kaweth,
  752.       kaweth_new_code_fix,
  753.       len_kaweth_new_code_fix,
  754.       100,
  755.       3)) < 0) {
  756. kaweth_err("Error downloading firmware fix (%d)", result);
  757. free_page((unsigned long)kaweth->firmware_buf);
  758. kfree(kaweth);
  759. return NULL;
  760. }
  761. if ((result = kaweth_download_firmware(kaweth,
  762.       kaweth_trigger_code,
  763.       len_kaweth_trigger_code,
  764.       126,
  765.       2)) < 0) {
  766. kaweth_err("Error downloading trigger code (%d)", result);
  767. free_page((unsigned long)kaweth->firmware_buf);
  768. kfree(kaweth);
  769. return NULL;
  770. }
  771. if ((result = kaweth_download_firmware(kaweth,
  772.       kaweth_trigger_code_fix,
  773.       len_kaweth_trigger_code_fix,
  774.       126,
  775.       3)) < 0) {
  776. kaweth_err("Error downloading trigger code fix (%d)", result);
  777. free_page((unsigned long)kaweth->firmware_buf);
  778. kfree(kaweth);
  779. return NULL;
  780. }
  781. if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
  782. kaweth_err("Error triggering firmware (%d)", result);
  783. free_page((unsigned long)kaweth->firmware_buf);
  784. kfree(kaweth);
  785. return NULL;
  786. }
  787. /* Device will now disappear for a moment...  */
  788. kaweth_info("Firmware loaded.  I'll be back...");
  789. free_page((unsigned long)kaweth->firmware_buf);
  790. kfree(kaweth);
  791. return NULL;
  792. }
  793. result = kaweth_read_configuration(kaweth);
  794. if(result < 0) {
  795. kaweth_err("Error reading configuration (%d), no net device created", result);
  796. kfree(kaweth);
  797. return NULL;
  798. }
  799. kaweth_info("Statistics collection: %x", kaweth->configuration.statistics_mask);
  800. kaweth_info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
  801. kaweth_info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size));
  802. kaweth_info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
  803.  (int)kaweth->configuration.hw_addr[0],
  804.  (int)kaweth->configuration.hw_addr[1],
  805.  (int)kaweth->configuration.hw_addr[2],
  806.  (int)kaweth->configuration.hw_addr[3],
  807.  (int)kaweth->configuration.hw_addr[4],
  808.  (int)kaweth->configuration.hw_addr[5]);
  809. if(!memcmp(&kaweth->configuration.hw_addr,
  810.                    &bcast_addr,
  811.    sizeof(bcast_addr))) {
  812. kaweth_err("Firmware not functioning properly, no net device created");
  813. kfree(kaweth);
  814. return NULL;
  815. }
  816. if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
  817. kaweth_dbg("Error setting URB size");
  818. return kaweth;
  819. }
  820. if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
  821. kaweth_err("Error setting SOFS wait");
  822. return kaweth;
  823. }
  824. result = kaweth_set_receive_filter(kaweth,
  825.                                            KAWETH_PACKET_FILTER_DIRECTED |
  826.                                            KAWETH_PACKET_FILTER_BROADCAST |
  827.                                            KAWETH_PACKET_FILTER_MULTICAST);
  828. if(result < 0) {
  829. kaweth_err("Error setting receive filter");
  830. return kaweth;
  831. }
  832. kaweth_dbg("Initializing net device.");
  833. kaweth->tx_urb = usb_alloc_urb(0);
  834. if (!kaweth->tx_urb)
  835. goto err_no_urb;
  836. kaweth->rx_urb = usb_alloc_urb(0);
  837. if (!kaweth->rx_urb)
  838. goto err_only_tx;
  839. kaweth->irq_urb = usb_alloc_urb(0);
  840. if (!kaweth->irq_urb)
  841. goto err_tx_and_rx;
  842. kaweth->net = init_etherdev(0, 0);
  843. if (!kaweth->net) {
  844. kaweth_err("Error calling init_etherdev.");
  845. return kaweth;
  846. }
  847. memcpy(kaweth->net->broadcast, &bcast_addr, sizeof(bcast_addr));
  848. memcpy(kaweth->net->dev_addr,
  849.                &kaweth->configuration.hw_addr,
  850.                sizeof(kaweth->configuration.hw_addr));
  851. kaweth->net->priv = kaweth;
  852. kaweth->net->open = kaweth_open;
  853. kaweth->net->stop = kaweth_close;
  854. kaweth->net->watchdog_timeo = KAWETH_TX_TIMEOUT;
  855. kaweth->net->tx_timeout = kaweth_tx_timeout;
  856. kaweth->net->do_ioctl = kaweth_ioctl;
  857. kaweth->net->hard_start_xmit = kaweth_start_xmit;
  858. kaweth->net->set_multicast_list = kaweth_set_rx_mode;
  859. kaweth->net->get_stats = kaweth_netdev_stats;
  860. kaweth->net->mtu = le16_to_cpu(kaweth->configuration.segment_size);
  861. memset(&kaweth->stats, 0, sizeof(kaweth->stats));
  862. kaweth_info("kaweth interface created at %s", kaweth->net->name);
  863. kaweth_dbg("Kaweth probe returning.");
  864. return kaweth;
  865. err_tx_and_rx:
  866. usb_free_urb(kaweth->rx_urb);
  867. err_only_tx:
  868. usb_free_urb(kaweth->tx_urb);
  869. err_no_urb:
  870. kfree(kaweth);
  871. return NULL;
  872. }
  873. /****************************************************************
  874.  *     kaweth_disconnect
  875.  ****************************************************************/
  876. static void kaweth_disconnect(struct usb_device *dev, void *ptr)
  877. {
  878. struct kaweth_device *kaweth = ptr;
  879. kaweth_info("Unregistering");
  880. if (!kaweth) {
  881. kaweth_warn("unregistering non-existant device");
  882. return;
  883. }
  884. kaweth->removed = 1;
  885. usb_unlink_urb(kaweth->irq_urb);
  886. usb_unlink_urb(kaweth->rx_urb);
  887. /* we need to wait for the urb to be cancelled, if it is active */
  888. spin_lock_irq(&kaweth->device_lock);
  889. if (usb_unlink_urb(kaweth->tx_urb) == -EINPROGRESS) {
  890. spin_unlock_irq(&kaweth->device_lock);
  891. wait_event(kaweth->term_wait, kaweth->end);
  892. } else {
  893. spin_unlock_irq(&kaweth->device_lock);
  894. }
  895. if(kaweth->net) {
  896. if(kaweth->net->flags & IFF_UP) {
  897. kaweth_dbg("Closing net device");
  898. dev_close(kaweth->net);
  899. }
  900. kaweth_dbg("Unregistering net device");
  901. unregister_netdev(kaweth->net);
  902. }
  903. usb_free_urb(kaweth->rx_urb);
  904. usb_free_urb(kaweth->tx_urb);
  905. kfree(kaweth);
  906. }
  907. // FIXME this completion stuff is a modified clone of
  908. // an OLD version of some stuff in usb.c ...
  909. struct kw_api_data {
  910. wait_queue_head_t wqh;
  911. int done;
  912. };
  913. /*-------------------------------------------------------------------*
  914.  * completion handler for compatibility wrappers (sync control/bulk) *
  915.  *-------------------------------------------------------------------*/
  916. static void usb_api_blocking_completion(struct urb *urb)
  917. {
  918.         struct kw_api_data *awd = (struct kw_api_data *)urb->context;
  919. awd->done=1;
  920. wake_up(&awd->wqh);
  921. }
  922. /*-------------------------------------------------------------------*
  923.  *                         COMPATIBILITY STUFF                       *
  924.  *-------------------------------------------------------------------*/
  925. // Starts urb and waits for completion or timeout
  926. static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
  927. {
  928.         DECLARE_WAITQUEUE(wait, current);
  929. struct kw_api_data awd;
  930.         int status;
  931.         init_waitqueue_head(&awd.wqh);
  932.         awd.done = 0;
  933.         set_current_state(TASK_INTERRUPTIBLE);
  934.         add_wait_queue(&awd.wqh, &wait);
  935.         urb->context = &awd;
  936.         status = usb_submit_urb(urb);
  937.         if (status) {
  938.                 // something went wrong
  939.                 usb_free_urb(urb);
  940.                 set_current_state(TASK_RUNNING);
  941.                 remove_wait_queue(&awd.wqh, &wait);
  942.                 return status;
  943.         }
  944. while (timeout && !awd.done)
  945. timeout = schedule_timeout(timeout);
  946.         set_current_state(TASK_RUNNING);
  947.         remove_wait_queue(&awd.wqh, &wait);
  948.         if (!timeout) {
  949.                 // timeout
  950.                 kaweth_warn("usb_control/bulk_msg: timeout");
  951.                 usb_unlink_urb(urb);  // remove urb safely
  952.                 status = -ETIMEDOUT;
  953.         }
  954. else {
  955.                 status = urb->status;
  956. }
  957.         if (actual_length) {
  958.                 *actual_length = urb->actual_length;
  959. }
  960.         usb_free_urb(urb);
  961.         return status;
  962. }
  963. /*-------------------------------------------------------------------*/
  964. // returns status (negative) or length (positive)
  965. int kaweth_internal_control_msg(struct usb_device *usb_dev, unsigned int pipe,
  966.                             struct usb_ctrlrequest *cmd, void *data, int len,
  967.     int timeout)
  968. {
  969.         struct urb *urb;
  970.         int retv;
  971.         int length;
  972.         urb = usb_alloc_urb(0);
  973.         if (!urb)
  974.                 return -ENOMEM;
  975.         FILL_CONTROL_URB(urb, usb_dev, pipe, (unsigned char*)cmd, data,
  976.  len, (usb_complete_t)usb_api_blocking_completion,0);
  977.         retv = usb_start_wait_urb(urb, timeout, &length);
  978.         if (retv < 0) {
  979.                 return retv;
  980. }
  981.         else {
  982.                 return length;
  983. }
  984. }
  985. /****************************************************************
  986.  *     kaweth_init
  987.  ****************************************************************/
  988. int __init kaweth_init(void)
  989. {
  990. kaweth_dbg("Driver loading");
  991. return usb_register(&kaweth_driver);
  992. }
  993. /****************************************************************
  994.  *     kaweth_exit
  995.  ****************************************************************/
  996. void __exit kaweth_exit(void)
  997. {
  998. usb_deregister(&kaweth_driver);
  999. }
  1000. module_init(kaweth_init);
  1001. module_exit(kaweth_exit);