pegasus.c 38 KB

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