kaweth.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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(0x0846, 0x1001) }, /* NetGear EA-101 */
  132. { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
  133. { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
  134. { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
  135. { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
  136. { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
  137. { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
  138. { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
  139. { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
  140. { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
  141. { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
  142. { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
  143. { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
  144. { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
  145. { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
  146. { USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
  147. { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
  148. {} /* Null terminator */
  149. };
  150. MODULE_DEVICE_TABLE (usb, usb_klsi_table);
  151. /****************************************************************
  152. * kaweth_driver
  153. ****************************************************************/
  154. static struct usb_driver kaweth_driver = {
  155. .name = driver_name,
  156. .probe = kaweth_probe,
  157. .disconnect = kaweth_disconnect,
  158. .suspend = kaweth_suspend,
  159. .resume = kaweth_resume,
  160. .id_table = usb_klsi_table,
  161. .supports_autosuspend = 1,
  162. };
  163. typedef __u8 eth_addr_t[6];
  164. /****************************************************************
  165. * usb_eth_dev
  166. ****************************************************************/
  167. struct usb_eth_dev {
  168. char *name;
  169. __u16 vendor;
  170. __u16 device;
  171. void *pdata;
  172. };
  173. /****************************************************************
  174. * kaweth_ethernet_configuration
  175. * Refer Table 8
  176. ****************************************************************/
  177. struct kaweth_ethernet_configuration
  178. {
  179. __u8 size;
  180. __u8 reserved1;
  181. __u8 reserved2;
  182. eth_addr_t hw_addr;
  183. __u32 statistics_mask;
  184. __le16 segment_size;
  185. __u16 max_multicast_filters;
  186. __u8 reserved3;
  187. } __attribute__ ((packed));
  188. /****************************************************************
  189. * kaweth_device
  190. ****************************************************************/
  191. struct kaweth_device
  192. {
  193. spinlock_t device_lock;
  194. __u32 status;
  195. int end;
  196. int suspend_lowmem_rx;
  197. int suspend_lowmem_ctrl;
  198. int linkstate;
  199. int opened;
  200. struct delayed_work lowmem_work;
  201. struct usb_device *dev;
  202. struct usb_interface *intf;
  203. struct net_device *net;
  204. wait_queue_head_t term_wait;
  205. struct urb *rx_urb;
  206. struct urb *tx_urb;
  207. struct urb *irq_urb;
  208. dma_addr_t intbufferhandle;
  209. __u8 *intbuffer;
  210. dma_addr_t rxbufferhandle;
  211. __u8 *rx_buf;
  212. struct sk_buff *tx_skb;
  213. __u8 *firmware_buf;
  214. __u8 scratch[KAWETH_SCRATCH_SIZE];
  215. __u16 packet_filter_bitmap;
  216. struct kaweth_ethernet_configuration configuration;
  217. struct net_device_stats stats;
  218. };
  219. /****************************************************************
  220. * kaweth_control
  221. ****************************************************************/
  222. static int kaweth_control(struct kaweth_device *kaweth,
  223. unsigned int pipe,
  224. __u8 request,
  225. __u8 requesttype,
  226. __u16 value,
  227. __u16 index,
  228. void *data,
  229. __u16 size,
  230. int timeout)
  231. {
  232. struct usb_ctrlrequest *dr;
  233. int retval;
  234. dbg("kaweth_control()");
  235. if(in_interrupt()) {
  236. dbg("in_interrupt()");
  237. return -EBUSY;
  238. }
  239. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  240. if (!dr) {
  241. dbg("kmalloc() failed");
  242. return -ENOMEM;
  243. }
  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. dbg("Reading kaweth configuration");
  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. dbg("Setting URB size to %d", (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. dbg("Set SOFS wait to %d", (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. dbg("Set receive filter to %d", (unsigned)receive_filter);
  320. retval = kaweth_control(kaweth,
  321. usb_sndctrlpipe(kaweth->dev, 0),
  322. KAWETH_COMMAND_SET_PACKET_FILTER,
  323. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  324. receive_filter,
  325. 0,
  326. (void *)&kaweth->scratch,
  327. 0,
  328. KAWETH_CONTROL_TIMEOUT);
  329. return retval;
  330. }
  331. /****************************************************************
  332. * kaweth_download_firmware
  333. ****************************************************************/
  334. static int kaweth_download_firmware(struct kaweth_device *kaweth,
  335. const char *fwname,
  336. __u8 interrupt,
  337. __u8 type)
  338. {
  339. const struct firmware *fw;
  340. int data_len;
  341. int ret;
  342. ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
  343. if (ret) {
  344. err("Firmware request failed\n");
  345. return ret;
  346. }
  347. if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
  348. err("Firmware too big: %zu", fw->size);
  349. return -ENOSPC;
  350. }
  351. data_len = fw->size;
  352. memcpy(kaweth->firmware_buf, fw->data, fw->size);
  353. release_firmware(fw);
  354. kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
  355. kaweth->firmware_buf[3] = data_len >> 8;
  356. kaweth->firmware_buf[4] = type;
  357. kaweth->firmware_buf[5] = interrupt;
  358. dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
  359. kaweth->firmware_buf[2]);
  360. dbg("Downloading firmware at %p to kaweth device at %p",
  361. fw->data, kaweth);
  362. dbg("Firmware length: %d", data_len);
  363. return kaweth_control(kaweth,
  364. usb_sndctrlpipe(kaweth->dev, 0),
  365. KAWETH_COMMAND_SCAN,
  366. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  367. 0,
  368. 0,
  369. (void *)kaweth->firmware_buf,
  370. data_len,
  371. KAWETH_CONTROL_TIMEOUT);
  372. }
  373. /****************************************************************
  374. * kaweth_trigger_firmware
  375. ****************************************************************/
  376. static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
  377. __u8 interrupt)
  378. {
  379. kaweth->firmware_buf[0] = 0xB6;
  380. kaweth->firmware_buf[1] = 0xC3;
  381. kaweth->firmware_buf[2] = 0x01;
  382. kaweth->firmware_buf[3] = 0x00;
  383. kaweth->firmware_buf[4] = 0x06;
  384. kaweth->firmware_buf[5] = interrupt;
  385. kaweth->firmware_buf[6] = 0x00;
  386. kaweth->firmware_buf[7] = 0x00;
  387. dbg("Triggering firmware");
  388. return kaweth_control(kaweth,
  389. usb_sndctrlpipe(kaweth->dev, 0),
  390. KAWETH_COMMAND_SCAN,
  391. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  392. 0,
  393. 0,
  394. (void *)kaweth->firmware_buf,
  395. 8,
  396. KAWETH_CONTROL_TIMEOUT);
  397. }
  398. /****************************************************************
  399. * kaweth_reset
  400. ****************************************************************/
  401. static int kaweth_reset(struct kaweth_device *kaweth)
  402. {
  403. int result;
  404. dbg("kaweth_reset(%p)", kaweth);
  405. result = kaweth_control(kaweth,
  406. usb_sndctrlpipe(kaweth->dev, 0),
  407. USB_REQ_SET_CONFIGURATION,
  408. 0,
  409. kaweth->dev->config[0].desc.bConfigurationValue,
  410. 0,
  411. NULL,
  412. 0,
  413. KAWETH_CONTROL_TIMEOUT);
  414. mdelay(10);
  415. dbg("kaweth_reset() returns %d.",result);
  416. return result;
  417. }
  418. static void kaweth_usb_receive(struct urb *);
  419. static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
  420. /****************************************************************
  421. int_callback
  422. *****************************************************************/
  423. static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
  424. {
  425. int status;
  426. status = usb_submit_urb (kaweth->irq_urb, mf);
  427. if (unlikely(status == -ENOMEM)) {
  428. kaweth->suspend_lowmem_ctrl = 1;
  429. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  430. } else {
  431. kaweth->suspend_lowmem_ctrl = 0;
  432. }
  433. if (status)
  434. err ("can't resubmit intr, %s-%s, status %d",
  435. kaweth->dev->bus->bus_name,
  436. kaweth->dev->devpath, status);
  437. }
  438. static void int_callback(struct urb *u)
  439. {
  440. struct kaweth_device *kaweth = u->context;
  441. int act_state;
  442. int status = u->status;
  443. switch (status) {
  444. case 0: /* success */
  445. break;
  446. case -ECONNRESET: /* unlink */
  447. case -ENOENT:
  448. case -ESHUTDOWN:
  449. return;
  450. /* -EPIPE: should clear the halt */
  451. default: /* error */
  452. goto resubmit;
  453. }
  454. /* we check the link state to report changes */
  455. if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
  456. if (act_state)
  457. netif_carrier_on(kaweth->net);
  458. else
  459. netif_carrier_off(kaweth->net);
  460. kaweth->linkstate = act_state;
  461. }
  462. resubmit:
  463. kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
  464. }
  465. static void kaweth_resubmit_tl(struct work_struct *work)
  466. {
  467. struct kaweth_device *kaweth =
  468. container_of(work, struct kaweth_device, lowmem_work.work);
  469. if (IS_BLOCKED(kaweth->status))
  470. return;
  471. if (kaweth->suspend_lowmem_rx)
  472. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  473. if (kaweth->suspend_lowmem_ctrl)
  474. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  475. }
  476. /****************************************************************
  477. * kaweth_resubmit_rx_urb
  478. ****************************************************************/
  479. static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
  480. gfp_t mem_flags)
  481. {
  482. int result;
  483. usb_fill_bulk_urb(kaweth->rx_urb,
  484. kaweth->dev,
  485. usb_rcvbulkpipe(kaweth->dev, 1),
  486. kaweth->rx_buf,
  487. KAWETH_BUF_SIZE,
  488. kaweth_usb_receive,
  489. kaweth);
  490. kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  491. kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
  492. if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
  493. if (result == -ENOMEM) {
  494. kaweth->suspend_lowmem_rx = 1;
  495. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  496. }
  497. err("resubmitting rx_urb %d failed", result);
  498. } else {
  499. kaweth->suspend_lowmem_rx = 0;
  500. }
  501. return result;
  502. }
  503. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
  504. /****************************************************************
  505. * kaweth_usb_receive
  506. ****************************************************************/
  507. static void kaweth_usb_receive(struct urb *urb)
  508. {
  509. struct kaweth_device *kaweth = urb->context;
  510. struct net_device *net = kaweth->net;
  511. int status = urb->status;
  512. int count = urb->actual_length;
  513. int count2 = urb->transfer_buffer_length;
  514. __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
  515. struct sk_buff *skb;
  516. if (unlikely(status == -EPIPE)) {
  517. kaweth->stats.rx_errors++;
  518. kaweth->end = 1;
  519. wake_up(&kaweth->term_wait);
  520. dbg("Status was -EPIPE.");
  521. return;
  522. }
  523. if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
  524. /* we are killed - set a flag and wake the disconnect handler */
  525. kaweth->end = 1;
  526. wake_up(&kaweth->term_wait);
  527. dbg("Status was -ECONNRESET or -ESHUTDOWN.");
  528. return;
  529. }
  530. if (unlikely(status == -EPROTO || status == -ETIME ||
  531. status == -EILSEQ)) {
  532. kaweth->stats.rx_errors++;
  533. dbg("Status was -EPROTO, -ETIME, or -EILSEQ.");
  534. return;
  535. }
  536. if (unlikely(status == -EOVERFLOW)) {
  537. kaweth->stats.rx_errors++;
  538. dbg("Status was -EOVERFLOW.");
  539. }
  540. spin_lock(&kaweth->device_lock);
  541. if (IS_BLOCKED(kaweth->status)) {
  542. spin_unlock(&kaweth->device_lock);
  543. return;
  544. }
  545. spin_unlock(&kaweth->device_lock);
  546. if(status && status != -EREMOTEIO && count != 1) {
  547. err("%s RX status: %d count: %d packet_len: %d",
  548. net->name,
  549. status,
  550. count,
  551. (int)pkt_len);
  552. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  553. return;
  554. }
  555. if(kaweth->net && (count > 2)) {
  556. if(pkt_len > (count - 2)) {
  557. err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
  558. err("Packet len & 2047: %x", pkt_len & 2047);
  559. err("Count 2: %x", 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. dbg("Opening network device.");
  585. res = usb_autopm_get_interface(kaweth->intf);
  586. if (res) {
  587. err("Interface cannot be resumed.");
  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_enable(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_enable(kaweth->intf);
  643. return 0;
  644. }
  645. static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  646. {
  647. struct kaweth_device *kaweth = netdev_priv(dev);
  648. strlcpy(info->driver, driver_name, sizeof(info->driver));
  649. usb_make_path(kaweth->dev, info->bus_info, sizeof (info->bus_info));
  650. }
  651. static u32 kaweth_get_link(struct net_device *dev)
  652. {
  653. struct kaweth_device *kaweth = netdev_priv(dev);
  654. return kaweth->linkstate;
  655. }
  656. static const struct ethtool_ops ops = {
  657. .get_drvinfo = kaweth_get_drvinfo,
  658. .get_link = kaweth_get_link
  659. };
  660. /****************************************************************
  661. * kaweth_usb_transmit_complete
  662. ****************************************************************/
  663. static void kaweth_usb_transmit_complete(struct urb *urb)
  664. {
  665. struct kaweth_device *kaweth = urb->context;
  666. struct sk_buff *skb = kaweth->tx_skb;
  667. int status = urb->status;
  668. if (unlikely(status != 0))
  669. if (status != -ENOENT)
  670. dbg("%s: TX status %d.", kaweth->net->name, status);
  671. netif_wake_queue(kaweth->net);
  672. dev_kfree_skb_irq(skb);
  673. }
  674. /****************************************************************
  675. * kaweth_start_xmit
  676. ****************************************************************/
  677. static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
  678. struct net_device *net)
  679. {
  680. struct kaweth_device *kaweth = netdev_priv(net);
  681. __le16 *private_header;
  682. int res;
  683. spin_lock_irq(&kaweth->device_lock);
  684. kaweth_async_set_rx_mode(kaweth);
  685. netif_stop_queue(net);
  686. if (IS_BLOCKED(kaweth->status)) {
  687. goto skip;
  688. }
  689. /* We now decide whether we can put our special header into the sk_buff */
  690. if (skb_cloned(skb) || skb_headroom(skb) < 2) {
  691. /* no such luck - we make our own */
  692. struct sk_buff *copied_skb;
  693. copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
  694. dev_kfree_skb_irq(skb);
  695. skb = copied_skb;
  696. if (!copied_skb) {
  697. kaweth->stats.tx_errors++;
  698. netif_start_queue(net);
  699. spin_unlock_irq(&kaweth->device_lock);
  700. return NETDEV_TX_OK;
  701. }
  702. }
  703. private_header = (__le16 *)__skb_push(skb, 2);
  704. *private_header = cpu_to_le16(skb->len-2);
  705. kaweth->tx_skb = skb;
  706. usb_fill_bulk_urb(kaweth->tx_urb,
  707. kaweth->dev,
  708. usb_sndbulkpipe(kaweth->dev, 2),
  709. private_header,
  710. skb->len,
  711. kaweth_usb_transmit_complete,
  712. kaweth);
  713. kaweth->end = 0;
  714. if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
  715. {
  716. dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
  717. skip:
  718. kaweth->stats.tx_errors++;
  719. netif_start_queue(net);
  720. dev_kfree_skb_irq(skb);
  721. }
  722. else
  723. {
  724. kaweth->stats.tx_packets++;
  725. kaweth->stats.tx_bytes += skb->len;
  726. net->trans_start = jiffies;
  727. }
  728. spin_unlock_irq(&kaweth->device_lock);
  729. return NETDEV_TX_OK;
  730. }
  731. /****************************************************************
  732. * kaweth_set_rx_mode
  733. ****************************************************************/
  734. static void kaweth_set_rx_mode(struct net_device *net)
  735. {
  736. struct kaweth_device *kaweth = netdev_priv(net);
  737. __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
  738. KAWETH_PACKET_FILTER_BROADCAST |
  739. KAWETH_PACKET_FILTER_MULTICAST;
  740. dbg("Setting Rx mode to %d", packet_filter_bitmap);
  741. netif_stop_queue(net);
  742. if (net->flags & IFF_PROMISC) {
  743. packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
  744. }
  745. else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
  746. packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
  747. }
  748. kaweth->packet_filter_bitmap = packet_filter_bitmap;
  749. netif_wake_queue(net);
  750. }
  751. /****************************************************************
  752. * kaweth_async_set_rx_mode
  753. ****************************************************************/
  754. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
  755. {
  756. int result;
  757. __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
  758. kaweth->packet_filter_bitmap = 0;
  759. if (packet_filter_bitmap == 0)
  760. return;
  761. if (in_interrupt())
  762. return;
  763. result = kaweth_control(kaweth,
  764. usb_sndctrlpipe(kaweth->dev, 0),
  765. KAWETH_COMMAND_SET_PACKET_FILTER,
  766. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  767. packet_filter_bitmap,
  768. 0,
  769. (void *)&kaweth->scratch,
  770. 0,
  771. KAWETH_CONTROL_TIMEOUT);
  772. if(result < 0) {
  773. err("Failed to set Rx mode: %d", result);
  774. }
  775. else {
  776. dbg("Set Rx mode to %d", packet_filter_bitmap);
  777. }
  778. }
  779. /****************************************************************
  780. * kaweth_netdev_stats
  781. ****************************************************************/
  782. static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
  783. {
  784. struct kaweth_device *kaweth = netdev_priv(dev);
  785. return &kaweth->stats;
  786. }
  787. /****************************************************************
  788. * kaweth_tx_timeout
  789. ****************************************************************/
  790. static void kaweth_tx_timeout(struct net_device *net)
  791. {
  792. struct kaweth_device *kaweth = netdev_priv(net);
  793. dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
  794. kaweth->stats.tx_errors++;
  795. net->trans_start = jiffies;
  796. usb_unlink_urb(kaweth->tx_urb);
  797. }
  798. /****************************************************************
  799. * kaweth_suspend
  800. ****************************************************************/
  801. static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
  802. {
  803. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  804. unsigned long flags;
  805. dbg("Suspending device");
  806. spin_lock_irqsave(&kaweth->device_lock, flags);
  807. kaweth->status |= KAWETH_STATUS_SUSPENDING;
  808. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  809. kaweth_kill_urbs(kaweth);
  810. return 0;
  811. }
  812. /****************************************************************
  813. * kaweth_resume
  814. ****************************************************************/
  815. static int kaweth_resume(struct usb_interface *intf)
  816. {
  817. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  818. unsigned long flags;
  819. dbg("Resuming device");
  820. spin_lock_irqsave(&kaweth->device_lock, flags);
  821. kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
  822. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  823. if (!kaweth->opened)
  824. return 0;
  825. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  826. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  827. return 0;
  828. }
  829. /****************************************************************
  830. * kaweth_probe
  831. ****************************************************************/
  832. static const struct net_device_ops kaweth_netdev_ops = {
  833. .ndo_open = kaweth_open,
  834. .ndo_stop = kaweth_close,
  835. .ndo_start_xmit = kaweth_start_xmit,
  836. .ndo_tx_timeout = kaweth_tx_timeout,
  837. .ndo_set_multicast_list = kaweth_set_rx_mode,
  838. .ndo_get_stats = kaweth_netdev_stats,
  839. .ndo_change_mtu = eth_change_mtu,
  840. .ndo_set_mac_address = eth_mac_addr,
  841. .ndo_validate_addr = eth_validate_addr,
  842. };
  843. static int kaweth_probe(
  844. struct usb_interface *intf,
  845. const struct usb_device_id *id /* from id_table */
  846. )
  847. {
  848. struct usb_device *dev = interface_to_usbdev(intf);
  849. struct kaweth_device *kaweth;
  850. struct net_device *netdev;
  851. const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  852. int result = 0;
  853. dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
  854. dev->devnum,
  855. le16_to_cpu(dev->descriptor.idVendor),
  856. le16_to_cpu(dev->descriptor.idProduct),
  857. le16_to_cpu(dev->descriptor.bcdDevice));
  858. dbg("Device at %p", dev);
  859. dbg("Descriptor length: %x type: %x",
  860. (int)dev->descriptor.bLength,
  861. (int)dev->descriptor.bDescriptorType);
  862. netdev = alloc_etherdev(sizeof(*kaweth));
  863. if (!netdev)
  864. return -ENOMEM;
  865. kaweth = netdev_priv(netdev);
  866. kaweth->dev = dev;
  867. kaweth->net = netdev;
  868. spin_lock_init(&kaweth->device_lock);
  869. init_waitqueue_head(&kaweth->term_wait);
  870. dbg("Resetting.");
  871. kaweth_reset(kaweth);
  872. /*
  873. * If high byte of bcdDevice is nonzero, firmware is already
  874. * downloaded. Don't try to do it again, or we'll hang the device.
  875. */
  876. if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
  877. dev_info(&intf->dev, "Firmware present in device.\n");
  878. } else {
  879. /* Download the firmware */
  880. dev_info(&intf->dev, "Downloading firmware...\n");
  881. kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
  882. if ((result = kaweth_download_firmware(kaweth,
  883. "kaweth/new_code.bin",
  884. 100,
  885. 2)) < 0) {
  886. err("Error downloading firmware (%d)", result);
  887. goto err_fw;
  888. }
  889. if ((result = kaweth_download_firmware(kaweth,
  890. "kaweth/new_code_fix.bin",
  891. 100,
  892. 3)) < 0) {
  893. err("Error downloading firmware fix (%d)", result);
  894. goto err_fw;
  895. }
  896. if ((result = kaweth_download_firmware(kaweth,
  897. "kaweth/trigger_code.bin",
  898. 126,
  899. 2)) < 0) {
  900. err("Error downloading trigger code (%d)", result);
  901. goto err_fw;
  902. }
  903. if ((result = kaweth_download_firmware(kaweth,
  904. "kaweth/trigger_code_fix.bin",
  905. 126,
  906. 3)) < 0) {
  907. err("Error downloading trigger code fix (%d)", result);
  908. goto err_fw;
  909. }
  910. if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
  911. err("Error triggering firmware (%d)", result);
  912. goto err_fw;
  913. }
  914. /* Device will now disappear for a moment... */
  915. dev_info(&intf->dev, "Firmware loaded. I'll be back...\n");
  916. err_fw:
  917. free_page((unsigned long)kaweth->firmware_buf);
  918. free_netdev(netdev);
  919. return -EIO;
  920. }
  921. result = kaweth_read_configuration(kaweth);
  922. if(result < 0) {
  923. err("Error reading configuration (%d), no net device created", result);
  924. goto err_free_netdev;
  925. }
  926. dev_info(&intf->dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
  927. dev_info(&intf->dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
  928. dev_info(&intf->dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
  929. dev_info(&intf->dev, "Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
  930. (int)kaweth->configuration.hw_addr[0],
  931. (int)kaweth->configuration.hw_addr[1],
  932. (int)kaweth->configuration.hw_addr[2],
  933. (int)kaweth->configuration.hw_addr[3],
  934. (int)kaweth->configuration.hw_addr[4],
  935. (int)kaweth->configuration.hw_addr[5]);
  936. if(!memcmp(&kaweth->configuration.hw_addr,
  937. &bcast_addr,
  938. sizeof(bcast_addr))) {
  939. err("Firmware not functioning properly, no net device created");
  940. goto err_free_netdev;
  941. }
  942. if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
  943. dbg("Error setting URB size");
  944. goto err_free_netdev;
  945. }
  946. if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
  947. err("Error setting SOFS wait");
  948. goto err_free_netdev;
  949. }
  950. result = kaweth_set_receive_filter(kaweth,
  951. KAWETH_PACKET_FILTER_DIRECTED |
  952. KAWETH_PACKET_FILTER_BROADCAST |
  953. KAWETH_PACKET_FILTER_MULTICAST);
  954. if(result < 0) {
  955. err("Error setting receive filter");
  956. goto err_free_netdev;
  957. }
  958. dbg("Initializing net device.");
  959. kaweth->intf = intf;
  960. kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  961. if (!kaweth->tx_urb)
  962. goto err_free_netdev;
  963. kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  964. if (!kaweth->rx_urb)
  965. goto err_only_tx;
  966. kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  967. if (!kaweth->irq_urb)
  968. goto err_tx_and_rx;
  969. kaweth->intbuffer = usb_buffer_alloc( kaweth->dev,
  970. INTBUFFERSIZE,
  971. GFP_KERNEL,
  972. &kaweth->intbufferhandle);
  973. if (!kaweth->intbuffer)
  974. goto err_tx_and_rx_and_irq;
  975. kaweth->rx_buf = usb_buffer_alloc( kaweth->dev,
  976. KAWETH_BUF_SIZE,
  977. GFP_KERNEL,
  978. &kaweth->rxbufferhandle);
  979. if (!kaweth->rx_buf)
  980. goto err_all_but_rxbuf;
  981. memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
  982. memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
  983. sizeof(kaweth->configuration.hw_addr));
  984. netdev->netdev_ops = &kaweth_netdev_ops;
  985. netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
  986. netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
  987. SET_ETHTOOL_OPS(netdev, &ops);
  988. /* kaweth is zeroed as part of alloc_netdev */
  989. INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
  990. usb_set_intfdata(intf, kaweth);
  991. #if 0
  992. // dma_supported() is deeply broken on almost all architectures
  993. if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
  994. kaweth->net->features |= NETIF_F_HIGHDMA;
  995. #endif
  996. SET_NETDEV_DEV(netdev, &intf->dev);
  997. if (register_netdev(netdev) != 0) {
  998. err("Error registering netdev.");
  999. goto err_intfdata;
  1000. }
  1001. dev_info(&intf->dev, "kaweth interface created at %s\n",
  1002. kaweth->net->name);
  1003. dbg("Kaweth probe returning.");
  1004. return 0;
  1005. err_intfdata:
  1006. usb_set_intfdata(intf, NULL);
  1007. usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  1008. err_all_but_rxbuf:
  1009. usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  1010. err_tx_and_rx_and_irq:
  1011. usb_free_urb(kaweth->irq_urb);
  1012. err_tx_and_rx:
  1013. usb_free_urb(kaweth->rx_urb);
  1014. err_only_tx:
  1015. usb_free_urb(kaweth->tx_urb);
  1016. err_free_netdev:
  1017. free_netdev(netdev);
  1018. return -EIO;
  1019. }
  1020. /****************************************************************
  1021. * kaweth_disconnect
  1022. ****************************************************************/
  1023. static void kaweth_disconnect(struct usb_interface *intf)
  1024. {
  1025. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  1026. struct net_device *netdev;
  1027. dev_info(&intf->dev, "Unregistering\n");
  1028. usb_set_intfdata(intf, NULL);
  1029. if (!kaweth) {
  1030. dev_warn(&intf->dev, "unregistering non-existant device\n");
  1031. return;
  1032. }
  1033. netdev = kaweth->net;
  1034. dbg("Unregistering net device");
  1035. unregister_netdev(netdev);
  1036. usb_free_urb(kaweth->rx_urb);
  1037. usb_free_urb(kaweth->tx_urb);
  1038. usb_free_urb(kaweth->irq_urb);
  1039. usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  1040. usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  1041. free_netdev(netdev);
  1042. }
  1043. // FIXME this completion stuff is a modified clone of
  1044. // an OLD version of some stuff in usb.c ...
  1045. struct usb_api_data {
  1046. wait_queue_head_t wqh;
  1047. int done;
  1048. };
  1049. /*-------------------------------------------------------------------*
  1050. * completion handler for compatibility wrappers (sync control/bulk) *
  1051. *-------------------------------------------------------------------*/
  1052. static void usb_api_blocking_completion(struct urb *urb)
  1053. {
  1054. struct usb_api_data *awd = (struct usb_api_data *)urb->context;
  1055. awd->done=1;
  1056. wake_up(&awd->wqh);
  1057. }
  1058. /*-------------------------------------------------------------------*
  1059. * COMPATIBILITY STUFF *
  1060. *-------------------------------------------------------------------*/
  1061. // Starts urb and waits for completion or timeout
  1062. static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
  1063. {
  1064. struct usb_api_data awd;
  1065. int status;
  1066. init_waitqueue_head(&awd.wqh);
  1067. awd.done = 0;
  1068. urb->context = &awd;
  1069. status = usb_submit_urb(urb, GFP_NOIO);
  1070. if (status) {
  1071. // something went wrong
  1072. usb_free_urb(urb);
  1073. return status;
  1074. }
  1075. if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
  1076. // timeout
  1077. dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
  1078. usb_kill_urb(urb); // remove urb safely
  1079. status = -ETIMEDOUT;
  1080. }
  1081. else {
  1082. status = urb->status;
  1083. }
  1084. if (actual_length) {
  1085. *actual_length = urb->actual_length;
  1086. }
  1087. usb_free_urb(urb);
  1088. return status;
  1089. }
  1090. /*-------------------------------------------------------------------*/
  1091. // returns status (negative) or length (positive)
  1092. static int kaweth_internal_control_msg(struct usb_device *usb_dev,
  1093. unsigned int pipe,
  1094. struct usb_ctrlrequest *cmd, void *data,
  1095. int len, int timeout)
  1096. {
  1097. struct urb *urb;
  1098. int retv;
  1099. int length = 0; /* shut up GCC */
  1100. urb = usb_alloc_urb(0, GFP_NOIO);
  1101. if (!urb)
  1102. return -ENOMEM;
  1103. usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
  1104. len, usb_api_blocking_completion, NULL);
  1105. retv = usb_start_wait_urb(urb, timeout, &length);
  1106. if (retv < 0) {
  1107. return retv;
  1108. }
  1109. else {
  1110. return length;
  1111. }
  1112. }
  1113. /****************************************************************
  1114. * kaweth_init
  1115. ****************************************************************/
  1116. static int __init kaweth_init(void)
  1117. {
  1118. dbg("Driver loading");
  1119. return usb_register(&kaweth_driver);
  1120. }
  1121. /****************************************************************
  1122. * kaweth_exit
  1123. ****************************************************************/
  1124. static void __exit kaweth_exit(void)
  1125. {
  1126. usb_deregister(&kaweth_driver);
  1127. }
  1128. module_init(kaweth_init);
  1129. module_exit(kaweth_exit);