pegasus.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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. /* Returns 0 on success, error on failure */
  276. static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
  277. {
  278. int i;
  279. __u8 data[4] = { phy, 0, 0, indx };
  280. __le16 regdi;
  281. int ret;
  282. set_register(pegasus, PhyCtrl, 0);
  283. set_registers(pegasus, PhyAddr, sizeof (data), data);
  284. set_register(pegasus, PhyCtrl, (indx | PHY_READ));
  285. for (i = 0; i < REG_TIMEOUT; i++) {
  286. ret = get_registers(pegasus, PhyCtrl, 1, data);
  287. if (ret == -ESHUTDOWN)
  288. goto fail;
  289. if (data[0] & PHY_DONE)
  290. break;
  291. }
  292. if (i < REG_TIMEOUT) {
  293. ret = get_registers(pegasus, PhyData, 2, &regdi);
  294. *regd = le16_to_cpu(regdi);
  295. return ret;
  296. }
  297. fail:
  298. if (netif_msg_drv(pegasus))
  299. dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
  300. return ret;
  301. }
  302. static int mdio_read(struct net_device *dev, int phy_id, int loc)
  303. {
  304. pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
  305. u16 res;
  306. read_mii_word(pegasus, phy_id, loc, &res);
  307. return (int)res;
  308. }
  309. static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
  310. {
  311. int i;
  312. __u8 data[4] = { phy, 0, 0, indx };
  313. int ret;
  314. data[1] = (u8) regd;
  315. data[2] = (u8) (regd >> 8);
  316. set_register(pegasus, PhyCtrl, 0);
  317. set_registers(pegasus, PhyAddr, sizeof(data), data);
  318. set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
  319. for (i = 0; i < REG_TIMEOUT; i++) {
  320. ret = get_registers(pegasus, PhyCtrl, 1, data);
  321. if (ret == -ESHUTDOWN)
  322. goto fail;
  323. if (data[0] & PHY_DONE)
  324. break;
  325. }
  326. if (i < REG_TIMEOUT)
  327. return ret;
  328. fail:
  329. if (netif_msg_drv(pegasus))
  330. dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
  331. return -ETIMEDOUT;
  332. }
  333. static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
  334. {
  335. pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
  336. write_mii_word(pegasus, phy_id, loc, val);
  337. }
  338. static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
  339. {
  340. int i;
  341. __u8 tmp;
  342. __le16 retdatai;
  343. int ret;
  344. set_register(pegasus, EpromCtrl, 0);
  345. set_register(pegasus, EpromOffset, index);
  346. set_register(pegasus, EpromCtrl, EPROM_READ);
  347. for (i = 0; i < REG_TIMEOUT; i++) {
  348. ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
  349. if (tmp & EPROM_DONE)
  350. break;
  351. if (ret == -ESHUTDOWN)
  352. goto fail;
  353. }
  354. if (i < REG_TIMEOUT) {
  355. ret = get_registers(pegasus, EpromData, 2, &retdatai);
  356. *retdata = le16_to_cpu(retdatai);
  357. return ret;
  358. }
  359. fail:
  360. if (netif_msg_drv(pegasus))
  361. dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
  362. return -ETIMEDOUT;
  363. }
  364. #ifdef PEGASUS_WRITE_EEPROM
  365. static inline void enable_eprom_write(pegasus_t * pegasus)
  366. {
  367. __u8 tmp;
  368. int ret;
  369. get_registers(pegasus, EthCtrl2, 1, &tmp);
  370. set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
  371. }
  372. static inline void disable_eprom_write(pegasus_t * pegasus)
  373. {
  374. __u8 tmp;
  375. int ret;
  376. get_registers(pegasus, EthCtrl2, 1, &tmp);
  377. set_register(pegasus, EpromCtrl, 0);
  378. set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
  379. }
  380. static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
  381. {
  382. int i;
  383. __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
  384. int ret;
  385. set_registers(pegasus, EpromOffset, 4, d);
  386. enable_eprom_write(pegasus);
  387. set_register(pegasus, EpromOffset, index);
  388. set_registers(pegasus, EpromData, 2, &data);
  389. set_register(pegasus, EpromCtrl, EPROM_WRITE);
  390. for (i = 0; i < REG_TIMEOUT; i++) {
  391. ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
  392. if (ret == -ESHUTDOWN)
  393. goto fail;
  394. if (tmp & EPROM_DONE)
  395. break;
  396. }
  397. disable_eprom_write(pegasus);
  398. if (i < REG_TIMEOUT)
  399. return ret;
  400. fail:
  401. if (netif_msg_drv(pegasus))
  402. dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
  403. return -ETIMEDOUT;
  404. }
  405. #endif /* PEGASUS_WRITE_EEPROM */
  406. static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
  407. {
  408. int i;
  409. __u16 w16;
  410. for (i = 0; i < 3; i++) {
  411. read_eprom_word(pegasus, i, &w16);
  412. ((__le16 *) id)[i] = cpu_to_le16p(&w16);
  413. }
  414. }
  415. static void set_ethernet_addr(pegasus_t * pegasus)
  416. {
  417. __u8 node_id[6];
  418. if (pegasus->features & PEGASUS_II) {
  419. get_registers(pegasus, 0x10, sizeof(node_id), node_id);
  420. } else {
  421. get_node_id(pegasus, node_id);
  422. set_registers(pegasus, EthID, sizeof (node_id), node_id);
  423. }
  424. memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
  425. }
  426. static inline int reset_mac(pegasus_t * pegasus)
  427. {
  428. __u8 data = 0x8;
  429. int i;
  430. set_register(pegasus, EthCtrl1, data);
  431. for (i = 0; i < REG_TIMEOUT; i++) {
  432. get_registers(pegasus, EthCtrl1, 1, &data);
  433. if (~data & 0x08) {
  434. if (loopback & 1)
  435. break;
  436. if (mii_mode && (pegasus->features & HAS_HOME_PNA))
  437. set_register(pegasus, Gpio1, 0x34);
  438. else
  439. set_register(pegasus, Gpio1, 0x26);
  440. set_register(pegasus, Gpio0, pegasus->features);
  441. set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
  442. break;
  443. }
  444. }
  445. if (i == REG_TIMEOUT)
  446. return -ETIMEDOUT;
  447. if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
  448. usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
  449. set_register(pegasus, Gpio0, 0x24);
  450. set_register(pegasus, Gpio0, 0x26);
  451. }
  452. if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
  453. __u16 auxmode;
  454. read_mii_word(pegasus, 3, 0x1b, &auxmode);
  455. write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
  456. }
  457. return 0;
  458. }
  459. static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
  460. {
  461. __u16 linkpart;
  462. __u8 data[4];
  463. pegasus_t *pegasus = netdev_priv(dev);
  464. int ret;
  465. read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
  466. data[0] = 0xc9;
  467. data[1] = 0;
  468. if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
  469. data[1] |= 0x20; /* set full duplex */
  470. if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
  471. data[1] |= 0x10; /* set 100 Mbps */
  472. if (mii_mode)
  473. data[1] = 0;
  474. data[2] = (loopback & 1) ? 0x09 : 0x01;
  475. memcpy(pegasus->eth_regs, data, sizeof (data));
  476. ret = set_registers(pegasus, EthCtrl0, 3, data);
  477. if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
  478. usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
  479. usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
  480. u16 auxmode;
  481. read_mii_word(pegasus, 0, 0x1b, &auxmode);
  482. write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
  483. }
  484. return ret;
  485. }
  486. static void fill_skb_pool(pegasus_t * pegasus)
  487. {
  488. int i;
  489. for (i = 0; i < RX_SKBS; i++) {
  490. if (pegasus->rx_pool[i])
  491. continue;
  492. pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
  493. /*
  494. ** we give up if the allocation fail. the tasklet will be
  495. ** rescheduled again anyway...
  496. */
  497. if (pegasus->rx_pool[i] == NULL)
  498. return;
  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;
  672. if (!pegasus)
  673. return;
  674. net = pegasus->net;
  675. if (!netif_device_present(net) || !netif_running(net))
  676. return;
  677. switch (urb->status) {
  678. case -EPIPE:
  679. /* FIXME schedule_work() to clear the tx halt */
  680. netif_stop_queue(net);
  681. if (netif_msg_tx_err(pegasus))
  682. printk(KERN_WARNING "%s: no tx stall recovery\n",
  683. net->name);
  684. return;
  685. case -ENOENT:
  686. case -ECONNRESET:
  687. case -ESHUTDOWN:
  688. if (netif_msg_ifdown(pegasus))
  689. pr_debug("%s: tx unlink, %d\n", net->name, urb->status);
  690. return;
  691. default:
  692. if (netif_msg_tx_err(pegasus))
  693. pr_info("%s: TX status %d\n", net->name, urb->status);
  694. /* FALL THROUGH */
  695. case 0:
  696. break;
  697. }
  698. net->trans_start = jiffies;
  699. netif_wake_queue(net);
  700. }
  701. static void intr_callback(struct urb *urb)
  702. {
  703. pegasus_t *pegasus = urb->context;
  704. struct net_device *net;
  705. int status;
  706. if (!pegasus)
  707. return;
  708. net = pegasus->net;
  709. switch (urb->status) {
  710. case 0:
  711. break;
  712. case -ECONNRESET: /* unlink */
  713. case -ENOENT:
  714. case -ESHUTDOWN:
  715. return;
  716. default:
  717. /* some Pegasus-I products report LOTS of data
  718. * toggle errors... avoid log spamming
  719. */
  720. if (netif_msg_timer(pegasus))
  721. pr_debug("%s: intr status %d\n", net->name,
  722. urb->status);
  723. }
  724. if (urb->actual_length >= 6) {
  725. u8 * d = urb->transfer_buffer;
  726. /* byte 0 == tx_status1, reg 2B */
  727. if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
  728. |LATE_COL|JABBER_TIMEOUT)) {
  729. pegasus->stats.tx_errors++;
  730. if (d[0] & TX_UNDERRUN)
  731. pegasus->stats.tx_fifo_errors++;
  732. if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
  733. pegasus->stats.tx_aborted_errors++;
  734. if (d[0] & LATE_COL)
  735. pegasus->stats.tx_window_errors++;
  736. }
  737. /* d[5].LINK_STATUS lies on some adapters.
  738. * d[0].NO_CARRIER kicks in only with failed TX.
  739. * ... so monitoring with MII may be safest.
  740. */
  741. /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
  742. pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
  743. }
  744. status = usb_submit_urb(urb, GFP_ATOMIC);
  745. if (status == -ENODEV)
  746. netif_device_detach(pegasus->net);
  747. if (status && netif_msg_timer(pegasus))
  748. printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
  749. net->name, status);
  750. }
  751. static void pegasus_tx_timeout(struct net_device *net)
  752. {
  753. pegasus_t *pegasus = netdev_priv(net);
  754. if (netif_msg_timer(pegasus))
  755. printk(KERN_WARNING "%s: tx timeout\n", net->name);
  756. usb_unlink_urb(pegasus->tx_urb);
  757. pegasus->stats.tx_errors++;
  758. }
  759. static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
  760. {
  761. pegasus_t *pegasus = netdev_priv(net);
  762. int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
  763. int res;
  764. __u16 l16 = skb->len;
  765. netif_stop_queue(net);
  766. ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
  767. skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
  768. usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
  769. usb_sndbulkpipe(pegasus->usb, 2),
  770. pegasus->tx_buff, count,
  771. write_bulk_callback, pegasus);
  772. if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
  773. if (netif_msg_tx_err(pegasus))
  774. printk(KERN_WARNING "%s: fail tx, %d\n",
  775. net->name, res);
  776. switch (res) {
  777. case -EPIPE: /* stall, or disconnect from TT */
  778. /* cleanup should already have been scheduled */
  779. break;
  780. case -ENODEV: /* disconnect() upcoming */
  781. netif_device_detach(pegasus->net);
  782. break;
  783. default:
  784. pegasus->stats.tx_errors++;
  785. netif_start_queue(net);
  786. }
  787. } else {
  788. pegasus->stats.tx_packets++;
  789. pegasus->stats.tx_bytes += skb->len;
  790. net->trans_start = jiffies;
  791. }
  792. dev_kfree_skb(skb);
  793. return 0;
  794. }
  795. static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
  796. {
  797. return &((pegasus_t *) netdev_priv(dev))->stats;
  798. }
  799. static inline void disable_net_traffic(pegasus_t * pegasus)
  800. {
  801. int tmp = 0;
  802. set_registers(pegasus, EthCtrl0, 2, &tmp);
  803. }
  804. static inline void get_interrupt_interval(pegasus_t * pegasus)
  805. {
  806. __u8 data[2];
  807. read_eprom_word(pegasus, 4, (__u16 *) data);
  808. if (pegasus->usb->speed != USB_SPEED_HIGH) {
  809. if (data[1] < 0x80) {
  810. if (netif_msg_timer(pegasus))
  811. dev_info(&pegasus->intf->dev, "intr interval "
  812. "changed from %ums to %ums\n",
  813. data[1], 0x80);
  814. data[1] = 0x80;
  815. #ifdef PEGASUS_WRITE_EEPROM
  816. write_eprom_word(pegasus, 4, *(__u16 *) data);
  817. #endif
  818. }
  819. }
  820. pegasus->intr_interval = data[1];
  821. }
  822. static void set_carrier(struct net_device *net)
  823. {
  824. pegasus_t *pegasus = netdev_priv(net);
  825. u16 tmp;
  826. if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
  827. return;
  828. if (tmp & BMSR_LSTATUS)
  829. netif_carrier_on(net);
  830. else
  831. netif_carrier_off(net);
  832. }
  833. static void free_all_urbs(pegasus_t * pegasus)
  834. {
  835. usb_free_urb(pegasus->intr_urb);
  836. usb_free_urb(pegasus->tx_urb);
  837. usb_free_urb(pegasus->rx_urb);
  838. usb_free_urb(pegasus->ctrl_urb);
  839. }
  840. static void unlink_all_urbs(pegasus_t * pegasus)
  841. {
  842. usb_kill_urb(pegasus->intr_urb);
  843. usb_kill_urb(pegasus->tx_urb);
  844. usb_kill_urb(pegasus->rx_urb);
  845. usb_kill_urb(pegasus->ctrl_urb);
  846. }
  847. static int alloc_urbs(pegasus_t * pegasus)
  848. {
  849. pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
  850. if (!pegasus->ctrl_urb) {
  851. return 0;
  852. }
  853. pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  854. if (!pegasus->rx_urb) {
  855. usb_free_urb(pegasus->ctrl_urb);
  856. return 0;
  857. }
  858. pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  859. if (!pegasus->tx_urb) {
  860. usb_free_urb(pegasus->rx_urb);
  861. usb_free_urb(pegasus->ctrl_urb);
  862. return 0;
  863. }
  864. pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  865. if (!pegasus->intr_urb) {
  866. usb_free_urb(pegasus->tx_urb);
  867. usb_free_urb(pegasus->rx_urb);
  868. usb_free_urb(pegasus->ctrl_urb);
  869. return 0;
  870. }
  871. return 1;
  872. }
  873. static int pegasus_open(struct net_device *net)
  874. {
  875. pegasus_t *pegasus = netdev_priv(net);
  876. int res;
  877. if (pegasus->rx_skb == NULL)
  878. pegasus->rx_skb = pull_skb(pegasus);
  879. /*
  880. ** Note: no point to free the pool. it is empty :-)
  881. */
  882. if (!pegasus->rx_skb)
  883. return -ENOMEM;
  884. res = set_registers(pegasus, EthID, 6, net->dev_addr);
  885. usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
  886. usb_rcvbulkpipe(pegasus->usb, 1),
  887. pegasus->rx_skb->data, PEGASUS_MTU + 8,
  888. read_bulk_callback, pegasus);
  889. if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
  890. if (res == -ENODEV)
  891. netif_device_detach(pegasus->net);
  892. if (netif_msg_ifup(pegasus))
  893. pr_debug("%s: failed rx_urb, %d", net->name, res);
  894. goto exit;
  895. }
  896. usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
  897. usb_rcvintpipe(pegasus->usb, 3),
  898. pegasus->intr_buff, sizeof (pegasus->intr_buff),
  899. intr_callback, pegasus, pegasus->intr_interval);
  900. if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
  901. if (res == -ENODEV)
  902. netif_device_detach(pegasus->net);
  903. if (netif_msg_ifup(pegasus))
  904. pr_debug("%s: failed intr_urb, %d\n", net->name, res);
  905. usb_kill_urb(pegasus->rx_urb);
  906. goto exit;
  907. }
  908. if ((res = enable_net_traffic(net, pegasus->usb))) {
  909. if (netif_msg_ifup(pegasus))
  910. pr_debug("%s: can't enable_net_traffic() - %d\n",
  911. net->name, res);
  912. res = -EIO;
  913. usb_kill_urb(pegasus->rx_urb);
  914. usb_kill_urb(pegasus->intr_urb);
  915. free_skb_pool(pegasus);
  916. goto exit;
  917. }
  918. set_carrier(net);
  919. netif_start_queue(net);
  920. if (netif_msg_ifup(pegasus))
  921. pr_debug("%s: open\n", net->name);
  922. res = 0;
  923. exit:
  924. return res;
  925. }
  926. static int pegasus_close(struct net_device *net)
  927. {
  928. pegasus_t *pegasus = netdev_priv(net);
  929. netif_stop_queue(net);
  930. if (!(pegasus->flags & PEGASUS_UNPLUG))
  931. disable_net_traffic(pegasus);
  932. tasklet_kill(&pegasus->rx_tl);
  933. unlink_all_urbs(pegasus);
  934. return 0;
  935. }
  936. static void pegasus_get_drvinfo(struct net_device *dev,
  937. struct ethtool_drvinfo *info)
  938. {
  939. pegasus_t *pegasus = netdev_priv(dev);
  940. strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
  941. strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
  942. usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
  943. }
  944. /* also handles three patterns of some kind in hardware */
  945. #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
  946. static void
  947. pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  948. {
  949. pegasus_t *pegasus = netdev_priv(dev);
  950. wol->supported = WAKE_MAGIC | WAKE_PHY;
  951. wol->wolopts = pegasus->wolopts;
  952. }
  953. static int
  954. pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  955. {
  956. pegasus_t *pegasus = netdev_priv(dev);
  957. u8 reg78 = 0x04;
  958. if (wol->wolopts & ~WOL_SUPPORTED)
  959. return -EINVAL;
  960. if (wol->wolopts & WAKE_MAGIC)
  961. reg78 |= 0x80;
  962. if (wol->wolopts & WAKE_PHY)
  963. reg78 |= 0x40;
  964. /* FIXME this 0x10 bit still needs to get set in the chip... */
  965. if (wol->wolopts)
  966. pegasus->eth_regs[0] |= 0x10;
  967. else
  968. pegasus->eth_regs[0] &= ~0x10;
  969. pegasus->wolopts = wol->wolopts;
  970. return set_register(pegasus, WakeupControl, reg78);
  971. }
  972. static inline void pegasus_reset_wol(struct net_device *dev)
  973. {
  974. struct ethtool_wolinfo wol;
  975. memset(&wol, 0, sizeof wol);
  976. (void) pegasus_set_wol(dev, &wol);
  977. }
  978. static int
  979. pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  980. {
  981. pegasus_t *pegasus;
  982. if (in_atomic())
  983. return 0;
  984. pegasus = netdev_priv(dev);
  985. mii_ethtool_gset(&pegasus->mii, ecmd);
  986. return 0;
  987. }
  988. static int
  989. pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  990. {
  991. pegasus_t *pegasus = netdev_priv(dev);
  992. return mii_ethtool_sset(&pegasus->mii, ecmd);
  993. }
  994. static int pegasus_nway_reset(struct net_device *dev)
  995. {
  996. pegasus_t *pegasus = netdev_priv(dev);
  997. return mii_nway_restart(&pegasus->mii);
  998. }
  999. static u32 pegasus_get_link(struct net_device *dev)
  1000. {
  1001. pegasus_t *pegasus = netdev_priv(dev);
  1002. return mii_link_ok(&pegasus->mii);
  1003. }
  1004. static u32 pegasus_get_msglevel(struct net_device *dev)
  1005. {
  1006. pegasus_t *pegasus = netdev_priv(dev);
  1007. return pegasus->msg_enable;
  1008. }
  1009. static void pegasus_set_msglevel(struct net_device *dev, u32 v)
  1010. {
  1011. pegasus_t *pegasus = netdev_priv(dev);
  1012. pegasus->msg_enable = v;
  1013. }
  1014. static struct ethtool_ops ops = {
  1015. .get_drvinfo = pegasus_get_drvinfo,
  1016. .get_settings = pegasus_get_settings,
  1017. .set_settings = pegasus_set_settings,
  1018. .nway_reset = pegasus_nway_reset,
  1019. .get_link = pegasus_get_link,
  1020. .get_msglevel = pegasus_get_msglevel,
  1021. .set_msglevel = pegasus_set_msglevel,
  1022. .get_wol = pegasus_get_wol,
  1023. .set_wol = pegasus_set_wol,
  1024. };
  1025. static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
  1026. {
  1027. __u16 *data = (__u16 *) & rq->ifr_ifru;
  1028. pegasus_t *pegasus = netdev_priv(net);
  1029. int res;
  1030. switch (cmd) {
  1031. case SIOCDEVPRIVATE:
  1032. data[0] = pegasus->phy;
  1033. case SIOCDEVPRIVATE + 1:
  1034. read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
  1035. res = 0;
  1036. break;
  1037. case SIOCDEVPRIVATE + 2:
  1038. if (!capable(CAP_NET_ADMIN))
  1039. return -EPERM;
  1040. write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
  1041. res = 0;
  1042. break;
  1043. default:
  1044. res = -EOPNOTSUPP;
  1045. }
  1046. return res;
  1047. }
  1048. static void pegasus_set_multicast(struct net_device *net)
  1049. {
  1050. pegasus_t *pegasus = netdev_priv(net);
  1051. if (net->flags & IFF_PROMISC) {
  1052. pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
  1053. if (netif_msg_link(pegasus))
  1054. pr_info("%s: Promiscuous mode enabled.\n", net->name);
  1055. } else if (net->mc_count ||
  1056. (net->flags & IFF_ALLMULTI)) {
  1057. pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
  1058. pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
  1059. if (netif_msg_link(pegasus))
  1060. pr_info("%s: set allmulti\n", net->name);
  1061. } else {
  1062. pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
  1063. pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
  1064. }
  1065. pegasus->flags |= ETH_REGS_CHANGE;
  1066. ctrl_callback(pegasus->ctrl_urb);
  1067. }
  1068. static __u8 mii_phy_probe(pegasus_t * pegasus)
  1069. {
  1070. int i;
  1071. __u16 tmp;
  1072. for (i = 0; i < 32; i++) {
  1073. read_mii_word(pegasus, i, MII_BMSR, &tmp);
  1074. if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
  1075. continue;
  1076. else
  1077. return i;
  1078. }
  1079. return 0xff;
  1080. }
  1081. static inline void setup_pegasus_II(pegasus_t * pegasus)
  1082. {
  1083. __u8 data = 0xa5;
  1084. set_register(pegasus, Reg1d, 0);
  1085. set_register(pegasus, Reg7b, 1);
  1086. mdelay(100);
  1087. if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
  1088. set_register(pegasus, Reg7b, 0);
  1089. else
  1090. set_register(pegasus, Reg7b, 2);
  1091. set_register(pegasus, 0x83, data);
  1092. get_registers(pegasus, 0x83, 1, &data);
  1093. if (data == 0xa5) {
  1094. pegasus->chip = 0x8513;
  1095. } else {
  1096. pegasus->chip = 0;
  1097. }
  1098. set_register(pegasus, 0x80, 0xc0);
  1099. set_register(pegasus, 0x83, 0xff);
  1100. set_register(pegasus, 0x84, 0x01);
  1101. if (pegasus->features & HAS_HOME_PNA && mii_mode)
  1102. set_register(pegasus, Reg81, 6);
  1103. else
  1104. set_register(pegasus, Reg81, 2);
  1105. }
  1106. static struct workqueue_struct *pegasus_workqueue = NULL;
  1107. #define CARRIER_CHECK_DELAY (2 * HZ)
  1108. static void check_carrier(struct work_struct *work)
  1109. {
  1110. pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
  1111. set_carrier(pegasus->net);
  1112. if (!(pegasus->flags & PEGASUS_UNPLUG)) {
  1113. queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
  1114. CARRIER_CHECK_DELAY);
  1115. }
  1116. }
  1117. static int pegasus_probe(struct usb_interface *intf,
  1118. const struct usb_device_id *id)
  1119. {
  1120. struct usb_device *dev = interface_to_usbdev(intf);
  1121. struct net_device *net;
  1122. pegasus_t *pegasus;
  1123. int dev_index = id - pegasus_ids;
  1124. int res = -ENOMEM;
  1125. DECLARE_MAC_BUF(mac);
  1126. usb_get_dev(dev);
  1127. net = alloc_etherdev(sizeof(struct pegasus));
  1128. if (!net) {
  1129. dev_err(&intf->dev, "can't allocate %s\n", "device");
  1130. goto out;
  1131. }
  1132. pegasus = netdev_priv(net);
  1133. pegasus->dev_index = dev_index;
  1134. init_waitqueue_head(&pegasus->ctrl_wait);
  1135. if (!alloc_urbs(pegasus)) {
  1136. dev_err(&intf->dev, "can't allocate %s\n", "urbs");
  1137. goto out1;
  1138. }
  1139. tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
  1140. INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
  1141. pegasus->intf = intf;
  1142. pegasus->usb = dev;
  1143. pegasus->net = net;
  1144. net->open = pegasus_open;
  1145. net->stop = pegasus_close;
  1146. net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
  1147. net->tx_timeout = pegasus_tx_timeout;
  1148. net->do_ioctl = pegasus_ioctl;
  1149. net->hard_start_xmit = pegasus_start_xmit;
  1150. net->set_multicast_list = pegasus_set_multicast;
  1151. net->get_stats = pegasus_netdev_stats;
  1152. SET_ETHTOOL_OPS(net, &ops);
  1153. pegasus->mii.dev = net;
  1154. pegasus->mii.mdio_read = mdio_read;
  1155. pegasus->mii.mdio_write = mdio_write;
  1156. pegasus->mii.phy_id_mask = 0x1f;
  1157. pegasus->mii.reg_num_mask = 0x1f;
  1158. spin_lock_init(&pegasus->rx_pool_lock);
  1159. pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  1160. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  1161. pegasus->features = usb_dev_id[dev_index].private;
  1162. get_interrupt_interval(pegasus);
  1163. if (reset_mac(pegasus)) {
  1164. dev_err(&intf->dev, "can't reset MAC\n");
  1165. res = -EIO;
  1166. goto out2;
  1167. }
  1168. set_ethernet_addr(pegasus);
  1169. fill_skb_pool(pegasus);
  1170. if (pegasus->features & PEGASUS_II) {
  1171. dev_info(&intf->dev, "setup Pegasus II specific registers\n");
  1172. setup_pegasus_II(pegasus);
  1173. }
  1174. pegasus->phy = mii_phy_probe(pegasus);
  1175. if (pegasus->phy == 0xff) {
  1176. dev_warn(&intf->dev, "can't locate MII phy, using default\n");
  1177. pegasus->phy = 1;
  1178. }
  1179. pegasus->mii.phy_id = pegasus->phy;
  1180. usb_set_intfdata(intf, pegasus);
  1181. SET_NETDEV_DEV(net, &intf->dev);
  1182. pegasus_reset_wol(net);
  1183. res = register_netdev(net);
  1184. if (res)
  1185. goto out3;
  1186. queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
  1187. CARRIER_CHECK_DELAY);
  1188. dev_info(&intf->dev, "%s, %s, %s\n",
  1189. net->name,
  1190. usb_dev_id[dev_index].name,
  1191. print_mac(mac, net->dev_addr));
  1192. return 0;
  1193. out3:
  1194. usb_set_intfdata(intf, NULL);
  1195. free_skb_pool(pegasus);
  1196. out2:
  1197. free_all_urbs(pegasus);
  1198. out1:
  1199. free_netdev(net);
  1200. out:
  1201. usb_put_dev(dev);
  1202. return res;
  1203. }
  1204. static void pegasus_disconnect(struct usb_interface *intf)
  1205. {
  1206. struct pegasus *pegasus = usb_get_intfdata(intf);
  1207. usb_set_intfdata(intf, NULL);
  1208. if (!pegasus) {
  1209. dev_dbg(&intf->dev, "unregistering non-bound device?\n");
  1210. return;
  1211. }
  1212. pegasus->flags |= PEGASUS_UNPLUG;
  1213. cancel_delayed_work(&pegasus->carrier_check);
  1214. unregister_netdev(pegasus->net);
  1215. usb_put_dev(interface_to_usbdev(intf));
  1216. unlink_all_urbs(pegasus);
  1217. free_all_urbs(pegasus);
  1218. free_skb_pool(pegasus);
  1219. if (pegasus->rx_skb != NULL) {
  1220. dev_kfree_skb(pegasus->rx_skb);
  1221. pegasus->rx_skb = NULL;
  1222. }
  1223. free_netdev(pegasus->net);
  1224. }
  1225. static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
  1226. {
  1227. struct pegasus *pegasus = usb_get_intfdata(intf);
  1228. netif_device_detach (pegasus->net);
  1229. cancel_delayed_work(&pegasus->carrier_check);
  1230. if (netif_running(pegasus->net)) {
  1231. usb_kill_urb(pegasus->rx_urb);
  1232. usb_kill_urb(pegasus->intr_urb);
  1233. }
  1234. return 0;
  1235. }
  1236. static int pegasus_resume (struct usb_interface *intf)
  1237. {
  1238. struct pegasus *pegasus = usb_get_intfdata(intf);
  1239. netif_device_attach (pegasus->net);
  1240. if (netif_running(pegasus->net)) {
  1241. pegasus->rx_urb->status = 0;
  1242. pegasus->rx_urb->actual_length = 0;
  1243. read_bulk_callback(pegasus->rx_urb);
  1244. pegasus->intr_urb->status = 0;
  1245. pegasus->intr_urb->actual_length = 0;
  1246. intr_callback(pegasus->intr_urb);
  1247. }
  1248. queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
  1249. CARRIER_CHECK_DELAY);
  1250. return 0;
  1251. }
  1252. static struct usb_driver pegasus_driver = {
  1253. .name = driver_name,
  1254. .probe = pegasus_probe,
  1255. .disconnect = pegasus_disconnect,
  1256. .id_table = pegasus_ids,
  1257. .suspend = pegasus_suspend,
  1258. .resume = pegasus_resume,
  1259. };
  1260. static void parse_id(char *id)
  1261. {
  1262. unsigned int vendor_id=0, device_id=0, flags=0, i=0;
  1263. char *token, *name=NULL;
  1264. if ((token = strsep(&id, ":")) != NULL)
  1265. name = token;
  1266. /* name now points to a null terminated string*/
  1267. if ((token = strsep(&id, ":")) != NULL)
  1268. vendor_id = simple_strtoul(token, NULL, 16);
  1269. if ((token = strsep(&id, ":")) != NULL)
  1270. device_id = simple_strtoul(token, NULL, 16);
  1271. flags = simple_strtoul(id, NULL, 16);
  1272. pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
  1273. driver_name, name, vendor_id, device_id, flags);
  1274. if (vendor_id > 0x10000 || vendor_id == 0)
  1275. return;
  1276. if (device_id > 0x10000 || device_id == 0)
  1277. return;
  1278. for (i=0; usb_dev_id[i].name; i++);
  1279. usb_dev_id[i].name = name;
  1280. usb_dev_id[i].vendor = vendor_id;
  1281. usb_dev_id[i].device = device_id;
  1282. usb_dev_id[i].private = flags;
  1283. pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  1284. pegasus_ids[i].idVendor = vendor_id;
  1285. pegasus_ids[i].idProduct = device_id;
  1286. }
  1287. static int __init pegasus_init(void)
  1288. {
  1289. pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
  1290. if (devid)
  1291. parse_id(devid);
  1292. pegasus_workqueue = create_singlethread_workqueue("pegasus");
  1293. if (!pegasus_workqueue)
  1294. return -ENOMEM;
  1295. return usb_register(&pegasus_driver);
  1296. }
  1297. static void __exit pegasus_exit(void)
  1298. {
  1299. destroy_workqueue(pegasus_workqueue);
  1300. usb_deregister(&pegasus_driver);
  1301. }
  1302. module_init(pegasus_init);
  1303. module_exit(pegasus_exit);