pegasus.c 37 KB

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