kaweth.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. release_firmware(fw);
  351. return -ENOSPC;
  352. }
  353. data_len = fw->size;
  354. memcpy(kaweth->firmware_buf, fw->data, fw->size);
  355. release_firmware(fw);
  356. kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
  357. kaweth->firmware_buf[3] = data_len >> 8;
  358. kaweth->firmware_buf[4] = type;
  359. kaweth->firmware_buf[5] = interrupt;
  360. dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
  361. kaweth->firmware_buf[2]);
  362. dbg("Downloading firmware at %p to kaweth device at %p",
  363. fw->data, kaweth);
  364. dbg("Firmware length: %d", data_len);
  365. return kaweth_control(kaweth,
  366. usb_sndctrlpipe(kaweth->dev, 0),
  367. KAWETH_COMMAND_SCAN,
  368. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  369. 0,
  370. 0,
  371. (void *)kaweth->firmware_buf,
  372. data_len,
  373. KAWETH_CONTROL_TIMEOUT);
  374. }
  375. /****************************************************************
  376. * kaweth_trigger_firmware
  377. ****************************************************************/
  378. static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
  379. __u8 interrupt)
  380. {
  381. kaweth->firmware_buf[0] = 0xB6;
  382. kaweth->firmware_buf[1] = 0xC3;
  383. kaweth->firmware_buf[2] = 0x01;
  384. kaweth->firmware_buf[3] = 0x00;
  385. kaweth->firmware_buf[4] = 0x06;
  386. kaweth->firmware_buf[5] = interrupt;
  387. kaweth->firmware_buf[6] = 0x00;
  388. kaweth->firmware_buf[7] = 0x00;
  389. dbg("Triggering firmware");
  390. return kaweth_control(kaweth,
  391. usb_sndctrlpipe(kaweth->dev, 0),
  392. KAWETH_COMMAND_SCAN,
  393. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  394. 0,
  395. 0,
  396. (void *)kaweth->firmware_buf,
  397. 8,
  398. KAWETH_CONTROL_TIMEOUT);
  399. }
  400. /****************************************************************
  401. * kaweth_reset
  402. ****************************************************************/
  403. static int kaweth_reset(struct kaweth_device *kaweth)
  404. {
  405. int result;
  406. dbg("kaweth_reset(%p)", kaweth);
  407. result = usb_reset_configuration(kaweth->dev);
  408. mdelay(10);
  409. dbg("kaweth_reset() returns %d.",result);
  410. return result;
  411. }
  412. static void kaweth_usb_receive(struct urb *);
  413. static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
  414. /****************************************************************
  415. int_callback
  416. *****************************************************************/
  417. static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
  418. {
  419. int status;
  420. status = usb_submit_urb (kaweth->irq_urb, mf);
  421. if (unlikely(status == -ENOMEM)) {
  422. kaweth->suspend_lowmem_ctrl = 1;
  423. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  424. } else {
  425. kaweth->suspend_lowmem_ctrl = 0;
  426. }
  427. if (status)
  428. err ("can't resubmit intr, %s-%s, status %d",
  429. kaweth->dev->bus->bus_name,
  430. kaweth->dev->devpath, status);
  431. }
  432. static void int_callback(struct urb *u)
  433. {
  434. struct kaweth_device *kaweth = u->context;
  435. int act_state;
  436. int status = u->status;
  437. switch (status) {
  438. case 0: /* success */
  439. break;
  440. case -ECONNRESET: /* unlink */
  441. case -ENOENT:
  442. case -ESHUTDOWN:
  443. return;
  444. /* -EPIPE: should clear the halt */
  445. default: /* error */
  446. goto resubmit;
  447. }
  448. /* we check the link state to report changes */
  449. if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
  450. if (act_state)
  451. netif_carrier_on(kaweth->net);
  452. else
  453. netif_carrier_off(kaweth->net);
  454. kaweth->linkstate = act_state;
  455. }
  456. resubmit:
  457. kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
  458. }
  459. static void kaweth_resubmit_tl(struct work_struct *work)
  460. {
  461. struct kaweth_device *kaweth =
  462. container_of(work, struct kaweth_device, lowmem_work.work);
  463. if (IS_BLOCKED(kaweth->status))
  464. return;
  465. if (kaweth->suspend_lowmem_rx)
  466. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  467. if (kaweth->suspend_lowmem_ctrl)
  468. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  469. }
  470. /****************************************************************
  471. * kaweth_resubmit_rx_urb
  472. ****************************************************************/
  473. static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
  474. gfp_t mem_flags)
  475. {
  476. int result;
  477. usb_fill_bulk_urb(kaweth->rx_urb,
  478. kaweth->dev,
  479. usb_rcvbulkpipe(kaweth->dev, 1),
  480. kaweth->rx_buf,
  481. KAWETH_BUF_SIZE,
  482. kaweth_usb_receive,
  483. kaweth);
  484. kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  485. kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
  486. if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
  487. if (result == -ENOMEM) {
  488. kaweth->suspend_lowmem_rx = 1;
  489. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  490. }
  491. err("resubmitting rx_urb %d failed", result);
  492. } else {
  493. kaweth->suspend_lowmem_rx = 0;
  494. }
  495. return result;
  496. }
  497. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
  498. /****************************************************************
  499. * kaweth_usb_receive
  500. ****************************************************************/
  501. static void kaweth_usb_receive(struct urb *urb)
  502. {
  503. struct kaweth_device *kaweth = urb->context;
  504. struct net_device *net = kaweth->net;
  505. int status = urb->status;
  506. int count = urb->actual_length;
  507. int count2 = urb->transfer_buffer_length;
  508. __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
  509. struct sk_buff *skb;
  510. if (unlikely(status == -EPIPE)) {
  511. kaweth->stats.rx_errors++;
  512. kaweth->end = 1;
  513. wake_up(&kaweth->term_wait);
  514. dbg("Status was -EPIPE.");
  515. return;
  516. }
  517. if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
  518. /* we are killed - set a flag and wake the disconnect handler */
  519. kaweth->end = 1;
  520. wake_up(&kaweth->term_wait);
  521. dbg("Status was -ECONNRESET or -ESHUTDOWN.");
  522. return;
  523. }
  524. if (unlikely(status == -EPROTO || status == -ETIME ||
  525. status == -EILSEQ)) {
  526. kaweth->stats.rx_errors++;
  527. dbg("Status was -EPROTO, -ETIME, or -EILSEQ.");
  528. return;
  529. }
  530. if (unlikely(status == -EOVERFLOW)) {
  531. kaweth->stats.rx_errors++;
  532. dbg("Status was -EOVERFLOW.");
  533. }
  534. spin_lock(&kaweth->device_lock);
  535. if (IS_BLOCKED(kaweth->status)) {
  536. spin_unlock(&kaweth->device_lock);
  537. return;
  538. }
  539. spin_unlock(&kaweth->device_lock);
  540. if(status && status != -EREMOTEIO && count != 1) {
  541. err("%s RX status: %d count: %d packet_len: %d",
  542. net->name,
  543. status,
  544. count,
  545. (int)pkt_len);
  546. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  547. return;
  548. }
  549. if(kaweth->net && (count > 2)) {
  550. if(pkt_len > (count - 2)) {
  551. err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
  552. err("Packet len & 2047: %x", pkt_len & 2047);
  553. err("Count 2: %x", count2);
  554. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  555. return;
  556. }
  557. if(!(skb = dev_alloc_skb(pkt_len+2))) {
  558. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  559. return;
  560. }
  561. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  562. skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
  563. skb_put(skb, pkt_len);
  564. skb->protocol = eth_type_trans(skb, net);
  565. netif_rx(skb);
  566. kaweth->stats.rx_packets++;
  567. kaweth->stats.rx_bytes += pkt_len;
  568. }
  569. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  570. }
  571. /****************************************************************
  572. * kaweth_open
  573. ****************************************************************/
  574. static int kaweth_open(struct net_device *net)
  575. {
  576. struct kaweth_device *kaweth = netdev_priv(net);
  577. int res;
  578. dbg("Opening network device.");
  579. res = usb_autopm_get_interface(kaweth->intf);
  580. if (res) {
  581. err("Interface cannot be resumed.");
  582. return -EIO;
  583. }
  584. res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
  585. if (res)
  586. goto err_out;
  587. usb_fill_int_urb(
  588. kaweth->irq_urb,
  589. kaweth->dev,
  590. usb_rcvintpipe(kaweth->dev, 3),
  591. kaweth->intbuffer,
  592. INTBUFFERSIZE,
  593. int_callback,
  594. kaweth,
  595. 250); /* overriding the descriptor */
  596. kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
  597. kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  598. res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
  599. if (res) {
  600. usb_kill_urb(kaweth->rx_urb);
  601. goto err_out;
  602. }
  603. kaweth->opened = 1;
  604. netif_start_queue(net);
  605. kaweth_async_set_rx_mode(kaweth);
  606. return 0;
  607. err_out:
  608. usb_autopm_put_interface(kaweth->intf);
  609. return -EIO;
  610. }
  611. /****************************************************************
  612. * kaweth_kill_urbs
  613. ****************************************************************/
  614. static void kaweth_kill_urbs(struct kaweth_device *kaweth)
  615. {
  616. usb_kill_urb(kaweth->irq_urb);
  617. usb_kill_urb(kaweth->rx_urb);
  618. usb_kill_urb(kaweth->tx_urb);
  619. cancel_delayed_work_sync(&kaweth->lowmem_work);
  620. /* a scheduled work may have resubmitted,
  621. we hit them again */
  622. usb_kill_urb(kaweth->irq_urb);
  623. usb_kill_urb(kaweth->rx_urb);
  624. }
  625. /****************************************************************
  626. * kaweth_close
  627. ****************************************************************/
  628. static int kaweth_close(struct net_device *net)
  629. {
  630. struct kaweth_device *kaweth = netdev_priv(net);
  631. netif_stop_queue(net);
  632. kaweth->opened = 0;
  633. kaweth->status |= KAWETH_STATUS_CLOSING;
  634. kaweth_kill_urbs(kaweth);
  635. kaweth->status &= ~KAWETH_STATUS_CLOSING;
  636. usb_autopm_put_interface(kaweth->intf);
  637. return 0;
  638. }
  639. static u32 kaweth_get_link(struct net_device *dev)
  640. {
  641. struct kaweth_device *kaweth = netdev_priv(dev);
  642. return kaweth->linkstate;
  643. }
  644. static const struct ethtool_ops ops = {
  645. .get_link = kaweth_get_link
  646. };
  647. /****************************************************************
  648. * kaweth_usb_transmit_complete
  649. ****************************************************************/
  650. static void kaweth_usb_transmit_complete(struct urb *urb)
  651. {
  652. struct kaweth_device *kaweth = urb->context;
  653. struct sk_buff *skb = kaweth->tx_skb;
  654. int status = urb->status;
  655. if (unlikely(status != 0))
  656. if (status != -ENOENT)
  657. dbg("%s: TX status %d.", kaweth->net->name, status);
  658. netif_wake_queue(kaweth->net);
  659. dev_kfree_skb_irq(skb);
  660. }
  661. /****************************************************************
  662. * kaweth_start_xmit
  663. ****************************************************************/
  664. static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
  665. struct net_device *net)
  666. {
  667. struct kaweth_device *kaweth = netdev_priv(net);
  668. __le16 *private_header;
  669. int res;
  670. spin_lock_irq(&kaweth->device_lock);
  671. kaweth_async_set_rx_mode(kaweth);
  672. netif_stop_queue(net);
  673. if (IS_BLOCKED(kaweth->status)) {
  674. goto skip;
  675. }
  676. /* We now decide whether we can put our special header into the sk_buff */
  677. if (skb_cloned(skb) || skb_headroom(skb) < 2) {
  678. /* no such luck - we make our own */
  679. struct sk_buff *copied_skb;
  680. copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
  681. dev_kfree_skb_irq(skb);
  682. skb = copied_skb;
  683. if (!copied_skb) {
  684. kaweth->stats.tx_errors++;
  685. netif_start_queue(net);
  686. spin_unlock_irq(&kaweth->device_lock);
  687. return NETDEV_TX_OK;
  688. }
  689. }
  690. private_header = (__le16 *)__skb_push(skb, 2);
  691. *private_header = cpu_to_le16(skb->len-2);
  692. kaweth->tx_skb = skb;
  693. usb_fill_bulk_urb(kaweth->tx_urb,
  694. kaweth->dev,
  695. usb_sndbulkpipe(kaweth->dev, 2),
  696. private_header,
  697. skb->len,
  698. kaweth_usb_transmit_complete,
  699. kaweth);
  700. kaweth->end = 0;
  701. if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
  702. {
  703. dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
  704. skip:
  705. kaweth->stats.tx_errors++;
  706. netif_start_queue(net);
  707. dev_kfree_skb_irq(skb);
  708. }
  709. else
  710. {
  711. kaweth->stats.tx_packets++;
  712. kaweth->stats.tx_bytes += skb->len;
  713. }
  714. spin_unlock_irq(&kaweth->device_lock);
  715. return NETDEV_TX_OK;
  716. }
  717. /****************************************************************
  718. * kaweth_set_rx_mode
  719. ****************************************************************/
  720. static void kaweth_set_rx_mode(struct net_device *net)
  721. {
  722. struct kaweth_device *kaweth = netdev_priv(net);
  723. __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
  724. KAWETH_PACKET_FILTER_BROADCAST |
  725. KAWETH_PACKET_FILTER_MULTICAST;
  726. dbg("Setting Rx mode to %d", packet_filter_bitmap);
  727. netif_stop_queue(net);
  728. if (net->flags & IFF_PROMISC) {
  729. packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
  730. }
  731. else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
  732. packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
  733. }
  734. kaweth->packet_filter_bitmap = packet_filter_bitmap;
  735. netif_wake_queue(net);
  736. }
  737. /****************************************************************
  738. * kaweth_async_set_rx_mode
  739. ****************************************************************/
  740. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
  741. {
  742. int result;
  743. __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
  744. kaweth->packet_filter_bitmap = 0;
  745. if (packet_filter_bitmap == 0)
  746. return;
  747. if (in_interrupt())
  748. return;
  749. result = kaweth_control(kaweth,
  750. usb_sndctrlpipe(kaweth->dev, 0),
  751. KAWETH_COMMAND_SET_PACKET_FILTER,
  752. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  753. packet_filter_bitmap,
  754. 0,
  755. (void *)&kaweth->scratch,
  756. 0,
  757. KAWETH_CONTROL_TIMEOUT);
  758. if(result < 0) {
  759. err("Failed to set Rx mode: %d", result);
  760. }
  761. else {
  762. dbg("Set Rx mode to %d", packet_filter_bitmap);
  763. }
  764. }
  765. /****************************************************************
  766. * kaweth_netdev_stats
  767. ****************************************************************/
  768. static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
  769. {
  770. struct kaweth_device *kaweth = netdev_priv(dev);
  771. return &kaweth->stats;
  772. }
  773. /****************************************************************
  774. * kaweth_tx_timeout
  775. ****************************************************************/
  776. static void kaweth_tx_timeout(struct net_device *net)
  777. {
  778. struct kaweth_device *kaweth = netdev_priv(net);
  779. dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
  780. kaweth->stats.tx_errors++;
  781. net->trans_start = jiffies;
  782. usb_unlink_urb(kaweth->tx_urb);
  783. }
  784. /****************************************************************
  785. * kaweth_suspend
  786. ****************************************************************/
  787. static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
  788. {
  789. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  790. unsigned long flags;
  791. dbg("Suspending device");
  792. spin_lock_irqsave(&kaweth->device_lock, flags);
  793. kaweth->status |= KAWETH_STATUS_SUSPENDING;
  794. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  795. kaweth_kill_urbs(kaweth);
  796. return 0;
  797. }
  798. /****************************************************************
  799. * kaweth_resume
  800. ****************************************************************/
  801. static int kaweth_resume(struct usb_interface *intf)
  802. {
  803. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  804. unsigned long flags;
  805. dbg("Resuming device");
  806. spin_lock_irqsave(&kaweth->device_lock, flags);
  807. kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
  808. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  809. if (!kaweth->opened)
  810. return 0;
  811. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  812. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  813. return 0;
  814. }
  815. /****************************************************************
  816. * kaweth_probe
  817. ****************************************************************/
  818. static const struct net_device_ops kaweth_netdev_ops = {
  819. .ndo_open = kaweth_open,
  820. .ndo_stop = kaweth_close,
  821. .ndo_start_xmit = kaweth_start_xmit,
  822. .ndo_tx_timeout = kaweth_tx_timeout,
  823. .ndo_set_multicast_list = kaweth_set_rx_mode,
  824. .ndo_get_stats = kaweth_netdev_stats,
  825. .ndo_change_mtu = eth_change_mtu,
  826. .ndo_set_mac_address = eth_mac_addr,
  827. .ndo_validate_addr = eth_validate_addr,
  828. };
  829. static int kaweth_probe(
  830. struct usb_interface *intf,
  831. const struct usb_device_id *id /* from id_table */
  832. )
  833. {
  834. struct usb_device *dev = interface_to_usbdev(intf);
  835. struct kaweth_device *kaweth;
  836. struct net_device *netdev;
  837. const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  838. int result = 0;
  839. dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
  840. dev->devnum,
  841. le16_to_cpu(dev->descriptor.idVendor),
  842. le16_to_cpu(dev->descriptor.idProduct),
  843. le16_to_cpu(dev->descriptor.bcdDevice));
  844. dbg("Device at %p", dev);
  845. dbg("Descriptor length: %x type: %x",
  846. (int)dev->descriptor.bLength,
  847. (int)dev->descriptor.bDescriptorType);
  848. netdev = alloc_etherdev(sizeof(*kaweth));
  849. if (!netdev)
  850. return -ENOMEM;
  851. kaweth = netdev_priv(netdev);
  852. kaweth->dev = dev;
  853. kaweth->net = netdev;
  854. spin_lock_init(&kaweth->device_lock);
  855. init_waitqueue_head(&kaweth->term_wait);
  856. dbg("Resetting.");
  857. kaweth_reset(kaweth);
  858. /*
  859. * If high byte of bcdDevice is nonzero, firmware is already
  860. * downloaded. Don't try to do it again, or we'll hang the device.
  861. */
  862. if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
  863. dev_info(&intf->dev, "Firmware present in device.\n");
  864. } else {
  865. /* Download the firmware */
  866. dev_info(&intf->dev, "Downloading firmware...\n");
  867. kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
  868. if ((result = kaweth_download_firmware(kaweth,
  869. "kaweth/new_code.bin",
  870. 100,
  871. 2)) < 0) {
  872. err("Error downloading firmware (%d)", result);
  873. goto err_fw;
  874. }
  875. if ((result = kaweth_download_firmware(kaweth,
  876. "kaweth/new_code_fix.bin",
  877. 100,
  878. 3)) < 0) {
  879. err("Error downloading firmware fix (%d)", result);
  880. goto err_fw;
  881. }
  882. if ((result = kaweth_download_firmware(kaweth,
  883. "kaweth/trigger_code.bin",
  884. 126,
  885. 2)) < 0) {
  886. err("Error downloading trigger code (%d)", result);
  887. goto err_fw;
  888. }
  889. if ((result = kaweth_download_firmware(kaweth,
  890. "kaweth/trigger_code_fix.bin",
  891. 126,
  892. 3)) < 0) {
  893. err("Error downloading trigger code fix (%d)", result);
  894. goto err_fw;
  895. }
  896. if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
  897. err("Error triggering firmware (%d)", result);
  898. goto err_fw;
  899. }
  900. /* Device will now disappear for a moment... */
  901. dev_info(&intf->dev, "Firmware loaded. I'll be back...\n");
  902. err_fw:
  903. free_page((unsigned long)kaweth->firmware_buf);
  904. free_netdev(netdev);
  905. return -EIO;
  906. }
  907. result = kaweth_read_configuration(kaweth);
  908. if(result < 0) {
  909. err("Error reading configuration (%d), no net device created", result);
  910. goto err_free_netdev;
  911. }
  912. dev_info(&intf->dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
  913. dev_info(&intf->dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
  914. dev_info(&intf->dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
  915. dev_info(&intf->dev, "Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
  916. (int)kaweth->configuration.hw_addr[0],
  917. (int)kaweth->configuration.hw_addr[1],
  918. (int)kaweth->configuration.hw_addr[2],
  919. (int)kaweth->configuration.hw_addr[3],
  920. (int)kaweth->configuration.hw_addr[4],
  921. (int)kaweth->configuration.hw_addr[5]);
  922. if(!memcmp(&kaweth->configuration.hw_addr,
  923. &bcast_addr,
  924. sizeof(bcast_addr))) {
  925. err("Firmware not functioning properly, no net device created");
  926. goto err_free_netdev;
  927. }
  928. if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
  929. dbg("Error setting URB size");
  930. goto err_free_netdev;
  931. }
  932. if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
  933. err("Error setting SOFS wait");
  934. goto err_free_netdev;
  935. }
  936. result = kaweth_set_receive_filter(kaweth,
  937. KAWETH_PACKET_FILTER_DIRECTED |
  938. KAWETH_PACKET_FILTER_BROADCAST |
  939. KAWETH_PACKET_FILTER_MULTICAST);
  940. if(result < 0) {
  941. err("Error setting receive filter");
  942. goto err_free_netdev;
  943. }
  944. dbg("Initializing net device.");
  945. kaweth->intf = intf;
  946. kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  947. if (!kaweth->tx_urb)
  948. goto err_free_netdev;
  949. kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  950. if (!kaweth->rx_urb)
  951. goto err_only_tx;
  952. kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  953. if (!kaweth->irq_urb)
  954. goto err_tx_and_rx;
  955. kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
  956. INTBUFFERSIZE,
  957. GFP_KERNEL,
  958. &kaweth->intbufferhandle);
  959. if (!kaweth->intbuffer)
  960. goto err_tx_and_rx_and_irq;
  961. kaweth->rx_buf = usb_alloc_coherent( kaweth->dev,
  962. KAWETH_BUF_SIZE,
  963. GFP_KERNEL,
  964. &kaweth->rxbufferhandle);
  965. if (!kaweth->rx_buf)
  966. goto err_all_but_rxbuf;
  967. memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
  968. memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
  969. sizeof(kaweth->configuration.hw_addr));
  970. netdev->netdev_ops = &kaweth_netdev_ops;
  971. netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
  972. netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
  973. SET_ETHTOOL_OPS(netdev, &ops);
  974. /* kaweth is zeroed as part of alloc_netdev */
  975. INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
  976. usb_set_intfdata(intf, kaweth);
  977. #if 0
  978. // dma_supported() is deeply broken on almost all architectures
  979. if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
  980. kaweth->net->features |= NETIF_F_HIGHDMA;
  981. #endif
  982. SET_NETDEV_DEV(netdev, &intf->dev);
  983. if (register_netdev(netdev) != 0) {
  984. err("Error registering netdev.");
  985. goto err_intfdata;
  986. }
  987. dev_info(&intf->dev, "kaweth interface created at %s\n",
  988. kaweth->net->name);
  989. dbg("Kaweth probe returning.");
  990. return 0;
  991. err_intfdata:
  992. usb_set_intfdata(intf, NULL);
  993. usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  994. err_all_but_rxbuf:
  995. usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  996. err_tx_and_rx_and_irq:
  997. usb_free_urb(kaweth->irq_urb);
  998. err_tx_and_rx:
  999. usb_free_urb(kaweth->rx_urb);
  1000. err_only_tx:
  1001. usb_free_urb(kaweth->tx_urb);
  1002. err_free_netdev:
  1003. free_netdev(netdev);
  1004. return -EIO;
  1005. }
  1006. /****************************************************************
  1007. * kaweth_disconnect
  1008. ****************************************************************/
  1009. static void kaweth_disconnect(struct usb_interface *intf)
  1010. {
  1011. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  1012. struct net_device *netdev;
  1013. dev_info(&intf->dev, "Unregistering\n");
  1014. usb_set_intfdata(intf, NULL);
  1015. if (!kaweth) {
  1016. dev_warn(&intf->dev, "unregistering non-existent device\n");
  1017. return;
  1018. }
  1019. netdev = kaweth->net;
  1020. dbg("Unregistering net device");
  1021. unregister_netdev(netdev);
  1022. usb_free_urb(kaweth->rx_urb);
  1023. usb_free_urb(kaweth->tx_urb);
  1024. usb_free_urb(kaweth->irq_urb);
  1025. usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  1026. usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  1027. free_netdev(netdev);
  1028. }
  1029. // FIXME this completion stuff is a modified clone of
  1030. // an OLD version of some stuff in usb.c ...
  1031. struct usb_api_data {
  1032. wait_queue_head_t wqh;
  1033. int done;
  1034. };
  1035. /*-------------------------------------------------------------------*
  1036. * completion handler for compatibility wrappers (sync control/bulk) *
  1037. *-------------------------------------------------------------------*/
  1038. static void usb_api_blocking_completion(struct urb *urb)
  1039. {
  1040. struct usb_api_data *awd = (struct usb_api_data *)urb->context;
  1041. awd->done=1;
  1042. wake_up(&awd->wqh);
  1043. }
  1044. /*-------------------------------------------------------------------*
  1045. * COMPATIBILITY STUFF *
  1046. *-------------------------------------------------------------------*/
  1047. // Starts urb and waits for completion or timeout
  1048. static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
  1049. {
  1050. struct usb_api_data awd;
  1051. int status;
  1052. init_waitqueue_head(&awd.wqh);
  1053. awd.done = 0;
  1054. urb->context = &awd;
  1055. status = usb_submit_urb(urb, GFP_NOIO);
  1056. if (status) {
  1057. // something went wrong
  1058. usb_free_urb(urb);
  1059. return status;
  1060. }
  1061. if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
  1062. // timeout
  1063. dev_warn(&urb->dev->dev, "usb_control/bulk_msg: timeout\n");
  1064. usb_kill_urb(urb); // remove urb safely
  1065. status = -ETIMEDOUT;
  1066. }
  1067. else {
  1068. status = urb->status;
  1069. }
  1070. if (actual_length) {
  1071. *actual_length = urb->actual_length;
  1072. }
  1073. usb_free_urb(urb);
  1074. return status;
  1075. }
  1076. /*-------------------------------------------------------------------*/
  1077. // returns status (negative) or length (positive)
  1078. static int kaweth_internal_control_msg(struct usb_device *usb_dev,
  1079. unsigned int pipe,
  1080. struct usb_ctrlrequest *cmd, void *data,
  1081. int len, int timeout)
  1082. {
  1083. struct urb *urb;
  1084. int retv;
  1085. int length = 0; /* shut up GCC */
  1086. urb = usb_alloc_urb(0, GFP_NOIO);
  1087. if (!urb)
  1088. return -ENOMEM;
  1089. usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
  1090. len, usb_api_blocking_completion, NULL);
  1091. retv = usb_start_wait_urb(urb, timeout, &length);
  1092. if (retv < 0) {
  1093. return retv;
  1094. }
  1095. else {
  1096. return length;
  1097. }
  1098. }
  1099. /****************************************************************
  1100. * kaweth_init
  1101. ****************************************************************/
  1102. static int __init kaweth_init(void)
  1103. {
  1104. dbg("Driver loading");
  1105. return usb_register(&kaweth_driver);
  1106. }
  1107. /****************************************************************
  1108. * kaweth_exit
  1109. ****************************************************************/
  1110. static void __exit kaweth_exit(void)
  1111. {
  1112. usb_deregister(&kaweth_driver);
  1113. }
  1114. module_init(kaweth_init);
  1115. module_exit(kaweth_exit);