kaweth.c 37 KB

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