catc.c 24 KB

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