kaweth.c 36 KB

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