pegasus.c 39 KB

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