pegasus.c 35 KB

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