kaweth.c 36 KB

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