pegasus.c 39 KB

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