pegasus.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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] = 0xc9;
  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. goto goon;
  411. }
  412. if (!count || count < 4)
  413. goto goon;
  414. rx_status = buf[count - 2];
  415. if (rx_status & 0x1e) {
  416. netif_dbg(pegasus, rx_err, net,
  417. "RX packet error %x\n", rx_status);
  418. pegasus->stats.rx_errors++;
  419. if (rx_status & 0x06) /* long or runt */
  420. pegasus->stats.rx_length_errors++;
  421. if (rx_status & 0x08)
  422. pegasus->stats.rx_crc_errors++;
  423. if (rx_status & 0x10) /* extra bits */
  424. pegasus->stats.rx_frame_errors++;
  425. goto goon;
  426. }
  427. if (pegasus->chip == 0x8513) {
  428. pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
  429. pkt_len &= 0x0fff;
  430. pegasus->rx_skb->data += 2;
  431. } else {
  432. pkt_len = buf[count - 3] << 8;
  433. pkt_len += buf[count - 4];
  434. pkt_len &= 0xfff;
  435. pkt_len -= 8;
  436. }
  437. /*
  438. * If the packet is unreasonably long, quietly drop it rather than
  439. * kernel panicing by calling skb_put.
  440. */
  441. if (pkt_len > PEGASUS_MTU)
  442. goto goon;
  443. /*
  444. * at this point we are sure pegasus->rx_skb != NULL
  445. * so we go ahead and pass up the packet.
  446. */
  447. skb_put(pegasus->rx_skb, pkt_len);
  448. pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
  449. netif_rx(pegasus->rx_skb);
  450. pegasus->stats.rx_packets++;
  451. pegasus->stats.rx_bytes += pkt_len;
  452. if (pegasus->flags & PEGASUS_UNPLUG)
  453. return;
  454. pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
  455. GFP_ATOMIC);
  456. if (pegasus->rx_skb == NULL)
  457. goto tl_sched;
  458. goon:
  459. usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
  460. usb_rcvbulkpipe(pegasus->usb, 1),
  461. pegasus->rx_skb->data, PEGASUS_MTU + 8,
  462. read_bulk_callback, pegasus);
  463. rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
  464. if (rx_status == -ENODEV)
  465. netif_device_detach(pegasus->net);
  466. else if (rx_status) {
  467. pegasus->flags |= PEGASUS_RX_URB_FAIL;
  468. goto tl_sched;
  469. } else {
  470. pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
  471. }
  472. return;
  473. tl_sched:
  474. tasklet_schedule(&pegasus->rx_tl);
  475. }
  476. static void rx_fixup(unsigned long data)
  477. {
  478. pegasus_t *pegasus;
  479. int status;
  480. pegasus = (pegasus_t *) data;
  481. if (pegasus->flags & PEGASUS_UNPLUG)
  482. return;
  483. if (pegasus->flags & PEGASUS_RX_URB_FAIL)
  484. if (pegasus->rx_skb)
  485. goto try_again;
  486. if (pegasus->rx_skb == NULL)
  487. pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
  488. PEGASUS_MTU,
  489. GFP_ATOMIC);
  490. if (pegasus->rx_skb == NULL) {
  491. netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
  492. tasklet_schedule(&pegasus->rx_tl);
  493. return;
  494. }
  495. usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
  496. usb_rcvbulkpipe(pegasus->usb, 1),
  497. pegasus->rx_skb->data, PEGASUS_MTU + 8,
  498. read_bulk_callback, pegasus);
  499. try_again:
  500. status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
  501. if (status == -ENODEV)
  502. netif_device_detach(pegasus->net);
  503. else if (status) {
  504. pegasus->flags |= PEGASUS_RX_URB_FAIL;
  505. tasklet_schedule(&pegasus->rx_tl);
  506. } else {
  507. pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
  508. }
  509. }
  510. static void write_bulk_callback(struct urb *urb)
  511. {
  512. pegasus_t *pegasus = urb->context;
  513. struct net_device *net;
  514. int status = urb->status;
  515. if (!pegasus)
  516. return;
  517. net = pegasus->net;
  518. if (!netif_device_present(net) || !netif_running(net))
  519. return;
  520. switch (status) {
  521. case -EPIPE:
  522. /* FIXME schedule_work() to clear the tx halt */
  523. netif_stop_queue(net);
  524. netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
  525. return;
  526. case -ENOENT:
  527. case -ECONNRESET:
  528. case -ESHUTDOWN:
  529. netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
  530. return;
  531. default:
  532. netif_info(pegasus, tx_err, net, "TX status %d\n", status);
  533. /* FALL THROUGH */
  534. case 0:
  535. break;
  536. }
  537. net->trans_start = jiffies; /* prevent tx timeout */
  538. netif_wake_queue(net);
  539. }
  540. static void intr_callback(struct urb *urb)
  541. {
  542. pegasus_t *pegasus = urb->context;
  543. struct net_device *net;
  544. int res, status = urb->status;
  545. if (!pegasus)
  546. return;
  547. net = pegasus->net;
  548. switch (status) {
  549. case 0:
  550. break;
  551. case -ECONNRESET: /* unlink */
  552. case -ENOENT:
  553. case -ESHUTDOWN:
  554. return;
  555. default:
  556. /* some Pegasus-I products report LOTS of data
  557. * toggle errors... avoid log spamming
  558. */
  559. netif_dbg(pegasus, timer, net, "intr status %d\n", status);
  560. }
  561. if (urb->actual_length >= 6) {
  562. u8 *d = urb->transfer_buffer;
  563. /* byte 0 == tx_status1, reg 2B */
  564. if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
  565. |LATE_COL|JABBER_TIMEOUT)) {
  566. pegasus->stats.tx_errors++;
  567. if (d[0] & TX_UNDERRUN)
  568. pegasus->stats.tx_fifo_errors++;
  569. if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
  570. pegasus->stats.tx_aborted_errors++;
  571. if (d[0] & LATE_COL)
  572. pegasus->stats.tx_window_errors++;
  573. }
  574. /* d[5].LINK_STATUS lies on some adapters.
  575. * d[0].NO_CARRIER kicks in only with failed TX.
  576. * ... so monitoring with MII may be safest.
  577. */
  578. /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
  579. pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
  580. }
  581. res = usb_submit_urb(urb, GFP_ATOMIC);
  582. if (res == -ENODEV)
  583. netif_device_detach(pegasus->net);
  584. if (res)
  585. netif_err(pegasus, timer, net,
  586. "can't resubmit interrupt urb, %d\n", res);
  587. }
  588. static void pegasus_tx_timeout(struct net_device *net)
  589. {
  590. pegasus_t *pegasus = netdev_priv(net);
  591. netif_warn(pegasus, timer, net, "tx timeout\n");
  592. usb_unlink_urb(pegasus->tx_urb);
  593. pegasus->stats.tx_errors++;
  594. }
  595. static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
  596. struct net_device *net)
  597. {
  598. pegasus_t *pegasus = netdev_priv(net);
  599. int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
  600. int res;
  601. __u16 l16 = skb->len;
  602. netif_stop_queue(net);
  603. ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
  604. skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
  605. usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
  606. usb_sndbulkpipe(pegasus->usb, 2),
  607. pegasus->tx_buff, count,
  608. write_bulk_callback, pegasus);
  609. if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
  610. netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
  611. switch (res) {
  612. case -EPIPE: /* stall, or disconnect from TT */
  613. /* cleanup should already have been scheduled */
  614. break;
  615. case -ENODEV: /* disconnect() upcoming */
  616. case -EPERM:
  617. netif_device_detach(pegasus->net);
  618. break;
  619. default:
  620. pegasus->stats.tx_errors++;
  621. netif_start_queue(net);
  622. }
  623. } else {
  624. pegasus->stats.tx_packets++;
  625. pegasus->stats.tx_bytes += skb->len;
  626. }
  627. dev_kfree_skb(skb);
  628. return NETDEV_TX_OK;
  629. }
  630. static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
  631. {
  632. return &((pegasus_t *) netdev_priv(dev))->stats;
  633. }
  634. static inline void disable_net_traffic(pegasus_t *pegasus)
  635. {
  636. __le16 tmp = cpu_to_le16(0);
  637. set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
  638. }
  639. static inline void get_interrupt_interval(pegasus_t *pegasus)
  640. {
  641. u16 data;
  642. u8 interval;
  643. read_eprom_word(pegasus, 4, &data);
  644. interval = data >> 8;
  645. if (pegasus->usb->speed != USB_SPEED_HIGH) {
  646. if (interval < 0x80) {
  647. netif_info(pegasus, timer, pegasus->net,
  648. "intr interval changed from %ums to %ums\n",
  649. interval, 0x80);
  650. interval = 0x80;
  651. data = (data & 0x00FF) | ((u16)interval << 8);
  652. #ifdef PEGASUS_WRITE_EEPROM
  653. write_eprom_word(pegasus, 4, data);
  654. #endif
  655. }
  656. }
  657. pegasus->intr_interval = interval;
  658. }
  659. static void set_carrier(struct net_device *net)
  660. {
  661. pegasus_t *pegasus = netdev_priv(net);
  662. u16 tmp;
  663. if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
  664. return;
  665. if (tmp & BMSR_LSTATUS)
  666. netif_carrier_on(net);
  667. else
  668. netif_carrier_off(net);
  669. }
  670. static void free_all_urbs(pegasus_t *pegasus)
  671. {
  672. usb_free_urb(pegasus->intr_urb);
  673. usb_free_urb(pegasus->tx_urb);
  674. usb_free_urb(pegasus->rx_urb);
  675. }
  676. static void unlink_all_urbs(pegasus_t *pegasus)
  677. {
  678. usb_kill_urb(pegasus->intr_urb);
  679. usb_kill_urb(pegasus->tx_urb);
  680. usb_kill_urb(pegasus->rx_urb);
  681. }
  682. static int alloc_urbs(pegasus_t *pegasus)
  683. {
  684. int res = -ENOMEM;
  685. pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  686. if (!pegasus->rx_urb) {
  687. return res;
  688. }
  689. pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  690. if (!pegasus->tx_urb) {
  691. usb_free_urb(pegasus->rx_urb);
  692. return res;
  693. }
  694. pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  695. if (!pegasus->intr_urb) {
  696. usb_free_urb(pegasus->tx_urb);
  697. usb_free_urb(pegasus->rx_urb);
  698. return res;
  699. }
  700. return 0;
  701. }
  702. static int pegasus_open(struct net_device *net)
  703. {
  704. pegasus_t *pegasus = netdev_priv(net);
  705. int res=-ENOMEM;
  706. if (pegasus->rx_skb == NULL)
  707. pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
  708. PEGASUS_MTU,
  709. GFP_KERNEL);
  710. if (!pegasus->rx_skb)
  711. goto exit;
  712. res = set_registers(pegasus, EthID, 6, net->dev_addr);
  713. usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
  714. usb_rcvbulkpipe(pegasus->usb, 1),
  715. pegasus->rx_skb->data, PEGASUS_MTU + 8,
  716. read_bulk_callback, pegasus);
  717. if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
  718. if (res == -ENODEV)
  719. netif_device_detach(pegasus->net);
  720. netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
  721. goto exit;
  722. }
  723. usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
  724. usb_rcvintpipe(pegasus->usb, 3),
  725. pegasus->intr_buff, sizeof(pegasus->intr_buff),
  726. intr_callback, pegasus, pegasus->intr_interval);
  727. if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
  728. if (res == -ENODEV)
  729. netif_device_detach(pegasus->net);
  730. netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
  731. usb_kill_urb(pegasus->rx_urb);
  732. goto exit;
  733. }
  734. res = enable_net_traffic(net, pegasus->usb);
  735. if (res < 0) {
  736. netif_dbg(pegasus, ifup, net,
  737. "can't enable_net_traffic() - %d\n", res);
  738. res = -EIO;
  739. usb_kill_urb(pegasus->rx_urb);
  740. usb_kill_urb(pegasus->intr_urb);
  741. goto exit;
  742. }
  743. set_carrier(net);
  744. netif_start_queue(net);
  745. netif_dbg(pegasus, ifup, net, "open\n");
  746. res = 0;
  747. exit:
  748. return res;
  749. }
  750. static int pegasus_close(struct net_device *net)
  751. {
  752. pegasus_t *pegasus = netdev_priv(net);
  753. netif_stop_queue(net);
  754. if (!(pegasus->flags & PEGASUS_UNPLUG))
  755. disable_net_traffic(pegasus);
  756. tasklet_kill(&pegasus->rx_tl);
  757. unlink_all_urbs(pegasus);
  758. return 0;
  759. }
  760. static void pegasus_get_drvinfo(struct net_device *dev,
  761. struct ethtool_drvinfo *info)
  762. {
  763. pegasus_t *pegasus = netdev_priv(dev);
  764. strlcpy(info->driver, driver_name, sizeof(info->driver));
  765. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  766. usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
  767. }
  768. /* also handles three patterns of some kind in hardware */
  769. #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
  770. static void
  771. pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  772. {
  773. pegasus_t *pegasus = netdev_priv(dev);
  774. wol->supported = WAKE_MAGIC | WAKE_PHY;
  775. wol->wolopts = pegasus->wolopts;
  776. }
  777. static int
  778. pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  779. {
  780. pegasus_t *pegasus = netdev_priv(dev);
  781. u8 reg78 = 0x04;
  782. int ret;
  783. if (wol->wolopts & ~WOL_SUPPORTED)
  784. return -EINVAL;
  785. if (wol->wolopts & WAKE_MAGIC)
  786. reg78 |= 0x80;
  787. if (wol->wolopts & WAKE_PHY)
  788. reg78 |= 0x40;
  789. /* FIXME this 0x10 bit still needs to get set in the chip... */
  790. if (wol->wolopts)
  791. pegasus->eth_regs[0] |= 0x10;
  792. else
  793. pegasus->eth_regs[0] &= ~0x10;
  794. pegasus->wolopts = wol->wolopts;
  795. ret = set_register(pegasus, WakeupControl, reg78);
  796. if (!ret)
  797. ret = device_set_wakeup_enable(&pegasus->usb->dev,
  798. wol->wolopts);
  799. return ret;
  800. }
  801. static inline void pegasus_reset_wol(struct net_device *dev)
  802. {
  803. struct ethtool_wolinfo wol;
  804. memset(&wol, 0, sizeof wol);
  805. (void) pegasus_set_wol(dev, &wol);
  806. }
  807. static int
  808. pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  809. {
  810. pegasus_t *pegasus;
  811. pegasus = netdev_priv(dev);
  812. mii_ethtool_gset(&pegasus->mii, ecmd);
  813. return 0;
  814. }
  815. static int
  816. pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  817. {
  818. pegasus_t *pegasus = netdev_priv(dev);
  819. return mii_ethtool_sset(&pegasus->mii, ecmd);
  820. }
  821. static int pegasus_nway_reset(struct net_device *dev)
  822. {
  823. pegasus_t *pegasus = netdev_priv(dev);
  824. return mii_nway_restart(&pegasus->mii);
  825. }
  826. static u32 pegasus_get_link(struct net_device *dev)
  827. {
  828. pegasus_t *pegasus = netdev_priv(dev);
  829. return mii_link_ok(&pegasus->mii);
  830. }
  831. static u32 pegasus_get_msglevel(struct net_device *dev)
  832. {
  833. pegasus_t *pegasus = netdev_priv(dev);
  834. return pegasus->msg_enable;
  835. }
  836. static void pegasus_set_msglevel(struct net_device *dev, u32 v)
  837. {
  838. pegasus_t *pegasus = netdev_priv(dev);
  839. pegasus->msg_enable = v;
  840. }
  841. static const struct ethtool_ops ops = {
  842. .get_drvinfo = pegasus_get_drvinfo,
  843. .get_settings = pegasus_get_settings,
  844. .set_settings = pegasus_set_settings,
  845. .nway_reset = pegasus_nway_reset,
  846. .get_link = pegasus_get_link,
  847. .get_msglevel = pegasus_get_msglevel,
  848. .set_msglevel = pegasus_set_msglevel,
  849. .get_wol = pegasus_get_wol,
  850. .set_wol = pegasus_set_wol,
  851. };
  852. static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
  853. {
  854. __u16 *data = (__u16 *) &rq->ifr_ifru;
  855. pegasus_t *pegasus = netdev_priv(net);
  856. int res;
  857. switch (cmd) {
  858. case SIOCDEVPRIVATE:
  859. data[0] = pegasus->phy;
  860. case SIOCDEVPRIVATE + 1:
  861. read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
  862. res = 0;
  863. break;
  864. case SIOCDEVPRIVATE + 2:
  865. if (!capable(CAP_NET_ADMIN))
  866. return -EPERM;
  867. write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
  868. res = 0;
  869. break;
  870. default:
  871. res = -EOPNOTSUPP;
  872. }
  873. return res;
  874. }
  875. static void pegasus_set_multicast(struct net_device *net)
  876. {
  877. pegasus_t *pegasus = netdev_priv(net);
  878. if (net->flags & IFF_PROMISC) {
  879. pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
  880. netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
  881. } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
  882. pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
  883. pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
  884. netif_dbg(pegasus, link, net, "set allmulti\n");
  885. } else {
  886. pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
  887. pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
  888. }
  889. update_eth_regs_async(pegasus);
  890. }
  891. static __u8 mii_phy_probe(pegasus_t *pegasus)
  892. {
  893. int i;
  894. __u16 tmp;
  895. for (i = 0; i < 32; i++) {
  896. read_mii_word(pegasus, i, MII_BMSR, &tmp);
  897. if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
  898. continue;
  899. else
  900. return i;
  901. }
  902. return 0xff;
  903. }
  904. static inline void setup_pegasus_II(pegasus_t *pegasus)
  905. {
  906. __u8 data = 0xa5;
  907. set_register(pegasus, Reg1d, 0);
  908. set_register(pegasus, Reg7b, 1);
  909. mdelay(100);
  910. if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
  911. set_register(pegasus, Reg7b, 0);
  912. else
  913. set_register(pegasus, Reg7b, 2);
  914. set_register(pegasus, 0x83, data);
  915. get_registers(pegasus, 0x83, 1, &data);
  916. if (data == 0xa5)
  917. pegasus->chip = 0x8513;
  918. else
  919. pegasus->chip = 0;
  920. set_register(pegasus, 0x80, 0xc0);
  921. set_register(pegasus, 0x83, 0xff);
  922. set_register(pegasus, 0x84, 0x01);
  923. if (pegasus->features & HAS_HOME_PNA && mii_mode)
  924. set_register(pegasus, Reg81, 6);
  925. else
  926. set_register(pegasus, Reg81, 2);
  927. }
  928. static int pegasus_count;
  929. static struct workqueue_struct *pegasus_workqueue;
  930. #define CARRIER_CHECK_DELAY (2 * HZ)
  931. static void check_carrier(struct work_struct *work)
  932. {
  933. pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
  934. set_carrier(pegasus->net);
  935. if (!(pegasus->flags & PEGASUS_UNPLUG)) {
  936. queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
  937. CARRIER_CHECK_DELAY);
  938. }
  939. }
  940. static int pegasus_blacklisted(struct usb_device *udev)
  941. {
  942. struct usb_device_descriptor *udd = &udev->descriptor;
  943. /* Special quirk to keep the driver from handling the Belkin Bluetooth
  944. * dongle which happens to have the same ID.
  945. */
  946. if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
  947. (udd->idProduct == cpu_to_le16(0x0121)) &&
  948. (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
  949. (udd->bDeviceProtocol == 1))
  950. return 1;
  951. return 0;
  952. }
  953. /* we rely on probe() and remove() being serialized so we
  954. * don't need extra locking on pegasus_count.
  955. */
  956. static void pegasus_dec_workqueue(void)
  957. {
  958. pegasus_count--;
  959. if (pegasus_count == 0) {
  960. destroy_workqueue(pegasus_workqueue);
  961. pegasus_workqueue = NULL;
  962. }
  963. }
  964. static int pegasus_probe(struct usb_interface *intf,
  965. const struct usb_device_id *id)
  966. {
  967. struct usb_device *dev = interface_to_usbdev(intf);
  968. struct net_device *net;
  969. pegasus_t *pegasus;
  970. int dev_index = id - pegasus_ids;
  971. int res = -ENOMEM;
  972. if (pegasus_blacklisted(dev))
  973. return -ENODEV;
  974. if (pegasus_count == 0) {
  975. pegasus_workqueue = create_singlethread_workqueue("pegasus");
  976. if (!pegasus_workqueue)
  977. return -ENOMEM;
  978. }
  979. pegasus_count++;
  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_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
  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. get_interrupt_interval(pegasus);
  1007. if (reset_mac(pegasus)) {
  1008. dev_err(&intf->dev, "can't reset MAC\n");
  1009. res = -EIO;
  1010. goto out2;
  1011. }
  1012. set_ethernet_addr(pegasus);
  1013. if (pegasus->features & PEGASUS_II) {
  1014. dev_info(&intf->dev, "setup Pegasus II specific registers\n");
  1015. setup_pegasus_II(pegasus);
  1016. }
  1017. pegasus->phy = mii_phy_probe(pegasus);
  1018. if (pegasus->phy == 0xff) {
  1019. dev_warn(&intf->dev, "can't locate MII phy, using default\n");
  1020. pegasus->phy = 1;
  1021. }
  1022. pegasus->mii.phy_id = pegasus->phy;
  1023. usb_set_intfdata(intf, pegasus);
  1024. SET_NETDEV_DEV(net, &intf->dev);
  1025. pegasus_reset_wol(net);
  1026. res = register_netdev(net);
  1027. if (res)
  1028. goto out3;
  1029. queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
  1030. CARRIER_CHECK_DELAY);
  1031. dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
  1032. usb_dev_id[dev_index].name, net->dev_addr);
  1033. return 0;
  1034. out3:
  1035. usb_set_intfdata(intf, NULL);
  1036. out2:
  1037. free_all_urbs(pegasus);
  1038. out1:
  1039. free_netdev(net);
  1040. out:
  1041. pegasus_dec_workqueue();
  1042. return res;
  1043. }
  1044. static void pegasus_disconnect(struct usb_interface *intf)
  1045. {
  1046. struct pegasus *pegasus = usb_get_intfdata(intf);
  1047. usb_set_intfdata(intf, NULL);
  1048. if (!pegasus) {
  1049. dev_dbg(&intf->dev, "unregistering non-bound device?\n");
  1050. return;
  1051. }
  1052. pegasus->flags |= PEGASUS_UNPLUG;
  1053. cancel_delayed_work(&pegasus->carrier_check);
  1054. unregister_netdev(pegasus->net);
  1055. unlink_all_urbs(pegasus);
  1056. free_all_urbs(pegasus);
  1057. if (pegasus->rx_skb != NULL) {
  1058. dev_kfree_skb(pegasus->rx_skb);
  1059. pegasus->rx_skb = NULL;
  1060. }
  1061. free_netdev(pegasus->net);
  1062. pegasus_dec_workqueue();
  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(&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(pegasus_workqueue, &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_do_ioctl = pegasus_ioctl,
  1095. .ndo_start_xmit = pegasus_start_xmit,
  1096. .ndo_set_rx_mode = pegasus_set_multicast,
  1097. .ndo_get_stats = pegasus_netdev_stats,
  1098. .ndo_tx_timeout = pegasus_tx_timeout,
  1099. .ndo_change_mtu = eth_change_mtu,
  1100. .ndo_set_mac_address = eth_mac_addr,
  1101. .ndo_validate_addr = eth_validate_addr,
  1102. };
  1103. static struct usb_driver pegasus_driver = {
  1104. .name = driver_name,
  1105. .probe = pegasus_probe,
  1106. .disconnect = pegasus_disconnect,
  1107. .id_table = pegasus_ids,
  1108. .suspend = pegasus_suspend,
  1109. .resume = pegasus_resume,
  1110. .disable_hub_initiated_lpm = 1,
  1111. };
  1112. static void __init parse_id(char *id)
  1113. {
  1114. unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
  1115. char *token, *name = NULL;
  1116. if ((token = strsep(&id, ":")) != NULL)
  1117. name = token;
  1118. /* name now points to a null terminated string*/
  1119. if ((token = strsep(&id, ":")) != NULL)
  1120. vendor_id = simple_strtoul(token, NULL, 16);
  1121. if ((token = strsep(&id, ":")) != NULL)
  1122. device_id = simple_strtoul(token, NULL, 16);
  1123. flags = simple_strtoul(id, NULL, 16);
  1124. pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
  1125. driver_name, name, vendor_id, device_id, flags);
  1126. if (vendor_id > 0x10000 || vendor_id == 0)
  1127. return;
  1128. if (device_id > 0x10000 || device_id == 0)
  1129. return;
  1130. for (i = 0; usb_dev_id[i].name; i++);
  1131. usb_dev_id[i].name = name;
  1132. usb_dev_id[i].vendor = vendor_id;
  1133. usb_dev_id[i].device = device_id;
  1134. usb_dev_id[i].private = flags;
  1135. pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  1136. pegasus_ids[i].idVendor = vendor_id;
  1137. pegasus_ids[i].idProduct = device_id;
  1138. }
  1139. static int __init pegasus_init(void)
  1140. {
  1141. pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
  1142. if (devid)
  1143. parse_id(devid);
  1144. return usb_register(&pegasus_driver);
  1145. }
  1146. static void __exit pegasus_exit(void)
  1147. {
  1148. usb_deregister(&pegasus_driver);
  1149. }
  1150. module_init(pegasus_init);
  1151. module_exit(pegasus_exit);