kaweth.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:30k
源码类别:

嵌入式Linux

开发平台:

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