kaweth.c 36 KB

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