pegasus.c 38 KB

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