kaweth.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. #ifdef DEBUG
  65. #define kaweth_dbg(format, arg...) printk(KERN_DEBUG __FILE__ ": " format "\n" ,##arg)
  66. #else
  67. #define kaweth_dbg(format, arg...) do {} while (0)
  68. #endif
  69. #define kaweth_err(format, arg...) printk(KERN_ERR __FILE__ ": " format "\n" ,##arg)
  70. #define kaweth_info(format, arg...) printk(KERN_INFO __FILE__ ": " format "\n" , ##arg)
  71. #define kaweth_warn(format, arg...) printk(KERN_WARNING __FILE__ ": " format "\n" , ##arg)
  72. #include "kawethfw.h"
  73. #define KAWETH_MTU 1514
  74. #define KAWETH_BUF_SIZE 1664
  75. #define KAWETH_TX_TIMEOUT (5 * HZ)
  76. #define KAWETH_SCRATCH_SIZE 32
  77. #define KAWETH_FIRMWARE_BUF_SIZE 4096
  78. #define KAWETH_CONTROL_TIMEOUT (30 * HZ)
  79. #define KAWETH_STATUS_BROKEN 0x0000001
  80. #define KAWETH_STATUS_CLOSING 0x0000002
  81. #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
  82. #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
  83. #define KAWETH_PACKET_FILTER_DIRECTED 0x04
  84. #define KAWETH_PACKET_FILTER_BROADCAST 0x08
  85. #define KAWETH_PACKET_FILTER_MULTICAST 0x10
  86. /* Table 7 */
  87. #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
  88. #define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
  89. #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
  90. #define KAWETH_COMMAND_STATISTICS 0x03
  91. #define KAWETH_COMMAND_SET_TEMP_MAC 0x06
  92. #define KAWETH_COMMAND_GET_TEMP_MAC 0x07
  93. #define KAWETH_COMMAND_SET_URB_SIZE 0x08
  94. #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
  95. #define KAWETH_COMMAND_SCAN 0xFF
  96. #define KAWETH_SOFS_TO_WAIT 0x05
  97. #define INTBUFFERSIZE 4
  98. #define STATE_OFFSET 0
  99. #define STATE_MASK 0x40
  100. #define STATE_SHIFT 5
  101. 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>");
  102. MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
  103. MODULE_LICENSE("GPL");
  104. static const char driver_name[] = "kaweth";
  105. static int kaweth_probe(
  106. struct usb_interface *intf,
  107. const struct usb_device_id *id /* from id_table */
  108. );
  109. static void kaweth_disconnect(struct usb_interface *intf);
  110. static int kaweth_internal_control_msg(struct usb_device *usb_dev,
  111. unsigned int pipe,
  112. struct usb_ctrlrequest *cmd, void *data,
  113. int len, int timeout);
  114. /****************************************************************
  115. * usb_device_id
  116. ****************************************************************/
  117. static struct usb_device_id usb_klsi_table[] = {
  118. { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
  119. { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
  120. { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
  121. { USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
  122. { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
  123. { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
  124. { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
  125. { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
  126. { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
  127. { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
  128. { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
  129. { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
  130. { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
  131. { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
  132. { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
  133. { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
  134. { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
  135. { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
  136. { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
  137. { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
  138. { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
  139. { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
  140. { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
  141. { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
  142. { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
  143. { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
  144. { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
  145. { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
  146. { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
  147. { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
  148. { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
  149. { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
  150. { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
  151. {} /* Null terminator */
  152. };
  153. MODULE_DEVICE_TABLE (usb, usb_klsi_table);
  154. /****************************************************************
  155. * kaweth_driver
  156. ****************************************************************/
  157. static struct usb_driver kaweth_driver = {
  158. .name = driver_name,
  159. .probe = kaweth_probe,
  160. .disconnect = kaweth_disconnect,
  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. struct work_struct lowmem_work;
  200. struct usb_device *dev;
  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. kaweth_dbg("kaweth_control()");
  232. if(in_interrupt()) {
  233. kaweth_dbg("in_interrupt()");
  234. return -EBUSY;
  235. }
  236. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  237. if (!dr) {
  238. kaweth_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. kaweth_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. kaweth_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. kaweth_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. kaweth_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. kaweth_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. kaweth_dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
  345. kaweth->firmware_buf[2]);
  346. kaweth_dbg("Downloading firmware at %p to kaweth device at %p",
  347. data,
  348. kaweth);
  349. kaweth_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. kaweth_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. kaweth_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. kaweth_dbg("kaweth_reset() returns %d.",result);
  403. return result;
  404. }
  405. static void kaweth_usb_receive(struct urb *, struct pt_regs *regs);
  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, struct pt_regs *regs)
  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(void *d)
  452. {
  453. struct kaweth_device *kaweth = (struct kaweth_device *)d;
  454. if (kaweth->status | KAWETH_STATUS_CLOSING)
  455. return;
  456. if (kaweth->suspend_lowmem_rx)
  457. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  458. if (kaweth->suspend_lowmem_ctrl)
  459. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  460. }
  461. /****************************************************************
  462. * kaweth_resubmit_rx_urb
  463. ****************************************************************/
  464. static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
  465. gfp_t mem_flags)
  466. {
  467. int result;
  468. usb_fill_bulk_urb(kaweth->rx_urb,
  469. kaweth->dev,
  470. usb_rcvbulkpipe(kaweth->dev, 1),
  471. kaweth->rx_buf,
  472. KAWETH_BUF_SIZE,
  473. kaweth_usb_receive,
  474. kaweth);
  475. kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  476. kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
  477. if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
  478. if (result == -ENOMEM) {
  479. kaweth->suspend_lowmem_rx = 1;
  480. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  481. }
  482. kaweth_err("resubmitting rx_urb %d failed", result);
  483. } else {
  484. kaweth->suspend_lowmem_rx = 0;
  485. }
  486. return result;
  487. }
  488. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
  489. /****************************************************************
  490. * kaweth_usb_receive
  491. ****************************************************************/
  492. static void kaweth_usb_receive(struct urb *urb, struct pt_regs *regs)
  493. {
  494. struct kaweth_device *kaweth = urb->context;
  495. struct net_device *net = kaweth->net;
  496. int count = urb->actual_length;
  497. int count2 = urb->transfer_buffer_length;
  498. __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
  499. struct sk_buff *skb;
  500. if(unlikely(urb->status == -ECONNRESET || urb->status == -ESHUTDOWN))
  501. /* we are killed - set a flag and wake the disconnect handler */
  502. {
  503. kaweth->end = 1;
  504. wake_up(&kaweth->term_wait);
  505. return;
  506. }
  507. if (kaweth->status & KAWETH_STATUS_CLOSING)
  508. return;
  509. if(urb->status && urb->status != -EREMOTEIO && count != 1) {
  510. kaweth_err("%s RX status: %d count: %d packet_len: %d",
  511. net->name,
  512. urb->status,
  513. count,
  514. (int)pkt_len);
  515. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  516. return;
  517. }
  518. if(kaweth->net && (count > 2)) {
  519. if(pkt_len > (count - 2)) {
  520. kaweth_err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
  521. kaweth_err("Packet len & 2047: %x", pkt_len & 2047);
  522. kaweth_err("Count 2: %x", count2);
  523. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  524. return;
  525. }
  526. if(!(skb = dev_alloc_skb(pkt_len+2))) {
  527. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  528. return;
  529. }
  530. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  531. skb->dev = net;
  532. eth_copy_and_sum(skb, kaweth->rx_buf + 2, pkt_len, 0);
  533. skb_put(skb, pkt_len);
  534. skb->protocol = eth_type_trans(skb, net);
  535. netif_rx(skb);
  536. kaweth->stats.rx_packets++;
  537. kaweth->stats.rx_bytes += pkt_len;
  538. }
  539. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  540. }
  541. /****************************************************************
  542. * kaweth_open
  543. ****************************************************************/
  544. static int kaweth_open(struct net_device *net)
  545. {
  546. struct kaweth_device *kaweth = netdev_priv(net);
  547. int res;
  548. kaweth_dbg("Opening network device.");
  549. res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
  550. if (res)
  551. return -EIO;
  552. usb_fill_int_urb(
  553. kaweth->irq_urb,
  554. kaweth->dev,
  555. usb_rcvintpipe(kaweth->dev, 3),
  556. kaweth->intbuffer,
  557. INTBUFFERSIZE,
  558. int_callback,
  559. kaweth,
  560. 250); /* overriding the descriptor */
  561. kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
  562. kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  563. res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
  564. if (res) {
  565. usb_kill_urb(kaweth->rx_urb);
  566. return -EIO;
  567. }
  568. netif_start_queue(net);
  569. kaweth_async_set_rx_mode(kaweth);
  570. return 0;
  571. }
  572. /****************************************************************
  573. * kaweth_close
  574. ****************************************************************/
  575. static int kaweth_close(struct net_device *net)
  576. {
  577. struct kaweth_device *kaweth = netdev_priv(net);
  578. netif_stop_queue(net);
  579. kaweth->status |= KAWETH_STATUS_CLOSING;
  580. usb_kill_urb(kaweth->irq_urb);
  581. usb_kill_urb(kaweth->rx_urb);
  582. usb_kill_urb(kaweth->tx_urb);
  583. flush_scheduled_work();
  584. /* a scheduled work may have resubmitted,
  585. we hit them again */
  586. usb_kill_urb(kaweth->irq_urb);
  587. usb_kill_urb(kaweth->rx_urb);
  588. kaweth->status &= ~KAWETH_STATUS_CLOSING;
  589. return 0;
  590. }
  591. static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  592. {
  593. strlcpy(info->driver, driver_name, sizeof(info->driver));
  594. }
  595. static struct ethtool_ops ops = {
  596. .get_drvinfo = kaweth_get_drvinfo
  597. };
  598. /****************************************************************
  599. * kaweth_usb_transmit_complete
  600. ****************************************************************/
  601. static void kaweth_usb_transmit_complete(struct urb *urb, struct pt_regs *regs)
  602. {
  603. struct kaweth_device *kaweth = urb->context;
  604. struct sk_buff *skb = kaweth->tx_skb;
  605. if (unlikely(urb->status != 0))
  606. if (urb->status != -ENOENT)
  607. kaweth_dbg("%s: TX status %d.", kaweth->net->name, urb->status);
  608. netif_wake_queue(kaweth->net);
  609. dev_kfree_skb_irq(skb);
  610. }
  611. /****************************************************************
  612. * kaweth_start_xmit
  613. ****************************************************************/
  614. static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
  615. {
  616. struct kaweth_device *kaweth = netdev_priv(net);
  617. __le16 *private_header;
  618. int res;
  619. spin_lock(&kaweth->device_lock);
  620. kaweth_async_set_rx_mode(kaweth);
  621. netif_stop_queue(net);
  622. /* We now decide whether we can put our special header into the sk_buff */
  623. if (skb_cloned(skb) || skb_headroom(skb) < 2) {
  624. /* no such luck - we make our own */
  625. struct sk_buff *copied_skb;
  626. copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
  627. dev_kfree_skb_irq(skb);
  628. skb = copied_skb;
  629. if (!copied_skb) {
  630. kaweth->stats.tx_errors++;
  631. netif_start_queue(net);
  632. spin_unlock(&kaweth->device_lock);
  633. return 0;
  634. }
  635. }
  636. private_header = (__le16 *)__skb_push(skb, 2);
  637. *private_header = cpu_to_le16(skb->len-2);
  638. kaweth->tx_skb = skb;
  639. usb_fill_bulk_urb(kaweth->tx_urb,
  640. kaweth->dev,
  641. usb_sndbulkpipe(kaweth->dev, 2),
  642. private_header,
  643. skb->len,
  644. kaweth_usb_transmit_complete,
  645. kaweth);
  646. kaweth->end = 0;
  647. if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
  648. {
  649. kaweth_warn("kaweth failed tx_urb %d", res);
  650. kaweth->stats.tx_errors++;
  651. netif_start_queue(net);
  652. dev_kfree_skb_irq(skb);
  653. }
  654. else
  655. {
  656. kaweth->stats.tx_packets++;
  657. kaweth->stats.tx_bytes += skb->len;
  658. net->trans_start = jiffies;
  659. }
  660. spin_unlock(&kaweth->device_lock);
  661. return 0;
  662. }
  663. /****************************************************************
  664. * kaweth_set_rx_mode
  665. ****************************************************************/
  666. static void kaweth_set_rx_mode(struct net_device *net)
  667. {
  668. struct kaweth_device *kaweth = netdev_priv(net);
  669. __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
  670. KAWETH_PACKET_FILTER_BROADCAST |
  671. KAWETH_PACKET_FILTER_MULTICAST;
  672. kaweth_dbg("Setting Rx mode to %d", packet_filter_bitmap);
  673. netif_stop_queue(net);
  674. if (net->flags & IFF_PROMISC) {
  675. packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
  676. }
  677. else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
  678. packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
  679. }
  680. kaweth->packet_filter_bitmap = packet_filter_bitmap;
  681. netif_wake_queue(net);
  682. }
  683. /****************************************************************
  684. * kaweth_async_set_rx_mode
  685. ****************************************************************/
  686. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
  687. {
  688. __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
  689. kaweth->packet_filter_bitmap = 0;
  690. if (packet_filter_bitmap == 0)
  691. return;
  692. {
  693. int result;
  694. result = kaweth_control(kaweth,
  695. usb_sndctrlpipe(kaweth->dev, 0),
  696. KAWETH_COMMAND_SET_PACKET_FILTER,
  697. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  698. packet_filter_bitmap,
  699. 0,
  700. (void *)&kaweth->scratch,
  701. 0,
  702. KAWETH_CONTROL_TIMEOUT);
  703. if(result < 0) {
  704. kaweth_err("Failed to set Rx mode: %d", result);
  705. }
  706. else {
  707. kaweth_dbg("Set Rx mode to %d", packet_filter_bitmap);
  708. }
  709. }
  710. }
  711. /****************************************************************
  712. * kaweth_netdev_stats
  713. ****************************************************************/
  714. static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
  715. {
  716. struct kaweth_device *kaweth = netdev_priv(dev);
  717. return &kaweth->stats;
  718. }
  719. /****************************************************************
  720. * kaweth_tx_timeout
  721. ****************************************************************/
  722. static void kaweth_tx_timeout(struct net_device *net)
  723. {
  724. struct kaweth_device *kaweth = netdev_priv(net);
  725. kaweth_warn("%s: Tx timed out. Resetting.", net->name);
  726. kaweth->stats.tx_errors++;
  727. net->trans_start = jiffies;
  728. usb_unlink_urb(kaweth->tx_urb);
  729. }
  730. /****************************************************************
  731. * kaweth_probe
  732. ****************************************************************/
  733. static int kaweth_probe(
  734. struct usb_interface *intf,
  735. const struct usb_device_id *id /* from id_table */
  736. )
  737. {
  738. struct usb_device *dev = interface_to_usbdev(intf);
  739. struct kaweth_device *kaweth;
  740. struct net_device *netdev;
  741. const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  742. int result = 0;
  743. kaweth_dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
  744. dev->devnum,
  745. le16_to_cpu(dev->descriptor.idVendor),
  746. le16_to_cpu(dev->descriptor.idProduct),
  747. le16_to_cpu(dev->descriptor.bcdDevice));
  748. kaweth_dbg("Device at %p", dev);
  749. kaweth_dbg("Descriptor length: %x type: %x",
  750. (int)dev->descriptor.bLength,
  751. (int)dev->descriptor.bDescriptorType);
  752. netdev = alloc_etherdev(sizeof(*kaweth));
  753. if (!netdev)
  754. return -ENOMEM;
  755. kaweth = netdev_priv(netdev);
  756. kaweth->dev = dev;
  757. kaweth->net = netdev;
  758. spin_lock_init(&kaweth->device_lock);
  759. init_waitqueue_head(&kaweth->term_wait);
  760. kaweth_dbg("Resetting.");
  761. kaweth_reset(kaweth);
  762. /*
  763. * If high byte of bcdDevice is nonzero, firmware is already
  764. * downloaded. Don't try to do it again, or we'll hang the device.
  765. */
  766. if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
  767. kaweth_info("Firmware present in device.");
  768. } else {
  769. /* Download the firmware */
  770. kaweth_info("Downloading firmware...");
  771. kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
  772. if ((result = kaweth_download_firmware(kaweth,
  773. kaweth_new_code,
  774. len_kaweth_new_code,
  775. 100,
  776. 2)) < 0) {
  777. kaweth_err("Error downloading firmware (%d)", result);
  778. goto err_fw;
  779. }
  780. if ((result = kaweth_download_firmware(kaweth,
  781. kaweth_new_code_fix,
  782. len_kaweth_new_code_fix,
  783. 100,
  784. 3)) < 0) {
  785. kaweth_err("Error downloading firmware fix (%d)", result);
  786. goto err_fw;
  787. }
  788. if ((result = kaweth_download_firmware(kaweth,
  789. kaweth_trigger_code,
  790. len_kaweth_trigger_code,
  791. 126,
  792. 2)) < 0) {
  793. kaweth_err("Error downloading trigger code (%d)", result);
  794. goto err_fw;
  795. }
  796. if ((result = kaweth_download_firmware(kaweth,
  797. kaweth_trigger_code_fix,
  798. len_kaweth_trigger_code_fix,
  799. 126,
  800. 3)) < 0) {
  801. kaweth_err("Error downloading trigger code fix (%d)", result);
  802. goto err_fw;
  803. }
  804. if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
  805. kaweth_err("Error triggering firmware (%d)", result);
  806. goto err_fw;
  807. }
  808. /* Device will now disappear for a moment... */
  809. kaweth_info("Firmware loaded. I'll be back...");
  810. err_fw:
  811. free_page((unsigned long)kaweth->firmware_buf);
  812. free_netdev(netdev);
  813. return -EIO;
  814. }
  815. result = kaweth_read_configuration(kaweth);
  816. if(result < 0) {
  817. kaweth_err("Error reading configuration (%d), no net device created", result);
  818. goto err_free_netdev;
  819. }
  820. kaweth_info("Statistics collection: %x", kaweth->configuration.statistics_mask);
  821. kaweth_info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
  822. kaweth_info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size));
  823. kaweth_info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
  824. (int)kaweth->configuration.hw_addr[0],
  825. (int)kaweth->configuration.hw_addr[1],
  826. (int)kaweth->configuration.hw_addr[2],
  827. (int)kaweth->configuration.hw_addr[3],
  828. (int)kaweth->configuration.hw_addr[4],
  829. (int)kaweth->configuration.hw_addr[5]);
  830. if(!memcmp(&kaweth->configuration.hw_addr,
  831. &bcast_addr,
  832. sizeof(bcast_addr))) {
  833. kaweth_err("Firmware not functioning properly, no net device created");
  834. goto err_free_netdev;
  835. }
  836. if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
  837. kaweth_dbg("Error setting URB size");
  838. goto err_free_netdev;
  839. }
  840. if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
  841. kaweth_err("Error setting SOFS wait");
  842. goto err_free_netdev;
  843. }
  844. result = kaweth_set_receive_filter(kaweth,
  845. KAWETH_PACKET_FILTER_DIRECTED |
  846. KAWETH_PACKET_FILTER_BROADCAST |
  847. KAWETH_PACKET_FILTER_MULTICAST);
  848. if(result < 0) {
  849. kaweth_err("Error setting receive filter");
  850. goto err_free_netdev;
  851. }
  852. kaweth_dbg("Initializing net device.");
  853. kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  854. if (!kaweth->tx_urb)
  855. goto err_free_netdev;
  856. kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  857. if (!kaweth->rx_urb)
  858. goto err_only_tx;
  859. kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  860. if (!kaweth->irq_urb)
  861. goto err_tx_and_rx;
  862. kaweth->intbuffer = usb_buffer_alloc( kaweth->dev,
  863. INTBUFFERSIZE,
  864. GFP_KERNEL,
  865. &kaweth->intbufferhandle);
  866. if (!kaweth->intbuffer)
  867. goto err_tx_and_rx_and_irq;
  868. kaweth->rx_buf = usb_buffer_alloc( kaweth->dev,
  869. KAWETH_BUF_SIZE,
  870. GFP_KERNEL,
  871. &kaweth->rxbufferhandle);
  872. if (!kaweth->rx_buf)
  873. goto err_all_but_rxbuf;
  874. memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
  875. memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
  876. sizeof(kaweth->configuration.hw_addr));
  877. netdev->open = kaweth_open;
  878. netdev->stop = kaweth_close;
  879. netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
  880. netdev->tx_timeout = kaweth_tx_timeout;
  881. netdev->hard_start_xmit = kaweth_start_xmit;
  882. netdev->set_multicast_list = kaweth_set_rx_mode;
  883. netdev->get_stats = kaweth_netdev_stats;
  884. netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
  885. SET_ETHTOOL_OPS(netdev, &ops);
  886. /* kaweth is zeroed as part of alloc_netdev */
  887. INIT_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl, (void *)kaweth);
  888. SET_MODULE_OWNER(netdev);
  889. usb_set_intfdata(intf, kaweth);
  890. #if 0
  891. // dma_supported() is deeply broken on almost all architectures
  892. if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
  893. kaweth->net->features |= NETIF_F_HIGHDMA;
  894. #endif
  895. SET_NETDEV_DEV(netdev, &intf->dev);
  896. if (register_netdev(netdev) != 0) {
  897. kaweth_err("Error registering netdev.");
  898. goto err_intfdata;
  899. }
  900. kaweth_info("kaweth interface created at %s", kaweth->net->name);
  901. kaweth_dbg("Kaweth probe returning.");
  902. return 0;
  903. err_intfdata:
  904. usb_set_intfdata(intf, NULL);
  905. usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  906. err_all_but_rxbuf:
  907. usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  908. err_tx_and_rx_and_irq:
  909. usb_free_urb(kaweth->irq_urb);
  910. err_tx_and_rx:
  911. usb_free_urb(kaweth->rx_urb);
  912. err_only_tx:
  913. usb_free_urb(kaweth->tx_urb);
  914. err_free_netdev:
  915. free_netdev(netdev);
  916. return -EIO;
  917. }
  918. /****************************************************************
  919. * kaweth_disconnect
  920. ****************************************************************/
  921. static void kaweth_disconnect(struct usb_interface *intf)
  922. {
  923. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  924. struct net_device *netdev;
  925. kaweth_info("Unregistering");
  926. usb_set_intfdata(intf, NULL);
  927. if (!kaweth) {
  928. kaweth_warn("unregistering non-existant device");
  929. return;
  930. }
  931. netdev = kaweth->net;
  932. kaweth_dbg("Unregistering net device");
  933. unregister_netdev(netdev);
  934. usb_free_urb(kaweth->rx_urb);
  935. usb_free_urb(kaweth->tx_urb);
  936. usb_free_urb(kaweth->irq_urb);
  937. usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  938. usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  939. free_netdev(netdev);
  940. }
  941. // FIXME this completion stuff is a modified clone of
  942. // an OLD version of some stuff in usb.c ...
  943. struct usb_api_data {
  944. wait_queue_head_t wqh;
  945. int done;
  946. };
  947. /*-------------------------------------------------------------------*
  948. * completion handler for compatibility wrappers (sync control/bulk) *
  949. *-------------------------------------------------------------------*/
  950. static void usb_api_blocking_completion(struct urb *urb, struct pt_regs *regs)
  951. {
  952. struct usb_api_data *awd = (struct usb_api_data *)urb->context;
  953. awd->done=1;
  954. wake_up(&awd->wqh);
  955. }
  956. /*-------------------------------------------------------------------*
  957. * COMPATIBILITY STUFF *
  958. *-------------------------------------------------------------------*/
  959. // Starts urb and waits for completion or timeout
  960. static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
  961. {
  962. struct usb_api_data awd;
  963. int status;
  964. init_waitqueue_head(&awd.wqh);
  965. awd.done = 0;
  966. urb->context = &awd;
  967. status = usb_submit_urb(urb, GFP_NOIO);
  968. if (status) {
  969. // something went wrong
  970. usb_free_urb(urb);
  971. return status;
  972. }
  973. if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
  974. // timeout
  975. kaweth_warn("usb_control/bulk_msg: timeout");
  976. usb_kill_urb(urb); // remove urb safely
  977. status = -ETIMEDOUT;
  978. }
  979. else {
  980. status = urb->status;
  981. }
  982. if (actual_length) {
  983. *actual_length = urb->actual_length;
  984. }
  985. usb_free_urb(urb);
  986. return status;
  987. }
  988. /*-------------------------------------------------------------------*/
  989. // returns status (negative) or length (positive)
  990. static int kaweth_internal_control_msg(struct usb_device *usb_dev,
  991. unsigned int pipe,
  992. struct usb_ctrlrequest *cmd, void *data,
  993. int len, int timeout)
  994. {
  995. struct urb *urb;
  996. int retv;
  997. int length;
  998. urb = usb_alloc_urb(0, GFP_NOIO);
  999. if (!urb)
  1000. return -ENOMEM;
  1001. usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
  1002. len, usb_api_blocking_completion, NULL);
  1003. retv = usb_start_wait_urb(urb, timeout, &length);
  1004. if (retv < 0) {
  1005. return retv;
  1006. }
  1007. else {
  1008. return length;
  1009. }
  1010. }
  1011. /****************************************************************
  1012. * kaweth_init
  1013. ****************************************************************/
  1014. static int __init kaweth_init(void)
  1015. {
  1016. kaweth_dbg("Driver loading");
  1017. return usb_register(&kaweth_driver);
  1018. }
  1019. /****************************************************************
  1020. * kaweth_exit
  1021. ****************************************************************/
  1022. static void __exit kaweth_exit(void)
  1023. {
  1024. usb_deregister(&kaweth_driver);
  1025. }
  1026. module_init(kaweth_init);
  1027. module_exit(kaweth_exit);