pegasus.c 36 KB

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