catc.c 24 KB

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