pegasus.c 37 KB

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