kaweth.c 38 KB

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