pegasus.c 36 KB

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