kaweth.c 36 KB

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