catc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. * Copyright (c) 2001 Vojtech Pavlik
  3. *
  4. * CATC EL1210A NetMate USB Ethernet driver
  5. *
  6. * Sponsored by SuSE
  7. *
  8. * Based on the work of
  9. * Donald Becker
  10. *
  11. * Old chipset support added by Simon Evans <spse@secret.org.uk> 2002
  12. * - adds support for Belkin F5U011
  13. */
  14. /*
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. * Should you need to contact me, the author, you can do so either by
  30. * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
  31. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  32. */
  33. #include <linux/init.h>
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/string.h>
  37. #include <linux/slab.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/ethtool.h>
  43. #include <linux/crc32.h>
  44. #include <linux/bitops.h>
  45. #include <asm/uaccess.h>
  46. #undef DEBUG
  47. #include <linux/usb.h>
  48. /*
  49. * Version information.
  50. */
  51. #define DRIVER_VERSION "v2.8"
  52. #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@suse.cz>"
  53. #define DRIVER_DESC "CATC EL1210A NetMate USB Ethernet driver"
  54. #define SHORT_DRIVER_DESC "EL1210A NetMate USB Ethernet"
  55. MODULE_AUTHOR(DRIVER_AUTHOR);
  56. MODULE_DESCRIPTION(DRIVER_DESC);
  57. MODULE_LICENSE("GPL");
  58. static const char driver_name[] = "catc";
  59. /*
  60. * Some defines.
  61. */
  62. #define STATS_UPDATE (HZ) /* Time between stats updates */
  63. #define TX_TIMEOUT (5*HZ) /* Max time the queue can be stopped */
  64. #define PKT_SZ 1536 /* Max Ethernet packet size */
  65. #define RX_MAX_BURST 15 /* Max packets per rx buffer (> 0, < 16) */
  66. #define TX_MAX_BURST 15 /* Max full sized packets per tx buffer (> 0) */
  67. #define CTRL_QUEUE 16 /* Max control requests in flight (power of two) */
  68. #define RX_PKT_SZ 1600 /* Max size of receive packet for F5U011 */
  69. /*
  70. * Control requests.
  71. */
  72. enum control_requests {
  73. ReadMem = 0xf1,
  74. GetMac = 0xf2,
  75. Reset = 0xf4,
  76. SetMac = 0xf5,
  77. SetRxMode = 0xf5, /* F5U011 only */
  78. WriteROM = 0xf8,
  79. SetReg = 0xfa,
  80. GetReg = 0xfb,
  81. WriteMem = 0xfc,
  82. ReadROM = 0xfd,
  83. };
  84. /*
  85. * Registers.
  86. */
  87. enum register_offsets {
  88. TxBufCount = 0x20,
  89. RxBufCount = 0x21,
  90. OpModes = 0x22,
  91. TxQed = 0x23,
  92. RxQed = 0x24,
  93. MaxBurst = 0x25,
  94. RxUnit = 0x60,
  95. EthStatus = 0x61,
  96. StationAddr0 = 0x67,
  97. EthStats = 0x69,
  98. LEDCtrl = 0x81,
  99. };
  100. enum eth_stats {
  101. TxSingleColl = 0x00,
  102. TxMultiColl = 0x02,
  103. TxExcessColl = 0x04,
  104. RxFramErr = 0x06,
  105. };
  106. enum op_mode_bits {
  107. Op3MemWaits = 0x03,
  108. OpLenInclude = 0x08,
  109. OpRxMerge = 0x10,
  110. OpTxMerge = 0x20,
  111. OpWin95bugfix = 0x40,
  112. OpLoopback = 0x80,
  113. };
  114. enum rx_filter_bits {
  115. RxEnable = 0x01,
  116. RxPolarity = 0x02,
  117. RxForceOK = 0x04,
  118. RxMultiCast = 0x08,
  119. RxPromisc = 0x10,
  120. AltRxPromisc = 0x20, /* F5U011 uses different bit */
  121. };
  122. enum led_values {
  123. LEDFast = 0x01,
  124. LEDSlow = 0x02,
  125. LEDFlash = 0x03,
  126. LEDPulse = 0x04,
  127. LEDLink = 0x08,
  128. };
  129. enum link_status {
  130. LinkNoChange = 0,
  131. LinkGood = 1,
  132. LinkBad = 2
  133. };
  134. /*
  135. * The catc struct.
  136. */
  137. #define CTRL_RUNNING 0
  138. #define RX_RUNNING 1
  139. #define TX_RUNNING 2
  140. struct catc {
  141. struct net_device *netdev;
  142. struct usb_device *usbdev;
  143. struct net_device_stats stats;
  144. unsigned long flags;
  145. unsigned int tx_ptr, tx_idx;
  146. unsigned int ctrl_head, ctrl_tail;
  147. spinlock_t tx_lock, ctrl_lock;
  148. u8 tx_buf[2][TX_MAX_BURST * (PKT_SZ + 2)];
  149. u8 rx_buf[RX_MAX_BURST * (PKT_SZ + 2)];
  150. u8 irq_buf[2];
  151. u8 ctrl_buf[64];
  152. struct usb_ctrlrequest ctrl_dr;
  153. struct timer_list timer;
  154. u8 stats_buf[8];
  155. u16 stats_vals[4];
  156. unsigned long last_stats;
  157. u8 multicast[64];
  158. struct ctrl_queue {
  159. u8 dir;
  160. u8 request;
  161. u16 value;
  162. u16 index;
  163. void *buf;
  164. int len;
  165. void (*callback)(struct catc *catc, struct ctrl_queue *q);
  166. } ctrl_queue[CTRL_QUEUE];
  167. struct urb *tx_urb, *rx_urb, *irq_urb, *ctrl_urb;
  168. u8 is_f5u011; /* Set if device is an F5U011 */
  169. u8 rxmode[2]; /* Used for F5U011 */
  170. atomic_t recq_sz; /* Used for F5U011 - counter of waiting rx packets */
  171. };
  172. /*
  173. * Useful macros.
  174. */
  175. #define catc_get_mac(catc, mac) catc_ctrl_msg(catc, USB_DIR_IN, GetMac, 0, 0, mac, 6)
  176. #define catc_reset(catc) catc_ctrl_msg(catc, USB_DIR_OUT, Reset, 0, 0, NULL, 0)
  177. #define catc_set_reg(catc, reg, val) catc_ctrl_msg(catc, USB_DIR_OUT, SetReg, val, reg, NULL, 0)
  178. #define catc_get_reg(catc, reg, buf) catc_ctrl_msg(catc, USB_DIR_IN, GetReg, 0, reg, buf, 1)
  179. #define catc_write_mem(catc, addr, buf, size) catc_ctrl_msg(catc, USB_DIR_OUT, WriteMem, 0, addr, buf, size)
  180. #define catc_read_mem(catc, addr, buf, size) catc_ctrl_msg(catc, USB_DIR_IN, ReadMem, 0, addr, buf, size)
  181. #define f5u011_rxmode(catc, rxmode) catc_ctrl_msg(catc, USB_DIR_OUT, SetRxMode, 0, 1, rxmode, 2)
  182. #define f5u011_rxmode_async(catc, rxmode) catc_ctrl_async(catc, USB_DIR_OUT, SetRxMode, 0, 1, &rxmode, 2, NULL)
  183. #define f5u011_mchash_async(catc, hash) catc_ctrl_async(catc, USB_DIR_OUT, SetRxMode, 0, 2, &hash, 8, NULL)
  184. #define catc_set_reg_async(catc, reg, val) catc_ctrl_async(catc, USB_DIR_OUT, SetReg, val, reg, NULL, 0, NULL)
  185. #define catc_get_reg_async(catc, reg, cb) catc_ctrl_async(catc, USB_DIR_IN, GetReg, 0, reg, NULL, 1, cb)
  186. #define catc_write_mem_async(catc, addr, buf, size) catc_ctrl_async(catc, USB_DIR_OUT, WriteMem, 0, addr, buf, size, NULL)
  187. /*
  188. * Receive routines.
  189. */
  190. static void catc_rx_done(struct urb *urb)
  191. {
  192. struct catc *catc = urb->context;
  193. u8 *pkt_start = urb->transfer_buffer;
  194. struct sk_buff *skb;
  195. int pkt_len, pkt_offset = 0;
  196. if (!catc->is_f5u011) {
  197. clear_bit(RX_RUNNING, &catc->flags);
  198. pkt_offset = 2;
  199. }
  200. if (urb->status) {
  201. dbg("rx_done, status %d, length %d", urb->status, urb->actual_length);
  202. return;
  203. }
  204. do {
  205. if(!catc->is_f5u011) {
  206. pkt_len = le16_to_cpup((__le16*)pkt_start);
  207. if (pkt_len > urb->actual_length) {
  208. catc->stats.rx_length_errors++;
  209. catc->stats.rx_errors++;
  210. break;
  211. }
  212. } else {
  213. pkt_len = urb->actual_length;
  214. }
  215. if (!(skb = dev_alloc_skb(pkt_len)))
  216. return;
  217. skb_copy_to_linear_data(skb, pkt_start + pkt_offset, pkt_len);
  218. skb_put(skb, pkt_len);
  219. skb->protocol = eth_type_trans(skb, catc->netdev);
  220. netif_rx(skb);
  221. catc->stats.rx_packets++;
  222. catc->stats.rx_bytes += pkt_len;
  223. /* F5U011 only does one packet per RX */
  224. if (catc->is_f5u011)
  225. break;
  226. pkt_start += (((pkt_len + 1) >> 6) + 1) << 6;
  227. } while (pkt_start - (u8 *) urb->transfer_buffer < urb->actual_length);
  228. catc->netdev->last_rx = jiffies;
  229. if (catc->is_f5u011) {
  230. if (atomic_read(&catc->recq_sz)) {
  231. int status;
  232. atomic_dec(&catc->recq_sz);
  233. dbg("getting extra packet");
  234. urb->dev = catc->usbdev;
  235. if ((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  236. dbg("submit(rx_urb) status %d", status);
  237. }
  238. } else {
  239. clear_bit(RX_RUNNING, &catc->flags);
  240. }
  241. }
  242. }
  243. static void catc_irq_done(struct urb *urb)
  244. {
  245. struct catc *catc = urb->context;
  246. u8 *data = urb->transfer_buffer;
  247. int status;
  248. unsigned int hasdata = 0, linksts = LinkNoChange;
  249. if (!catc->is_f5u011) {
  250. hasdata = data[1] & 0x80;
  251. if (data[1] & 0x40)
  252. linksts = LinkGood;
  253. else if (data[1] & 0x20)
  254. linksts = LinkBad;
  255. } else {
  256. hasdata = (unsigned int)(be16_to_cpup((__be16*)data) & 0x0fff);
  257. if (data[0] == 0x90)
  258. linksts = LinkGood;
  259. else if (data[0] == 0xA0)
  260. linksts = LinkBad;
  261. }
  262. switch (urb->status) {
  263. case 0: /* success */
  264. break;
  265. case -ECONNRESET: /* unlink */
  266. case -ENOENT:
  267. case -ESHUTDOWN:
  268. return;
  269. /* -EPIPE: should clear the halt */
  270. default: /* error */
  271. dbg("irq_done, status %d, data %02x %02x.", urb->status, data[0], data[1]);
  272. goto resubmit;
  273. }
  274. if (linksts == LinkGood) {
  275. netif_carrier_on(catc->netdev);
  276. dbg("link ok");
  277. }
  278. if (linksts == LinkBad) {
  279. netif_carrier_off(catc->netdev);
  280. dbg("link bad");
  281. }
  282. if (hasdata) {
  283. if (test_and_set_bit(RX_RUNNING, &catc->flags)) {
  284. if (catc->is_f5u011)
  285. atomic_inc(&catc->recq_sz);
  286. } else {
  287. catc->rx_urb->dev = catc->usbdev;
  288. if ((status = usb_submit_urb(catc->rx_urb, GFP_ATOMIC)) < 0) {
  289. err("submit(rx_urb) status %d", status);
  290. }
  291. }
  292. }
  293. resubmit:
  294. status = usb_submit_urb (urb, GFP_ATOMIC);
  295. if (status)
  296. err ("can't resubmit intr, %s-%s, status %d",
  297. catc->usbdev->bus->bus_name,
  298. catc->usbdev->devpath, status);
  299. }
  300. /*
  301. * Transmit routines.
  302. */
  303. static int catc_tx_run(struct catc *catc)
  304. {
  305. int status;
  306. if (catc->is_f5u011)
  307. catc->tx_ptr = (catc->tx_ptr + 63) & ~63;
  308. catc->tx_urb->transfer_buffer_length = catc->tx_ptr;
  309. catc->tx_urb->transfer_buffer = catc->tx_buf[catc->tx_idx];
  310. catc->tx_urb->dev = catc->usbdev;
  311. if ((status = usb_submit_urb(catc->tx_urb, GFP_ATOMIC)) < 0)
  312. err("submit(tx_urb), status %d", status);
  313. catc->tx_idx = !catc->tx_idx;
  314. catc->tx_ptr = 0;
  315. catc->netdev->trans_start = jiffies;
  316. return status;
  317. }
  318. static void catc_tx_done(struct urb *urb)
  319. {
  320. struct catc *catc = urb->context;
  321. unsigned long flags;
  322. int r;
  323. if (urb->status == -ECONNRESET) {
  324. dbg("Tx Reset.");
  325. urb->status = 0;
  326. catc->netdev->trans_start = jiffies;
  327. catc->stats.tx_errors++;
  328. clear_bit(TX_RUNNING, &catc->flags);
  329. netif_wake_queue(catc->netdev);
  330. return;
  331. }
  332. if (urb->status) {
  333. dbg("tx_done, status %d, length %d", urb->status, urb->actual_length);
  334. return;
  335. }
  336. spin_lock_irqsave(&catc->tx_lock, flags);
  337. if (catc->tx_ptr) {
  338. r = catc_tx_run(catc);
  339. if (unlikely(r < 0))
  340. clear_bit(TX_RUNNING, &catc->flags);
  341. } else {
  342. clear_bit(TX_RUNNING, &catc->flags);
  343. }
  344. netif_wake_queue(catc->netdev);
  345. spin_unlock_irqrestore(&catc->tx_lock, flags);
  346. }
  347. static int catc_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  348. {
  349. struct catc *catc = netdev_priv(netdev);
  350. unsigned long flags;
  351. int r = 0;
  352. char *tx_buf;
  353. spin_lock_irqsave(&catc->tx_lock, flags);
  354. catc->tx_ptr = (((catc->tx_ptr - 1) >> 6) + 1) << 6;
  355. tx_buf = catc->tx_buf[catc->tx_idx] + catc->tx_ptr;
  356. if (catc->is_f5u011)
  357. *(__be16 *)tx_buf = cpu_to_be16(skb->len);
  358. else
  359. *(__le16 *)tx_buf = cpu_to_le16(skb->len);
  360. skb_copy_from_linear_data(skb, tx_buf + 2, skb->len);
  361. catc->tx_ptr += skb->len + 2;
  362. if (!test_and_set_bit(TX_RUNNING, &catc->flags)) {
  363. r = catc_tx_run(catc);
  364. if (r < 0)
  365. clear_bit(TX_RUNNING, &catc->flags);
  366. }
  367. if ((catc->is_f5u011 && catc->tx_ptr)
  368. || (catc->tx_ptr >= ((TX_MAX_BURST - 1) * (PKT_SZ + 2))))
  369. netif_stop_queue(netdev);
  370. spin_unlock_irqrestore(&catc->tx_lock, flags);
  371. if (r >= 0) {
  372. catc->stats.tx_bytes += skb->len;
  373. catc->stats.tx_packets++;
  374. }
  375. dev_kfree_skb(skb);
  376. return 0;
  377. }
  378. static void catc_tx_timeout(struct net_device *netdev)
  379. {
  380. struct catc *catc = netdev_priv(netdev);
  381. dev_warn(&netdev->dev, "Transmit timed out.\n");
  382. usb_unlink_urb(catc->tx_urb);
  383. }
  384. /*
  385. * Control messages.
  386. */
  387. static int catc_ctrl_msg(struct catc *catc, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
  388. {
  389. int retval = usb_control_msg(catc->usbdev,
  390. dir ? usb_rcvctrlpipe(catc->usbdev, 0) : usb_sndctrlpipe(catc->usbdev, 0),
  391. request, 0x40 | dir, value, index, buf, len, 1000);
  392. return retval < 0 ? retval : 0;
  393. }
  394. static void catc_ctrl_run(struct catc *catc)
  395. {
  396. struct ctrl_queue *q = catc->ctrl_queue + catc->ctrl_tail;
  397. struct usb_device *usbdev = catc->usbdev;
  398. struct urb *urb = catc->ctrl_urb;
  399. struct usb_ctrlrequest *dr = &catc->ctrl_dr;
  400. int status;
  401. dr->bRequest = q->request;
  402. dr->bRequestType = 0x40 | q->dir;
  403. dr->wValue = cpu_to_le16(q->value);
  404. dr->wIndex = cpu_to_le16(q->index);
  405. dr->wLength = cpu_to_le16(q->len);
  406. urb->pipe = q->dir ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0);
  407. urb->transfer_buffer_length = q->len;
  408. urb->transfer_buffer = catc->ctrl_buf;
  409. urb->setup_packet = (void *) dr;
  410. urb->dev = usbdev;
  411. if (!q->dir && q->buf && q->len)
  412. memcpy(catc->ctrl_buf, q->buf, q->len);
  413. if ((status = usb_submit_urb(catc->ctrl_urb, GFP_KERNEL)))
  414. err("submit(ctrl_urb) status %d", status);
  415. }
  416. static void catc_ctrl_done(struct urb *urb)
  417. {
  418. struct catc *catc = urb->context;
  419. struct ctrl_queue *q;
  420. unsigned long flags;
  421. if (urb->status)
  422. dbg("ctrl_done, status %d, len %d.", urb->status, urb->actual_length);
  423. spin_lock_irqsave(&catc->ctrl_lock, flags);
  424. q = catc->ctrl_queue + catc->ctrl_tail;
  425. if (q->dir) {
  426. if (q->buf && q->len)
  427. memcpy(q->buf, catc->ctrl_buf, q->len);
  428. else
  429. q->buf = catc->ctrl_buf;
  430. }
  431. if (q->callback)
  432. q->callback(catc, q);
  433. catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
  434. if (catc->ctrl_head != catc->ctrl_tail)
  435. catc_ctrl_run(catc);
  436. else
  437. clear_bit(CTRL_RUNNING, &catc->flags);
  438. spin_unlock_irqrestore(&catc->ctrl_lock, flags);
  439. }
  440. static int catc_ctrl_async(struct catc *catc, u8 dir, u8 request, u16 value,
  441. u16 index, void *buf, int len, void (*callback)(struct catc *catc, struct ctrl_queue *q))
  442. {
  443. struct ctrl_queue *q;
  444. int retval = 0;
  445. unsigned long flags;
  446. spin_lock_irqsave(&catc->ctrl_lock, flags);
  447. q = catc->ctrl_queue + catc->ctrl_head;
  448. q->dir = dir;
  449. q->request = request;
  450. q->value = value;
  451. q->index = index;
  452. q->buf = buf;
  453. q->len = len;
  454. q->callback = callback;
  455. catc->ctrl_head = (catc->ctrl_head + 1) & (CTRL_QUEUE - 1);
  456. if (catc->ctrl_head == catc->ctrl_tail) {
  457. err("ctrl queue full");
  458. catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
  459. retval = -1;
  460. }
  461. if (!test_and_set_bit(CTRL_RUNNING, &catc->flags))
  462. catc_ctrl_run(catc);
  463. spin_unlock_irqrestore(&catc->ctrl_lock, flags);
  464. return retval;
  465. }
  466. /*
  467. * Statistics.
  468. */
  469. static void catc_stats_done(struct catc *catc, struct ctrl_queue *q)
  470. {
  471. int index = q->index - EthStats;
  472. u16 data, last;
  473. catc->stats_buf[index] = *((char *)q->buf);
  474. if (index & 1)
  475. return;
  476. data = ((u16)catc->stats_buf[index] << 8) | catc->stats_buf[index + 1];
  477. last = catc->stats_vals[index >> 1];
  478. switch (index) {
  479. case TxSingleColl:
  480. case TxMultiColl:
  481. catc->stats.collisions += data - last;
  482. break;
  483. case TxExcessColl:
  484. catc->stats.tx_aborted_errors += data - last;
  485. catc->stats.tx_errors += data - last;
  486. break;
  487. case RxFramErr:
  488. catc->stats.rx_frame_errors += data - last;
  489. catc->stats.rx_errors += data - last;
  490. break;
  491. }
  492. catc->stats_vals[index >> 1] = data;
  493. }
  494. static void catc_stats_timer(unsigned long data)
  495. {
  496. struct catc *catc = (void *) data;
  497. int i;
  498. for (i = 0; i < 8; i++)
  499. catc_get_reg_async(catc, EthStats + 7 - i, catc_stats_done);
  500. mod_timer(&catc->timer, jiffies + STATS_UPDATE);
  501. }
  502. static struct net_device_stats *catc_get_stats(struct net_device *netdev)
  503. {
  504. struct catc *catc = netdev_priv(netdev);
  505. return &catc->stats;
  506. }
  507. /*
  508. * Receive modes. Broadcast, Multicast, Promisc.
  509. */
  510. static void catc_multicast(unsigned char *addr, u8 *multicast)
  511. {
  512. u32 crc;
  513. crc = ether_crc_le(6, addr);
  514. multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
  515. }
  516. static void catc_set_multicast_list(struct net_device *netdev)
  517. {
  518. struct catc *catc = netdev_priv(netdev);
  519. struct dev_mc_list *mc;
  520. u8 broadcast[6];
  521. u8 rx = RxEnable | RxPolarity | RxMultiCast;
  522. int i;
  523. memset(broadcast, 0xff, 6);
  524. memset(catc->multicast, 0, 64);
  525. catc_multicast(broadcast, catc->multicast);
  526. catc_multicast(netdev->dev_addr, catc->multicast);
  527. if (netdev->flags & IFF_PROMISC) {
  528. memset(catc->multicast, 0xff, 64);
  529. rx |= (!catc->is_f5u011) ? RxPromisc : AltRxPromisc;
  530. }
  531. if (netdev->flags & IFF_ALLMULTI) {
  532. memset(catc->multicast, 0xff, 64);
  533. } else {
  534. for (i = 0, mc = netdev->mc_list; mc && i < netdev->mc_count; i++, mc = mc->next) {
  535. u32 crc = ether_crc_le(6, mc->dmi_addr);
  536. if (!catc->is_f5u011) {
  537. catc->multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
  538. } else {
  539. catc->multicast[7-(crc >> 29)] |= 1 << ((crc >> 26) & 7);
  540. }
  541. }
  542. }
  543. if (!catc->is_f5u011) {
  544. catc_set_reg_async(catc, RxUnit, rx);
  545. catc_write_mem_async(catc, 0xfa80, catc->multicast, 64);
  546. } else {
  547. f5u011_mchash_async(catc, catc->multicast);
  548. if (catc->rxmode[0] != rx) {
  549. catc->rxmode[0] = rx;
  550. dbg("Setting RX mode to %2.2X %2.2X", catc->rxmode[0], catc->rxmode[1]);
  551. f5u011_rxmode_async(catc, catc->rxmode);
  552. }
  553. }
  554. }
  555. static void catc_get_drvinfo(struct net_device *dev,
  556. struct ethtool_drvinfo *info)
  557. {
  558. struct catc *catc = netdev_priv(dev);
  559. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  560. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  561. usb_make_path (catc->usbdev, info->bus_info, sizeof info->bus_info);
  562. }
  563. static int catc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  564. {
  565. struct catc *catc = netdev_priv(dev);
  566. if (!catc->is_f5u011)
  567. return -EOPNOTSUPP;
  568. cmd->supported = SUPPORTED_10baseT_Half | SUPPORTED_TP;
  569. cmd->advertising = ADVERTISED_10baseT_Half | ADVERTISED_TP;
  570. cmd->speed = SPEED_10;
  571. cmd->duplex = DUPLEX_HALF;
  572. cmd->port = PORT_TP;
  573. cmd->phy_address = 0;
  574. cmd->transceiver = XCVR_INTERNAL;
  575. cmd->autoneg = AUTONEG_DISABLE;
  576. cmd->maxtxpkt = 1;
  577. cmd->maxrxpkt = 1;
  578. return 0;
  579. }
  580. static struct ethtool_ops ops = {
  581. .get_drvinfo = catc_get_drvinfo,
  582. .get_settings = catc_get_settings,
  583. .get_link = ethtool_op_get_link
  584. };
  585. /*
  586. * Open, close.
  587. */
  588. static int catc_open(struct net_device *netdev)
  589. {
  590. struct catc *catc = netdev_priv(netdev);
  591. int status;
  592. catc->irq_urb->dev = catc->usbdev;
  593. if ((status = usb_submit_urb(catc->irq_urb, GFP_KERNEL)) < 0) {
  594. err("submit(irq_urb) status %d", status);
  595. return -1;
  596. }
  597. netif_start_queue(netdev);
  598. if (!catc->is_f5u011)
  599. mod_timer(&catc->timer, jiffies + STATS_UPDATE);
  600. return 0;
  601. }
  602. static int catc_stop(struct net_device *netdev)
  603. {
  604. struct catc *catc = netdev_priv(netdev);
  605. netif_stop_queue(netdev);
  606. if (!catc->is_f5u011)
  607. del_timer_sync(&catc->timer);
  608. usb_kill_urb(catc->rx_urb);
  609. usb_kill_urb(catc->tx_urb);
  610. usb_kill_urb(catc->irq_urb);
  611. usb_kill_urb(catc->ctrl_urb);
  612. return 0;
  613. }
  614. /*
  615. * USB probe, disconnect.
  616. */
  617. static int catc_probe(struct usb_interface *intf, const struct usb_device_id *id)
  618. {
  619. struct usb_device *usbdev = interface_to_usbdev(intf);
  620. struct net_device *netdev;
  621. struct catc *catc;
  622. u8 broadcast[6];
  623. int i, pktsz;
  624. if (usb_set_interface(usbdev,
  625. intf->altsetting->desc.bInterfaceNumber, 1)) {
  626. err("Can't set altsetting 1.");
  627. return -EIO;
  628. }
  629. netdev = alloc_etherdev(sizeof(struct catc));
  630. if (!netdev)
  631. return -ENOMEM;
  632. catc = netdev_priv(netdev);
  633. netdev->open = catc_open;
  634. netdev->hard_start_xmit = catc_hard_start_xmit;
  635. netdev->stop = catc_stop;
  636. netdev->get_stats = catc_get_stats;
  637. netdev->tx_timeout = catc_tx_timeout;
  638. netdev->watchdog_timeo = TX_TIMEOUT;
  639. netdev->set_multicast_list = catc_set_multicast_list;
  640. SET_ETHTOOL_OPS(netdev, &ops);
  641. catc->usbdev = usbdev;
  642. catc->netdev = netdev;
  643. spin_lock_init(&catc->tx_lock);
  644. spin_lock_init(&catc->ctrl_lock);
  645. init_timer(&catc->timer);
  646. catc->timer.data = (long) catc;
  647. catc->timer.function = catc_stats_timer;
  648. catc->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
  649. catc->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  650. catc->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  651. catc->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  652. if ((!catc->ctrl_urb) || (!catc->tx_urb) ||
  653. (!catc->rx_urb) || (!catc->irq_urb)) {
  654. err("No free urbs available.");
  655. usb_free_urb(catc->ctrl_urb);
  656. usb_free_urb(catc->tx_urb);
  657. usb_free_urb(catc->rx_urb);
  658. usb_free_urb(catc->irq_urb);
  659. free_netdev(netdev);
  660. return -ENOMEM;
  661. }
  662. /* The F5U011 has the same vendor/product as the netmate but a device version of 0x130 */
  663. if (le16_to_cpu(usbdev->descriptor.idVendor) == 0x0423 &&
  664. le16_to_cpu(usbdev->descriptor.idProduct) == 0xa &&
  665. le16_to_cpu(catc->usbdev->descriptor.bcdDevice) == 0x0130) {
  666. dbg("Testing for f5u011");
  667. catc->is_f5u011 = 1;
  668. atomic_set(&catc->recq_sz, 0);
  669. pktsz = RX_PKT_SZ;
  670. } else {
  671. pktsz = RX_MAX_BURST * (PKT_SZ + 2);
  672. }
  673. usb_fill_control_urb(catc->ctrl_urb, usbdev, usb_sndctrlpipe(usbdev, 0),
  674. NULL, NULL, 0, catc_ctrl_done, catc);
  675. usb_fill_bulk_urb(catc->tx_urb, usbdev, usb_sndbulkpipe(usbdev, 1),
  676. NULL, 0, catc_tx_done, catc);
  677. usb_fill_bulk_urb(catc->rx_urb, usbdev, usb_rcvbulkpipe(usbdev, 1),
  678. catc->rx_buf, pktsz, catc_rx_done, catc);
  679. usb_fill_int_urb(catc->irq_urb, usbdev, usb_rcvintpipe(usbdev, 2),
  680. catc->irq_buf, 2, catc_irq_done, catc, 1);
  681. if (!catc->is_f5u011) {
  682. dbg("Checking memory size\n");
  683. i = 0x12345678;
  684. catc_write_mem(catc, 0x7a80, &i, 4);
  685. i = 0x87654321;
  686. catc_write_mem(catc, 0xfa80, &i, 4);
  687. catc_read_mem(catc, 0x7a80, &i, 4);
  688. switch (i) {
  689. case 0x12345678:
  690. catc_set_reg(catc, TxBufCount, 8);
  691. catc_set_reg(catc, RxBufCount, 32);
  692. dbg("64k Memory\n");
  693. break;
  694. default:
  695. dev_warn(&intf->dev,
  696. "Couldn't detect memory size, assuming 32k\n");
  697. case 0x87654321:
  698. catc_set_reg(catc, TxBufCount, 4);
  699. catc_set_reg(catc, RxBufCount, 16);
  700. dbg("32k Memory\n");
  701. break;
  702. }
  703. dbg("Getting MAC from SEEROM.");
  704. catc_get_mac(catc, netdev->dev_addr);
  705. dbg("Setting MAC into registers.");
  706. for (i = 0; i < 6; i++)
  707. catc_set_reg(catc, StationAddr0 - i, netdev->dev_addr[i]);
  708. dbg("Filling the multicast list.");
  709. memset(broadcast, 0xff, 6);
  710. catc_multicast(broadcast, catc->multicast);
  711. catc_multicast(netdev->dev_addr, catc->multicast);
  712. catc_write_mem(catc, 0xfa80, catc->multicast, 64);
  713. dbg("Clearing error counters.");
  714. for (i = 0; i < 8; i++)
  715. catc_set_reg(catc, EthStats + i, 0);
  716. catc->last_stats = jiffies;
  717. dbg("Enabling.");
  718. catc_set_reg(catc, MaxBurst, RX_MAX_BURST);
  719. catc_set_reg(catc, OpModes, OpTxMerge | OpRxMerge | OpLenInclude | Op3MemWaits);
  720. catc_set_reg(catc, LEDCtrl, LEDLink);
  721. catc_set_reg(catc, RxUnit, RxEnable | RxPolarity | RxMultiCast);
  722. } else {
  723. dbg("Performing reset\n");
  724. catc_reset(catc);
  725. catc_get_mac(catc, netdev->dev_addr);
  726. dbg("Setting RX Mode");
  727. catc->rxmode[0] = RxEnable | RxPolarity | RxMultiCast;
  728. catc->rxmode[1] = 0;
  729. f5u011_rxmode(catc, catc->rxmode);
  730. }
  731. dbg("Init done.");
  732. printk(KERN_INFO "%s: %s USB Ethernet at usb-%s-%s, ",
  733. netdev->name, (catc->is_f5u011) ? "Belkin F5U011" : "CATC EL1210A NetMate",
  734. usbdev->bus->bus_name, usbdev->devpath);
  735. for (i = 0; i < 5; i++) printk("%2.2x:", netdev->dev_addr[i]);
  736. printk("%2.2x.\n", netdev->dev_addr[i]);
  737. usb_set_intfdata(intf, catc);
  738. SET_NETDEV_DEV(netdev, &intf->dev);
  739. if (register_netdev(netdev) != 0) {
  740. usb_set_intfdata(intf, NULL);
  741. usb_free_urb(catc->ctrl_urb);
  742. usb_free_urb(catc->tx_urb);
  743. usb_free_urb(catc->rx_urb);
  744. usb_free_urb(catc->irq_urb);
  745. free_netdev(netdev);
  746. return -EIO;
  747. }
  748. return 0;
  749. }
  750. static void catc_disconnect(struct usb_interface *intf)
  751. {
  752. struct catc *catc = usb_get_intfdata(intf);
  753. usb_set_intfdata(intf, NULL);
  754. if (catc) {
  755. unregister_netdev(catc->netdev);
  756. usb_free_urb(catc->ctrl_urb);
  757. usb_free_urb(catc->tx_urb);
  758. usb_free_urb(catc->rx_urb);
  759. usb_free_urb(catc->irq_urb);
  760. free_netdev(catc->netdev);
  761. }
  762. }
  763. /*
  764. * Module functions and tables.
  765. */
  766. static struct usb_device_id catc_id_table [] = {
  767. { USB_DEVICE(0x0423, 0xa) }, /* CATC Netmate, Belkin F5U011 */
  768. { USB_DEVICE(0x0423, 0xc) }, /* CATC Netmate II, Belkin F5U111 */
  769. { USB_DEVICE(0x08d1, 0x1) }, /* smartBridges smartNIC */
  770. { }
  771. };
  772. MODULE_DEVICE_TABLE(usb, catc_id_table);
  773. static struct usb_driver catc_driver = {
  774. .name = driver_name,
  775. .probe = catc_probe,
  776. .disconnect = catc_disconnect,
  777. .id_table = catc_id_table,
  778. };
  779. static int __init catc_init(void)
  780. {
  781. int result = usb_register(&catc_driver);
  782. if (result == 0)
  783. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  784. DRIVER_DESC "\n");
  785. return result;
  786. }
  787. static void __exit catc_exit(void)
  788. {
  789. usb_deregister(&catc_driver);
  790. }
  791. module_init(catc_init);
  792. module_exit(catc_exit);