pegasus.c 36 KB

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