pegasus.c 34 KB

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