pegasus.c 38 KB

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