kaweth.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. /****************************************************************
  2. *
  3. * kaweth.c - driver for KL5KUSB101 based USB->Ethernet
  4. *
  5. * (c) 2000 Interlan Communications
  6. * (c) 2000 Stephane Alnet
  7. * (C) 2001 Brad Hards
  8. * (C) 2002 Oliver Neukum
  9. *
  10. * Original author: The Zapman <zapman@interlan.net>
  11. * Inspired by, and much credit goes to Michael Rothwell
  12. * <rothwell@interlan.net> for the test equipment, help, and patience
  13. * Based off of (and with thanks to) Petko Manolov's pegaus.c driver.
  14. * Also many thanks to Joel Silverman and Ed Surprenant at Kawasaki
  15. * for providing the firmware and driver resources.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software Foundation,
  29. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  30. *
  31. ****************************************************************/
  32. /* TODO:
  33. * Develop test procedures for USB net interfaces
  34. * Run test procedures
  35. * Fix bugs from previous two steps
  36. * Snoop other OSs for any tricks we're not doing
  37. * Reduce arbitrary timeouts
  38. * Smart multicast support
  39. * Temporary MAC change support
  40. * Tunable SOFs parameter - ioctl()?
  41. * Ethernet stats collection
  42. * Code formatting improvements
  43. */
  44. #include <linux/module.h>
  45. #include <linux/slab.h>
  46. #include <linux/string.h>
  47. #include <linux/init.h>
  48. #include <linux/delay.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/usb.h>
  52. #include <linux/types.h>
  53. #include <linux/ethtool.h>
  54. #include <linux/dma-mapping.h>
  55. #include <linux/wait.h>
  56. #include <linux/firmware.h>
  57. #include <asm/uaccess.h>
  58. #include <asm/byteorder.h>
  59. #undef DEBUG
  60. #define KAWETH_MTU 1514
  61. #define KAWETH_BUF_SIZE 1664
  62. #define KAWETH_TX_TIMEOUT (5 * HZ)
  63. #define KAWETH_SCRATCH_SIZE 32
  64. #define KAWETH_FIRMWARE_BUF_SIZE 4096
  65. #define KAWETH_CONTROL_TIMEOUT (30000)
  66. #define KAWETH_STATUS_BROKEN 0x0000001
  67. #define KAWETH_STATUS_CLOSING 0x0000002
  68. #define KAWETH_STATUS_SUSPENDING 0x0000004
  69. #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
  70. #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
  71. #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
  72. #define KAWETH_PACKET_FILTER_DIRECTED 0x04
  73. #define KAWETH_PACKET_FILTER_BROADCAST 0x08
  74. #define KAWETH_PACKET_FILTER_MULTICAST 0x10
  75. /* Table 7 */
  76. #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
  77. #define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
  78. #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
  79. #define KAWETH_COMMAND_STATISTICS 0x03
  80. #define KAWETH_COMMAND_SET_TEMP_MAC 0x06
  81. #define KAWETH_COMMAND_GET_TEMP_MAC 0x07
  82. #define KAWETH_COMMAND_SET_URB_SIZE 0x08
  83. #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
  84. #define KAWETH_COMMAND_SCAN 0xFF
  85. #define KAWETH_SOFS_TO_WAIT 0x05
  86. #define INTBUFFERSIZE 4
  87. #define STATE_OFFSET 0
  88. #define STATE_MASK 0x40
  89. #define STATE_SHIFT 5
  90. #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
  91. MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr>, Brad Hards <bhards@bigpond.net.au> and Oliver Neukum <oliver@neukum.org>");
  92. MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
  93. MODULE_LICENSE("GPL");
  94. MODULE_FIRMWARE("kaweth/new_code.bin");
  95. MODULE_FIRMWARE("kaweth/new_code_fix.bin");
  96. MODULE_FIRMWARE("kaweth/trigger_code.bin");
  97. MODULE_FIRMWARE("kaweth/trigger_code_fix.bin");
  98. static const char driver_name[] = "kaweth";
  99. static int kaweth_probe(
  100. struct usb_interface *intf,
  101. const struct usb_device_id *id /* from id_table */
  102. );
  103. static void kaweth_disconnect(struct usb_interface *intf);
  104. static int kaweth_internal_control_msg(struct usb_device *usb_dev,
  105. unsigned int pipe,
  106. struct usb_ctrlrequest *cmd, void *data,
  107. int len, int timeout);
  108. static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
  109. static int kaweth_resume(struct usb_interface *intf);
  110. /****************************************************************
  111. * usb_device_id
  112. ****************************************************************/
  113. static struct usb_device_id usb_klsi_table[] = {
  114. { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
  115. { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
  116. { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
  117. { USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
  118. { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
  119. { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
  120. { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
  121. { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
  122. { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
  123. { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
  124. { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
  125. { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
  126. { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
  127. { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
  128. { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
  129. { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
  130. { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
  131. { USB_DEVICE(0x07c9, 0xb010) }, /* Allied Telesyn AT-USB10 USB Ethernet Adapter */
  132. { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
  133. { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
  134. { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
  135. { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
  136. { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
  137. { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
  138. { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
  139. { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
  140. { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
  141. { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
  142. { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
  143. { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
  144. { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
  145. { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
  146. { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
  147. { USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
  148. { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
  149. {} /* Null terminator */
  150. };
  151. MODULE_DEVICE_TABLE (usb, usb_klsi_table);
  152. /****************************************************************
  153. * kaweth_driver
  154. ****************************************************************/
  155. static struct usb_driver kaweth_driver = {
  156. .name = driver_name,
  157. .probe = kaweth_probe,
  158. .disconnect = kaweth_disconnect,
  159. .suspend = kaweth_suspend,
  160. .resume = kaweth_resume,
  161. .id_table = usb_klsi_table,
  162. .supports_autosuspend = 1,
  163. .disable_hub_initiated_lpm = 1,
  164. };
  165. typedef __u8 eth_addr_t[6];
  166. /****************************************************************
  167. * usb_eth_dev
  168. ****************************************************************/
  169. struct usb_eth_dev {
  170. char *name;
  171. __u16 vendor;
  172. __u16 device;
  173. void *pdata;
  174. };
  175. /****************************************************************
  176. * kaweth_ethernet_configuration
  177. * Refer Table 8
  178. ****************************************************************/
  179. struct kaweth_ethernet_configuration
  180. {
  181. __u8 size;
  182. __u8 reserved1;
  183. __u8 reserved2;
  184. eth_addr_t hw_addr;
  185. __u32 statistics_mask;
  186. __le16 segment_size;
  187. __u16 max_multicast_filters;
  188. __u8 reserved3;
  189. } __packed;
  190. /****************************************************************
  191. * kaweth_device
  192. ****************************************************************/
  193. struct kaweth_device
  194. {
  195. spinlock_t device_lock;
  196. __u32 status;
  197. int end;
  198. int suspend_lowmem_rx;
  199. int suspend_lowmem_ctrl;
  200. int linkstate;
  201. int opened;
  202. struct delayed_work lowmem_work;
  203. struct usb_device *dev;
  204. struct usb_interface *intf;
  205. struct net_device *net;
  206. wait_queue_head_t term_wait;
  207. struct urb *rx_urb;
  208. struct urb *tx_urb;
  209. struct urb *irq_urb;
  210. dma_addr_t intbufferhandle;
  211. __u8 *intbuffer;
  212. dma_addr_t rxbufferhandle;
  213. __u8 *rx_buf;
  214. struct sk_buff *tx_skb;
  215. __u8 *firmware_buf;
  216. __u8 scratch[KAWETH_SCRATCH_SIZE];
  217. __u16 packet_filter_bitmap;
  218. struct kaweth_ethernet_configuration configuration;
  219. struct net_device_stats stats;
  220. };
  221. /****************************************************************
  222. * kaweth_control
  223. ****************************************************************/
  224. static int kaweth_control(struct kaweth_device *kaweth,
  225. unsigned int pipe,
  226. __u8 request,
  227. __u8 requesttype,
  228. __u16 value,
  229. __u16 index,
  230. void *data,
  231. __u16 size,
  232. int timeout)
  233. {
  234. struct usb_ctrlrequest *dr;
  235. int retval;
  236. netdev_dbg(kaweth->net, "kaweth_control()\n");
  237. if(in_interrupt()) {
  238. netdev_dbg(kaweth->net, "in_interrupt()\n");
  239. return -EBUSY;
  240. }
  241. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  242. if (!dr)
  243. return -ENOMEM;
  244. dr->bRequestType = requesttype;
  245. dr->bRequest = request;
  246. dr->wValue = cpu_to_le16(value);
  247. dr->wIndex = cpu_to_le16(index);
  248. dr->wLength = cpu_to_le16(size);
  249. retval = kaweth_internal_control_msg(kaweth->dev,
  250. pipe,
  251. dr,
  252. data,
  253. size,
  254. timeout);
  255. kfree(dr);
  256. return retval;
  257. }
  258. /****************************************************************
  259. * kaweth_read_configuration
  260. ****************************************************************/
  261. static int kaweth_read_configuration(struct kaweth_device *kaweth)
  262. {
  263. int retval;
  264. netdev_dbg(kaweth->net, "Reading kaweth configuration\n");
  265. retval = kaweth_control(kaweth,
  266. usb_rcvctrlpipe(kaweth->dev, 0),
  267. KAWETH_COMMAND_GET_ETHERNET_DESC,
  268. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  269. 0,
  270. 0,
  271. (void *)&kaweth->configuration,
  272. sizeof(kaweth->configuration),
  273. KAWETH_CONTROL_TIMEOUT);
  274. return retval;
  275. }
  276. /****************************************************************
  277. * kaweth_set_urb_size
  278. ****************************************************************/
  279. static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
  280. {
  281. int retval;
  282. netdev_dbg(kaweth->net, "Setting URB size to %d\n", (unsigned)urb_size);
  283. retval = kaweth_control(kaweth,
  284. usb_sndctrlpipe(kaweth->dev, 0),
  285. KAWETH_COMMAND_SET_URB_SIZE,
  286. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  287. urb_size,
  288. 0,
  289. (void *)&kaweth->scratch,
  290. 0,
  291. KAWETH_CONTROL_TIMEOUT);
  292. return retval;
  293. }
  294. /****************************************************************
  295. * kaweth_set_sofs_wait
  296. ****************************************************************/
  297. static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
  298. {
  299. int retval;
  300. netdev_dbg(kaweth->net, "Set SOFS wait to %d\n", (unsigned)sofs_wait);
  301. retval = kaweth_control(kaweth,
  302. usb_sndctrlpipe(kaweth->dev, 0),
  303. KAWETH_COMMAND_SET_SOFS_WAIT,
  304. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  305. sofs_wait,
  306. 0,
  307. (void *)&kaweth->scratch,
  308. 0,
  309. KAWETH_CONTROL_TIMEOUT);
  310. return retval;
  311. }
  312. /****************************************************************
  313. * kaweth_set_receive_filter
  314. ****************************************************************/
  315. static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
  316. __u16 receive_filter)
  317. {
  318. int retval;
  319. netdev_dbg(kaweth->net, "Set receive filter to %d\n",
  320. (unsigned)receive_filter);
  321. retval = kaweth_control(kaweth,
  322. usb_sndctrlpipe(kaweth->dev, 0),
  323. KAWETH_COMMAND_SET_PACKET_FILTER,
  324. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  325. receive_filter,
  326. 0,
  327. (void *)&kaweth->scratch,
  328. 0,
  329. KAWETH_CONTROL_TIMEOUT);
  330. return retval;
  331. }
  332. /****************************************************************
  333. * kaweth_download_firmware
  334. ****************************************************************/
  335. static int kaweth_download_firmware(struct kaweth_device *kaweth,
  336. const char *fwname,
  337. __u8 interrupt,
  338. __u8 type)
  339. {
  340. const struct firmware *fw;
  341. int data_len;
  342. int ret;
  343. ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
  344. if (ret) {
  345. dev_err(&kaweth->intf->dev, "Firmware request failed\n");
  346. return ret;
  347. }
  348. if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
  349. dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
  350. fw->size);
  351. release_firmware(fw);
  352. return -ENOSPC;
  353. }
  354. data_len = fw->size;
  355. memcpy(kaweth->firmware_buf, fw->data, fw->size);
  356. release_firmware(fw);
  357. kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
  358. kaweth->firmware_buf[3] = data_len >> 8;
  359. kaweth->firmware_buf[4] = type;
  360. kaweth->firmware_buf[5] = interrupt;
  361. netdev_dbg(kaweth->net, "High: %i, Low:%i\n", kaweth->firmware_buf[3],
  362. kaweth->firmware_buf[2]);
  363. netdev_dbg(kaweth->net,
  364. "Downloading firmware at %p to kaweth device at %p\n",
  365. kaweth->firmware_buf, kaweth);
  366. netdev_dbg(kaweth->net, "Firmware length: %d\n", data_len);
  367. return kaweth_control(kaweth,
  368. usb_sndctrlpipe(kaweth->dev, 0),
  369. KAWETH_COMMAND_SCAN,
  370. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  371. 0,
  372. 0,
  373. (void *)kaweth->firmware_buf,
  374. data_len,
  375. KAWETH_CONTROL_TIMEOUT);
  376. }
  377. /****************************************************************
  378. * kaweth_trigger_firmware
  379. ****************************************************************/
  380. static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
  381. __u8 interrupt)
  382. {
  383. kaweth->firmware_buf[0] = 0xB6;
  384. kaweth->firmware_buf[1] = 0xC3;
  385. kaweth->firmware_buf[2] = 0x01;
  386. kaweth->firmware_buf[3] = 0x00;
  387. kaweth->firmware_buf[4] = 0x06;
  388. kaweth->firmware_buf[5] = interrupt;
  389. kaweth->firmware_buf[6] = 0x00;
  390. kaweth->firmware_buf[7] = 0x00;
  391. netdev_dbg(kaweth->net, "Triggering firmware\n");
  392. return kaweth_control(kaweth,
  393. usb_sndctrlpipe(kaweth->dev, 0),
  394. KAWETH_COMMAND_SCAN,
  395. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  396. 0,
  397. 0,
  398. (void *)kaweth->firmware_buf,
  399. 8,
  400. KAWETH_CONTROL_TIMEOUT);
  401. }
  402. /****************************************************************
  403. * kaweth_reset
  404. ****************************************************************/
  405. static int kaweth_reset(struct kaweth_device *kaweth)
  406. {
  407. int result;
  408. netdev_dbg(kaweth->net, "kaweth_reset(%p)\n", kaweth);
  409. result = usb_reset_configuration(kaweth->dev);
  410. mdelay(10);
  411. netdev_dbg(kaweth->net, "kaweth_reset() returns %d.\n", result);
  412. return result;
  413. }
  414. static void kaweth_usb_receive(struct urb *);
  415. static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
  416. /****************************************************************
  417. int_callback
  418. *****************************************************************/
  419. static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
  420. {
  421. int status;
  422. status = usb_submit_urb (kaweth->irq_urb, mf);
  423. if (unlikely(status == -ENOMEM)) {
  424. kaweth->suspend_lowmem_ctrl = 1;
  425. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  426. } else {
  427. kaweth->suspend_lowmem_ctrl = 0;
  428. }
  429. if (status)
  430. dev_err(&kaweth->intf->dev,
  431. "can't resubmit intr, %s-%s, status %d\n",
  432. kaweth->dev->bus->bus_name,
  433. kaweth->dev->devpath, status);
  434. }
  435. static void int_callback(struct urb *u)
  436. {
  437. struct kaweth_device *kaweth = u->context;
  438. int act_state;
  439. int status = u->status;
  440. switch (status) {
  441. case 0: /* success */
  442. break;
  443. case -ECONNRESET: /* unlink */
  444. case -ENOENT:
  445. case -ESHUTDOWN:
  446. return;
  447. /* -EPIPE: should clear the halt */
  448. default: /* error */
  449. goto resubmit;
  450. }
  451. /* we check the link state to report changes */
  452. if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
  453. if (act_state)
  454. netif_carrier_on(kaweth->net);
  455. else
  456. netif_carrier_off(kaweth->net);
  457. kaweth->linkstate = act_state;
  458. }
  459. resubmit:
  460. kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
  461. }
  462. static void kaweth_resubmit_tl(struct work_struct *work)
  463. {
  464. struct kaweth_device *kaweth =
  465. container_of(work, struct kaweth_device, lowmem_work.work);
  466. if (IS_BLOCKED(kaweth->status))
  467. return;
  468. if (kaweth->suspend_lowmem_rx)
  469. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  470. if (kaweth->suspend_lowmem_ctrl)
  471. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  472. }
  473. /****************************************************************
  474. * kaweth_resubmit_rx_urb
  475. ****************************************************************/
  476. static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
  477. gfp_t mem_flags)
  478. {
  479. int result;
  480. usb_fill_bulk_urb(kaweth->rx_urb,
  481. kaweth->dev,
  482. usb_rcvbulkpipe(kaweth->dev, 1),
  483. kaweth->rx_buf,
  484. KAWETH_BUF_SIZE,
  485. kaweth_usb_receive,
  486. kaweth);
  487. kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  488. kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
  489. if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
  490. if (result == -ENOMEM) {
  491. kaweth->suspend_lowmem_rx = 1;
  492. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  493. }
  494. dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
  495. result);
  496. } else {
  497. kaweth->suspend_lowmem_rx = 0;
  498. }
  499. return result;
  500. }
  501. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
  502. /****************************************************************
  503. * kaweth_usb_receive
  504. ****************************************************************/
  505. static void kaweth_usb_receive(struct urb *urb)
  506. {
  507. struct device *dev = &urb->dev->dev;
  508. struct kaweth_device *kaweth = urb->context;
  509. struct net_device *net = kaweth->net;
  510. int status = urb->status;
  511. int count = urb->actual_length;
  512. int count2 = urb->transfer_buffer_length;
  513. __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
  514. struct sk_buff *skb;
  515. if (unlikely(status == -EPIPE)) {
  516. kaweth->stats.rx_errors++;
  517. kaweth->end = 1;
  518. wake_up(&kaweth->term_wait);
  519. dev_dbg(dev, "Status was -EPIPE.\n");
  520. return;
  521. }
  522. if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
  523. /* we are killed - set a flag and wake the disconnect handler */
  524. kaweth->end = 1;
  525. wake_up(&kaweth->term_wait);
  526. dev_dbg(dev, "Status was -ECONNRESET or -ESHUTDOWN.\n");
  527. return;
  528. }
  529. if (unlikely(status == -EPROTO || status == -ETIME ||
  530. status == -EILSEQ)) {
  531. kaweth->stats.rx_errors++;
  532. dev_dbg(dev, "Status was -EPROTO, -ETIME, or -EILSEQ.\n");
  533. return;
  534. }
  535. if (unlikely(status == -EOVERFLOW)) {
  536. kaweth->stats.rx_errors++;
  537. dev_dbg(dev, "Status was -EOVERFLOW.\n");
  538. }
  539. spin_lock(&kaweth->device_lock);
  540. if (IS_BLOCKED(kaweth->status)) {
  541. spin_unlock(&kaweth->device_lock);
  542. return;
  543. }
  544. spin_unlock(&kaweth->device_lock);
  545. if(status && status != -EREMOTEIO && count != 1) {
  546. dev_err(&kaweth->intf->dev,
  547. "%s RX status: %d count: %d packet_len: %d\n",
  548. net->name, status, count, (int)pkt_len);
  549. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  550. return;
  551. }
  552. if(kaweth->net && (count > 2)) {
  553. if(pkt_len > (count - 2)) {
  554. dev_err(&kaweth->intf->dev,
  555. "Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
  556. pkt_len, count);
  557. dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
  558. pkt_len & 2047);
  559. dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
  560. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  561. return;
  562. }
  563. if(!(skb = dev_alloc_skb(pkt_len+2))) {
  564. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  565. return;
  566. }
  567. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  568. skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
  569. skb_put(skb, pkt_len);
  570. skb->protocol = eth_type_trans(skb, net);
  571. netif_rx(skb);
  572. kaweth->stats.rx_packets++;
  573. kaweth->stats.rx_bytes += pkt_len;
  574. }
  575. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  576. }
  577. /****************************************************************
  578. * kaweth_open
  579. ****************************************************************/
  580. static int kaweth_open(struct net_device *net)
  581. {
  582. struct kaweth_device *kaweth = netdev_priv(net);
  583. int res;
  584. netdev_dbg(kaweth->net, "Opening network device.\n");
  585. res = usb_autopm_get_interface(kaweth->intf);
  586. if (res) {
  587. dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
  588. return -EIO;
  589. }
  590. res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
  591. if (res)
  592. goto err_out;
  593. usb_fill_int_urb(
  594. kaweth->irq_urb,
  595. kaweth->dev,
  596. usb_rcvintpipe(kaweth->dev, 3),
  597. kaweth->intbuffer,
  598. INTBUFFERSIZE,
  599. int_callback,
  600. kaweth,
  601. 250); /* overriding the descriptor */
  602. kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
  603. kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  604. res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
  605. if (res) {
  606. usb_kill_urb(kaweth->rx_urb);
  607. goto err_out;
  608. }
  609. kaweth->opened = 1;
  610. netif_start_queue(net);
  611. kaweth_async_set_rx_mode(kaweth);
  612. return 0;
  613. err_out:
  614. usb_autopm_put_interface(kaweth->intf);
  615. return -EIO;
  616. }
  617. /****************************************************************
  618. * kaweth_kill_urbs
  619. ****************************************************************/
  620. static void kaweth_kill_urbs(struct kaweth_device *kaweth)
  621. {
  622. usb_kill_urb(kaweth->irq_urb);
  623. usb_kill_urb(kaweth->rx_urb);
  624. usb_kill_urb(kaweth->tx_urb);
  625. cancel_delayed_work_sync(&kaweth->lowmem_work);
  626. /* a scheduled work may have resubmitted,
  627. we hit them again */
  628. usb_kill_urb(kaweth->irq_urb);
  629. usb_kill_urb(kaweth->rx_urb);
  630. }
  631. /****************************************************************
  632. * kaweth_close
  633. ****************************************************************/
  634. static int kaweth_close(struct net_device *net)
  635. {
  636. struct kaweth_device *kaweth = netdev_priv(net);
  637. netif_stop_queue(net);
  638. kaweth->opened = 0;
  639. kaweth->status |= KAWETH_STATUS_CLOSING;
  640. kaweth_kill_urbs(kaweth);
  641. kaweth->status &= ~KAWETH_STATUS_CLOSING;
  642. usb_autopm_put_interface(kaweth->intf);
  643. return 0;
  644. }
  645. static u32 kaweth_get_link(struct net_device *dev)
  646. {
  647. struct kaweth_device *kaweth = netdev_priv(dev);
  648. return kaweth->linkstate;
  649. }
  650. static const struct ethtool_ops ops = {
  651. .get_link = kaweth_get_link
  652. };
  653. /****************************************************************
  654. * kaweth_usb_transmit_complete
  655. ****************************************************************/
  656. static void kaweth_usb_transmit_complete(struct urb *urb)
  657. {
  658. struct kaweth_device *kaweth = urb->context;
  659. struct sk_buff *skb = kaweth->tx_skb;
  660. int status = urb->status;
  661. if (unlikely(status != 0))
  662. if (status != -ENOENT)
  663. dev_dbg(&urb->dev->dev, "%s: TX status %d.\n",
  664. kaweth->net->name, status);
  665. netif_wake_queue(kaweth->net);
  666. dev_kfree_skb_irq(skb);
  667. }
  668. /****************************************************************
  669. * kaweth_start_xmit
  670. ****************************************************************/
  671. static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
  672. struct net_device *net)
  673. {
  674. struct kaweth_device *kaweth = netdev_priv(net);
  675. __le16 *private_header;
  676. int res;
  677. spin_lock_irq(&kaweth->device_lock);
  678. kaweth_async_set_rx_mode(kaweth);
  679. netif_stop_queue(net);
  680. if (IS_BLOCKED(kaweth->status)) {
  681. goto skip;
  682. }
  683. /* We now decide whether we can put our special header into the sk_buff */
  684. if (skb_cloned(skb) || skb_headroom(skb) < 2) {
  685. /* no such luck - we make our own */
  686. struct sk_buff *copied_skb;
  687. copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
  688. dev_kfree_skb_irq(skb);
  689. skb = copied_skb;
  690. if (!copied_skb) {
  691. kaweth->stats.tx_errors++;
  692. netif_start_queue(net);
  693. spin_unlock_irq(&kaweth->device_lock);
  694. return NETDEV_TX_OK;
  695. }
  696. }
  697. private_header = (__le16 *)__skb_push(skb, 2);
  698. *private_header = cpu_to_le16(skb->len-2);
  699. kaweth->tx_skb = skb;
  700. usb_fill_bulk_urb(kaweth->tx_urb,
  701. kaweth->dev,
  702. usb_sndbulkpipe(kaweth->dev, 2),
  703. private_header,
  704. skb->len,
  705. kaweth_usb_transmit_complete,
  706. kaweth);
  707. kaweth->end = 0;
  708. if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
  709. {
  710. dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
  711. skip:
  712. kaweth->stats.tx_errors++;
  713. netif_start_queue(net);
  714. dev_kfree_skb_irq(skb);
  715. }
  716. else
  717. {
  718. kaweth->stats.tx_packets++;
  719. kaweth->stats.tx_bytes += skb->len;
  720. }
  721. spin_unlock_irq(&kaweth->device_lock);
  722. return NETDEV_TX_OK;
  723. }
  724. /****************************************************************
  725. * kaweth_set_rx_mode
  726. ****************************************************************/
  727. static void kaweth_set_rx_mode(struct net_device *net)
  728. {
  729. struct kaweth_device *kaweth = netdev_priv(net);
  730. __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
  731. KAWETH_PACKET_FILTER_BROADCAST |
  732. KAWETH_PACKET_FILTER_MULTICAST;
  733. netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
  734. netif_stop_queue(net);
  735. if (net->flags & IFF_PROMISC) {
  736. packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
  737. }
  738. else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
  739. packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
  740. }
  741. kaweth->packet_filter_bitmap = packet_filter_bitmap;
  742. netif_wake_queue(net);
  743. }
  744. /****************************************************************
  745. * kaweth_async_set_rx_mode
  746. ****************************************************************/
  747. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
  748. {
  749. int result;
  750. __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
  751. kaweth->packet_filter_bitmap = 0;
  752. if (packet_filter_bitmap == 0)
  753. return;
  754. if (in_interrupt())
  755. return;
  756. result = kaweth_control(kaweth,
  757. usb_sndctrlpipe(kaweth->dev, 0),
  758. KAWETH_COMMAND_SET_PACKET_FILTER,
  759. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  760. packet_filter_bitmap,
  761. 0,
  762. (void *)&kaweth->scratch,
  763. 0,
  764. KAWETH_CONTROL_TIMEOUT);
  765. if(result < 0) {
  766. dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
  767. result);
  768. }
  769. else {
  770. netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
  771. packet_filter_bitmap);
  772. }
  773. }
  774. /****************************************************************
  775. * kaweth_netdev_stats
  776. ****************************************************************/
  777. static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
  778. {
  779. struct kaweth_device *kaweth = netdev_priv(dev);
  780. return &kaweth->stats;
  781. }
  782. /****************************************************************
  783. * kaweth_tx_timeout
  784. ****************************************************************/
  785. static void kaweth_tx_timeout(struct net_device *net)
  786. {
  787. struct kaweth_device *kaweth = netdev_priv(net);
  788. dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
  789. kaweth->stats.tx_errors++;
  790. net->trans_start = jiffies;
  791. usb_unlink_urb(kaweth->tx_urb);
  792. }
  793. /****************************************************************
  794. * kaweth_suspend
  795. ****************************************************************/
  796. static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
  797. {
  798. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  799. unsigned long flags;
  800. dev_dbg(&intf->dev, "Suspending device\n");
  801. spin_lock_irqsave(&kaweth->device_lock, flags);
  802. kaweth->status |= KAWETH_STATUS_SUSPENDING;
  803. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  804. kaweth_kill_urbs(kaweth);
  805. return 0;
  806. }
  807. /****************************************************************
  808. * kaweth_resume
  809. ****************************************************************/
  810. static int kaweth_resume(struct usb_interface *intf)
  811. {
  812. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  813. unsigned long flags;
  814. dev_dbg(&intf->dev, "Resuming device\n");
  815. spin_lock_irqsave(&kaweth->device_lock, flags);
  816. kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
  817. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  818. if (!kaweth->opened)
  819. return 0;
  820. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  821. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  822. return 0;
  823. }
  824. /****************************************************************
  825. * kaweth_probe
  826. ****************************************************************/
  827. static const struct net_device_ops kaweth_netdev_ops = {
  828. .ndo_open = kaweth_open,
  829. .ndo_stop = kaweth_close,
  830. .ndo_start_xmit = kaweth_start_xmit,
  831. .ndo_tx_timeout = kaweth_tx_timeout,
  832. .ndo_set_rx_mode = kaweth_set_rx_mode,
  833. .ndo_get_stats = kaweth_netdev_stats,
  834. .ndo_change_mtu = eth_change_mtu,
  835. .ndo_set_mac_address = eth_mac_addr,
  836. .ndo_validate_addr = eth_validate_addr,
  837. };
  838. static int kaweth_probe(
  839. struct usb_interface *intf,
  840. const struct usb_device_id *id /* from id_table */
  841. )
  842. {
  843. struct device *dev = &intf->dev;
  844. struct usb_device *udev = interface_to_usbdev(intf);
  845. struct kaweth_device *kaweth;
  846. struct net_device *netdev;
  847. const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  848. int result = 0;
  849. dev_dbg(dev,
  850. "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
  851. udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
  852. le16_to_cpu(udev->descriptor.idProduct),
  853. le16_to_cpu(udev->descriptor.bcdDevice));
  854. dev_dbg(dev, "Device at %p\n", udev);
  855. dev_dbg(dev, "Descriptor length: %x type: %x\n",
  856. (int)udev->descriptor.bLength,
  857. (int)udev->descriptor.bDescriptorType);
  858. netdev = alloc_etherdev(sizeof(*kaweth));
  859. if (!netdev)
  860. return -ENOMEM;
  861. kaweth = netdev_priv(netdev);
  862. kaweth->dev = udev;
  863. kaweth->net = netdev;
  864. spin_lock_init(&kaweth->device_lock);
  865. init_waitqueue_head(&kaweth->term_wait);
  866. dev_dbg(dev, "Resetting.\n");
  867. kaweth_reset(kaweth);
  868. /*
  869. * If high byte of bcdDevice is nonzero, firmware is already
  870. * downloaded. Don't try to do it again, or we'll hang the device.
  871. */
  872. if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
  873. dev_info(dev, "Firmware present in device.\n");
  874. } else {
  875. /* Download the firmware */
  876. dev_info(dev, "Downloading firmware...\n");
  877. kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
  878. if ((result = kaweth_download_firmware(kaweth,
  879. "kaweth/new_code.bin",
  880. 100,
  881. 2)) < 0) {
  882. dev_err(dev, "Error downloading firmware (%d)\n",
  883. result);
  884. goto err_fw;
  885. }
  886. if ((result = kaweth_download_firmware(kaweth,
  887. "kaweth/new_code_fix.bin",
  888. 100,
  889. 3)) < 0) {
  890. dev_err(dev, "Error downloading firmware fix (%d)\n",
  891. result);
  892. goto err_fw;
  893. }
  894. if ((result = kaweth_download_firmware(kaweth,
  895. "kaweth/trigger_code.bin",
  896. 126,
  897. 2)) < 0) {
  898. dev_err(dev, "Error downloading trigger code (%d)\n",
  899. result);
  900. goto err_fw;
  901. }
  902. if ((result = kaweth_download_firmware(kaweth,
  903. "kaweth/trigger_code_fix.bin",
  904. 126,
  905. 3)) < 0) {
  906. dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
  907. goto err_fw;
  908. }
  909. if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
  910. dev_err(dev, "Error triggering firmware (%d)\n", result);
  911. goto err_fw;
  912. }
  913. /* Device will now disappear for a moment... */
  914. dev_info(dev, "Firmware loaded. I'll be back...\n");
  915. err_fw:
  916. free_page((unsigned long)kaweth->firmware_buf);
  917. free_netdev(netdev);
  918. return -EIO;
  919. }
  920. result = kaweth_read_configuration(kaweth);
  921. if(result < 0) {
  922. dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
  923. goto err_free_netdev;
  924. }
  925. dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
  926. dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
  927. dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
  928. dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
  929. if(!memcmp(&kaweth->configuration.hw_addr,
  930. &bcast_addr,
  931. sizeof(bcast_addr))) {
  932. dev_err(dev, "Firmware not functioning properly, no net device created\n");
  933. goto err_free_netdev;
  934. }
  935. if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
  936. dev_dbg(dev, "Error setting URB size\n");
  937. goto err_free_netdev;
  938. }
  939. if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
  940. dev_err(dev, "Error setting SOFS wait\n");
  941. goto err_free_netdev;
  942. }
  943. result = kaweth_set_receive_filter(kaweth,
  944. KAWETH_PACKET_FILTER_DIRECTED |
  945. KAWETH_PACKET_FILTER_BROADCAST |
  946. KAWETH_PACKET_FILTER_MULTICAST);
  947. if(result < 0) {
  948. dev_err(dev, "Error setting receive filter\n");
  949. goto err_free_netdev;
  950. }
  951. dev_dbg(dev, "Initializing net device.\n");
  952. kaweth->intf = intf;
  953. kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  954. if (!kaweth->tx_urb)
  955. goto err_free_netdev;
  956. kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  957. if (!kaweth->rx_urb)
  958. goto err_only_tx;
  959. kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  960. if (!kaweth->irq_urb)
  961. goto err_tx_and_rx;
  962. kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
  963. INTBUFFERSIZE,
  964. GFP_KERNEL,
  965. &kaweth->intbufferhandle);
  966. if (!kaweth->intbuffer)
  967. goto err_tx_and_rx_and_irq;
  968. kaweth->rx_buf = usb_alloc_coherent( kaweth->dev,
  969. KAWETH_BUF_SIZE,
  970. GFP_KERNEL,
  971. &kaweth->rxbufferhandle);
  972. if (!kaweth->rx_buf)
  973. goto err_all_but_rxbuf;
  974. memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
  975. memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
  976. sizeof(kaweth->configuration.hw_addr));
  977. netdev->netdev_ops = &kaweth_netdev_ops;
  978. netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
  979. netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
  980. SET_ETHTOOL_OPS(netdev, &ops);
  981. /* kaweth is zeroed as part of alloc_netdev */
  982. INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
  983. usb_set_intfdata(intf, kaweth);
  984. #if 0
  985. // dma_supported() is deeply broken on almost all architectures
  986. if (dma_supported (dev, 0xffffffffffffffffULL))
  987. kaweth->net->features |= NETIF_F_HIGHDMA;
  988. #endif
  989. SET_NETDEV_DEV(netdev, dev);
  990. if (register_netdev(netdev) != 0) {
  991. dev_err(dev, "Error registering netdev.\n");
  992. goto err_intfdata;
  993. }
  994. dev_info(dev, "kaweth interface created at %s\n",
  995. kaweth->net->name);
  996. dev_dbg(dev, "Kaweth probe returning.\n");
  997. return 0;
  998. err_intfdata:
  999. usb_set_intfdata(intf, NULL);
  1000. usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  1001. err_all_but_rxbuf:
  1002. usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  1003. err_tx_and_rx_and_irq:
  1004. usb_free_urb(kaweth->irq_urb);
  1005. err_tx_and_rx:
  1006. usb_free_urb(kaweth->rx_urb);
  1007. err_only_tx:
  1008. usb_free_urb(kaweth->tx_urb);
  1009. err_free_netdev:
  1010. free_netdev(netdev);
  1011. return -EIO;
  1012. }
  1013. /****************************************************************
  1014. * kaweth_disconnect
  1015. ****************************************************************/
  1016. static void kaweth_disconnect(struct usb_interface *intf)
  1017. {
  1018. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  1019. struct net_device *netdev;
  1020. dev_info(&intf->dev, "Unregistering\n");
  1021. usb_set_intfdata(intf, NULL);
  1022. if (!kaweth) {
  1023. dev_warn(&intf->dev, "unregistering non-existent device\n");
  1024. return;
  1025. }
  1026. netdev = kaweth->net;
  1027. netdev_dbg(kaweth->net, "Unregistering net device\n");
  1028. unregister_netdev(netdev);
  1029. usb_free_urb(kaweth->rx_urb);
  1030. usb_free_urb(kaweth->tx_urb);
  1031. usb_free_urb(kaweth->irq_urb);
  1032. usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  1033. usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  1034. free_netdev(netdev);
  1035. }
  1036. // FIXME this completion stuff is a modified clone of
  1037. // an OLD version of some stuff in usb.c ...
  1038. struct usb_api_data {
  1039. wait_queue_head_t wqh;
  1040. int done;
  1041. };
  1042. /*-------------------------------------------------------------------*
  1043. * completion handler for compatibility wrappers (sync control/bulk) *
  1044. *-------------------------------------------------------------------*/
  1045. static void usb_api_blocking_completion(struct urb *urb)
  1046. {
  1047. struct usb_api_data *awd = (struct usb_api_data *)urb->context;
  1048. awd->done=1;
  1049. wake_up(&awd->wqh);
  1050. }
  1051. /*-------------------------------------------------------------------*
  1052. * COMPATIBILITY STUFF *
  1053. *-------------------------------------------------------------------*/
  1054. // Starts urb and waits for completion or timeout
  1055. static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
  1056. {
  1057. struct usb_api_data awd;
  1058. int status;
  1059. init_waitqueue_head(&awd.wqh);
  1060. awd.done = 0;
  1061. urb->context = &awd;
  1062. status = usb_submit_urb(urb, GFP_NOIO);
  1063. if (status) {
  1064. // something went wrong
  1065. usb_free_urb(urb);
  1066. return status;
  1067. }
  1068. if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
  1069. // timeout
  1070. dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
  1071. usb_kill_urb(urb); // remove urb safely
  1072. status = -ETIMEDOUT;
  1073. }
  1074. else {
  1075. status = urb->status;
  1076. }
  1077. if (actual_length) {
  1078. *actual_length = urb->actual_length;
  1079. }
  1080. usb_free_urb(urb);
  1081. return status;
  1082. }
  1083. /*-------------------------------------------------------------------*/
  1084. // returns status (negative) or length (positive)
  1085. static int kaweth_internal_control_msg(struct usb_device *usb_dev,
  1086. unsigned int pipe,
  1087. struct usb_ctrlrequest *cmd, void *data,
  1088. int len, int timeout)
  1089. {
  1090. struct urb *urb;
  1091. int retv;
  1092. int length = 0; /* shut up GCC */
  1093. urb = usb_alloc_urb(0, GFP_ATOMIC);
  1094. if (!urb)
  1095. return -ENOMEM;
  1096. usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
  1097. len, usb_api_blocking_completion, NULL);
  1098. retv = usb_start_wait_urb(urb, timeout, &length);
  1099. if (retv < 0) {
  1100. return retv;
  1101. }
  1102. else {
  1103. return length;
  1104. }
  1105. }
  1106. module_usb_driver(kaweth_driver);