pegasus.c 38 KB

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