kaweth.c 36 KB

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