pegasus.c 36 KB

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