pegasus.c 36 KB

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