kaweth.c 37 KB

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