pegasus.c 32 KB

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