kaweth.c 36 KB

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