catc.c 24 KB

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