pegasus.c 38 KB

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