pegasus.c 34 KB

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