pegasus.c 37 KB

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