kaweth.c 37 KB

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