kaweth.c 37 KB

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