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