pegasus.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 1999-2021 Petko Manolov (petkan@nucleusys.com)
  4. *
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/delay.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/mii.h>
  14. #include <linux/usb.h>
  15. #include <linux/module.h>
  16. #include <asm/byteorder.h>
  17. #include <linux/uaccess.h>
  18. #include "pegasus.h"
  19. #define DRIVER_AUTHOR "Petko Manolov <petkan@nucleusys.com>"
  20. #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
  21. static const char driver_name[] = "pegasus";
  22. #undef PEGASUS_WRITE_EEPROM
  23. #define BMSR_MEDIA (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
  24. BMSR_100FULL | BMSR_ANEGCAPABLE)
  25. #define CARRIER_CHECK_DELAY (2 * HZ)
  26. /*
  27. * USB endpoints.
  28. */
  29. enum pegasus_usb_ep {
  30. PEGASUS_USB_EP_CONTROL = 0,
  31. PEGASUS_USB_EP_BULK_IN = 1,
  32. PEGASUS_USB_EP_BULK_OUT = 2,
  33. PEGASUS_USB_EP_INT_IN = 3,
  34. };
  35. static bool loopback;
  36. static bool mii_mode;
  37. static char *devid;
  38. static struct usb_eth_dev usb_dev_id[] = {
  39. #define PEGASUS_DEV(pn, vid, pid, flags) \
  40. {.name = pn, .vendor = vid, .device = pid, .private = flags},
  41. #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
  42. PEGASUS_DEV(pn, vid, pid, flags)
  43. #include "pegasus.h"
  44. #undef PEGASUS_DEV
  45. #undef PEGASUS_DEV_CLASS
  46. {NULL, 0, 0, 0},
  47. {NULL, 0, 0, 0}
  48. };
  49. static struct usb_device_id pegasus_ids[] = {
  50. #define PEGASUS_DEV(pn, vid, pid, flags) \
  51. {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
  52. /*
  53. * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
  54. * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
  55. * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
  56. * case anyway, seeing as the pegasus is for "Wired" adaptors.
  57. */
  58. #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
  59. {.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
  60. .idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
  61. #include "pegasus.h"
  62. #undef PEGASUS_DEV
  63. #undef PEGASUS_DEV_CLASS
  64. {},
  65. {}
  66. };
  67. MODULE_AUTHOR(DRIVER_AUTHOR);
  68. MODULE_DESCRIPTION(DRIVER_DESC);
  69. MODULE_LICENSE("GPL");
  70. module_param(loopback, bool, 0);
  71. module_param(mii_mode, bool, 0);
  72. module_param(devid, charp, 0);
  73. MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
  74. MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
  75. MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
  76. /* use ethtool to change the level for any given device */
  77. static int msg_level = -1;
  78. module_param(msg_level, int, 0);
  79. MODULE_PARM_DESC(msg_level, "Override default message level");
  80. MODULE_DEVICE_TABLE(usb, pegasus_ids);
  81. static const struct net_device_ops pegasus_netdev_ops;
  82. /*****/
  83. static void async_ctrl_callback(struct urb *urb)
  84. {
  85. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  86. int status = urb->status;
  87. if (status < 0)
  88. dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
  89. kfree(req);
  90. usb_free_urb(urb);
  91. }
  92. static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
  93. {
  94. return usb_control_msg_recv(pegasus->usb, 0, PEGASUS_REQ_GET_REGS,
  95. PEGASUS_REQT_READ, 0, indx, data, size,
  96. 1000, GFP_NOIO);
  97. }
  98. static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
  99. const void *data)
  100. {
  101. int ret;
  102. ret = usb_control_msg_send(pegasus->usb, 0, PEGASUS_REQ_SET_REGS,
  103. PEGASUS_REQT_WRITE, 0, indx, data, size,
  104. 1000, GFP_NOIO);
  105. if (ret < 0)
  106. netif_dbg(pegasus, drv, pegasus->net, "%s failed with %d\n", __func__, ret);
  107. return ret;
  108. }
  109. /*
  110. * There is only one way to write to a single ADM8511 register and this is via
  111. * specific control request. 'data' is ignored by the device, but it is here to
  112. * not break the API.
  113. */
  114. static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
  115. {
  116. void *buf = &data;
  117. int ret;
  118. ret = usb_control_msg_send(pegasus->usb, 0, PEGASUS_REQ_SET_REG,
  119. PEGASUS_REQT_WRITE, data, indx, buf, 1,
  120. 1000, GFP_NOIO);
  121. if (ret < 0)
  122. netif_dbg(pegasus, drv, pegasus->net, "%s failed with %d\n", __func__, ret);
  123. return ret;
  124. }
  125. static int update_eth_regs_async(pegasus_t *pegasus)
  126. {
  127. int ret = -ENOMEM;
  128. struct urb *async_urb;
  129. struct usb_ctrlrequest *req;
  130. req = kmalloc_obj(struct usb_ctrlrequest, GFP_ATOMIC);
  131. if (req == NULL)
  132. return ret;
  133. async_urb = usb_alloc_urb(0, GFP_ATOMIC);
  134. if (async_urb == NULL) {
  135. kfree(req);
  136. return ret;
  137. }
  138. req->bRequestType = PEGASUS_REQT_WRITE;
  139. req->bRequest = PEGASUS_REQ_SET_REGS;
  140. req->wValue = cpu_to_le16(0);
  141. req->wIndex = cpu_to_le16(EthCtrl0);
  142. req->wLength = cpu_to_le16(3);
  143. usb_fill_control_urb(async_urb, pegasus->usb,
  144. usb_sndctrlpipe(pegasus->usb, 0), (void *)req,
  145. pegasus->eth_regs, 3, async_ctrl_callback, req);
  146. ret = usb_submit_urb(async_urb, GFP_ATOMIC);
  147. if (ret) {
  148. if (ret == -ENODEV)
  149. netif_device_detach(pegasus->net);
  150. netif_err(pegasus, drv, pegasus->net,
  151. "%s returned %d\n", __func__, ret);
  152. usb_free_urb(async_urb);
  153. kfree(req);
  154. }
  155. return ret;
  156. }
  157. static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
  158. {
  159. int i, ret;
  160. __le16 regdi;
  161. __u8 data[4] = { phy, 0, 0, indx };
  162. if (cmd & PHY_WRITE) {
  163. __le16 *t = (__le16 *) & data[1];
  164. *t = cpu_to_le16(*regd);
  165. }
  166. set_register(p, PhyCtrl, 0);
  167. set_registers(p, PhyAddr, sizeof(data), data);
  168. set_register(p, PhyCtrl, (indx | cmd));
  169. for (i = 0; i < REG_TIMEOUT; i++) {
  170. ret = get_registers(p, PhyCtrl, 1, data);
  171. if (ret < 0)
  172. goto fail;
  173. if (data[0] & PHY_DONE)
  174. break;
  175. }
  176. if (i >= REG_TIMEOUT) {
  177. ret = -ETIMEDOUT;
  178. goto fail;
  179. }
  180. if (cmd & PHY_READ) {
  181. ret = get_registers(p, PhyData, 2, &regdi);
  182. if (ret < 0)
  183. goto fail;
  184. *regd = le16_to_cpu(regdi);
  185. }
  186. return 0;
  187. fail:
  188. netif_dbg(p, drv, p->net, "%s failed\n", __func__);
  189. return ret;
  190. }
  191. /* Returns non-negative int on success, error on failure */
  192. static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
  193. {
  194. return __mii_op(pegasus, phy, indx, regd, PHY_READ);
  195. }
  196. /* Returns zero on success, error on failure */
  197. static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
  198. {
  199. return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
  200. }
  201. static int mdio_read(struct net_device *dev, int phy_id, int loc)
  202. {
  203. pegasus_t *pegasus = netdev_priv(dev);
  204. int ret;
  205. u16 res;
  206. ret = read_mii_word(pegasus, phy_id, loc, &res);
  207. if (ret < 0)
  208. return ret;
  209. return (int)res;
  210. }
  211. static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
  212. {
  213. pegasus_t *pegasus = netdev_priv(dev);
  214. u16 data = val;
  215. write_mii_word(pegasus, phy_id, loc, &data);
  216. }
  217. static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
  218. {
  219. int ret, i;
  220. __le16 retdatai;
  221. __u8 tmp = 0;
  222. set_register(pegasus, EpromCtrl, 0);
  223. set_register(pegasus, EpromOffset, index);
  224. set_register(pegasus, EpromCtrl, EPROM_READ);
  225. for (i = 0; i < REG_TIMEOUT; i++) {
  226. ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
  227. if (ret < 0)
  228. goto fail;
  229. if (tmp & EPROM_DONE)
  230. break;
  231. }
  232. if (i >= REG_TIMEOUT) {
  233. ret = -ETIMEDOUT;
  234. goto fail;
  235. }
  236. ret = get_registers(pegasus, EpromData, 2, &retdatai);
  237. if (ret < 0)
  238. goto fail;
  239. *retdata = le16_to_cpu(retdatai);
  240. return ret;
  241. fail:
  242. netif_dbg(pegasus, drv, pegasus->net, "%s failed\n", __func__);
  243. return ret;
  244. }
  245. #ifdef PEGASUS_WRITE_EEPROM
  246. static inline void enable_eprom_write(pegasus_t *pegasus)
  247. {
  248. __u8 tmp;
  249. get_registers(pegasus, EthCtrl2, 1, &tmp);
  250. set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
  251. }
  252. static inline void disable_eprom_write(pegasus_t *pegasus)
  253. {
  254. __u8 tmp;
  255. get_registers(pegasus, EthCtrl2, 1, &tmp);
  256. set_register(pegasus, EpromCtrl, 0);
  257. set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
  258. }
  259. static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
  260. {
  261. int i;
  262. __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
  263. int ret;
  264. __le16 le_data = cpu_to_le16(data);
  265. set_registers(pegasus, EpromOffset, 4, d);
  266. enable_eprom_write(pegasus);
  267. set_register(pegasus, EpromOffset, index);
  268. set_registers(pegasus, EpromData, 2, &le_data);
  269. set_register(pegasus, EpromCtrl, EPROM_WRITE);
  270. for (i = 0; i < REG_TIMEOUT; i++) {
  271. ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
  272. if (ret == -ESHUTDOWN)
  273. goto fail;
  274. if (tmp & EPROM_DONE)
  275. break;
  276. }
  277. disable_eprom_write(pegasus);
  278. if (i >= REG_TIMEOUT)
  279. goto fail;
  280. return ret;
  281. fail:
  282. netif_dbg(pegasus, drv, pegasus->net, "%s failed\n", __func__);
  283. return -ETIMEDOUT;
  284. }
  285. #endif /* PEGASUS_WRITE_EEPROM */
  286. static inline int get_node_id(pegasus_t *pegasus, u8 *id)
  287. {
  288. int i, ret;
  289. u16 w16;
  290. for (i = 0; i < 3; i++) {
  291. ret = read_eprom_word(pegasus, i, &w16);
  292. if (ret < 0)
  293. return ret;
  294. ((__le16 *) id)[i] = cpu_to_le16(w16);
  295. }
  296. return 0;
  297. }
  298. static void set_ethernet_addr(pegasus_t *pegasus)
  299. {
  300. int ret;
  301. u8 node_id[6];
  302. if (pegasus->features & PEGASUS_II) {
  303. ret = get_registers(pegasus, 0x10, sizeof(node_id), node_id);
  304. if (ret < 0)
  305. goto err;
  306. } else {
  307. ret = get_node_id(pegasus, node_id);
  308. if (ret < 0)
  309. goto err;
  310. ret = set_registers(pegasus, EthID, sizeof(node_id), node_id);
  311. if (ret < 0)
  312. goto err;
  313. }
  314. eth_hw_addr_set(pegasus->net, node_id);
  315. return;
  316. err:
  317. eth_hw_addr_random(pegasus->net);
  318. netif_dbg(pegasus, drv, pegasus->net, "software assigned MAC address.\n");
  319. return;
  320. }
  321. static inline int reset_mac(pegasus_t *pegasus)
  322. {
  323. int ret, i;
  324. __u8 data = 0x8;
  325. set_register(pegasus, EthCtrl1, data);
  326. for (i = 0; i < REG_TIMEOUT; i++) {
  327. ret = get_registers(pegasus, EthCtrl1, 1, &data);
  328. if (ret < 0)
  329. goto fail;
  330. if (~data & 0x08) {
  331. if (loopback)
  332. break;
  333. if (mii_mode && (pegasus->features & HAS_HOME_PNA))
  334. set_register(pegasus, Gpio1, 0x34);
  335. else
  336. set_register(pegasus, Gpio1, 0x26);
  337. set_register(pegasus, Gpio0, pegasus->features);
  338. set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
  339. break;
  340. }
  341. }
  342. if (i == REG_TIMEOUT)
  343. return -ETIMEDOUT;
  344. if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
  345. usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
  346. set_register(pegasus, Gpio0, 0x24);
  347. set_register(pegasus, Gpio0, 0x26);
  348. }
  349. if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
  350. __u16 auxmode;
  351. ret = read_mii_word(pegasus, 3, 0x1b, &auxmode);
  352. if (ret < 0)
  353. goto fail;
  354. auxmode |= 4;
  355. write_mii_word(pegasus, 3, 0x1b, &auxmode);
  356. }
  357. return 0;
  358. fail:
  359. netif_dbg(pegasus, drv, pegasus->net, "%s failed\n", __func__);
  360. return ret;
  361. }
  362. static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
  363. {
  364. pegasus_t *pegasus = netdev_priv(dev);
  365. int ret;
  366. __u16 linkpart;
  367. __u8 data[4];
  368. ret = read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
  369. if (ret < 0)
  370. goto fail;
  371. data[0] = 0xc8; /* TX & RX enable, append status, no CRC */
  372. data[1] = 0;
  373. if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
  374. data[1] |= 0x20; /* set full duplex */
  375. if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
  376. data[1] |= 0x10; /* set 100 Mbps */
  377. if (mii_mode)
  378. data[1] = 0;
  379. data[2] = loopback ? 0x09 : 0x01;
  380. memcpy(pegasus->eth_regs, data, sizeof(data));
  381. ret = set_registers(pegasus, EthCtrl0, 3, data);
  382. if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
  383. usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
  384. usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
  385. u16 auxmode;
  386. ret = read_mii_word(pegasus, 0, 0x1b, &auxmode);
  387. if (ret < 0)
  388. goto fail;
  389. auxmode |= 4;
  390. write_mii_word(pegasus, 0, 0x1b, &auxmode);
  391. }
  392. return ret;
  393. fail:
  394. netif_dbg(pegasus, drv, pegasus->net, "%s failed\n", __func__);
  395. return ret;
  396. }
  397. static void read_bulk_callback(struct urb *urb)
  398. {
  399. pegasus_t *pegasus = urb->context;
  400. struct net_device *net;
  401. u8 *buf = urb->transfer_buffer;
  402. int rx_status, count = urb->actual_length;
  403. int status = urb->status;
  404. __u16 pkt_len;
  405. if (!pegasus)
  406. return;
  407. net = pegasus->net;
  408. if (!netif_device_present(net) || !netif_running(net))
  409. return;
  410. switch (status) {
  411. case 0:
  412. break;
  413. case -ETIME:
  414. netif_dbg(pegasus, rx_err, net, "reset MAC\n");
  415. pegasus->flags &= ~PEGASUS_RX_BUSY;
  416. break;
  417. case -EPIPE: /* stall, or disconnect from TT */
  418. /* FIXME schedule work to clear the halt */
  419. netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
  420. return;
  421. case -ENOENT:
  422. case -ECONNRESET:
  423. case -ESHUTDOWN:
  424. netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
  425. return;
  426. default:
  427. netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
  428. goto goon;
  429. }
  430. if (count < 4)
  431. goto goon;
  432. rx_status = buf[count - 2];
  433. if (rx_status & 0x1c) {
  434. netif_dbg(pegasus, rx_err, net,
  435. "RX packet error %x\n", rx_status);
  436. net->stats.rx_errors++;
  437. if (rx_status & 0x04) /* runt */
  438. net->stats.rx_length_errors++;
  439. if (rx_status & 0x08)
  440. net->stats.rx_crc_errors++;
  441. if (rx_status & 0x10) /* extra bits */
  442. net->stats.rx_frame_errors++;
  443. goto goon;
  444. }
  445. if (pegasus->chip == 0x8513) {
  446. pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
  447. pkt_len &= 0x0fff;
  448. pegasus->rx_skb->data += 2;
  449. } else {
  450. pkt_len = buf[count - 3] << 8;
  451. pkt_len += buf[count - 4];
  452. pkt_len &= 0xfff;
  453. pkt_len -= 4;
  454. }
  455. /*
  456. * If the packet is unreasonably long, quietly drop it rather than
  457. * kernel panicing by calling skb_put.
  458. */
  459. if (pkt_len > PEGASUS_MTU)
  460. goto goon;
  461. /*
  462. * at this point we are sure pegasus->rx_skb != NULL
  463. * so we go ahead and pass up the packet.
  464. */
  465. skb_put(pegasus->rx_skb, pkt_len);
  466. pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
  467. netif_rx(pegasus->rx_skb);
  468. net->stats.rx_packets++;
  469. net->stats.rx_bytes += pkt_len;
  470. if (pegasus->flags & PEGASUS_UNPLUG)
  471. return;
  472. pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
  473. GFP_ATOMIC);
  474. if (pegasus->rx_skb == NULL)
  475. goto tl_sched;
  476. goon:
  477. usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
  478. usb_rcvbulkpipe(pegasus->usb, PEGASUS_USB_EP_BULK_IN),
  479. pegasus->rx_skb->data, PEGASUS_MTU,
  480. read_bulk_callback, pegasus);
  481. rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
  482. if (rx_status == -ENODEV)
  483. netif_device_detach(pegasus->net);
  484. else if (rx_status) {
  485. pegasus->flags |= PEGASUS_RX_URB_FAIL;
  486. goto tl_sched;
  487. } else {
  488. pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
  489. }
  490. return;
  491. tl_sched:
  492. tasklet_schedule(&pegasus->rx_tl);
  493. }
  494. static void rx_fixup(struct tasklet_struct *t)
  495. {
  496. pegasus_t *pegasus = from_tasklet(pegasus, t, rx_tl);
  497. int status;
  498. if (pegasus->flags & PEGASUS_UNPLUG)
  499. return;
  500. if (pegasus->flags & PEGASUS_RX_URB_FAIL)
  501. if (pegasus->rx_skb)
  502. goto try_again;
  503. if (pegasus->rx_skb == NULL)
  504. pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
  505. PEGASUS_MTU,
  506. GFP_ATOMIC);
  507. if (pegasus->rx_skb == NULL) {
  508. netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
  509. tasklet_schedule(&pegasus->rx_tl);
  510. return;
  511. }
  512. usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
  513. usb_rcvbulkpipe(pegasus->usb, PEGASUS_USB_EP_BULK_IN),
  514. pegasus->rx_skb->data, PEGASUS_MTU,
  515. read_bulk_callback, pegasus);
  516. try_again:
  517. status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
  518. if (status == -ENODEV)
  519. netif_device_detach(pegasus->net);
  520. else if (status) {
  521. pegasus->flags |= PEGASUS_RX_URB_FAIL;
  522. tasklet_schedule(&pegasus->rx_tl);
  523. } else {
  524. pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
  525. }
  526. }
  527. static void write_bulk_callback(struct urb *urb)
  528. {
  529. pegasus_t *pegasus = urb->context;
  530. struct net_device *net;
  531. int status = urb->status;
  532. if (!pegasus)
  533. return;
  534. net = pegasus->net;
  535. if (!netif_device_present(net) || !netif_running(net))
  536. return;
  537. switch (status) {
  538. case -EPIPE:
  539. /* FIXME schedule_work() to clear the tx halt */
  540. netif_stop_queue(net);
  541. netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
  542. return;
  543. case -ENOENT:
  544. case -ECONNRESET:
  545. case -ESHUTDOWN:
  546. netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
  547. return;
  548. default:
  549. netif_info(pegasus, tx_err, net, "TX status %d\n", status);
  550. fallthrough;
  551. case 0:
  552. break;
  553. }
  554. netif_trans_update(net); /* prevent tx timeout */
  555. netif_wake_queue(net);
  556. }
  557. static void intr_callback(struct urb *urb)
  558. {
  559. pegasus_t *pegasus = urb->context;
  560. struct net_device *net;
  561. int res, status = urb->status;
  562. if (!pegasus)
  563. return;
  564. net = pegasus->net;
  565. switch (status) {
  566. case 0:
  567. break;
  568. case -ECONNRESET: /* unlink */
  569. case -ENOENT:
  570. case -ESHUTDOWN:
  571. return;
  572. default:
  573. /* some Pegasus-I products report LOTS of data
  574. * toggle errors... avoid log spamming
  575. */
  576. netif_dbg(pegasus, timer, net, "intr status %d\n", status);
  577. }
  578. if (urb->actual_length >= 6) {
  579. u8 *d = urb->transfer_buffer;
  580. /* byte 0 == tx_status1, reg 2B */
  581. if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
  582. |LATE_COL|JABBER_TIMEOUT)) {
  583. net->stats.tx_errors++;
  584. if (d[0] & TX_UNDERRUN)
  585. net->stats.tx_fifo_errors++;
  586. if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
  587. net->stats.tx_aborted_errors++;
  588. if (d[0] & LATE_COL)
  589. net->stats.tx_window_errors++;
  590. }
  591. /* d[5].LINK_STATUS lies on some adapters.
  592. * d[0].NO_CARRIER kicks in only with failed TX.
  593. * ... so monitoring with MII may be safest.
  594. */
  595. /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
  596. net->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
  597. }
  598. res = usb_submit_urb(urb, GFP_ATOMIC);
  599. if (res == -ENODEV)
  600. netif_device_detach(pegasus->net);
  601. if (res)
  602. netif_err(pegasus, timer, net,
  603. "can't resubmit interrupt urb, %d\n", res);
  604. }
  605. static void pegasus_tx_timeout(struct net_device *net, unsigned int txqueue)
  606. {
  607. pegasus_t *pegasus = netdev_priv(net);
  608. netif_warn(pegasus, timer, net, "tx timeout\n");
  609. usb_unlink_urb(pegasus->tx_urb);
  610. net->stats.tx_errors++;
  611. }
  612. static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
  613. struct net_device *net)
  614. {
  615. pegasus_t *pegasus = netdev_priv(net);
  616. int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
  617. int res;
  618. __u16 l16 = skb->len;
  619. netif_stop_queue(net);
  620. ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
  621. skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
  622. usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
  623. usb_sndbulkpipe(pegasus->usb, PEGASUS_USB_EP_BULK_OUT),
  624. pegasus->tx_buff, count,
  625. write_bulk_callback, pegasus);
  626. if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
  627. netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
  628. switch (res) {
  629. case -EPIPE: /* stall, or disconnect from TT */
  630. /* cleanup should already have been scheduled */
  631. break;
  632. case -ENODEV: /* disconnect() upcoming */
  633. case -EPERM:
  634. netif_device_detach(pegasus->net);
  635. break;
  636. default:
  637. net->stats.tx_errors++;
  638. netif_start_queue(net);
  639. }
  640. } else {
  641. net->stats.tx_packets++;
  642. net->stats.tx_bytes += skb->len;
  643. }
  644. dev_kfree_skb(skb);
  645. return NETDEV_TX_OK;
  646. }
  647. static inline void disable_net_traffic(pegasus_t *pegasus)
  648. {
  649. __le16 tmp = cpu_to_le16(0);
  650. set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
  651. }
  652. static inline int get_interrupt_interval(pegasus_t *pegasus)
  653. {
  654. u16 data;
  655. u8 interval;
  656. int ret;
  657. ret = read_eprom_word(pegasus, 4, &data);
  658. if (ret < 0)
  659. return ret;
  660. interval = data >> 8;
  661. if (pegasus->usb->speed != USB_SPEED_HIGH) {
  662. if (interval < 0x80) {
  663. netif_info(pegasus, timer, pegasus->net,
  664. "intr interval changed from %ums to %ums\n",
  665. interval, 0x80);
  666. interval = 0x80;
  667. data = (data & 0x00FF) | ((u16)interval << 8);
  668. #ifdef PEGASUS_WRITE_EEPROM
  669. write_eprom_word(pegasus, 4, data);
  670. #endif
  671. }
  672. }
  673. pegasus->intr_interval = interval;
  674. return 0;
  675. }
  676. static void set_carrier(struct net_device *net)
  677. {
  678. pegasus_t *pegasus = netdev_priv(net);
  679. u16 tmp;
  680. if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
  681. return;
  682. if (tmp & BMSR_LSTATUS)
  683. netif_carrier_on(net);
  684. else
  685. netif_carrier_off(net);
  686. }
  687. static void free_all_urbs(pegasus_t *pegasus)
  688. {
  689. usb_free_urb(pegasus->intr_urb);
  690. usb_free_urb(pegasus->tx_urb);
  691. usb_free_urb(pegasus->rx_urb);
  692. }
  693. static void unlink_all_urbs(pegasus_t *pegasus)
  694. {
  695. usb_kill_urb(pegasus->intr_urb);
  696. usb_kill_urb(pegasus->tx_urb);
  697. usb_kill_urb(pegasus->rx_urb);
  698. }
  699. static int alloc_urbs(pegasus_t *pegasus)
  700. {
  701. static const u8 bulk_ep_addr[] = {
  702. 1 | USB_DIR_IN,
  703. 2 | USB_DIR_OUT,
  704. 0};
  705. static const u8 int_ep_addr[] = {
  706. 3 | USB_DIR_IN,
  707. 0};
  708. int res = -ENOMEM;
  709. if (!usb_check_bulk_endpoints(pegasus->intf, bulk_ep_addr) ||
  710. !usb_check_int_endpoints(pegasus->intf, int_ep_addr))
  711. return -ENODEV;
  712. pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  713. if (!pegasus->rx_urb) {
  714. return res;
  715. }
  716. pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  717. if (!pegasus->tx_urb) {
  718. usb_free_urb(pegasus->rx_urb);
  719. return res;
  720. }
  721. pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  722. if (!pegasus->intr_urb) {
  723. usb_free_urb(pegasus->tx_urb);
  724. usb_free_urb(pegasus->rx_urb);
  725. return res;
  726. }
  727. return 0;
  728. }
  729. static int pegasus_open(struct net_device *net)
  730. {
  731. pegasus_t *pegasus = netdev_priv(net);
  732. int res=-ENOMEM;
  733. if (pegasus->rx_skb == NULL)
  734. pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
  735. PEGASUS_MTU,
  736. GFP_KERNEL);
  737. if (!pegasus->rx_skb)
  738. goto exit;
  739. set_registers(pegasus, EthID, 6, net->dev_addr);
  740. usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
  741. usb_rcvbulkpipe(pegasus->usb, PEGASUS_USB_EP_BULK_IN),
  742. pegasus->rx_skb->data, PEGASUS_MTU,
  743. read_bulk_callback, pegasus);
  744. if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
  745. if (res == -ENODEV)
  746. netif_device_detach(pegasus->net);
  747. netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
  748. goto exit;
  749. }
  750. usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
  751. usb_rcvintpipe(pegasus->usb, PEGASUS_USB_EP_INT_IN),
  752. pegasus->intr_buff, sizeof(pegasus->intr_buff),
  753. intr_callback, pegasus, pegasus->intr_interval);
  754. if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
  755. if (res == -ENODEV)
  756. netif_device_detach(pegasus->net);
  757. netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
  758. usb_kill_urb(pegasus->rx_urb);
  759. goto exit;
  760. }
  761. res = enable_net_traffic(net, pegasus->usb);
  762. if (res < 0) {
  763. netif_dbg(pegasus, ifup, net,
  764. "can't enable_net_traffic() - %d\n", res);
  765. res = -EIO;
  766. usb_kill_urb(pegasus->rx_urb);
  767. usb_kill_urb(pegasus->intr_urb);
  768. goto exit;
  769. }
  770. set_carrier(net);
  771. netif_start_queue(net);
  772. netif_dbg(pegasus, ifup, net, "open\n");
  773. res = 0;
  774. exit:
  775. return res;
  776. }
  777. static int pegasus_close(struct net_device *net)
  778. {
  779. pegasus_t *pegasus = netdev_priv(net);
  780. netif_stop_queue(net);
  781. if (!(pegasus->flags & PEGASUS_UNPLUG))
  782. disable_net_traffic(pegasus);
  783. tasklet_kill(&pegasus->rx_tl);
  784. unlink_all_urbs(pegasus);
  785. return 0;
  786. }
  787. static void pegasus_get_drvinfo(struct net_device *dev,
  788. struct ethtool_drvinfo *info)
  789. {
  790. pegasus_t *pegasus = netdev_priv(dev);
  791. strscpy(info->driver, driver_name, sizeof(info->driver));
  792. usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
  793. }
  794. /* also handles three patterns of some kind in hardware */
  795. #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
  796. static void
  797. pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  798. {
  799. pegasus_t *pegasus = netdev_priv(dev);
  800. wol->supported = WAKE_MAGIC | WAKE_PHY;
  801. wol->wolopts = pegasus->wolopts;
  802. }
  803. static int
  804. pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  805. {
  806. pegasus_t *pegasus = netdev_priv(dev);
  807. u8 reg78 = 0x04;
  808. int ret;
  809. if (wol->wolopts & ~WOL_SUPPORTED)
  810. return -EINVAL;
  811. if (wol->wolopts & WAKE_MAGIC)
  812. reg78 |= 0x80;
  813. if (wol->wolopts & WAKE_PHY)
  814. reg78 |= 0x40;
  815. /* FIXME this 0x10 bit still needs to get set in the chip... */
  816. if (wol->wolopts)
  817. pegasus->eth_regs[0] |= 0x10;
  818. else
  819. pegasus->eth_regs[0] &= ~0x10;
  820. pegasus->wolopts = wol->wolopts;
  821. ret = set_register(pegasus, WakeupControl, reg78);
  822. if (!ret)
  823. ret = device_set_wakeup_enable(&pegasus->usb->dev,
  824. wol->wolopts);
  825. return ret;
  826. }
  827. static inline void pegasus_reset_wol(struct net_device *dev)
  828. {
  829. struct ethtool_wolinfo wol;
  830. memset(&wol, 0, sizeof wol);
  831. (void) pegasus_set_wol(dev, &wol);
  832. }
  833. static int
  834. pegasus_get_link_ksettings(struct net_device *dev,
  835. struct ethtool_link_ksettings *ecmd)
  836. {
  837. pegasus_t *pegasus;
  838. pegasus = netdev_priv(dev);
  839. mii_ethtool_get_link_ksettings(&pegasus->mii, ecmd);
  840. return 0;
  841. }
  842. static int
  843. pegasus_set_link_ksettings(struct net_device *dev,
  844. const struct ethtool_link_ksettings *ecmd)
  845. {
  846. pegasus_t *pegasus = netdev_priv(dev);
  847. return mii_ethtool_set_link_ksettings(&pegasus->mii, ecmd);
  848. }
  849. static int pegasus_nway_reset(struct net_device *dev)
  850. {
  851. pegasus_t *pegasus = netdev_priv(dev);
  852. return mii_nway_restart(&pegasus->mii);
  853. }
  854. static u32 pegasus_get_link(struct net_device *dev)
  855. {
  856. pegasus_t *pegasus = netdev_priv(dev);
  857. return mii_link_ok(&pegasus->mii);
  858. }
  859. static u32 pegasus_get_msglevel(struct net_device *dev)
  860. {
  861. pegasus_t *pegasus = netdev_priv(dev);
  862. return pegasus->msg_enable;
  863. }
  864. static void pegasus_set_msglevel(struct net_device *dev, u32 v)
  865. {
  866. pegasus_t *pegasus = netdev_priv(dev);
  867. pegasus->msg_enable = v;
  868. }
  869. static const struct ethtool_ops ops = {
  870. .get_drvinfo = pegasus_get_drvinfo,
  871. .nway_reset = pegasus_nway_reset,
  872. .get_link = pegasus_get_link,
  873. .get_msglevel = pegasus_get_msglevel,
  874. .set_msglevel = pegasus_set_msglevel,
  875. .get_wol = pegasus_get_wol,
  876. .set_wol = pegasus_set_wol,
  877. .get_link_ksettings = pegasus_get_link_ksettings,
  878. .set_link_ksettings = pegasus_set_link_ksettings,
  879. };
  880. static int pegasus_siocdevprivate(struct net_device *net, struct ifreq *rq,
  881. void __user *udata, int cmd)
  882. {
  883. __u16 *data = (__u16 *) &rq->ifr_ifru;
  884. pegasus_t *pegasus = netdev_priv(net);
  885. int res;
  886. switch (cmd) {
  887. case SIOCDEVPRIVATE:
  888. data[0] = pegasus->phy;
  889. fallthrough;
  890. case SIOCDEVPRIVATE + 1:
  891. res = read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
  892. break;
  893. case SIOCDEVPRIVATE + 2:
  894. if (!capable(CAP_NET_ADMIN))
  895. return -EPERM;
  896. write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
  897. res = 0;
  898. break;
  899. default:
  900. res = -EOPNOTSUPP;
  901. }
  902. return res;
  903. }
  904. static void pegasus_set_multicast(struct net_device *net)
  905. {
  906. pegasus_t *pegasus = netdev_priv(net);
  907. if (net->flags & IFF_PROMISC) {
  908. pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
  909. netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
  910. } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
  911. pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
  912. pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
  913. netif_dbg(pegasus, link, net, "set allmulti\n");
  914. } else {
  915. pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
  916. pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
  917. }
  918. update_eth_regs_async(pegasus);
  919. }
  920. static __u8 mii_phy_probe(pegasus_t *pegasus)
  921. {
  922. int i, ret;
  923. __u16 tmp;
  924. for (i = 0; i < 32; i++) {
  925. ret = read_mii_word(pegasus, i, MII_BMSR, &tmp);
  926. if (ret < 0)
  927. goto fail;
  928. if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
  929. continue;
  930. else
  931. return i;
  932. }
  933. fail:
  934. return 0xff;
  935. }
  936. static inline void setup_pegasus_II(pegasus_t *pegasus)
  937. {
  938. int ret;
  939. __u8 data = 0xa5;
  940. set_register(pegasus, Reg1d, 0);
  941. set_register(pegasus, Reg7b, 1);
  942. msleep(100);
  943. if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
  944. set_register(pegasus, Reg7b, 0);
  945. else
  946. set_register(pegasus, Reg7b, 2);
  947. set_register(pegasus, 0x83, data);
  948. ret = get_registers(pegasus, 0x83, 1, &data);
  949. if (ret < 0)
  950. goto fail;
  951. if (data == 0xa5)
  952. pegasus->chip = 0x8513;
  953. else
  954. pegasus->chip = 0;
  955. set_register(pegasus, 0x80, 0xc0);
  956. set_register(pegasus, 0x83, 0xff);
  957. set_register(pegasus, 0x84, 0x01);
  958. if (pegasus->features & HAS_HOME_PNA && mii_mode)
  959. set_register(pegasus, Reg81, 6);
  960. else
  961. set_register(pegasus, Reg81, 2);
  962. return;
  963. fail:
  964. netif_dbg(pegasus, drv, pegasus->net, "%s failed\n", __func__);
  965. }
  966. static void check_carrier(struct work_struct *work)
  967. {
  968. pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
  969. set_carrier(pegasus->net);
  970. if (!(pegasus->flags & PEGASUS_UNPLUG)) {
  971. queue_delayed_work(system_long_wq, &pegasus->carrier_check,
  972. CARRIER_CHECK_DELAY);
  973. }
  974. }
  975. static int pegasus_blacklisted(struct usb_device *udev)
  976. {
  977. struct usb_device_descriptor *udd = &udev->descriptor;
  978. /* Special quirk to keep the driver from handling the Belkin Bluetooth
  979. * dongle which happens to have the same ID.
  980. */
  981. if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
  982. (udd->idProduct == cpu_to_le16(0x0121)) &&
  983. (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
  984. (udd->bDeviceProtocol == 1))
  985. return 1;
  986. return 0;
  987. }
  988. static int pegasus_probe(struct usb_interface *intf,
  989. const struct usb_device_id *id)
  990. {
  991. struct usb_device *dev = interface_to_usbdev(intf);
  992. struct net_device *net;
  993. pegasus_t *pegasus;
  994. int dev_index = id - pegasus_ids;
  995. int res = -ENOMEM;
  996. static const u8 bulk_ep_addr[] = {
  997. PEGASUS_USB_EP_BULK_IN | USB_DIR_IN,
  998. PEGASUS_USB_EP_BULK_OUT | USB_DIR_OUT,
  999. 0};
  1000. static const u8 int_ep_addr[] = {
  1001. PEGASUS_USB_EP_INT_IN | USB_DIR_IN,
  1002. 0};
  1003. if (pegasus_blacklisted(dev))
  1004. return -ENODEV;
  1005. /* Verify that all required endpoints are present */
  1006. if (!usb_check_bulk_endpoints(intf, bulk_ep_addr) ||
  1007. !usb_check_int_endpoints(intf, int_ep_addr)) {
  1008. dev_err(&intf->dev, "Missing or invalid endpoints\n");
  1009. return -ENODEV;
  1010. }
  1011. net = alloc_etherdev(sizeof(struct pegasus));
  1012. if (!net)
  1013. goto out;
  1014. pegasus = netdev_priv(net);
  1015. pegasus->dev_index = dev_index;
  1016. pegasus->intf = intf;
  1017. res = alloc_urbs(pegasus);
  1018. if (res < 0) {
  1019. dev_err(&intf->dev, "can't allocate %s\n", "urbs");
  1020. goto out1;
  1021. }
  1022. tasklet_setup(&pegasus->rx_tl, rx_fixup);
  1023. INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
  1024. pegasus->usb = dev;
  1025. pegasus->net = net;
  1026. net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
  1027. net->netdev_ops = &pegasus_netdev_ops;
  1028. net->ethtool_ops = &ops;
  1029. pegasus->mii.dev = net;
  1030. pegasus->mii.mdio_read = mdio_read;
  1031. pegasus->mii.mdio_write = mdio_write;
  1032. pegasus->mii.phy_id_mask = 0x1f;
  1033. pegasus->mii.reg_num_mask = 0x1f;
  1034. pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
  1035. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  1036. pegasus->features = usb_dev_id[dev_index].private;
  1037. res = get_interrupt_interval(pegasus);
  1038. if (res)
  1039. goto out2;
  1040. if (reset_mac(pegasus)) {
  1041. dev_err(&intf->dev, "can't reset MAC\n");
  1042. res = -EIO;
  1043. goto out2;
  1044. }
  1045. set_ethernet_addr(pegasus);
  1046. if (pegasus->features & PEGASUS_II) {
  1047. dev_info(&intf->dev, "setup Pegasus II specific registers\n");
  1048. setup_pegasus_II(pegasus);
  1049. }
  1050. pegasus->phy = mii_phy_probe(pegasus);
  1051. if (pegasus->phy == 0xff) {
  1052. dev_warn(&intf->dev, "can't locate MII phy, using default\n");
  1053. pegasus->phy = 1;
  1054. }
  1055. pegasus->mii.phy_id = pegasus->phy;
  1056. usb_set_intfdata(intf, pegasus);
  1057. SET_NETDEV_DEV(net, &intf->dev);
  1058. pegasus_reset_wol(net);
  1059. res = register_netdev(net);
  1060. if (res)
  1061. goto out3;
  1062. queue_delayed_work(system_long_wq, &pegasus->carrier_check,
  1063. CARRIER_CHECK_DELAY);
  1064. dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
  1065. usb_dev_id[dev_index].name, net->dev_addr);
  1066. return 0;
  1067. out3:
  1068. usb_set_intfdata(intf, NULL);
  1069. out2:
  1070. free_all_urbs(pegasus);
  1071. out1:
  1072. free_netdev(net);
  1073. out:
  1074. return res;
  1075. }
  1076. static void pegasus_disconnect(struct usb_interface *intf)
  1077. {
  1078. struct pegasus *pegasus = usb_get_intfdata(intf);
  1079. usb_set_intfdata(intf, NULL);
  1080. if (!pegasus) {
  1081. dev_dbg(&intf->dev, "unregistering non-bound device?\n");
  1082. return;
  1083. }
  1084. pegasus->flags |= PEGASUS_UNPLUG;
  1085. cancel_delayed_work_sync(&pegasus->carrier_check);
  1086. unregister_netdev(pegasus->net);
  1087. unlink_all_urbs(pegasus);
  1088. free_all_urbs(pegasus);
  1089. if (pegasus->rx_skb != NULL) {
  1090. dev_kfree_skb(pegasus->rx_skb);
  1091. pegasus->rx_skb = NULL;
  1092. }
  1093. free_netdev(pegasus->net);
  1094. }
  1095. static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
  1096. {
  1097. struct pegasus *pegasus = usb_get_intfdata(intf);
  1098. netif_device_detach(pegasus->net);
  1099. cancel_delayed_work_sync(&pegasus->carrier_check);
  1100. if (netif_running(pegasus->net)) {
  1101. usb_kill_urb(pegasus->rx_urb);
  1102. usb_kill_urb(pegasus->intr_urb);
  1103. }
  1104. return 0;
  1105. }
  1106. static int pegasus_resume(struct usb_interface *intf)
  1107. {
  1108. struct pegasus *pegasus = usb_get_intfdata(intf);
  1109. netif_device_attach(pegasus->net);
  1110. if (netif_running(pegasus->net)) {
  1111. pegasus->rx_urb->status = 0;
  1112. pegasus->rx_urb->actual_length = 0;
  1113. read_bulk_callback(pegasus->rx_urb);
  1114. pegasus->intr_urb->status = 0;
  1115. pegasus->intr_urb->actual_length = 0;
  1116. intr_callback(pegasus->intr_urb);
  1117. }
  1118. queue_delayed_work(system_long_wq, &pegasus->carrier_check,
  1119. CARRIER_CHECK_DELAY);
  1120. return 0;
  1121. }
  1122. static const struct net_device_ops pegasus_netdev_ops = {
  1123. .ndo_open = pegasus_open,
  1124. .ndo_stop = pegasus_close,
  1125. .ndo_siocdevprivate = pegasus_siocdevprivate,
  1126. .ndo_start_xmit = pegasus_start_xmit,
  1127. .ndo_set_rx_mode = pegasus_set_multicast,
  1128. .ndo_tx_timeout = pegasus_tx_timeout,
  1129. .ndo_set_mac_address = eth_mac_addr,
  1130. .ndo_validate_addr = eth_validate_addr,
  1131. };
  1132. static struct usb_driver pegasus_driver = {
  1133. .name = driver_name,
  1134. .probe = pegasus_probe,
  1135. .disconnect = pegasus_disconnect,
  1136. .id_table = pegasus_ids,
  1137. .suspend = pegasus_suspend,
  1138. .resume = pegasus_resume,
  1139. .disable_hub_initiated_lpm = 1,
  1140. };
  1141. static void __init parse_id(char *id)
  1142. {
  1143. unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
  1144. char *token, *name = NULL;
  1145. if ((token = strsep(&id, ":")) != NULL)
  1146. name = token;
  1147. /* name now points to a null terminated string*/
  1148. if ((token = strsep(&id, ":")) != NULL)
  1149. vendor_id = simple_strtoul(token, NULL, 16);
  1150. if ((token = strsep(&id, ":")) != NULL)
  1151. device_id = simple_strtoul(token, NULL, 16);
  1152. flags = simple_strtoul(id, NULL, 16);
  1153. pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
  1154. driver_name, name, vendor_id, device_id, flags);
  1155. if (vendor_id > 0x10000 || vendor_id == 0)
  1156. return;
  1157. if (device_id > 0x10000 || device_id == 0)
  1158. return;
  1159. for (i = 0; usb_dev_id[i].name; i++);
  1160. usb_dev_id[i].name = name;
  1161. usb_dev_id[i].vendor = vendor_id;
  1162. usb_dev_id[i].device = device_id;
  1163. usb_dev_id[i].private = flags;
  1164. pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  1165. pegasus_ids[i].idVendor = vendor_id;
  1166. pegasus_ids[i].idProduct = device_id;
  1167. }
  1168. static int __init pegasus_init(void)
  1169. {
  1170. pr_info("%s: " DRIVER_DESC "\n", driver_name);
  1171. if (devid)
  1172. parse_id(devid);
  1173. return usb_register(&pegasus_driver);
  1174. }
  1175. static void __exit pegasus_exit(void)
  1176. {
  1177. usb_deregister(&pegasus_driver);
  1178. }
  1179. module_init(pegasus_init);
  1180. module_exit(pegasus_exit);