pegasus.c 32 KB

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