kaweth.c 37 KB

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