kaweth.c 37 KB

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