pegasus.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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] = 0xc8; /* TX & RX enable, append status, no CRC */
  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 < 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 -= 4;
  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,
  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,
  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. netif_trans_update(net); /* 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,
  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 = 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. get_interrupt_interval(pegasus);
  1026. if (reset_mac(pegasus)) {
  1027. dev_err(&intf->dev, "can't reset MAC\n");
  1028. res = -EIO;
  1029. goto out2;
  1030. }
  1031. set_ethernet_addr(pegasus);
  1032. if (pegasus->features & PEGASUS_II) {
  1033. dev_info(&intf->dev, "setup Pegasus II specific registers\n");
  1034. setup_pegasus_II(pegasus);
  1035. }
  1036. pegasus->phy = mii_phy_probe(pegasus);
  1037. if (pegasus->phy == 0xff) {
  1038. dev_warn(&intf->dev, "can't locate MII phy, using default\n");
  1039. pegasus->phy = 1;
  1040. }
  1041. pegasus->mii.phy_id = pegasus->phy;
  1042. usb_set_intfdata(intf, pegasus);
  1043. SET_NETDEV_DEV(net, &intf->dev);
  1044. pegasus_reset_wol(net);
  1045. res = register_netdev(net);
  1046. if (res)
  1047. goto out3;
  1048. queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
  1049. CARRIER_CHECK_DELAY);
  1050. dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
  1051. usb_dev_id[dev_index].name, net->dev_addr);
  1052. return 0;
  1053. out3:
  1054. usb_set_intfdata(intf, NULL);
  1055. out2:
  1056. free_all_urbs(pegasus);
  1057. out1:
  1058. free_netdev(net);
  1059. out:
  1060. pegasus_dec_workqueue();
  1061. return res;
  1062. }
  1063. static void pegasus_disconnect(struct usb_interface *intf)
  1064. {
  1065. struct pegasus *pegasus = usb_get_intfdata(intf);
  1066. usb_set_intfdata(intf, NULL);
  1067. if (!pegasus) {
  1068. dev_dbg(&intf->dev, "unregistering non-bound device?\n");
  1069. return;
  1070. }
  1071. pegasus->flags |= PEGASUS_UNPLUG;
  1072. cancel_delayed_work(&pegasus->carrier_check);
  1073. unregister_netdev(pegasus->net);
  1074. unlink_all_urbs(pegasus);
  1075. free_all_urbs(pegasus);
  1076. if (pegasus->rx_skb != NULL) {
  1077. dev_kfree_skb(pegasus->rx_skb);
  1078. pegasus->rx_skb = NULL;
  1079. }
  1080. free_netdev(pegasus->net);
  1081. pegasus_dec_workqueue();
  1082. }
  1083. static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
  1084. {
  1085. struct pegasus *pegasus = usb_get_intfdata(intf);
  1086. netif_device_detach(pegasus->net);
  1087. cancel_delayed_work(&pegasus->carrier_check);
  1088. if (netif_running(pegasus->net)) {
  1089. usb_kill_urb(pegasus->rx_urb);
  1090. usb_kill_urb(pegasus->intr_urb);
  1091. }
  1092. return 0;
  1093. }
  1094. static int pegasus_resume(struct usb_interface *intf)
  1095. {
  1096. struct pegasus *pegasus = usb_get_intfdata(intf);
  1097. netif_device_attach(pegasus->net);
  1098. if (netif_running(pegasus->net)) {
  1099. pegasus->rx_urb->status = 0;
  1100. pegasus->rx_urb->actual_length = 0;
  1101. read_bulk_callback(pegasus->rx_urb);
  1102. pegasus->intr_urb->status = 0;
  1103. pegasus->intr_urb->actual_length = 0;
  1104. intr_callback(pegasus->intr_urb);
  1105. }
  1106. queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
  1107. CARRIER_CHECK_DELAY);
  1108. return 0;
  1109. }
  1110. static const struct net_device_ops pegasus_netdev_ops = {
  1111. .ndo_open = pegasus_open,
  1112. .ndo_stop = pegasus_close,
  1113. .ndo_do_ioctl = pegasus_ioctl,
  1114. .ndo_start_xmit = pegasus_start_xmit,
  1115. .ndo_set_rx_mode = pegasus_set_multicast,
  1116. .ndo_get_stats = pegasus_netdev_stats,
  1117. .ndo_tx_timeout = pegasus_tx_timeout,
  1118. .ndo_change_mtu = eth_change_mtu,
  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);