catc.c 23 KB

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