kaweth.c 38 KB

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