kaweth.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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. .owner = THIS_MODULE,
  159. .name = driver_name,
  160. .probe = kaweth_probe,
  161. .disconnect = kaweth_disconnect,
  162. .id_table = usb_klsi_table,
  163. };
  164. typedef __u8 eth_addr_t[6];
  165. /****************************************************************
  166. * usb_eth_dev
  167. ****************************************************************/
  168. struct usb_eth_dev {
  169. char *name;
  170. __u16 vendor;
  171. __u16 device;
  172. void *pdata;
  173. };
  174. /****************************************************************
  175. * kaweth_ethernet_configuration
  176. * Refer Table 8
  177. ****************************************************************/
  178. struct kaweth_ethernet_configuration
  179. {
  180. __u8 size;
  181. __u8 reserved1;
  182. __u8 reserved2;
  183. eth_addr_t hw_addr;
  184. __u32 statistics_mask;
  185. __le16 segment_size;
  186. __u16 max_multicast_filters;
  187. __u8 reserved3;
  188. } __attribute__ ((packed));
  189. /****************************************************************
  190. * kaweth_device
  191. ****************************************************************/
  192. struct kaweth_device
  193. {
  194. spinlock_t device_lock;
  195. __u32 status;
  196. int end;
  197. int removed;
  198. int suspend_lowmem_rx;
  199. int suspend_lowmem_ctrl;
  200. int linkstate;
  201. struct work_struct lowmem_work;
  202. struct usb_device *dev;
  203. struct net_device *net;
  204. wait_queue_head_t term_wait;
  205. struct urb *rx_urb;
  206. struct urb *tx_urb;
  207. struct urb *irq_urb;
  208. dma_addr_t intbufferhandle;
  209. __u8 *intbuffer;
  210. dma_addr_t rxbufferhandle;
  211. __u8 *rx_buf;
  212. struct sk_buff *tx_skb;
  213. __u8 *firmware_buf;
  214. __u8 scratch[KAWETH_SCRATCH_SIZE];
  215. __u16 packet_filter_bitmap;
  216. struct kaweth_ethernet_configuration configuration;
  217. struct net_device_stats stats;
  218. };
  219. /****************************************************************
  220. * kaweth_control
  221. ****************************************************************/
  222. static int kaweth_control(struct kaweth_device *kaweth,
  223. unsigned int pipe,
  224. __u8 request,
  225. __u8 requesttype,
  226. __u16 value,
  227. __u16 index,
  228. void *data,
  229. __u16 size,
  230. int timeout)
  231. {
  232. struct usb_ctrlrequest *dr;
  233. kaweth_dbg("kaweth_control()");
  234. if(in_interrupt()) {
  235. kaweth_dbg("in_interrupt()");
  236. return -EBUSY;
  237. }
  238. dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
  239. if (!dr) {
  240. kaweth_dbg("kmalloc() failed");
  241. return -ENOMEM;
  242. }
  243. dr->bRequestType= requesttype;
  244. dr->bRequest = request;
  245. dr->wValue = cpu_to_le16p(&value);
  246. dr->wIndex = cpu_to_le16p(&index);
  247. dr->wLength = cpu_to_le16p(&size);
  248. return kaweth_internal_control_msg(kaweth->dev,
  249. pipe,
  250. dr,
  251. data,
  252. size,
  253. timeout);
  254. }
  255. /****************************************************************
  256. * kaweth_read_configuration
  257. ****************************************************************/
  258. static int kaweth_read_configuration(struct kaweth_device *kaweth)
  259. {
  260. int retval;
  261. kaweth_dbg("Reading kaweth configuration");
  262. retval = kaweth_control(kaweth,
  263. usb_rcvctrlpipe(kaweth->dev, 0),
  264. KAWETH_COMMAND_GET_ETHERNET_DESC,
  265. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  266. 0,
  267. 0,
  268. (void *)&kaweth->configuration,
  269. sizeof(kaweth->configuration),
  270. KAWETH_CONTROL_TIMEOUT);
  271. return retval;
  272. }
  273. /****************************************************************
  274. * kaweth_set_urb_size
  275. ****************************************************************/
  276. static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
  277. {
  278. int retval;
  279. kaweth_dbg("Setting URB size to %d", (unsigned)urb_size);
  280. retval = kaweth_control(kaweth,
  281. usb_sndctrlpipe(kaweth->dev, 0),
  282. KAWETH_COMMAND_SET_URB_SIZE,
  283. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  284. urb_size,
  285. 0,
  286. (void *)&kaweth->scratch,
  287. 0,
  288. KAWETH_CONTROL_TIMEOUT);
  289. return retval;
  290. }
  291. /****************************************************************
  292. * kaweth_set_sofs_wait
  293. ****************************************************************/
  294. static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
  295. {
  296. int retval;
  297. kaweth_dbg("Set SOFS wait to %d", (unsigned)sofs_wait);
  298. retval = kaweth_control(kaweth,
  299. usb_sndctrlpipe(kaweth->dev, 0),
  300. KAWETH_COMMAND_SET_SOFS_WAIT,
  301. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  302. sofs_wait,
  303. 0,
  304. (void *)&kaweth->scratch,
  305. 0,
  306. KAWETH_CONTROL_TIMEOUT);
  307. return retval;
  308. }
  309. /****************************************************************
  310. * kaweth_set_receive_filter
  311. ****************************************************************/
  312. static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
  313. __u16 receive_filter)
  314. {
  315. int retval;
  316. kaweth_dbg("Set receive filter to %d", (unsigned)receive_filter);
  317. retval = kaweth_control(kaweth,
  318. usb_sndctrlpipe(kaweth->dev, 0),
  319. KAWETH_COMMAND_SET_PACKET_FILTER,
  320. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  321. receive_filter,
  322. 0,
  323. (void *)&kaweth->scratch,
  324. 0,
  325. KAWETH_CONTROL_TIMEOUT);
  326. return retval;
  327. }
  328. /****************************************************************
  329. * kaweth_download_firmware
  330. ****************************************************************/
  331. static int kaweth_download_firmware(struct kaweth_device *kaweth,
  332. __u8 *data,
  333. __u16 data_len,
  334. __u8 interrupt,
  335. __u8 type)
  336. {
  337. if(data_len > KAWETH_FIRMWARE_BUF_SIZE) {
  338. kaweth_err("Firmware too big: %d", data_len);
  339. return -ENOSPC;
  340. }
  341. memcpy(kaweth->firmware_buf, data, data_len);
  342. kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
  343. kaweth->firmware_buf[3] = data_len >> 8;
  344. kaweth->firmware_buf[4] = type;
  345. kaweth->firmware_buf[5] = interrupt;
  346. kaweth_dbg("High: %i, Low:%i", kaweth->firmware_buf[3],
  347. kaweth->firmware_buf[2]);
  348. kaweth_dbg("Downloading firmware at %p to kaweth device at %p",
  349. data,
  350. kaweth);
  351. kaweth_dbg("Firmware length: %d", data_len);
  352. return kaweth_control(kaweth,
  353. usb_sndctrlpipe(kaweth->dev, 0),
  354. KAWETH_COMMAND_SCAN,
  355. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  356. 0,
  357. 0,
  358. (void *)kaweth->firmware_buf,
  359. data_len,
  360. KAWETH_CONTROL_TIMEOUT);
  361. }
  362. /****************************************************************
  363. * kaweth_trigger_firmware
  364. ****************************************************************/
  365. static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
  366. __u8 interrupt)
  367. {
  368. kaweth->firmware_buf[0] = 0xB6;
  369. kaweth->firmware_buf[1] = 0xC3;
  370. kaweth->firmware_buf[2] = 0x01;
  371. kaweth->firmware_buf[3] = 0x00;
  372. kaweth->firmware_buf[4] = 0x06;
  373. kaweth->firmware_buf[5] = interrupt;
  374. kaweth->firmware_buf[6] = 0x00;
  375. kaweth->firmware_buf[7] = 0x00;
  376. kaweth_dbg("Triggering firmware");
  377. return kaweth_control(kaweth,
  378. usb_sndctrlpipe(kaweth->dev, 0),
  379. KAWETH_COMMAND_SCAN,
  380. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  381. 0,
  382. 0,
  383. (void *)kaweth->firmware_buf,
  384. 8,
  385. KAWETH_CONTROL_TIMEOUT);
  386. }
  387. /****************************************************************
  388. * kaweth_reset
  389. ****************************************************************/
  390. static int kaweth_reset(struct kaweth_device *kaweth)
  391. {
  392. int result;
  393. kaweth_dbg("kaweth_reset(%p)", kaweth);
  394. result = kaweth_control(kaweth,
  395. usb_sndctrlpipe(kaweth->dev, 0),
  396. USB_REQ_SET_CONFIGURATION,
  397. 0,
  398. kaweth->dev->config[0].desc.bConfigurationValue,
  399. 0,
  400. NULL,
  401. 0,
  402. KAWETH_CONTROL_TIMEOUT);
  403. udelay(10000);
  404. kaweth_dbg("kaweth_reset() returns %d.",result);
  405. return result;
  406. }
  407. static void kaweth_usb_receive(struct urb *, struct pt_regs *regs);
  408. static int kaweth_resubmit_rx_urb(struct kaweth_device *, unsigned);
  409. /****************************************************************
  410. int_callback
  411. *****************************************************************/
  412. static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, int mf)
  413. {
  414. int status;
  415. status = usb_submit_urb (kaweth->irq_urb, mf);
  416. if (unlikely(status == -ENOMEM)) {
  417. kaweth->suspend_lowmem_ctrl = 1;
  418. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  419. } else {
  420. kaweth->suspend_lowmem_ctrl = 0;
  421. }
  422. if (status)
  423. err ("can't resubmit intr, %s-%s, status %d",
  424. kaweth->dev->bus->bus_name,
  425. kaweth->dev->devpath, status);
  426. }
  427. static void int_callback(struct urb *u, struct pt_regs *regs)
  428. {
  429. struct kaweth_device *kaweth = u->context;
  430. int act_state;
  431. switch (u->status) {
  432. case 0: /* success */
  433. break;
  434. case -ECONNRESET: /* unlink */
  435. case -ENOENT:
  436. case -ESHUTDOWN:
  437. return;
  438. /* -EPIPE: should clear the halt */
  439. default: /* error */
  440. goto resubmit;
  441. }
  442. /* we check the link state to report changes */
  443. if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
  444. if (act_state)
  445. netif_carrier_on(kaweth->net);
  446. else
  447. netif_carrier_off(kaweth->net);
  448. kaweth->linkstate = act_state;
  449. }
  450. resubmit:
  451. kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
  452. }
  453. static void kaweth_resubmit_tl(void *d)
  454. {
  455. struct kaweth_device *kaweth = (struct kaweth_device *)d;
  456. if (kaweth->status | KAWETH_STATUS_CLOSING)
  457. return;
  458. if (kaweth->suspend_lowmem_rx)
  459. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  460. if (kaweth->suspend_lowmem_ctrl)
  461. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  462. }
  463. /****************************************************************
  464. * kaweth_resubmit_rx_urb
  465. ****************************************************************/
  466. static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
  467. unsigned mem_flags)
  468. {
  469. int result;
  470. usb_fill_bulk_urb(kaweth->rx_urb,
  471. kaweth->dev,
  472. usb_rcvbulkpipe(kaweth->dev, 1),
  473. kaweth->rx_buf,
  474. KAWETH_BUF_SIZE,
  475. kaweth_usb_receive,
  476. kaweth);
  477. kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  478. kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
  479. if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
  480. if (result == -ENOMEM) {
  481. kaweth->suspend_lowmem_rx = 1;
  482. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  483. }
  484. kaweth_err("resubmitting rx_urb %d failed", result);
  485. } else {
  486. kaweth->suspend_lowmem_rx = 0;
  487. }
  488. return result;
  489. }
  490. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth);
  491. /****************************************************************
  492. * kaweth_usb_receive
  493. ****************************************************************/
  494. static void kaweth_usb_receive(struct urb *urb, struct pt_regs *regs)
  495. {
  496. struct kaweth_device *kaweth = urb->context;
  497. struct net_device *net = kaweth->net;
  498. int count = urb->actual_length;
  499. int count2 = urb->transfer_buffer_length;
  500. __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
  501. struct sk_buff *skb;
  502. if(unlikely(urb->status == -ECONNRESET || urb->status == -ESHUTDOWN))
  503. /* we are killed - set a flag and wake the disconnect handler */
  504. {
  505. kaweth->end = 1;
  506. wake_up(&kaweth->term_wait);
  507. return;
  508. }
  509. if (kaweth->status & KAWETH_STATUS_CLOSING)
  510. return;
  511. if(urb->status && urb->status != -EREMOTEIO && count != 1) {
  512. kaweth_err("%s RX status: %d count: %d packet_len: %d",
  513. net->name,
  514. urb->status,
  515. count,
  516. (int)pkt_len);
  517. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  518. return;
  519. }
  520. if(kaweth->net && (count > 2)) {
  521. if(pkt_len > (count - 2)) {
  522. kaweth_err("Packet length too long for USB frame (pkt_len: %x, count: %x)",pkt_len, count);
  523. kaweth_err("Packet len & 2047: %x", pkt_len & 2047);
  524. kaweth_err("Count 2: %x", count2);
  525. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  526. return;
  527. }
  528. if(!(skb = dev_alloc_skb(pkt_len+2))) {
  529. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  530. return;
  531. }
  532. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  533. skb->dev = net;
  534. eth_copy_and_sum(skb, kaweth->rx_buf + 2, pkt_len, 0);
  535. skb_put(skb, pkt_len);
  536. skb->protocol = eth_type_trans(skb, net);
  537. netif_rx(skb);
  538. kaweth->stats.rx_packets++;
  539. kaweth->stats.rx_bytes += pkt_len;
  540. }
  541. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  542. }
  543. /****************************************************************
  544. * kaweth_open
  545. ****************************************************************/
  546. static int kaweth_open(struct net_device *net)
  547. {
  548. struct kaweth_device *kaweth = netdev_priv(net);
  549. int res;
  550. kaweth_dbg("Opening network device.");
  551. res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
  552. if (res)
  553. return -EIO;
  554. usb_fill_int_urb(
  555. kaweth->irq_urb,
  556. kaweth->dev,
  557. usb_rcvintpipe(kaweth->dev, 3),
  558. kaweth->intbuffer,
  559. INTBUFFERSIZE,
  560. int_callback,
  561. kaweth,
  562. 250); /* overriding the descriptor */
  563. kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
  564. kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  565. res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
  566. if (res) {
  567. usb_kill_urb(kaweth->rx_urb);
  568. return -EIO;
  569. }
  570. netif_start_queue(net);
  571. kaweth_async_set_rx_mode(kaweth);
  572. return 0;
  573. }
  574. /****************************************************************
  575. * kaweth_close
  576. ****************************************************************/
  577. static int kaweth_close(struct net_device *net)
  578. {
  579. struct kaweth_device *kaweth = netdev_priv(net);
  580. netif_stop_queue(net);
  581. kaweth->status |= KAWETH_STATUS_CLOSING;
  582. usb_kill_urb(kaweth->irq_urb);
  583. usb_kill_urb(kaweth->rx_urb);
  584. flush_scheduled_work();
  585. /* a scheduled work may have resubmitted,
  586. we hit them again */
  587. usb_kill_urb(kaweth->irq_urb);
  588. usb_kill_urb(kaweth->rx_urb);
  589. kaweth->status &= ~KAWETH_STATUS_CLOSING;
  590. return 0;
  591. }
  592. static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  593. {
  594. strlcpy(info->driver, driver_name, sizeof(info->driver));
  595. }
  596. static struct ethtool_ops ops = {
  597. .get_drvinfo = kaweth_get_drvinfo
  598. };
  599. /****************************************************************
  600. * kaweth_usb_transmit_complete
  601. ****************************************************************/
  602. static void kaweth_usb_transmit_complete(struct urb *urb, struct pt_regs *regs)
  603. {
  604. struct kaweth_device *kaweth = urb->context;
  605. struct sk_buff *skb = kaweth->tx_skb;
  606. if (unlikely(urb->status != 0))
  607. if (urb->status != -ENOENT)
  608. kaweth_dbg("%s: TX status %d.", kaweth->net->name, urb->status);
  609. netif_wake_queue(kaweth->net);
  610. dev_kfree_skb_irq(skb);
  611. }
  612. /****************************************************************
  613. * kaweth_start_xmit
  614. ****************************************************************/
  615. static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
  616. {
  617. struct kaweth_device *kaweth = netdev_priv(net);
  618. __le16 *private_header;
  619. int res;
  620. spin_lock(&kaweth->device_lock);
  621. if (kaweth->removed) {
  622. /* our device is undergoing disconnection - we bail out */
  623. spin_unlock(&kaweth->device_lock);
  624. dev_kfree_skb_irq(skb);
  625. return 0;
  626. }
  627. kaweth_async_set_rx_mode(kaweth);
  628. netif_stop_queue(net);
  629. /* We now decide whether we can put our special header into the sk_buff */
  630. if (skb_cloned(skb) || skb_headroom(skb) < 2) {
  631. /* no such luck - we make our own */
  632. struct sk_buff *copied_skb;
  633. copied_skb = skb_copy_expand(skb, 2, 0, GFP_ATOMIC);
  634. dev_kfree_skb_irq(skb);
  635. skb = copied_skb;
  636. if (!copied_skb) {
  637. kaweth->stats.tx_errors++;
  638. netif_start_queue(net);
  639. spin_unlock(&kaweth->device_lock);
  640. return 0;
  641. }
  642. }
  643. private_header = (__le16 *)__skb_push(skb, 2);
  644. *private_header = cpu_to_le16(skb->len-2);
  645. kaweth->tx_skb = skb;
  646. usb_fill_bulk_urb(kaweth->tx_urb,
  647. kaweth->dev,
  648. usb_sndbulkpipe(kaweth->dev, 2),
  649. private_header,
  650. skb->len,
  651. kaweth_usb_transmit_complete,
  652. kaweth);
  653. kaweth->end = 0;
  654. if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
  655. {
  656. kaweth_warn("kaweth failed tx_urb %d", res);
  657. kaweth->stats.tx_errors++;
  658. netif_start_queue(net);
  659. dev_kfree_skb_irq(skb);
  660. }
  661. else
  662. {
  663. kaweth->stats.tx_packets++;
  664. kaweth->stats.tx_bytes += skb->len;
  665. net->trans_start = jiffies;
  666. }
  667. spin_unlock(&kaweth->device_lock);
  668. return 0;
  669. }
  670. /****************************************************************
  671. * kaweth_set_rx_mode
  672. ****************************************************************/
  673. static void kaweth_set_rx_mode(struct net_device *net)
  674. {
  675. struct kaweth_device *kaweth = netdev_priv(net);
  676. __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
  677. KAWETH_PACKET_FILTER_BROADCAST |
  678. KAWETH_PACKET_FILTER_MULTICAST;
  679. kaweth_dbg("Setting Rx mode to %d", packet_filter_bitmap);
  680. netif_stop_queue(net);
  681. if (net->flags & IFF_PROMISC) {
  682. packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
  683. }
  684. else if ((net->mc_count) || (net->flags & IFF_ALLMULTI)) {
  685. packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
  686. }
  687. kaweth->packet_filter_bitmap = packet_filter_bitmap;
  688. netif_wake_queue(net);
  689. }
  690. /****************************************************************
  691. * kaweth_async_set_rx_mode
  692. ****************************************************************/
  693. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
  694. {
  695. __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
  696. kaweth->packet_filter_bitmap = 0;
  697. if (packet_filter_bitmap == 0)
  698. return;
  699. {
  700. int result;
  701. result = kaweth_control(kaweth,
  702. usb_sndctrlpipe(kaweth->dev, 0),
  703. KAWETH_COMMAND_SET_PACKET_FILTER,
  704. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  705. packet_filter_bitmap,
  706. 0,
  707. (void *)&kaweth->scratch,
  708. 0,
  709. KAWETH_CONTROL_TIMEOUT);
  710. if(result < 0) {
  711. kaweth_err("Failed to set Rx mode: %d", result);
  712. }
  713. else {
  714. kaweth_dbg("Set Rx mode to %d", packet_filter_bitmap);
  715. }
  716. }
  717. }
  718. /****************************************************************
  719. * kaweth_netdev_stats
  720. ****************************************************************/
  721. static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
  722. {
  723. struct kaweth_device *kaweth = netdev_priv(dev);
  724. return &kaweth->stats;
  725. }
  726. /****************************************************************
  727. * kaweth_tx_timeout
  728. ****************************************************************/
  729. static void kaweth_tx_timeout(struct net_device *net)
  730. {
  731. struct kaweth_device *kaweth = netdev_priv(net);
  732. kaweth_warn("%s: Tx timed out. Resetting.", net->name);
  733. kaweth->stats.tx_errors++;
  734. net->trans_start = jiffies;
  735. usb_unlink_urb(kaweth->tx_urb);
  736. }
  737. /****************************************************************
  738. * kaweth_probe
  739. ****************************************************************/
  740. static int kaweth_probe(
  741. struct usb_interface *intf,
  742. const struct usb_device_id *id /* from id_table */
  743. )
  744. {
  745. struct usb_device *dev = interface_to_usbdev(intf);
  746. struct kaweth_device *kaweth;
  747. struct net_device *netdev;
  748. const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  749. int result = 0;
  750. kaweth_dbg("Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x",
  751. dev->devnum,
  752. le16_to_cpu(dev->descriptor.idVendor),
  753. le16_to_cpu(dev->descriptor.idProduct),
  754. le16_to_cpu(dev->descriptor.bcdDevice));
  755. kaweth_dbg("Device at %p", dev);
  756. kaweth_dbg("Descriptor length: %x type: %x",
  757. (int)dev->descriptor.bLength,
  758. (int)dev->descriptor.bDescriptorType);
  759. netdev = alloc_etherdev(sizeof(*kaweth));
  760. if (!netdev)
  761. return -ENOMEM;
  762. kaweth = netdev_priv(netdev);
  763. kaweth->dev = dev;
  764. kaweth->net = netdev;
  765. spin_lock_init(&kaweth->device_lock);
  766. init_waitqueue_head(&kaweth->term_wait);
  767. kaweth_dbg("Resetting.");
  768. kaweth_reset(kaweth);
  769. /*
  770. * If high byte of bcdDevice is nonzero, firmware is already
  771. * downloaded. Don't try to do it again, or we'll hang the device.
  772. */
  773. if (le16_to_cpu(dev->descriptor.bcdDevice) >> 8) {
  774. kaweth_info("Firmware present in device.");
  775. } else {
  776. /* Download the firmware */
  777. kaweth_info("Downloading firmware...");
  778. kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
  779. if ((result = kaweth_download_firmware(kaweth,
  780. kaweth_new_code,
  781. len_kaweth_new_code,
  782. 100,
  783. 2)) < 0) {
  784. kaweth_err("Error downloading firmware (%d)", result);
  785. goto err_fw;
  786. }
  787. if ((result = kaweth_download_firmware(kaweth,
  788. kaweth_new_code_fix,
  789. len_kaweth_new_code_fix,
  790. 100,
  791. 3)) < 0) {
  792. kaweth_err("Error downloading firmware fix (%d)", result);
  793. goto err_fw;
  794. }
  795. if ((result = kaweth_download_firmware(kaweth,
  796. kaweth_trigger_code,
  797. len_kaweth_trigger_code,
  798. 126,
  799. 2)) < 0) {
  800. kaweth_err("Error downloading trigger code (%d)", result);
  801. goto err_fw;
  802. }
  803. if ((result = kaweth_download_firmware(kaweth,
  804. kaweth_trigger_code_fix,
  805. len_kaweth_trigger_code_fix,
  806. 126,
  807. 3)) < 0) {
  808. kaweth_err("Error downloading trigger code fix (%d)", result);
  809. goto err_fw;
  810. }
  811. if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
  812. kaweth_err("Error triggering firmware (%d)", result);
  813. goto err_fw;
  814. }
  815. /* Device will now disappear for a moment... */
  816. kaweth_info("Firmware loaded. I'll be back...");
  817. err_fw:
  818. free_page((unsigned long)kaweth->firmware_buf);
  819. free_netdev(netdev);
  820. return -EIO;
  821. }
  822. result = kaweth_read_configuration(kaweth);
  823. if(result < 0) {
  824. kaweth_err("Error reading configuration (%d), no net device created", result);
  825. goto err_free_netdev;
  826. }
  827. kaweth_info("Statistics collection: %x", kaweth->configuration.statistics_mask);
  828. kaweth_info("Multicast filter limit: %x", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
  829. kaweth_info("MTU: %d", le16_to_cpu(kaweth->configuration.segment_size));
  830. kaweth_info("Read MAC address %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
  831. (int)kaweth->configuration.hw_addr[0],
  832. (int)kaweth->configuration.hw_addr[1],
  833. (int)kaweth->configuration.hw_addr[2],
  834. (int)kaweth->configuration.hw_addr[3],
  835. (int)kaweth->configuration.hw_addr[4],
  836. (int)kaweth->configuration.hw_addr[5]);
  837. if(!memcmp(&kaweth->configuration.hw_addr,
  838. &bcast_addr,
  839. sizeof(bcast_addr))) {
  840. kaweth_err("Firmware not functioning properly, no net device created");
  841. goto err_free_netdev;
  842. }
  843. if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
  844. kaweth_dbg("Error setting URB size");
  845. goto err_free_netdev;
  846. }
  847. if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
  848. kaweth_err("Error setting SOFS wait");
  849. goto err_free_netdev;
  850. }
  851. result = kaweth_set_receive_filter(kaweth,
  852. KAWETH_PACKET_FILTER_DIRECTED |
  853. KAWETH_PACKET_FILTER_BROADCAST |
  854. KAWETH_PACKET_FILTER_MULTICAST);
  855. if(result < 0) {
  856. kaweth_err("Error setting receive filter");
  857. goto err_free_netdev;
  858. }
  859. kaweth_dbg("Initializing net device.");
  860. kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  861. if (!kaweth->tx_urb)
  862. goto err_free_netdev;
  863. kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  864. if (!kaweth->rx_urb)
  865. goto err_only_tx;
  866. kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  867. if (!kaweth->irq_urb)
  868. goto err_tx_and_rx;
  869. kaweth->intbuffer = usb_buffer_alloc( kaweth->dev,
  870. INTBUFFERSIZE,
  871. GFP_KERNEL,
  872. &kaweth->intbufferhandle);
  873. if (!kaweth->intbuffer)
  874. goto err_tx_and_rx_and_irq;
  875. kaweth->rx_buf = usb_buffer_alloc( kaweth->dev,
  876. KAWETH_BUF_SIZE,
  877. GFP_KERNEL,
  878. &kaweth->rxbufferhandle);
  879. if (!kaweth->rx_buf)
  880. goto err_all_but_rxbuf;
  881. memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
  882. memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
  883. sizeof(kaweth->configuration.hw_addr));
  884. netdev->open = kaweth_open;
  885. netdev->stop = kaweth_close;
  886. netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
  887. netdev->tx_timeout = kaweth_tx_timeout;
  888. netdev->hard_start_xmit = kaweth_start_xmit;
  889. netdev->set_multicast_list = kaweth_set_rx_mode;
  890. netdev->get_stats = kaweth_netdev_stats;
  891. netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
  892. SET_ETHTOOL_OPS(netdev, &ops);
  893. /* kaweth is zeroed as part of alloc_netdev */
  894. INIT_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl, (void *)kaweth);
  895. SET_MODULE_OWNER(netdev);
  896. usb_set_intfdata(intf, kaweth);
  897. #if 0
  898. // dma_supported() is deeply broken on almost all architectures
  899. if (dma_supported (&intf->dev, 0xffffffffffffffffULL))
  900. kaweth->net->features |= NETIF_F_HIGHDMA;
  901. #endif
  902. SET_NETDEV_DEV(netdev, &intf->dev);
  903. if (register_netdev(netdev) != 0) {
  904. kaweth_err("Error registering netdev.");
  905. goto err_intfdata;
  906. }
  907. kaweth_info("kaweth interface created at %s", kaweth->net->name);
  908. kaweth_dbg("Kaweth probe returning.");
  909. return 0;
  910. err_intfdata:
  911. usb_set_intfdata(intf, NULL);
  912. usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  913. err_all_but_rxbuf:
  914. usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  915. err_tx_and_rx_and_irq:
  916. usb_free_urb(kaweth->irq_urb);
  917. err_tx_and_rx:
  918. usb_free_urb(kaweth->rx_urb);
  919. err_only_tx:
  920. usb_free_urb(kaweth->tx_urb);
  921. err_free_netdev:
  922. free_netdev(netdev);
  923. return -EIO;
  924. }
  925. /****************************************************************
  926. * kaweth_disconnect
  927. ****************************************************************/
  928. static void kaweth_disconnect(struct usb_interface *intf)
  929. {
  930. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  931. struct net_device *netdev;
  932. kaweth_info("Unregistering");
  933. usb_set_intfdata(intf, NULL);
  934. if (!kaweth) {
  935. kaweth_warn("unregistering non-existant device");
  936. return;
  937. }
  938. netdev = kaweth->net;
  939. kaweth->removed = 1;
  940. usb_kill_urb(kaweth->irq_urb);
  941. usb_kill_urb(kaweth->rx_urb);
  942. usb_kill_urb(kaweth->tx_urb);
  943. kaweth_dbg("Unregistering net device");
  944. unregister_netdev(netdev);
  945. usb_free_urb(kaweth->rx_urb);
  946. usb_free_urb(kaweth->tx_urb);
  947. usb_free_urb(kaweth->irq_urb);
  948. usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  949. usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  950. free_netdev(netdev);
  951. }
  952. // FIXME this completion stuff is a modified clone of
  953. // an OLD version of some stuff in usb.c ...
  954. struct usb_api_data {
  955. wait_queue_head_t wqh;
  956. int done;
  957. };
  958. /*-------------------------------------------------------------------*
  959. * completion handler for compatibility wrappers (sync control/bulk) *
  960. *-------------------------------------------------------------------*/
  961. static void usb_api_blocking_completion(struct urb *urb, struct pt_regs *regs)
  962. {
  963. struct usb_api_data *awd = (struct usb_api_data *)urb->context;
  964. awd->done=1;
  965. wake_up(&awd->wqh);
  966. }
  967. /*-------------------------------------------------------------------*
  968. * COMPATIBILITY STUFF *
  969. *-------------------------------------------------------------------*/
  970. // Starts urb and waits for completion or timeout
  971. static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
  972. {
  973. struct usb_api_data awd;
  974. int status;
  975. init_waitqueue_head(&awd.wqh);
  976. awd.done = 0;
  977. urb->context = &awd;
  978. status = usb_submit_urb(urb, GFP_NOIO);
  979. if (status) {
  980. // something went wrong
  981. usb_free_urb(urb);
  982. return status;
  983. }
  984. if (!wait_event_timeout(awd.wqh, awd.done, timeout)) {
  985. // timeout
  986. kaweth_warn("usb_control/bulk_msg: timeout");
  987. usb_kill_urb(urb); // remove urb safely
  988. status = -ETIMEDOUT;
  989. }
  990. else {
  991. status = urb->status;
  992. }
  993. if (actual_length) {
  994. *actual_length = urb->actual_length;
  995. }
  996. usb_free_urb(urb);
  997. return status;
  998. }
  999. /*-------------------------------------------------------------------*/
  1000. // returns status (negative) or length (positive)
  1001. static int kaweth_internal_control_msg(struct usb_device *usb_dev,
  1002. unsigned int pipe,
  1003. struct usb_ctrlrequest *cmd, void *data,
  1004. int len, int timeout)
  1005. {
  1006. struct urb *urb;
  1007. int retv;
  1008. int length;
  1009. urb = usb_alloc_urb(0, GFP_NOIO);
  1010. if (!urb)
  1011. return -ENOMEM;
  1012. usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data,
  1013. len, usb_api_blocking_completion, NULL);
  1014. retv = usb_start_wait_urb(urb, timeout, &length);
  1015. if (retv < 0) {
  1016. return retv;
  1017. }
  1018. else {
  1019. return length;
  1020. }
  1021. }
  1022. /****************************************************************
  1023. * kaweth_init
  1024. ****************************************************************/
  1025. static int __init kaweth_init(void)
  1026. {
  1027. kaweth_dbg("Driver loading");
  1028. return usb_register(&kaweth_driver);
  1029. }
  1030. /****************************************************************
  1031. * kaweth_exit
  1032. ****************************************************************/
  1033. static void __exit kaweth_exit(void)
  1034. {
  1035. usb_deregister(&kaweth_driver);
  1036. }
  1037. module_init(kaweth_init);
  1038. module_exit(kaweth_exit);