catc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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/netdevice.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/ethtool.h>
  42. #include <linux/crc32.h>
  43. #include <linux/bitops.h>
  44. #include <linux/gfp.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. unsigned long flags;
  144. unsigned int tx_ptr, tx_idx;
  145. unsigned int ctrl_head, ctrl_tail;
  146. spinlock_t tx_lock, ctrl_lock;
  147. u8 tx_buf[2][TX_MAX_BURST * (PKT_SZ + 2)];
  148. u8 rx_buf[RX_MAX_BURST * (PKT_SZ + 2)];
  149. u8 irq_buf[2];
  150. u8 ctrl_buf[64];
  151. struct usb_ctrlrequest ctrl_dr;
  152. struct timer_list timer;
  153. u8 stats_buf[8];
  154. u16 stats_vals[4];
  155. unsigned long last_stats;
  156. u8 multicast[64];
  157. struct ctrl_queue {
  158. u8 dir;
  159. u8 request;
  160. u16 value;
  161. u16 index;
  162. void *buf;
  163. int len;
  164. void (*callback)(struct catc *catc, struct ctrl_queue *q);
  165. } ctrl_queue[CTRL_QUEUE];
  166. struct urb *tx_urb, *rx_urb, *irq_urb, *ctrl_urb;
  167. u8 is_f5u011; /* Set if device is an F5U011 */
  168. u8 rxmode[2]; /* Used for F5U011 */
  169. atomic_t recq_sz; /* Used for F5U011 - counter of waiting rx packets */
  170. };
  171. /*
  172. * Useful macros.
  173. */
  174. #define catc_get_mac(catc, mac) catc_ctrl_msg(catc, USB_DIR_IN, GetMac, 0, 0, mac, 6)
  175. #define catc_reset(catc) catc_ctrl_msg(catc, USB_DIR_OUT, Reset, 0, 0, NULL, 0)
  176. #define catc_set_reg(catc, reg, val) catc_ctrl_msg(catc, USB_DIR_OUT, SetReg, val, reg, NULL, 0)
  177. #define catc_get_reg(catc, reg, buf) catc_ctrl_msg(catc, USB_DIR_IN, GetReg, 0, reg, buf, 1)
  178. #define catc_write_mem(catc, addr, buf, size) catc_ctrl_msg(catc, USB_DIR_OUT, WriteMem, 0, addr, buf, size)
  179. #define catc_read_mem(catc, addr, buf, size) catc_ctrl_msg(catc, USB_DIR_IN, ReadMem, 0, addr, buf, size)
  180. #define f5u011_rxmode(catc, rxmode) catc_ctrl_msg(catc, USB_DIR_OUT, SetRxMode, 0, 1, rxmode, 2)
  181. #define f5u011_rxmode_async(catc, rxmode) catc_ctrl_async(catc, USB_DIR_OUT, SetRxMode, 0, 1, &rxmode, 2, NULL)
  182. #define f5u011_mchash_async(catc, hash) catc_ctrl_async(catc, USB_DIR_OUT, SetRxMode, 0, 2, &hash, 8, NULL)
  183. #define catc_set_reg_async(catc, reg, val) catc_ctrl_async(catc, USB_DIR_OUT, SetReg, val, reg, NULL, 0, NULL)
  184. #define catc_get_reg_async(catc, reg, cb) catc_ctrl_async(catc, USB_DIR_IN, GetReg, 0, reg, NULL, 1, cb)
  185. #define catc_write_mem_async(catc, addr, buf, size) catc_ctrl_async(catc, USB_DIR_OUT, WriteMem, 0, addr, buf, size, NULL)
  186. /*
  187. * Receive routines.
  188. */
  189. static void catc_rx_done(struct urb *urb)
  190. {
  191. struct catc *catc = urb->context;
  192. u8 *pkt_start = urb->transfer_buffer;
  193. struct sk_buff *skb;
  194. int pkt_len, pkt_offset = 0;
  195. int status = urb->status;
  196. if (!catc->is_f5u011) {
  197. clear_bit(RX_RUNNING, &catc->flags);
  198. pkt_offset = 2;
  199. }
  200. if (status) {
  201. dev_dbg(&urb->dev->dev, "rx_done, status %d, length %d\n",
  202. status, urb->actual_length);
  203. return;
  204. }
  205. do {
  206. if(!catc->is_f5u011) {
  207. pkt_len = le16_to_cpup((__le16*)pkt_start);
  208. if (pkt_len > urb->actual_length) {
  209. catc->netdev->stats.rx_length_errors++;
  210. catc->netdev->stats.rx_errors++;
  211. break;
  212. }
  213. } else {
  214. pkt_len = urb->actual_length;
  215. }
  216. if (!(skb = dev_alloc_skb(pkt_len)))
  217. return;
  218. skb_copy_to_linear_data(skb, pkt_start + pkt_offset, pkt_len);
  219. skb_put(skb, pkt_len);
  220. skb->protocol = eth_type_trans(skb, catc->netdev);
  221. netif_rx(skb);
  222. catc->netdev->stats.rx_packets++;
  223. catc->netdev->stats.rx_bytes += pkt_len;
  224. /* F5U011 only does one packet per RX */
  225. if (catc->is_f5u011)
  226. break;
  227. pkt_start += (((pkt_len + 1) >> 6) + 1) << 6;
  228. } while (pkt_start - (u8 *) urb->transfer_buffer < urb->actual_length);
  229. if (catc->is_f5u011) {
  230. if (atomic_read(&catc->recq_sz)) {
  231. int state;
  232. atomic_dec(&catc->recq_sz);
  233. netdev_dbg(catc->netdev, "getting extra packet\n");
  234. urb->dev = catc->usbdev;
  235. if ((state = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  236. netdev_dbg(catc->netdev,
  237. "submit(rx_urb) status %d\n", state);
  238. }
  239. } else {
  240. clear_bit(RX_RUNNING, &catc->flags);
  241. }
  242. }
  243. }
  244. static void catc_irq_done(struct urb *urb)
  245. {
  246. struct catc *catc = urb->context;
  247. u8 *data = urb->transfer_buffer;
  248. int status = urb->status;
  249. unsigned int hasdata = 0, linksts = LinkNoChange;
  250. int res;
  251. if (!catc->is_f5u011) {
  252. hasdata = data[1] & 0x80;
  253. if (data[1] & 0x40)
  254. linksts = LinkGood;
  255. else if (data[1] & 0x20)
  256. linksts = LinkBad;
  257. } else {
  258. hasdata = (unsigned int)(be16_to_cpup((__be16*)data) & 0x0fff);
  259. if (data[0] == 0x90)
  260. linksts = LinkGood;
  261. else if (data[0] == 0xA0)
  262. linksts = LinkBad;
  263. }
  264. switch (status) {
  265. case 0: /* success */
  266. break;
  267. case -ECONNRESET: /* unlink */
  268. case -ENOENT:
  269. case -ESHUTDOWN:
  270. return;
  271. /* -EPIPE: should clear the halt */
  272. default: /* error */
  273. dev_dbg(&urb->dev->dev,
  274. "irq_done, status %d, data %02x %02x.\n",
  275. status, data[0], data[1]);
  276. goto resubmit;
  277. }
  278. if (linksts == LinkGood) {
  279. netif_carrier_on(catc->netdev);
  280. netdev_dbg(catc->netdev, "link ok\n");
  281. }
  282. if (linksts == LinkBad) {
  283. netif_carrier_off(catc->netdev);
  284. netdev_dbg(catc->netdev, "link bad\n");
  285. }
  286. if (hasdata) {
  287. if (test_and_set_bit(RX_RUNNING, &catc->flags)) {
  288. if (catc->is_f5u011)
  289. atomic_inc(&catc->recq_sz);
  290. } else {
  291. catc->rx_urb->dev = catc->usbdev;
  292. if ((res = usb_submit_urb(catc->rx_urb, GFP_ATOMIC)) < 0) {
  293. dev_err(&catc->usbdev->dev,
  294. "submit(rx_urb) status %d\n", res);
  295. }
  296. }
  297. }
  298. resubmit:
  299. res = usb_submit_urb (urb, GFP_ATOMIC);
  300. if (res)
  301. dev_err(&catc->usbdev->dev,
  302. "can't resubmit intr, %s-%s, status %d\n",
  303. catc->usbdev->bus->bus_name,
  304. catc->usbdev->devpath, res);
  305. }
  306. /*
  307. * Transmit routines.
  308. */
  309. static int catc_tx_run(struct catc *catc)
  310. {
  311. int status;
  312. if (catc->is_f5u011)
  313. catc->tx_ptr = (catc->tx_ptr + 63) & ~63;
  314. catc->tx_urb->transfer_buffer_length = catc->tx_ptr;
  315. catc->tx_urb->transfer_buffer = catc->tx_buf[catc->tx_idx];
  316. catc->tx_urb->dev = catc->usbdev;
  317. if ((status = usb_submit_urb(catc->tx_urb, GFP_ATOMIC)) < 0)
  318. dev_err(&catc->usbdev->dev, "submit(tx_urb), status %d\n",
  319. status);
  320. catc->tx_idx = !catc->tx_idx;
  321. catc->tx_ptr = 0;
  322. catc->netdev->trans_start = jiffies;
  323. return status;
  324. }
  325. static void catc_tx_done(struct urb *urb)
  326. {
  327. struct catc *catc = urb->context;
  328. unsigned long flags;
  329. int r, status = urb->status;
  330. if (status == -ECONNRESET) {
  331. dev_dbg(&urb->dev->dev, "Tx Reset.\n");
  332. urb->status = 0;
  333. catc->netdev->trans_start = jiffies;
  334. catc->netdev->stats.tx_errors++;
  335. clear_bit(TX_RUNNING, &catc->flags);
  336. netif_wake_queue(catc->netdev);
  337. return;
  338. }
  339. if (status) {
  340. dev_dbg(&urb->dev->dev, "tx_done, status %d, length %d\n",
  341. status, urb->actual_length);
  342. return;
  343. }
  344. spin_lock_irqsave(&catc->tx_lock, flags);
  345. if (catc->tx_ptr) {
  346. r = catc_tx_run(catc);
  347. if (unlikely(r < 0))
  348. clear_bit(TX_RUNNING, &catc->flags);
  349. } else {
  350. clear_bit(TX_RUNNING, &catc->flags);
  351. }
  352. netif_wake_queue(catc->netdev);
  353. spin_unlock_irqrestore(&catc->tx_lock, flags);
  354. }
  355. static netdev_tx_t catc_start_xmit(struct sk_buff *skb,
  356. struct net_device *netdev)
  357. {
  358. struct catc *catc = netdev_priv(netdev);
  359. unsigned long flags;
  360. int r = 0;
  361. char *tx_buf;
  362. spin_lock_irqsave(&catc->tx_lock, flags);
  363. catc->tx_ptr = (((catc->tx_ptr - 1) >> 6) + 1) << 6;
  364. tx_buf = catc->tx_buf[catc->tx_idx] + catc->tx_ptr;
  365. if (catc->is_f5u011)
  366. *(__be16 *)tx_buf = cpu_to_be16(skb->len);
  367. else
  368. *(__le16 *)tx_buf = cpu_to_le16(skb->len);
  369. skb_copy_from_linear_data(skb, tx_buf + 2, skb->len);
  370. catc->tx_ptr += skb->len + 2;
  371. if (!test_and_set_bit(TX_RUNNING, &catc->flags)) {
  372. r = catc_tx_run(catc);
  373. if (r < 0)
  374. clear_bit(TX_RUNNING, &catc->flags);
  375. }
  376. if ((catc->is_f5u011 && catc->tx_ptr) ||
  377. (catc->tx_ptr >= ((TX_MAX_BURST - 1) * (PKT_SZ + 2))))
  378. netif_stop_queue(netdev);
  379. spin_unlock_irqrestore(&catc->tx_lock, flags);
  380. if (r >= 0) {
  381. catc->netdev->stats.tx_bytes += skb->len;
  382. catc->netdev->stats.tx_packets++;
  383. }
  384. dev_kfree_skb(skb);
  385. return NETDEV_TX_OK;
  386. }
  387. static void catc_tx_timeout(struct net_device *netdev)
  388. {
  389. struct catc *catc = netdev_priv(netdev);
  390. dev_warn(&netdev->dev, "Transmit timed out.\n");
  391. usb_unlink_urb(catc->tx_urb);
  392. }
  393. /*
  394. * Control messages.
  395. */
  396. static int catc_ctrl_msg(struct catc *catc, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
  397. {
  398. int retval = usb_control_msg(catc->usbdev,
  399. dir ? usb_rcvctrlpipe(catc->usbdev, 0) : usb_sndctrlpipe(catc->usbdev, 0),
  400. request, 0x40 | dir, value, index, buf, len, 1000);
  401. return retval < 0 ? retval : 0;
  402. }
  403. static void catc_ctrl_run(struct catc *catc)
  404. {
  405. struct ctrl_queue *q = catc->ctrl_queue + catc->ctrl_tail;
  406. struct usb_device *usbdev = catc->usbdev;
  407. struct urb *urb = catc->ctrl_urb;
  408. struct usb_ctrlrequest *dr = &catc->ctrl_dr;
  409. int status;
  410. dr->bRequest = q->request;
  411. dr->bRequestType = 0x40 | q->dir;
  412. dr->wValue = cpu_to_le16(q->value);
  413. dr->wIndex = cpu_to_le16(q->index);
  414. dr->wLength = cpu_to_le16(q->len);
  415. urb->pipe = q->dir ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0);
  416. urb->transfer_buffer_length = q->len;
  417. urb->transfer_buffer = catc->ctrl_buf;
  418. urb->setup_packet = (void *) dr;
  419. urb->dev = usbdev;
  420. if (!q->dir && q->buf && q->len)
  421. memcpy(catc->ctrl_buf, q->buf, q->len);
  422. if ((status = usb_submit_urb(catc->ctrl_urb, GFP_ATOMIC)))
  423. dev_err(&catc->usbdev->dev, "submit(ctrl_urb) status %d\n",
  424. status);
  425. }
  426. static void catc_ctrl_done(struct urb *urb)
  427. {
  428. struct catc *catc = urb->context;
  429. struct ctrl_queue *q;
  430. unsigned long flags;
  431. int status = urb->status;
  432. if (status)
  433. dev_dbg(&urb->dev->dev, "ctrl_done, status %d, len %d.\n",
  434. status, urb->actual_length);
  435. spin_lock_irqsave(&catc->ctrl_lock, flags);
  436. q = catc->ctrl_queue + catc->ctrl_tail;
  437. if (q->dir) {
  438. if (q->buf && q->len)
  439. memcpy(q->buf, catc->ctrl_buf, q->len);
  440. else
  441. q->buf = catc->ctrl_buf;
  442. }
  443. if (q->callback)
  444. q->callback(catc, q);
  445. catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
  446. if (catc->ctrl_head != catc->ctrl_tail)
  447. catc_ctrl_run(catc);
  448. else
  449. clear_bit(CTRL_RUNNING, &catc->flags);
  450. spin_unlock_irqrestore(&catc->ctrl_lock, flags);
  451. }
  452. static int catc_ctrl_async(struct catc *catc, u8 dir, u8 request, u16 value,
  453. u16 index, void *buf, int len, void (*callback)(struct catc *catc, struct ctrl_queue *q))
  454. {
  455. struct ctrl_queue *q;
  456. int retval = 0;
  457. unsigned long flags;
  458. spin_lock_irqsave(&catc->ctrl_lock, flags);
  459. q = catc->ctrl_queue + catc->ctrl_head;
  460. q->dir = dir;
  461. q->request = request;
  462. q->value = value;
  463. q->index = index;
  464. q->buf = buf;
  465. q->len = len;
  466. q->callback = callback;
  467. catc->ctrl_head = (catc->ctrl_head + 1) & (CTRL_QUEUE - 1);
  468. if (catc->ctrl_head == catc->ctrl_tail) {
  469. dev_err(&catc->usbdev->dev, "ctrl queue full\n");
  470. catc->ctrl_tail = (catc->ctrl_tail + 1) & (CTRL_QUEUE - 1);
  471. retval = -1;
  472. }
  473. if (!test_and_set_bit(CTRL_RUNNING, &catc->flags))
  474. catc_ctrl_run(catc);
  475. spin_unlock_irqrestore(&catc->ctrl_lock, flags);
  476. return retval;
  477. }
  478. /*
  479. * Statistics.
  480. */
  481. static void catc_stats_done(struct catc *catc, struct ctrl_queue *q)
  482. {
  483. int index = q->index - EthStats;
  484. u16 data, last;
  485. catc->stats_buf[index] = *((char *)q->buf);
  486. if (index & 1)
  487. return;
  488. data = ((u16)catc->stats_buf[index] << 8) | catc->stats_buf[index + 1];
  489. last = catc->stats_vals[index >> 1];
  490. switch (index) {
  491. case TxSingleColl:
  492. case TxMultiColl:
  493. catc->netdev->stats.collisions += data - last;
  494. break;
  495. case TxExcessColl:
  496. catc->netdev->stats.tx_aborted_errors += data - last;
  497. catc->netdev->stats.tx_errors += data - last;
  498. break;
  499. case RxFramErr:
  500. catc->netdev->stats.rx_frame_errors += data - last;
  501. catc->netdev->stats.rx_errors += data - last;
  502. break;
  503. }
  504. catc->stats_vals[index >> 1] = data;
  505. }
  506. static void catc_stats_timer(unsigned long data)
  507. {
  508. struct catc *catc = (void *) data;
  509. int i;
  510. for (i = 0; i < 8; i++)
  511. catc_get_reg_async(catc, EthStats + 7 - i, catc_stats_done);
  512. mod_timer(&catc->timer, jiffies + STATS_UPDATE);
  513. }
  514. /*
  515. * Receive modes. Broadcast, Multicast, Promisc.
  516. */
  517. static void catc_multicast(unsigned char *addr, u8 *multicast)
  518. {
  519. u32 crc;
  520. crc = ether_crc_le(6, addr);
  521. multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
  522. }
  523. static void catc_set_multicast_list(struct net_device *netdev)
  524. {
  525. struct catc *catc = netdev_priv(netdev);
  526. struct netdev_hw_addr *ha;
  527. u8 broadcast[6];
  528. u8 rx = RxEnable | RxPolarity | RxMultiCast;
  529. memset(broadcast, 0xff, 6);
  530. memset(catc->multicast, 0, 64);
  531. catc_multicast(broadcast, catc->multicast);
  532. catc_multicast(netdev->dev_addr, catc->multicast);
  533. if (netdev->flags & IFF_PROMISC) {
  534. memset(catc->multicast, 0xff, 64);
  535. rx |= (!catc->is_f5u011) ? RxPromisc : AltRxPromisc;
  536. }
  537. if (netdev->flags & IFF_ALLMULTI) {
  538. memset(catc->multicast, 0xff, 64);
  539. } else {
  540. netdev_for_each_mc_addr(ha, netdev) {
  541. u32 crc = ether_crc_le(6, ha->addr);
  542. if (!catc->is_f5u011) {
  543. catc->multicast[(crc >> 3) & 0x3f] |= 1 << (crc & 7);
  544. } else {
  545. catc->multicast[7-(crc >> 29)] |= 1 << ((crc >> 26) & 7);
  546. }
  547. }
  548. }
  549. if (!catc->is_f5u011) {
  550. catc_set_reg_async(catc, RxUnit, rx);
  551. catc_write_mem_async(catc, 0xfa80, catc->multicast, 64);
  552. } else {
  553. f5u011_mchash_async(catc, catc->multicast);
  554. if (catc->rxmode[0] != rx) {
  555. catc->rxmode[0] = rx;
  556. netdev_dbg(catc->netdev,
  557. "Setting RX mode to %2.2X %2.2X\n",
  558. catc->rxmode[0], catc->rxmode[1]);
  559. f5u011_rxmode_async(catc, catc->rxmode);
  560. }
  561. }
  562. }
  563. static void catc_get_drvinfo(struct net_device *dev,
  564. struct ethtool_drvinfo *info)
  565. {
  566. struct catc *catc = netdev_priv(dev);
  567. strlcpy(info->driver, driver_name, sizeof(info->driver));
  568. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  569. usb_make_path(catc->usbdev, info->bus_info, sizeof(info->bus_info));
  570. }
  571. static int catc_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  572. {
  573. struct catc *catc = netdev_priv(dev);
  574. if (!catc->is_f5u011)
  575. return -EOPNOTSUPP;
  576. cmd->supported = SUPPORTED_10baseT_Half | SUPPORTED_TP;
  577. cmd->advertising = ADVERTISED_10baseT_Half | ADVERTISED_TP;
  578. ethtool_cmd_speed_set(cmd, SPEED_10);
  579. cmd->duplex = DUPLEX_HALF;
  580. cmd->port = PORT_TP;
  581. cmd->phy_address = 0;
  582. cmd->transceiver = XCVR_INTERNAL;
  583. cmd->autoneg = AUTONEG_DISABLE;
  584. cmd->maxtxpkt = 1;
  585. cmd->maxrxpkt = 1;
  586. return 0;
  587. }
  588. static const struct ethtool_ops ops = {
  589. .get_drvinfo = catc_get_drvinfo,
  590. .get_settings = catc_get_settings,
  591. .get_link = ethtool_op_get_link
  592. };
  593. /*
  594. * Open, close.
  595. */
  596. static int catc_open(struct net_device *netdev)
  597. {
  598. struct catc *catc = netdev_priv(netdev);
  599. int status;
  600. catc->irq_urb->dev = catc->usbdev;
  601. if ((status = usb_submit_urb(catc->irq_urb, GFP_KERNEL)) < 0) {
  602. dev_err(&catc->usbdev->dev, "submit(irq_urb) status %d\n",
  603. status);
  604. return -1;
  605. }
  606. netif_start_queue(netdev);
  607. if (!catc->is_f5u011)
  608. mod_timer(&catc->timer, jiffies + STATS_UPDATE);
  609. return 0;
  610. }
  611. static int catc_stop(struct net_device *netdev)
  612. {
  613. struct catc *catc = netdev_priv(netdev);
  614. netif_stop_queue(netdev);
  615. if (!catc->is_f5u011)
  616. del_timer_sync(&catc->timer);
  617. usb_kill_urb(catc->rx_urb);
  618. usb_kill_urb(catc->tx_urb);
  619. usb_kill_urb(catc->irq_urb);
  620. usb_kill_urb(catc->ctrl_urb);
  621. return 0;
  622. }
  623. static const struct net_device_ops catc_netdev_ops = {
  624. .ndo_open = catc_open,
  625. .ndo_stop = catc_stop,
  626. .ndo_start_xmit = catc_start_xmit,
  627. .ndo_tx_timeout = catc_tx_timeout,
  628. .ndo_set_rx_mode = catc_set_multicast_list,
  629. .ndo_change_mtu = eth_change_mtu,
  630. .ndo_set_mac_address = eth_mac_addr,
  631. .ndo_validate_addr = eth_validate_addr,
  632. };
  633. /*
  634. * USB probe, disconnect.
  635. */
  636. static int catc_probe(struct usb_interface *intf, const struct usb_device_id *id)
  637. {
  638. struct device *dev = &intf->dev;
  639. struct usb_device *usbdev = interface_to_usbdev(intf);
  640. struct net_device *netdev;
  641. struct catc *catc;
  642. u8 broadcast[6];
  643. int i, pktsz;
  644. if (usb_set_interface(usbdev,
  645. intf->altsetting->desc.bInterfaceNumber, 1)) {
  646. dev_err(dev, "Can't set altsetting 1.\n");
  647. return -EIO;
  648. }
  649. netdev = alloc_etherdev(sizeof(struct catc));
  650. if (!netdev)
  651. return -ENOMEM;
  652. catc = netdev_priv(netdev);
  653. netdev->netdev_ops = &catc_netdev_ops;
  654. netdev->watchdog_timeo = TX_TIMEOUT;
  655. SET_ETHTOOL_OPS(netdev, &ops);
  656. catc->usbdev = usbdev;
  657. catc->netdev = netdev;
  658. spin_lock_init(&catc->tx_lock);
  659. spin_lock_init(&catc->ctrl_lock);
  660. init_timer(&catc->timer);
  661. catc->timer.data = (long) catc;
  662. catc->timer.function = catc_stats_timer;
  663. catc->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
  664. catc->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  665. catc->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  666. catc->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  667. if ((!catc->ctrl_urb) || (!catc->tx_urb) ||
  668. (!catc->rx_urb) || (!catc->irq_urb)) {
  669. dev_err(&intf->dev, "No free urbs available.\n");
  670. usb_free_urb(catc->ctrl_urb);
  671. usb_free_urb(catc->tx_urb);
  672. usb_free_urb(catc->rx_urb);
  673. usb_free_urb(catc->irq_urb);
  674. free_netdev(netdev);
  675. return -ENOMEM;
  676. }
  677. /* The F5U011 has the same vendor/product as the netmate but a device version of 0x130 */
  678. if (le16_to_cpu(usbdev->descriptor.idVendor) == 0x0423 &&
  679. le16_to_cpu(usbdev->descriptor.idProduct) == 0xa &&
  680. le16_to_cpu(catc->usbdev->descriptor.bcdDevice) == 0x0130) {
  681. dev_dbg(dev, "Testing for f5u011\n");
  682. catc->is_f5u011 = 1;
  683. atomic_set(&catc->recq_sz, 0);
  684. pktsz = RX_PKT_SZ;
  685. } else {
  686. pktsz = RX_MAX_BURST * (PKT_SZ + 2);
  687. }
  688. usb_fill_control_urb(catc->ctrl_urb, usbdev, usb_sndctrlpipe(usbdev, 0),
  689. NULL, NULL, 0, catc_ctrl_done, catc);
  690. usb_fill_bulk_urb(catc->tx_urb, usbdev, usb_sndbulkpipe(usbdev, 1),
  691. NULL, 0, catc_tx_done, catc);
  692. usb_fill_bulk_urb(catc->rx_urb, usbdev, usb_rcvbulkpipe(usbdev, 1),
  693. catc->rx_buf, pktsz, catc_rx_done, catc);
  694. usb_fill_int_urb(catc->irq_urb, usbdev, usb_rcvintpipe(usbdev, 2),
  695. catc->irq_buf, 2, catc_irq_done, catc, 1);
  696. if (!catc->is_f5u011) {
  697. dev_dbg(dev, "Checking memory size\n");
  698. i = 0x12345678;
  699. catc_write_mem(catc, 0x7a80, &i, 4);
  700. i = 0x87654321;
  701. catc_write_mem(catc, 0xfa80, &i, 4);
  702. catc_read_mem(catc, 0x7a80, &i, 4);
  703. switch (i) {
  704. case 0x12345678:
  705. catc_set_reg(catc, TxBufCount, 8);
  706. catc_set_reg(catc, RxBufCount, 32);
  707. dev_dbg(dev, "64k Memory\n");
  708. break;
  709. default:
  710. dev_warn(&intf->dev,
  711. "Couldn't detect memory size, assuming 32k\n");
  712. case 0x87654321:
  713. catc_set_reg(catc, TxBufCount, 4);
  714. catc_set_reg(catc, RxBufCount, 16);
  715. dev_dbg(dev, "32k Memory\n");
  716. break;
  717. }
  718. dev_dbg(dev, "Getting MAC from SEEROM.\n");
  719. catc_get_mac(catc, netdev->dev_addr);
  720. dev_dbg(dev, "Setting MAC into registers.\n");
  721. for (i = 0; i < 6; i++)
  722. catc_set_reg(catc, StationAddr0 - i, netdev->dev_addr[i]);
  723. dev_dbg(dev, "Filling the multicast list.\n");
  724. memset(broadcast, 0xff, 6);
  725. catc_multicast(broadcast, catc->multicast);
  726. catc_multicast(netdev->dev_addr, catc->multicast);
  727. catc_write_mem(catc, 0xfa80, catc->multicast, 64);
  728. dev_dbg(dev, "Clearing error counters.\n");
  729. for (i = 0; i < 8; i++)
  730. catc_set_reg(catc, EthStats + i, 0);
  731. catc->last_stats = jiffies;
  732. dev_dbg(dev, "Enabling.\n");
  733. catc_set_reg(catc, MaxBurst, RX_MAX_BURST);
  734. catc_set_reg(catc, OpModes, OpTxMerge | OpRxMerge | OpLenInclude | Op3MemWaits);
  735. catc_set_reg(catc, LEDCtrl, LEDLink);
  736. catc_set_reg(catc, RxUnit, RxEnable | RxPolarity | RxMultiCast);
  737. } else {
  738. dev_dbg(dev, "Performing reset\n");
  739. catc_reset(catc);
  740. catc_get_mac(catc, netdev->dev_addr);
  741. dev_dbg(dev, "Setting RX Mode\n");
  742. catc->rxmode[0] = RxEnable | RxPolarity | RxMultiCast;
  743. catc->rxmode[1] = 0;
  744. f5u011_rxmode(catc, catc->rxmode);
  745. }
  746. dev_dbg(dev, "Init done.\n");
  747. printk(KERN_INFO "%s: %s USB Ethernet at usb-%s-%s, %pM.\n",
  748. netdev->name, (catc->is_f5u011) ? "Belkin F5U011" : "CATC EL1210A NetMate",
  749. usbdev->bus->bus_name, usbdev->devpath, netdev->dev_addr);
  750. usb_set_intfdata(intf, catc);
  751. SET_NETDEV_DEV(netdev, &intf->dev);
  752. if (register_netdev(netdev) != 0) {
  753. usb_set_intfdata(intf, NULL);
  754. usb_free_urb(catc->ctrl_urb);
  755. usb_free_urb(catc->tx_urb);
  756. usb_free_urb(catc->rx_urb);
  757. usb_free_urb(catc->irq_urb);
  758. free_netdev(netdev);
  759. return -EIO;
  760. }
  761. return 0;
  762. }
  763. static void catc_disconnect(struct usb_interface *intf)
  764. {
  765. struct catc *catc = usb_get_intfdata(intf);
  766. usb_set_intfdata(intf, NULL);
  767. if (catc) {
  768. unregister_netdev(catc->netdev);
  769. usb_free_urb(catc->ctrl_urb);
  770. usb_free_urb(catc->tx_urb);
  771. usb_free_urb(catc->rx_urb);
  772. usb_free_urb(catc->irq_urb);
  773. free_netdev(catc->netdev);
  774. }
  775. }
  776. /*
  777. * Module functions and tables.
  778. */
  779. static struct usb_device_id catc_id_table [] = {
  780. { USB_DEVICE(0x0423, 0xa) }, /* CATC Netmate, Belkin F5U011 */
  781. { USB_DEVICE(0x0423, 0xc) }, /* CATC Netmate II, Belkin F5U111 */
  782. { USB_DEVICE(0x08d1, 0x1) }, /* smartBridges smartNIC */
  783. { }
  784. };
  785. MODULE_DEVICE_TABLE(usb, catc_id_table);
  786. static struct usb_driver catc_driver = {
  787. .name = driver_name,
  788. .probe = catc_probe,
  789. .disconnect = catc_disconnect,
  790. .id_table = catc_id_table,
  791. .disable_hub_initiated_lpm = 1,
  792. };
  793. module_usb_driver(catc_driver);