pegasus.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  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. int status = urb->status;
  92. if (!pegasus)
  93. return;
  94. switch (status) {
  95. case 0:
  96. if (pegasus->flags & ETH_REGS_CHANGE) {
  97. pegasus->flags &= ~ETH_REGS_CHANGE;
  98. pegasus->flags |= ETH_REGS_CHANGED;
  99. update_eth_regs_async(pegasus);
  100. return;
  101. }
  102. break;
  103. case -EINPROGRESS:
  104. return;
  105. case -ENOENT:
  106. break;
  107. default:
  108. if (netif_msg_drv(pegasus) && printk_ratelimit())
  109. dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
  110. __func__, status);
  111. }
  112. pegasus->flags &= ~ETH_REGS_CHANGED;
  113. wake_up(&pegasus->ctrl_wait);
  114. }
  115. static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
  116. void *data)
  117. {
  118. int ret;
  119. char *buffer;
  120. DECLARE_WAITQUEUE(wait, current);
  121. buffer = kmalloc(size, GFP_KERNEL);
  122. if (!buffer) {
  123. if (netif_msg_drv(pegasus))
  124. dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
  125. __func__);
  126. return -ENOMEM;
  127. }
  128. add_wait_queue(&pegasus->ctrl_wait, &wait);
  129. set_current_state(TASK_UNINTERRUPTIBLE);
  130. while (pegasus->flags & ETH_REGS_CHANGED)
  131. schedule();
  132. remove_wait_queue(&pegasus->ctrl_wait, &wait);
  133. set_current_state(TASK_RUNNING);
  134. pegasus->dr.bRequestType = PEGASUS_REQT_READ;
  135. pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
  136. pegasus->dr.wValue = cpu_to_le16(0);
  137. pegasus->dr.wIndex = cpu_to_le16p(&indx);
  138. pegasus->dr.wLength = cpu_to_le16p(&size);
  139. pegasus->ctrl_urb->transfer_buffer_length = size;
  140. usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
  141. usb_rcvctrlpipe(pegasus->usb, 0),
  142. (char *) &pegasus->dr,
  143. buffer, size, ctrl_callback, pegasus);
  144. add_wait_queue(&pegasus->ctrl_wait, &wait);
  145. set_current_state(TASK_UNINTERRUPTIBLE);
  146. /* using ATOMIC, we'd never wake up if we slept */
  147. if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
  148. set_current_state(TASK_RUNNING);
  149. if (ret == -ENODEV)
  150. netif_device_detach(pegasus->net);
  151. if (netif_msg_drv(pegasus) && printk_ratelimit())
  152. dev_err(&pegasus->intf->dev, "%s, status %d\n",
  153. __func__, ret);
  154. goto out;
  155. }
  156. schedule();
  157. out:
  158. remove_wait_queue(&pegasus->ctrl_wait, &wait);
  159. memcpy(data, buffer, size);
  160. kfree(buffer);
  161. return ret;
  162. }
  163. static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
  164. void *data)
  165. {
  166. int ret;
  167. char *buffer;
  168. DECLARE_WAITQUEUE(wait, current);
  169. buffer = kmalloc(size, GFP_KERNEL);
  170. if (!buffer) {
  171. if (netif_msg_drv(pegasus))
  172. dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
  173. __func__);
  174. return -ENOMEM;
  175. }
  176. memcpy(buffer, data, size);
  177. add_wait_queue(&pegasus->ctrl_wait, &wait);
  178. set_current_state(TASK_UNINTERRUPTIBLE);
  179. while (pegasus->flags & ETH_REGS_CHANGED)
  180. schedule();
  181. remove_wait_queue(&pegasus->ctrl_wait, &wait);
  182. set_current_state(TASK_RUNNING);
  183. pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
  184. pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
  185. pegasus->dr.wValue = cpu_to_le16(0);
  186. pegasus->dr.wIndex = cpu_to_le16p(&indx);
  187. pegasus->dr.wLength = cpu_to_le16p(&size);
  188. pegasus->ctrl_urb->transfer_buffer_length = size;
  189. usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
  190. usb_sndctrlpipe(pegasus->usb, 0),
  191. (char *) &pegasus->dr,
  192. buffer, size, ctrl_callback, pegasus);
  193. add_wait_queue(&pegasus->ctrl_wait, &wait);
  194. set_current_state(TASK_UNINTERRUPTIBLE);
  195. if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
  196. if (ret == -ENODEV)
  197. netif_device_detach(pegasus->net);
  198. if (netif_msg_drv(pegasus))
  199. dev_err(&pegasus->intf->dev, "%s, status %d\n",
  200. __func__, ret);
  201. goto out;
  202. }
  203. schedule();
  204. out:
  205. remove_wait_queue(&pegasus->ctrl_wait, &wait);
  206. kfree(buffer);
  207. return ret;
  208. }
  209. static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
  210. {
  211. int ret;
  212. char *tmp;
  213. DECLARE_WAITQUEUE(wait, current);
  214. tmp = kmalloc(1, GFP_KERNEL);
  215. if (!tmp) {
  216. if (netif_msg_drv(pegasus))
  217. dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
  218. __func__);
  219. return -ENOMEM;
  220. }
  221. memcpy(tmp, &data, 1);
  222. add_wait_queue(&pegasus->ctrl_wait, &wait);
  223. set_current_state(TASK_UNINTERRUPTIBLE);
  224. while (pegasus->flags & ETH_REGS_CHANGED)
  225. schedule();
  226. remove_wait_queue(&pegasus->ctrl_wait, &wait);
  227. set_current_state(TASK_RUNNING);
  228. pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
  229. pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
  230. pegasus->dr.wValue = cpu_to_le16(data);
  231. pegasus->dr.wIndex = cpu_to_le16p(&indx);
  232. pegasus->dr.wLength = cpu_to_le16(1);
  233. pegasus->ctrl_urb->transfer_buffer_length = 1;
  234. usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
  235. usb_sndctrlpipe(pegasus->usb, 0),
  236. (char *) &pegasus->dr,
  237. tmp, 1, ctrl_callback, pegasus);
  238. add_wait_queue(&pegasus->ctrl_wait, &wait);
  239. set_current_state(TASK_UNINTERRUPTIBLE);
  240. if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
  241. if (ret == -ENODEV)
  242. netif_device_detach(pegasus->net);
  243. if (netif_msg_drv(pegasus) && printk_ratelimit())
  244. dev_err(&pegasus->intf->dev, "%s, status %d\n",
  245. __func__, ret);
  246. goto out;
  247. }
  248. schedule();
  249. out:
  250. remove_wait_queue(&pegasus->ctrl_wait, &wait);
  251. kfree(tmp);
  252. return ret;
  253. }
  254. static int update_eth_regs_async(pegasus_t * pegasus)
  255. {
  256. int ret;
  257. pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
  258. pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
  259. pegasus->dr.wValue = 0;
  260. pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
  261. pegasus->dr.wLength = cpu_to_le16(3);
  262. pegasus->ctrl_urb->transfer_buffer_length = 3;
  263. usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
  264. usb_sndctrlpipe(pegasus->usb, 0),
  265. (char *) &pegasus->dr,
  266. pegasus->eth_regs, 3, ctrl_callback, pegasus);
  267. if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
  268. if (ret == -ENODEV)
  269. netif_device_detach(pegasus->net);
  270. if (netif_msg_drv(pegasus))
  271. dev_err(&pegasus->intf->dev, "%s, status %d\n",
  272. __func__, ret);
  273. }
  274. return ret;
  275. }
  276. /* Returns 0 on success, error on failure */
  277. static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
  278. {
  279. int i;
  280. __u8 data[4] = { phy, 0, 0, indx };
  281. __le16 regdi;
  282. int ret;
  283. set_register(pegasus, PhyCtrl, 0);
  284. set_registers(pegasus, PhyAddr, sizeof (data), data);
  285. set_register(pegasus, PhyCtrl, (indx | PHY_READ));
  286. for (i = 0; i < REG_TIMEOUT; i++) {
  287. ret = get_registers(pegasus, PhyCtrl, 1, data);
  288. if (ret == -ESHUTDOWN)
  289. goto fail;
  290. if (data[0] & PHY_DONE)
  291. break;
  292. }
  293. if (i < REG_TIMEOUT) {
  294. ret = get_registers(pegasus, PhyData, 2, &regdi);
  295. *regd = le16_to_cpu(regdi);
  296. return ret;
  297. }
  298. fail:
  299. if (netif_msg_drv(pegasus))
  300. dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
  301. return ret;
  302. }
  303. static int mdio_read(struct net_device *dev, int phy_id, int loc)
  304. {
  305. pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
  306. u16 res;
  307. read_mii_word(pegasus, phy_id, loc, &res);
  308. return (int)res;
  309. }
  310. static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
  311. {
  312. int i;
  313. __u8 data[4] = { phy, 0, 0, indx };
  314. int ret;
  315. data[1] = (u8) regd;
  316. data[2] = (u8) (regd >> 8);
  317. set_register(pegasus, PhyCtrl, 0);
  318. set_registers(pegasus, PhyAddr, sizeof(data), data);
  319. set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
  320. for (i = 0; i < REG_TIMEOUT; i++) {
  321. ret = get_registers(pegasus, PhyCtrl, 1, data);
  322. if (ret == -ESHUTDOWN)
  323. goto fail;
  324. if (data[0] & PHY_DONE)
  325. break;
  326. }
  327. if (i < REG_TIMEOUT)
  328. return ret;
  329. fail:
  330. if (netif_msg_drv(pegasus))
  331. dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
  332. return -ETIMEDOUT;
  333. }
  334. static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
  335. {
  336. pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
  337. write_mii_word(pegasus, phy_id, loc, val);
  338. }
  339. static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
  340. {
  341. int i;
  342. __u8 tmp;
  343. __le16 retdatai;
  344. int ret;
  345. set_register(pegasus, EpromCtrl, 0);
  346. set_register(pegasus, EpromOffset, index);
  347. set_register(pegasus, EpromCtrl, EPROM_READ);
  348. for (i = 0; i < REG_TIMEOUT; i++) {
  349. ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
  350. if (tmp & EPROM_DONE)
  351. break;
  352. if (ret == -ESHUTDOWN)
  353. goto fail;
  354. }
  355. if (i < REG_TIMEOUT) {
  356. ret = get_registers(pegasus, EpromData, 2, &retdatai);
  357. *retdata = le16_to_cpu(retdatai);
  358. return ret;
  359. }
  360. fail:
  361. if (netif_msg_drv(pegasus))
  362. dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
  363. return -ETIMEDOUT;
  364. }
  365. #ifdef PEGASUS_WRITE_EEPROM
  366. static inline void enable_eprom_write(pegasus_t * pegasus)
  367. {
  368. __u8 tmp;
  369. int ret;
  370. get_registers(pegasus, EthCtrl2, 1, &tmp);
  371. set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
  372. }
  373. static inline void disable_eprom_write(pegasus_t * pegasus)
  374. {
  375. __u8 tmp;
  376. int ret;
  377. get_registers(pegasus, EthCtrl2, 1, &tmp);
  378. set_register(pegasus, EpromCtrl, 0);
  379. set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
  380. }
  381. static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
  382. {
  383. int i;
  384. __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
  385. int ret;
  386. set_registers(pegasus, EpromOffset, 4, d);
  387. enable_eprom_write(pegasus);
  388. set_register(pegasus, EpromOffset, index);
  389. set_registers(pegasus, EpromData, 2, &data);
  390. set_register(pegasus, EpromCtrl, EPROM_WRITE);
  391. for (i = 0; i < REG_TIMEOUT; i++) {
  392. ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
  393. if (ret == -ESHUTDOWN)
  394. goto fail;
  395. if (tmp & EPROM_DONE)
  396. break;
  397. }
  398. disable_eprom_write(pegasus);
  399. if (i < REG_TIMEOUT)
  400. return ret;
  401. fail:
  402. if (netif_msg_drv(pegasus))
  403. dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
  404. return -ETIMEDOUT;
  405. }
  406. #endif /* PEGASUS_WRITE_EEPROM */
  407. static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
  408. {
  409. int i;
  410. __u16 w16;
  411. for (i = 0; i < 3; i++) {
  412. read_eprom_word(pegasus, i, &w16);
  413. ((__le16 *) id)[i] = cpu_to_le16p(&w16);
  414. }
  415. }
  416. static void set_ethernet_addr(pegasus_t * pegasus)
  417. {
  418. __u8 node_id[6];
  419. if (pegasus->features & PEGASUS_II) {
  420. get_registers(pegasus, 0x10, sizeof(node_id), node_id);
  421. } else {
  422. get_node_id(pegasus, node_id);
  423. set_registers(pegasus, EthID, sizeof (node_id), node_id);
  424. }
  425. memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
  426. }
  427. static inline int reset_mac(pegasus_t * pegasus)
  428. {
  429. __u8 data = 0x8;
  430. int i;
  431. set_register(pegasus, EthCtrl1, data);
  432. for (i = 0; i < REG_TIMEOUT; i++) {
  433. get_registers(pegasus, EthCtrl1, 1, &data);
  434. if (~data & 0x08) {
  435. if (loopback & 1)
  436. break;
  437. if (mii_mode && (pegasus->features & HAS_HOME_PNA))
  438. set_register(pegasus, Gpio1, 0x34);
  439. else
  440. set_register(pegasus, Gpio1, 0x26);
  441. set_register(pegasus, Gpio0, pegasus->features);
  442. set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
  443. break;
  444. }
  445. }
  446. if (i == REG_TIMEOUT)
  447. return -ETIMEDOUT;
  448. if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
  449. usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
  450. set_register(pegasus, Gpio0, 0x24);
  451. set_register(pegasus, Gpio0, 0x26);
  452. }
  453. if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
  454. __u16 auxmode;
  455. read_mii_word(pegasus, 3, 0x1b, &auxmode);
  456. write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
  457. }
  458. return 0;
  459. }
  460. static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
  461. {
  462. __u16 linkpart;
  463. __u8 data[4];
  464. pegasus_t *pegasus = netdev_priv(dev);
  465. int ret;
  466. read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
  467. data[0] = 0xc9;
  468. data[1] = 0;
  469. if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
  470. data[1] |= 0x20; /* set full duplex */
  471. if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
  472. data[1] |= 0x10; /* set 100 Mbps */
  473. if (mii_mode)
  474. data[1] = 0;
  475. data[2] = (loopback & 1) ? 0x09 : 0x01;
  476. memcpy(pegasus->eth_regs, data, sizeof (data));
  477. ret = set_registers(pegasus, EthCtrl0, 3, data);
  478. if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
  479. usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
  480. usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
  481. u16 auxmode;
  482. read_mii_word(pegasus, 0, 0x1b, &auxmode);
  483. write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
  484. }
  485. return ret;
  486. }
  487. static void fill_skb_pool(pegasus_t * pegasus)
  488. {
  489. int i;
  490. for (i = 0; i < RX_SKBS; i++) {
  491. if (pegasus->rx_pool[i])
  492. continue;
  493. pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
  494. /*
  495. ** we give up if the allocation fail. the tasklet will be
  496. ** rescheduled again anyway...
  497. */
  498. if (pegasus->rx_pool[i] == NULL)
  499. return;
  500. skb_reserve(pegasus->rx_pool[i], 2);
  501. }
  502. }
  503. static void free_skb_pool(pegasus_t * pegasus)
  504. {
  505. int i;
  506. for (i = 0; i < RX_SKBS; i++) {
  507. if (pegasus->rx_pool[i]) {
  508. dev_kfree_skb(pegasus->rx_pool[i]);
  509. pegasus->rx_pool[i] = NULL;
  510. }
  511. }
  512. }
  513. static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
  514. {
  515. int i;
  516. struct sk_buff *skb;
  517. for (i = 0; i < RX_SKBS; i++) {
  518. if (likely(pegasus->rx_pool[i] != NULL)) {
  519. skb = pegasus->rx_pool[i];
  520. pegasus->rx_pool[i] = NULL;
  521. return skb;
  522. }
  523. }
  524. return NULL;
  525. }
  526. static void read_bulk_callback(struct urb *urb)
  527. {
  528. pegasus_t *pegasus = urb->context;
  529. struct net_device *net;
  530. int rx_status, count = urb->actual_length;
  531. int status = urb->status;
  532. u8 *buf = urb->transfer_buffer;
  533. __u16 pkt_len;
  534. if (!pegasus)
  535. return;
  536. net = pegasus->net;
  537. if (!netif_device_present(net) || !netif_running(net))
  538. return;
  539. switch (status) {
  540. case 0:
  541. break;
  542. case -ETIME:
  543. if (netif_msg_rx_err(pegasus))
  544. pr_debug("%s: reset MAC\n", net->name);
  545. pegasus->flags &= ~PEGASUS_RX_BUSY;
  546. break;
  547. case -EPIPE: /* stall, or disconnect from TT */
  548. /* FIXME schedule work to clear the halt */
  549. if (netif_msg_rx_err(pegasus))
  550. printk(KERN_WARNING "%s: no rx stall recovery\n",
  551. net->name);
  552. return;
  553. case -ENOENT:
  554. case -ECONNRESET:
  555. case -ESHUTDOWN:
  556. if (netif_msg_ifdown(pegasus))
  557. pr_debug("%s: rx unlink, %d\n", net->name, status);
  558. return;
  559. default:
  560. if (netif_msg_rx_err(pegasus))
  561. pr_debug("%s: RX status %d\n", net->name, status);
  562. goto goon;
  563. }
  564. if (!count || count < 4)
  565. goto goon;
  566. rx_status = buf[count - 2];
  567. if (rx_status & 0x1e) {
  568. if (netif_msg_rx_err(pegasus))
  569. pr_debug("%s: RX packet error %x\n",
  570. net->name, rx_status);
  571. pegasus->stats.rx_errors++;
  572. if (rx_status & 0x06) // long or runt
  573. pegasus->stats.rx_length_errors++;
  574. if (rx_status & 0x08)
  575. pegasus->stats.rx_crc_errors++;
  576. if (rx_status & 0x10) // extra bits
  577. pegasus->stats.rx_frame_errors++;
  578. goto goon;
  579. }
  580. if (pegasus->chip == 0x8513) {
  581. pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
  582. pkt_len &= 0x0fff;
  583. pegasus->rx_skb->data += 2;
  584. } else {
  585. pkt_len = buf[count - 3] << 8;
  586. pkt_len += buf[count - 4];
  587. pkt_len &= 0xfff;
  588. pkt_len -= 8;
  589. }
  590. /*
  591. * If the packet is unreasonably long, quietly drop it rather than
  592. * kernel panicing by calling skb_put.
  593. */
  594. if (pkt_len > PEGASUS_MTU)
  595. goto goon;
  596. /*
  597. * at this point we are sure pegasus->rx_skb != NULL
  598. * so we go ahead and pass up the packet.
  599. */
  600. skb_put(pegasus->rx_skb, pkt_len);
  601. pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
  602. netif_rx(pegasus->rx_skb);
  603. pegasus->stats.rx_packets++;
  604. pegasus->stats.rx_bytes += pkt_len;
  605. if (pegasus->flags & PEGASUS_UNPLUG)
  606. return;
  607. spin_lock(&pegasus->rx_pool_lock);
  608. pegasus->rx_skb = pull_skb(pegasus);
  609. spin_unlock(&pegasus->rx_pool_lock);
  610. if (pegasus->rx_skb == NULL)
  611. goto tl_sched;
  612. goon:
  613. usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
  614. usb_rcvbulkpipe(pegasus->usb, 1),
  615. pegasus->rx_skb->data, PEGASUS_MTU + 8,
  616. read_bulk_callback, pegasus);
  617. rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
  618. if (rx_status == -ENODEV)
  619. netif_device_detach(pegasus->net);
  620. else if (rx_status) {
  621. pegasus->flags |= PEGASUS_RX_URB_FAIL;
  622. goto tl_sched;
  623. } else {
  624. pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
  625. }
  626. return;
  627. tl_sched:
  628. tasklet_schedule(&pegasus->rx_tl);
  629. }
  630. static void rx_fixup(unsigned long data)
  631. {
  632. pegasus_t *pegasus;
  633. unsigned long flags;
  634. int status;
  635. pegasus = (pegasus_t *) data;
  636. if (pegasus->flags & PEGASUS_UNPLUG)
  637. return;
  638. spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
  639. fill_skb_pool(pegasus);
  640. if (pegasus->flags & PEGASUS_RX_URB_FAIL)
  641. if (pegasus->rx_skb)
  642. goto try_again;
  643. if (pegasus->rx_skb == NULL) {
  644. pegasus->rx_skb = pull_skb(pegasus);
  645. }
  646. if (pegasus->rx_skb == NULL) {
  647. if (netif_msg_rx_err(pegasus))
  648. printk(KERN_WARNING "%s: low on memory\n",
  649. pegasus->net->name);
  650. tasklet_schedule(&pegasus->rx_tl);
  651. goto done;
  652. }
  653. usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
  654. usb_rcvbulkpipe(pegasus->usb, 1),
  655. pegasus->rx_skb->data, PEGASUS_MTU + 8,
  656. read_bulk_callback, pegasus);
  657. try_again:
  658. status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
  659. if (status == -ENODEV)
  660. netif_device_detach(pegasus->net);
  661. else if (status) {
  662. pegasus->flags |= PEGASUS_RX_URB_FAIL;
  663. tasklet_schedule(&pegasus->rx_tl);
  664. } else {
  665. pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
  666. }
  667. done:
  668. spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
  669. }
  670. static void write_bulk_callback(struct urb *urb)
  671. {
  672. pegasus_t *pegasus = urb->context;
  673. struct net_device *net;
  674. int status = urb->status;
  675. if (!pegasus)
  676. return;
  677. net = pegasus->net;
  678. if (!netif_device_present(net) || !netif_running(net))
  679. return;
  680. switch (status) {
  681. case -EPIPE:
  682. /* FIXME schedule_work() to clear the tx halt */
  683. netif_stop_queue(net);
  684. if (netif_msg_tx_err(pegasus))
  685. printk(KERN_WARNING "%s: no tx stall recovery\n",
  686. net->name);
  687. return;
  688. case -ENOENT:
  689. case -ECONNRESET:
  690. case -ESHUTDOWN:
  691. if (netif_msg_ifdown(pegasus))
  692. pr_debug("%s: tx unlink, %d\n", net->name, status);
  693. return;
  694. default:
  695. if (netif_msg_tx_err(pegasus))
  696. pr_info("%s: TX status %d\n", net->name, status);
  697. /* FALL THROUGH */
  698. case 0:
  699. break;
  700. }
  701. net->trans_start = jiffies;
  702. netif_wake_queue(net);
  703. }
  704. static void intr_callback(struct urb *urb)
  705. {
  706. pegasus_t *pegasus = urb->context;
  707. struct net_device *net;
  708. int res, status = urb->status;
  709. if (!pegasus)
  710. return;
  711. net = pegasus->net;
  712. switch (status) {
  713. case 0:
  714. break;
  715. case -ECONNRESET: /* unlink */
  716. case -ENOENT:
  717. case -ESHUTDOWN:
  718. return;
  719. default:
  720. /* some Pegasus-I products report LOTS of data
  721. * toggle errors... avoid log spamming
  722. */
  723. if (netif_msg_timer(pegasus))
  724. pr_debug("%s: intr status %d\n", net->name,
  725. status);
  726. }
  727. if (urb->actual_length >= 6) {
  728. u8 * d = urb->transfer_buffer;
  729. /* byte 0 == tx_status1, reg 2B */
  730. if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
  731. |LATE_COL|JABBER_TIMEOUT)) {
  732. pegasus->stats.tx_errors++;
  733. if (d[0] & TX_UNDERRUN)
  734. pegasus->stats.tx_fifo_errors++;
  735. if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
  736. pegasus->stats.tx_aborted_errors++;
  737. if (d[0] & LATE_COL)
  738. pegasus->stats.tx_window_errors++;
  739. }
  740. /* d[5].LINK_STATUS lies on some adapters.
  741. * d[0].NO_CARRIER kicks in only with failed TX.
  742. * ... so monitoring with MII may be safest.
  743. */
  744. /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
  745. pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
  746. }
  747. res = usb_submit_urb(urb, GFP_ATOMIC);
  748. if (res == -ENODEV)
  749. netif_device_detach(pegasus->net);
  750. if (res && netif_msg_timer(pegasus))
  751. printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
  752. net->name, res);
  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. skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, 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. pegasus = netdev_priv(dev);
  986. mii_ethtool_gset(&pegasus->mii, ecmd);
  987. return 0;
  988. }
  989. static int
  990. pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  991. {
  992. pegasus_t *pegasus = netdev_priv(dev);
  993. return mii_ethtool_sset(&pegasus->mii, ecmd);
  994. }
  995. static int pegasus_nway_reset(struct net_device *dev)
  996. {
  997. pegasus_t *pegasus = netdev_priv(dev);
  998. return mii_nway_restart(&pegasus->mii);
  999. }
  1000. static u32 pegasus_get_link(struct net_device *dev)
  1001. {
  1002. pegasus_t *pegasus = netdev_priv(dev);
  1003. return mii_link_ok(&pegasus->mii);
  1004. }
  1005. static u32 pegasus_get_msglevel(struct net_device *dev)
  1006. {
  1007. pegasus_t *pegasus = netdev_priv(dev);
  1008. return pegasus->msg_enable;
  1009. }
  1010. static void pegasus_set_msglevel(struct net_device *dev, u32 v)
  1011. {
  1012. pegasus_t *pegasus = netdev_priv(dev);
  1013. pegasus->msg_enable = v;
  1014. }
  1015. static struct ethtool_ops ops = {
  1016. .get_drvinfo = pegasus_get_drvinfo,
  1017. .get_settings = pegasus_get_settings,
  1018. .set_settings = pegasus_set_settings,
  1019. .nway_reset = pegasus_nway_reset,
  1020. .get_link = pegasus_get_link,
  1021. .get_msglevel = pegasus_get_msglevel,
  1022. .set_msglevel = pegasus_set_msglevel,
  1023. .get_wol = pegasus_get_wol,
  1024. .set_wol = pegasus_set_wol,
  1025. };
  1026. static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
  1027. {
  1028. __u16 *data = (__u16 *) & rq->ifr_ifru;
  1029. pegasus_t *pegasus = netdev_priv(net);
  1030. int res;
  1031. switch (cmd) {
  1032. case SIOCDEVPRIVATE:
  1033. data[0] = pegasus->phy;
  1034. case SIOCDEVPRIVATE + 1:
  1035. read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
  1036. res = 0;
  1037. break;
  1038. case SIOCDEVPRIVATE + 2:
  1039. if (!capable(CAP_NET_ADMIN))
  1040. return -EPERM;
  1041. write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
  1042. res = 0;
  1043. break;
  1044. default:
  1045. res = -EOPNOTSUPP;
  1046. }
  1047. return res;
  1048. }
  1049. static void pegasus_set_multicast(struct net_device *net)
  1050. {
  1051. pegasus_t *pegasus = netdev_priv(net);
  1052. if (net->flags & IFF_PROMISC) {
  1053. pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
  1054. if (netif_msg_link(pegasus))
  1055. pr_info("%s: Promiscuous mode enabled.\n", net->name);
  1056. } else if (net->mc_count || (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_debug("%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->ctrl_urb->status = 0;
  1066. pegasus->flags |= ETH_REGS_CHANGE;
  1067. ctrl_callback(pegasus->ctrl_urb);
  1068. }
  1069. static __u8 mii_phy_probe(pegasus_t * pegasus)
  1070. {
  1071. int i;
  1072. __u16 tmp;
  1073. for (i = 0; i < 32; i++) {
  1074. read_mii_word(pegasus, i, MII_BMSR, &tmp);
  1075. if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
  1076. continue;
  1077. else
  1078. return i;
  1079. }
  1080. return 0xff;
  1081. }
  1082. static inline void setup_pegasus_II(pegasus_t * pegasus)
  1083. {
  1084. __u8 data = 0xa5;
  1085. set_register(pegasus, Reg1d, 0);
  1086. set_register(pegasus, Reg7b, 1);
  1087. mdelay(100);
  1088. if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
  1089. set_register(pegasus, Reg7b, 0);
  1090. else
  1091. set_register(pegasus, Reg7b, 2);
  1092. set_register(pegasus, 0x83, data);
  1093. get_registers(pegasus, 0x83, 1, &data);
  1094. if (data == 0xa5) {
  1095. pegasus->chip = 0x8513;
  1096. } else {
  1097. pegasus->chip = 0;
  1098. }
  1099. set_register(pegasus, 0x80, 0xc0);
  1100. set_register(pegasus, 0x83, 0xff);
  1101. set_register(pegasus, 0x84, 0x01);
  1102. if (pegasus->features & HAS_HOME_PNA && mii_mode)
  1103. set_register(pegasus, Reg81, 6);
  1104. else
  1105. set_register(pegasus, Reg81, 2);
  1106. }
  1107. static int pegasus_count;
  1108. static struct workqueue_struct *pegasus_workqueue = NULL;
  1109. #define CARRIER_CHECK_DELAY (2 * HZ)
  1110. static void check_carrier(struct work_struct *work)
  1111. {
  1112. pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
  1113. set_carrier(pegasus->net);
  1114. if (!(pegasus->flags & PEGASUS_UNPLUG)) {
  1115. queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
  1116. CARRIER_CHECK_DELAY);
  1117. }
  1118. }
  1119. static int pegasus_blacklisted(struct usb_device *udev)
  1120. {
  1121. struct usb_device_descriptor *udd = &udev->descriptor;
  1122. /* Special quirk to keep the driver from handling the Belkin Bluetooth
  1123. * dongle which happens to have the same ID.
  1124. */
  1125. if ((udd->idVendor == VENDOR_BELKIN && udd->idProduct == 0x0121) &&
  1126. (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
  1127. (udd->bDeviceProtocol == 1))
  1128. return 1;
  1129. return 0;
  1130. }
  1131. /* we rely on probe() and remove() being serialized so we
  1132. * don't need extra locking on pegasus_count.
  1133. */
  1134. static void pegasus_dec_workqueue(void)
  1135. {
  1136. pegasus_count--;
  1137. if (pegasus_count == 0) {
  1138. destroy_workqueue(pegasus_workqueue);
  1139. pegasus_workqueue = NULL;
  1140. }
  1141. }
  1142. static int pegasus_probe(struct usb_interface *intf,
  1143. const struct usb_device_id *id)
  1144. {
  1145. struct usb_device *dev = interface_to_usbdev(intf);
  1146. struct net_device *net;
  1147. pegasus_t *pegasus;
  1148. int dev_index = id - pegasus_ids;
  1149. int res = -ENOMEM;
  1150. if (pegasus_blacklisted(dev))
  1151. return -ENODEV;
  1152. if (pegasus_count == 0) {
  1153. pegasus_workqueue = create_singlethread_workqueue("pegasus");
  1154. if (!pegasus_workqueue)
  1155. return -ENOMEM;
  1156. }
  1157. pegasus_count++;
  1158. usb_get_dev(dev);
  1159. net = alloc_etherdev(sizeof(struct pegasus));
  1160. if (!net) {
  1161. dev_err(&intf->dev, "can't allocate %s\n", "device");
  1162. goto out;
  1163. }
  1164. pegasus = netdev_priv(net);
  1165. pegasus->dev_index = dev_index;
  1166. init_waitqueue_head(&pegasus->ctrl_wait);
  1167. if (!alloc_urbs(pegasus)) {
  1168. dev_err(&intf->dev, "can't allocate %s\n", "urbs");
  1169. goto out1;
  1170. }
  1171. tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
  1172. INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
  1173. pegasus->intf = intf;
  1174. pegasus->usb = dev;
  1175. pegasus->net = net;
  1176. net->open = pegasus_open;
  1177. net->stop = pegasus_close;
  1178. net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
  1179. net->tx_timeout = pegasus_tx_timeout;
  1180. net->do_ioctl = pegasus_ioctl;
  1181. net->hard_start_xmit = pegasus_start_xmit;
  1182. net->set_multicast_list = pegasus_set_multicast;
  1183. net->get_stats = pegasus_netdev_stats;
  1184. SET_ETHTOOL_OPS(net, &ops);
  1185. pegasus->mii.dev = net;
  1186. pegasus->mii.mdio_read = mdio_read;
  1187. pegasus->mii.mdio_write = mdio_write;
  1188. pegasus->mii.phy_id_mask = 0x1f;
  1189. pegasus->mii.reg_num_mask = 0x1f;
  1190. spin_lock_init(&pegasus->rx_pool_lock);
  1191. pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  1192. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  1193. pegasus->features = usb_dev_id[dev_index].private;
  1194. get_interrupt_interval(pegasus);
  1195. if (reset_mac(pegasus)) {
  1196. dev_err(&intf->dev, "can't reset MAC\n");
  1197. res = -EIO;
  1198. goto out2;
  1199. }
  1200. set_ethernet_addr(pegasus);
  1201. fill_skb_pool(pegasus);
  1202. if (pegasus->features & PEGASUS_II) {
  1203. dev_info(&intf->dev, "setup Pegasus II specific registers\n");
  1204. setup_pegasus_II(pegasus);
  1205. }
  1206. pegasus->phy = mii_phy_probe(pegasus);
  1207. if (pegasus->phy == 0xff) {
  1208. dev_warn(&intf->dev, "can't locate MII phy, using default\n");
  1209. pegasus->phy = 1;
  1210. }
  1211. pegasus->mii.phy_id = pegasus->phy;
  1212. usb_set_intfdata(intf, pegasus);
  1213. SET_NETDEV_DEV(net, &intf->dev);
  1214. pegasus_reset_wol(net);
  1215. res = register_netdev(net);
  1216. if (res)
  1217. goto out3;
  1218. queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
  1219. CARRIER_CHECK_DELAY);
  1220. dev_info(&intf->dev, "%s, %s, %pM\n",
  1221. net->name,
  1222. usb_dev_id[dev_index].name,
  1223. net->dev_addr);
  1224. return 0;
  1225. out3:
  1226. usb_set_intfdata(intf, NULL);
  1227. free_skb_pool(pegasus);
  1228. out2:
  1229. free_all_urbs(pegasus);
  1230. out1:
  1231. free_netdev(net);
  1232. out:
  1233. usb_put_dev(dev);
  1234. pegasus_dec_workqueue();
  1235. return res;
  1236. }
  1237. static void pegasus_disconnect(struct usb_interface *intf)
  1238. {
  1239. struct pegasus *pegasus = usb_get_intfdata(intf);
  1240. usb_set_intfdata(intf, NULL);
  1241. if (!pegasus) {
  1242. dev_dbg(&intf->dev, "unregistering non-bound device?\n");
  1243. return;
  1244. }
  1245. pegasus->flags |= PEGASUS_UNPLUG;
  1246. cancel_delayed_work(&pegasus->carrier_check);
  1247. unregister_netdev(pegasus->net);
  1248. usb_put_dev(interface_to_usbdev(intf));
  1249. unlink_all_urbs(pegasus);
  1250. free_all_urbs(pegasus);
  1251. free_skb_pool(pegasus);
  1252. if (pegasus->rx_skb != NULL) {
  1253. dev_kfree_skb(pegasus->rx_skb);
  1254. pegasus->rx_skb = NULL;
  1255. }
  1256. free_netdev(pegasus->net);
  1257. pegasus_dec_workqueue();
  1258. }
  1259. static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
  1260. {
  1261. struct pegasus *pegasus = usb_get_intfdata(intf);
  1262. netif_device_detach (pegasus->net);
  1263. cancel_delayed_work(&pegasus->carrier_check);
  1264. if (netif_running(pegasus->net)) {
  1265. usb_kill_urb(pegasus->rx_urb);
  1266. usb_kill_urb(pegasus->intr_urb);
  1267. }
  1268. return 0;
  1269. }
  1270. static int pegasus_resume (struct usb_interface *intf)
  1271. {
  1272. struct pegasus *pegasus = usb_get_intfdata(intf);
  1273. netif_device_attach (pegasus->net);
  1274. if (netif_running(pegasus->net)) {
  1275. pegasus->rx_urb->status = 0;
  1276. pegasus->rx_urb->actual_length = 0;
  1277. read_bulk_callback(pegasus->rx_urb);
  1278. pegasus->intr_urb->status = 0;
  1279. pegasus->intr_urb->actual_length = 0;
  1280. intr_callback(pegasus->intr_urb);
  1281. }
  1282. queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
  1283. CARRIER_CHECK_DELAY);
  1284. return 0;
  1285. }
  1286. static struct usb_driver pegasus_driver = {
  1287. .name = driver_name,
  1288. .probe = pegasus_probe,
  1289. .disconnect = pegasus_disconnect,
  1290. .id_table = pegasus_ids,
  1291. .suspend = pegasus_suspend,
  1292. .resume = pegasus_resume,
  1293. };
  1294. static void __init parse_id(char *id)
  1295. {
  1296. unsigned int vendor_id=0, device_id=0, flags=0, i=0;
  1297. char *token, *name=NULL;
  1298. if ((token = strsep(&id, ":")) != NULL)
  1299. name = token;
  1300. /* name now points to a null terminated string*/
  1301. if ((token = strsep(&id, ":")) != NULL)
  1302. vendor_id = simple_strtoul(token, NULL, 16);
  1303. if ((token = strsep(&id, ":")) != NULL)
  1304. device_id = simple_strtoul(token, NULL, 16);
  1305. flags = simple_strtoul(id, NULL, 16);
  1306. pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
  1307. driver_name, name, vendor_id, device_id, flags);
  1308. if (vendor_id > 0x10000 || vendor_id == 0)
  1309. return;
  1310. if (device_id > 0x10000 || device_id == 0)
  1311. return;
  1312. for (i=0; usb_dev_id[i].name; i++);
  1313. usb_dev_id[i].name = name;
  1314. usb_dev_id[i].vendor = vendor_id;
  1315. usb_dev_id[i].device = device_id;
  1316. usb_dev_id[i].private = flags;
  1317. pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
  1318. pegasus_ids[i].idVendor = vendor_id;
  1319. pegasus_ids[i].idProduct = device_id;
  1320. }
  1321. static int __init pegasus_init(void)
  1322. {
  1323. pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
  1324. if (devid)
  1325. parse_id(devid);
  1326. return usb_register(&pegasus_driver);
  1327. }
  1328. static void __exit pegasus_exit(void)
  1329. {
  1330. usb_deregister(&pegasus_driver);
  1331. }
  1332. module_init(pegasus_init);
  1333. module_exit(pegasus_exit);