pegasus.c 38 KB

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