pegasus.c 37 KB

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