pegasus.c 37 KB

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