kaweth.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /****************************************************************
  3. *
  4. * kaweth.c - driver for KL5KUSB101 based USB->Ethernet
  5. *
  6. * (c) 2000 Interlan Communications
  7. * (c) 2000 Stephane Alnet
  8. * (C) 2001 Brad Hards
  9. * (C) 2002 Oliver Neukum
  10. *
  11. * Original author: The Zapman <zapman@interlan.net>
  12. * Inspired by, and much credit goes to Michael Rothwell
  13. * <rothwell@interlan.net> for the test equipment, help, and patience
  14. * Based off of (and with thanks to) Petko Manolov's pegaus.c driver.
  15. * Also many thanks to Joel Silverman and Ed Surprenant at Kawasaki
  16. * for providing the firmware and driver resources.
  17. *
  18. ****************************************************************/
  19. /* TODO:
  20. * Develop test procedures for USB net interfaces
  21. * Run test procedures
  22. * Fix bugs from previous two steps
  23. * Snoop other OSs for any tricks we're not doing
  24. * Reduce arbitrary timeouts
  25. * Smart multicast support
  26. * Temporary MAC change support
  27. * Tunable SOFs parameter - ioctl()?
  28. * Ethernet stats collection
  29. * Code formatting improvements
  30. */
  31. #include <linux/module.h>
  32. #include <linux/slab.h>
  33. #include <linux/string.h>
  34. #include <linux/delay.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/usb.h>
  38. #include <linux/types.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/wait.h>
  42. #include <linux/firmware.h>
  43. #include <linux/uaccess.h>
  44. #include <asm/byteorder.h>
  45. #undef DEBUG
  46. #define KAWETH_MTU 1514
  47. #define KAWETH_BUF_SIZE 1664
  48. #define KAWETH_TX_TIMEOUT (5 * HZ)
  49. #define KAWETH_SCRATCH_SIZE 32
  50. #define KAWETH_FIRMWARE_BUF_SIZE 4096
  51. #define KAWETH_CONTROL_TIMEOUT (30000)
  52. #define KAWETH_STATUS_BROKEN 0x0000001
  53. #define KAWETH_STATUS_CLOSING 0x0000002
  54. #define KAWETH_STATUS_SUSPENDING 0x0000004
  55. #define KAWETH_STATUS_BLOCKED (KAWETH_STATUS_CLOSING | KAWETH_STATUS_SUSPENDING)
  56. #define KAWETH_PACKET_FILTER_PROMISCUOUS 0x01
  57. #define KAWETH_PACKET_FILTER_ALL_MULTICAST 0x02
  58. #define KAWETH_PACKET_FILTER_DIRECTED 0x04
  59. #define KAWETH_PACKET_FILTER_BROADCAST 0x08
  60. #define KAWETH_PACKET_FILTER_MULTICAST 0x10
  61. /* Table 7 */
  62. #define KAWETH_COMMAND_GET_ETHERNET_DESC 0x00
  63. #define KAWETH_COMMAND_MULTICAST_FILTERS 0x01
  64. #define KAWETH_COMMAND_SET_PACKET_FILTER 0x02
  65. #define KAWETH_COMMAND_STATISTICS 0x03
  66. #define KAWETH_COMMAND_SET_TEMP_MAC 0x06
  67. #define KAWETH_COMMAND_GET_TEMP_MAC 0x07
  68. #define KAWETH_COMMAND_SET_URB_SIZE 0x08
  69. #define KAWETH_COMMAND_SET_SOFS_WAIT 0x09
  70. #define KAWETH_COMMAND_SCAN 0xFF
  71. #define KAWETH_SOFS_TO_WAIT 0x05
  72. #define INTBUFFERSIZE 4
  73. #define STATE_OFFSET 0
  74. #define STATE_MASK 0x40
  75. #define STATE_SHIFT 5
  76. #define IS_BLOCKED(s) (s & KAWETH_STATUS_BLOCKED)
  77. 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>");
  78. MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
  79. MODULE_LICENSE("GPL");
  80. MODULE_FIRMWARE("kaweth/new_code.bin");
  81. MODULE_FIRMWARE("kaweth/new_code_fix.bin");
  82. MODULE_FIRMWARE("kaweth/trigger_code.bin");
  83. MODULE_FIRMWARE("kaweth/trigger_code_fix.bin");
  84. static const char driver_name[] = "kaweth";
  85. static int kaweth_probe(
  86. struct usb_interface *intf,
  87. const struct usb_device_id *id /* from id_table */
  88. );
  89. static void kaweth_disconnect(struct usb_interface *intf);
  90. static int kaweth_suspend(struct usb_interface *intf, pm_message_t message);
  91. static int kaweth_resume(struct usb_interface *intf);
  92. /****************************************************************
  93. * usb_device_id
  94. ****************************************************************/
  95. static const struct usb_device_id usb_klsi_table[] = {
  96. { USB_DEVICE(0x03e8, 0x0008) }, /* AOX Endpoints USB Ethernet */
  97. { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USB-ET/T */
  98. { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
  99. { USB_DEVICE(0x0506, 0x11f8) }, /* 3Com 3C460 */
  100. { USB_DEVICE(0x0557, 0x2002) }, /* ATEN USB Ethernet */
  101. { USB_DEVICE(0x0557, 0x4000) }, /* D-Link DSB-650C */
  102. { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Enet */
  103. { USB_DEVICE(0x0565, 0x0003) }, /* Optus@Home UEP1045A */
  104. { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Enet2 */
  105. { USB_DEVICE(0x05e9, 0x0008) }, /* KLSI KL5KUSB101B */
  106. { USB_DEVICE(0x05e9, 0x0009) }, /* KLSI KL5KUSB101B (Board change) */
  107. { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
  108. { USB_DEVICE(0x06e1, 0x0008) }, /* ADS USB-10BT */
  109. { USB_DEVICE(0x06e1, 0x0009) }, /* ADS USB-10BT */
  110. { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2202USB */
  111. { USB_DEVICE(0x07aa, 0x0001) }, /* Correga K.K. */
  112. { USB_DEVICE(0x07b8, 0x4000) }, /* D-Link DU-E10 */
  113. { USB_DEVICE(0x07c9, 0xb010) }, /* Allied Telesyn AT-USB10 USB Ethernet Adapter */
  114. { USB_DEVICE(0x0846, 0x1001) }, /* NetGear EA-101 */
  115. { USB_DEVICE(0x0846, 0x1002) }, /* NetGear EA-101 */
  116. { USB_DEVICE(0x085a, 0x0008) }, /* PortGear Ethernet Adapter */
  117. { USB_DEVICE(0x085a, 0x0009) }, /* PortGear Ethernet Adapter */
  118. { USB_DEVICE(0x087d, 0x5704) }, /* Jaton USB Ethernet Device Adapter */
  119. { USB_DEVICE(0x0951, 0x0008) }, /* Kingston Technology USB Ethernet Adapter */
  120. { USB_DEVICE(0x095a, 0x3003) }, /* Portsmith Express Ethernet Adapter */
  121. { USB_DEVICE(0x10bd, 0x1427) }, /* ASANTE USB To Ethernet Adapter */
  122. { USB_DEVICE(0x1342, 0x0204) }, /* Mobility USB-Ethernet Adapter */
  123. { USB_DEVICE(0x13d2, 0x0400) }, /* Shark Pocket Adapter */
  124. { USB_DEVICE(0x1485, 0x0001) }, /* Silicom U2E */
  125. { USB_DEVICE(0x1485, 0x0002) }, /* Psion Dacom Gold Port Ethernet */
  126. { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45 */
  127. { USB_DEVICE(0x1645, 0x0008) }, /* Entrega USB Ethernet Adapter */
  128. { USB_DEVICE(0x1645, 0x8005) }, /* PortGear Ethernet Adapter */
  129. { USB_DEVICE(0x1668, 0x0323) }, /* Actiontec USB Ethernet */
  130. { USB_DEVICE(0x2001, 0x4000) }, /* D-link DSB-650C */
  131. {} /* Null terminator */
  132. };
  133. MODULE_DEVICE_TABLE (usb, usb_klsi_table);
  134. /****************************************************************
  135. * kaweth_driver
  136. ****************************************************************/
  137. static struct usb_driver kaweth_driver = {
  138. .name = driver_name,
  139. .probe = kaweth_probe,
  140. .disconnect = kaweth_disconnect,
  141. .suspend = kaweth_suspend,
  142. .resume = kaweth_resume,
  143. .id_table = usb_klsi_table,
  144. .supports_autosuspend = 1,
  145. .disable_hub_initiated_lpm = 1,
  146. };
  147. typedef __u8 eth_addr_t[6];
  148. /****************************************************************
  149. * usb_eth_dev
  150. ****************************************************************/
  151. struct usb_eth_dev {
  152. char *name;
  153. __u16 vendor;
  154. __u16 device;
  155. void *pdata;
  156. };
  157. /****************************************************************
  158. * kaweth_ethernet_configuration
  159. * Refer Table 8
  160. ****************************************************************/
  161. struct kaweth_ethernet_configuration
  162. {
  163. __u8 size;
  164. __u8 reserved1;
  165. __u8 reserved2;
  166. eth_addr_t hw_addr;
  167. __u32 statistics_mask;
  168. __le16 segment_size;
  169. __u16 max_multicast_filters;
  170. __u8 reserved3;
  171. } __packed;
  172. /****************************************************************
  173. * kaweth_device
  174. ****************************************************************/
  175. struct kaweth_device
  176. {
  177. spinlock_t device_lock;
  178. __u32 status;
  179. int end;
  180. int suspend_lowmem_rx;
  181. int suspend_lowmem_ctrl;
  182. int linkstate;
  183. int opened;
  184. struct delayed_work lowmem_work;
  185. struct usb_device *dev;
  186. struct usb_interface *intf;
  187. struct net_device *net;
  188. wait_queue_head_t term_wait;
  189. struct urb *rx_urb;
  190. struct urb *tx_urb;
  191. struct urb *irq_urb;
  192. dma_addr_t intbufferhandle;
  193. __u8 *intbuffer;
  194. dma_addr_t rxbufferhandle;
  195. __u8 *rx_buf;
  196. struct sk_buff *tx_skb;
  197. __u8 *firmware_buf;
  198. __u8 scratch[KAWETH_SCRATCH_SIZE];
  199. __u16 packet_filter_bitmap;
  200. struct kaweth_ethernet_configuration configuration;
  201. };
  202. /****************************************************************
  203. * kaweth_read_configuration
  204. ****************************************************************/
  205. static int kaweth_read_configuration(struct kaweth_device *kaweth)
  206. {
  207. return usb_control_msg(kaweth->dev, usb_rcvctrlpipe(kaweth->dev, 0),
  208. KAWETH_COMMAND_GET_ETHERNET_DESC,
  209. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  210. 0, 0,
  211. &kaweth->configuration,
  212. sizeof(kaweth->configuration),
  213. KAWETH_CONTROL_TIMEOUT);
  214. }
  215. /****************************************************************
  216. * kaweth_set_urb_size
  217. ****************************************************************/
  218. static int kaweth_set_urb_size(struct kaweth_device *kaweth, __u16 urb_size)
  219. {
  220. netdev_dbg(kaweth->net, "Setting URB size to %d\n", (unsigned)urb_size);
  221. return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
  222. KAWETH_COMMAND_SET_URB_SIZE,
  223. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  224. urb_size, 0,
  225. &kaweth->scratch, 0,
  226. KAWETH_CONTROL_TIMEOUT);
  227. }
  228. /****************************************************************
  229. * kaweth_set_sofs_wait
  230. ****************************************************************/
  231. static int kaweth_set_sofs_wait(struct kaweth_device *kaweth, __u16 sofs_wait)
  232. {
  233. netdev_dbg(kaweth->net, "Set SOFS wait to %d\n", (unsigned)sofs_wait);
  234. return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
  235. KAWETH_COMMAND_SET_SOFS_WAIT,
  236. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  237. sofs_wait, 0,
  238. &kaweth->scratch, 0,
  239. KAWETH_CONTROL_TIMEOUT);
  240. }
  241. /****************************************************************
  242. * kaweth_set_receive_filter
  243. ****************************************************************/
  244. static int kaweth_set_receive_filter(struct kaweth_device *kaweth,
  245. __u16 receive_filter)
  246. {
  247. netdev_dbg(kaweth->net, "Set receive filter to %d\n",
  248. (unsigned)receive_filter);
  249. return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
  250. KAWETH_COMMAND_SET_PACKET_FILTER,
  251. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  252. receive_filter, 0,
  253. &kaweth->scratch, 0,
  254. KAWETH_CONTROL_TIMEOUT);
  255. }
  256. /****************************************************************
  257. * kaweth_download_firmware
  258. ****************************************************************/
  259. static int kaweth_download_firmware(struct kaweth_device *kaweth,
  260. const char *fwname,
  261. __u8 interrupt,
  262. __u8 type)
  263. {
  264. const struct firmware *fw;
  265. int data_len;
  266. int ret;
  267. ret = request_firmware(&fw, fwname, &kaweth->dev->dev);
  268. if (ret) {
  269. dev_err(&kaweth->intf->dev, "Firmware request failed\n");
  270. return ret;
  271. }
  272. if (fw->size > KAWETH_FIRMWARE_BUF_SIZE) {
  273. dev_err(&kaweth->intf->dev, "Firmware too big: %zu\n",
  274. fw->size);
  275. release_firmware(fw);
  276. return -ENOSPC;
  277. }
  278. data_len = fw->size;
  279. memcpy(kaweth->firmware_buf, fw->data, fw->size);
  280. release_firmware(fw);
  281. kaweth->firmware_buf[2] = (data_len & 0xFF) - 7;
  282. kaweth->firmware_buf[3] = data_len >> 8;
  283. kaweth->firmware_buf[4] = type;
  284. kaweth->firmware_buf[5] = interrupt;
  285. netdev_dbg(kaweth->net, "High: %i, Low:%i\n", kaweth->firmware_buf[3],
  286. kaweth->firmware_buf[2]);
  287. netdev_dbg(kaweth->net,
  288. "Downloading firmware at %p to kaweth device at %p\n",
  289. kaweth->firmware_buf, kaweth);
  290. netdev_dbg(kaweth->net, "Firmware length: %d\n", data_len);
  291. return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
  292. KAWETH_COMMAND_SCAN,
  293. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  294. 0, 0,
  295. kaweth->firmware_buf, data_len,
  296. KAWETH_CONTROL_TIMEOUT);
  297. }
  298. /****************************************************************
  299. * kaweth_trigger_firmware
  300. ****************************************************************/
  301. static int kaweth_trigger_firmware(struct kaweth_device *kaweth,
  302. __u8 interrupt)
  303. {
  304. kaweth->firmware_buf[0] = 0xB6;
  305. kaweth->firmware_buf[1] = 0xC3;
  306. kaweth->firmware_buf[2] = 0x01;
  307. kaweth->firmware_buf[3] = 0x00;
  308. kaweth->firmware_buf[4] = 0x06;
  309. kaweth->firmware_buf[5] = interrupt;
  310. kaweth->firmware_buf[6] = 0x00;
  311. kaweth->firmware_buf[7] = 0x00;
  312. return usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
  313. KAWETH_COMMAND_SCAN,
  314. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  315. 0, 0,
  316. (void *)kaweth->firmware_buf, 8,
  317. KAWETH_CONTROL_TIMEOUT);
  318. }
  319. /****************************************************************
  320. * kaweth_reset
  321. ****************************************************************/
  322. static int kaweth_reset(struct kaweth_device *kaweth)
  323. {
  324. int result;
  325. result = usb_reset_configuration(kaweth->dev);
  326. mdelay(10);
  327. netdev_dbg(kaweth->net, "kaweth_reset() returns %d.\n", result);
  328. return result;
  329. }
  330. static void kaweth_usb_receive(struct urb *);
  331. static int kaweth_resubmit_rx_urb(struct kaweth_device *, gfp_t);
  332. /****************************************************************
  333. int_callback
  334. *****************************************************************/
  335. static void kaweth_resubmit_int_urb(struct kaweth_device *kaweth, gfp_t mf)
  336. {
  337. int status;
  338. status = usb_submit_urb (kaweth->irq_urb, mf);
  339. if (unlikely(status == -ENOMEM)) {
  340. kaweth->suspend_lowmem_ctrl = 1;
  341. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  342. } else {
  343. kaweth->suspend_lowmem_ctrl = 0;
  344. }
  345. if (status)
  346. dev_err(&kaweth->intf->dev,
  347. "can't resubmit intr, %s-%s, status %d\n",
  348. kaweth->dev->bus->bus_name,
  349. kaweth->dev->devpath, status);
  350. }
  351. static void int_callback(struct urb *u)
  352. {
  353. struct kaweth_device *kaweth = u->context;
  354. int act_state;
  355. int status = u->status;
  356. switch (status) {
  357. case 0: /* success */
  358. break;
  359. case -ECONNRESET: /* unlink */
  360. case -ENOENT:
  361. case -ESHUTDOWN:
  362. return;
  363. /* -EPIPE: should clear the halt */
  364. default: /* error */
  365. goto resubmit;
  366. }
  367. /* we check the link state to report changes */
  368. if (kaweth->linkstate != (act_state = ( kaweth->intbuffer[STATE_OFFSET] | STATE_MASK) >> STATE_SHIFT)) {
  369. if (act_state)
  370. netif_carrier_on(kaweth->net);
  371. else
  372. netif_carrier_off(kaweth->net);
  373. kaweth->linkstate = act_state;
  374. }
  375. resubmit:
  376. kaweth_resubmit_int_urb(kaweth, GFP_ATOMIC);
  377. }
  378. static void kaweth_resubmit_tl(struct work_struct *work)
  379. {
  380. struct kaweth_device *kaweth =
  381. container_of(work, struct kaweth_device, lowmem_work.work);
  382. if (IS_BLOCKED(kaweth->status))
  383. return;
  384. if (kaweth->suspend_lowmem_rx)
  385. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  386. if (kaweth->suspend_lowmem_ctrl)
  387. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  388. }
  389. /****************************************************************
  390. * kaweth_resubmit_rx_urb
  391. ****************************************************************/
  392. static int kaweth_resubmit_rx_urb(struct kaweth_device *kaweth,
  393. gfp_t mem_flags)
  394. {
  395. int result;
  396. usb_fill_bulk_urb(kaweth->rx_urb,
  397. kaweth->dev,
  398. usb_rcvbulkpipe(kaweth->dev, 1),
  399. kaweth->rx_buf,
  400. KAWETH_BUF_SIZE,
  401. kaweth_usb_receive,
  402. kaweth);
  403. kaweth->rx_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  404. kaweth->rx_urb->transfer_dma = kaweth->rxbufferhandle;
  405. if((result = usb_submit_urb(kaweth->rx_urb, mem_flags))) {
  406. if (result == -ENOMEM) {
  407. kaweth->suspend_lowmem_rx = 1;
  408. schedule_delayed_work(&kaweth->lowmem_work, HZ/4);
  409. }
  410. dev_err(&kaweth->intf->dev, "resubmitting rx_urb %d failed\n",
  411. result);
  412. } else {
  413. kaweth->suspend_lowmem_rx = 0;
  414. }
  415. return result;
  416. }
  417. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth,
  418. bool may_sleep);
  419. /****************************************************************
  420. * kaweth_usb_receive
  421. ****************************************************************/
  422. static void kaweth_usb_receive(struct urb *urb)
  423. {
  424. struct device *dev = &urb->dev->dev;
  425. struct kaweth_device *kaweth = urb->context;
  426. struct net_device *net = kaweth->net;
  427. int status = urb->status;
  428. unsigned long flags;
  429. int count = urb->actual_length;
  430. int count2 = urb->transfer_buffer_length;
  431. __u16 pkt_len = le16_to_cpup((__le16 *)kaweth->rx_buf);
  432. struct sk_buff *skb;
  433. if (unlikely(status == -EPIPE)) {
  434. net->stats.rx_errors++;
  435. kaweth->end = 1;
  436. wake_up(&kaweth->term_wait);
  437. dev_dbg(dev, "Status was -EPIPE.\n");
  438. return;
  439. }
  440. if (unlikely(status == -ECONNRESET || status == -ESHUTDOWN)) {
  441. /* we are killed - set a flag and wake the disconnect handler */
  442. kaweth->end = 1;
  443. wake_up(&kaweth->term_wait);
  444. dev_dbg(dev, "Status was -ECONNRESET or -ESHUTDOWN.\n");
  445. return;
  446. }
  447. if (unlikely(status == -EPROTO || status == -ETIME ||
  448. status == -EILSEQ)) {
  449. net->stats.rx_errors++;
  450. dev_dbg(dev, "Status was -EPROTO, -ETIME, or -EILSEQ.\n");
  451. return;
  452. }
  453. if (unlikely(status == -EOVERFLOW)) {
  454. net->stats.rx_errors++;
  455. dev_dbg(dev, "Status was -EOVERFLOW.\n");
  456. }
  457. spin_lock_irqsave(&kaweth->device_lock, flags);
  458. if (IS_BLOCKED(kaweth->status)) {
  459. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  460. return;
  461. }
  462. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  463. if(status && status != -EREMOTEIO && count != 1) {
  464. dev_err(&kaweth->intf->dev,
  465. "%s RX status: %d count: %d packet_len: %d\n",
  466. net->name, status, count, (int)pkt_len);
  467. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  468. return;
  469. }
  470. if(kaweth->net && (count > 2)) {
  471. if(pkt_len > (count - 2)) {
  472. dev_err(&kaweth->intf->dev,
  473. "Packet length too long for USB frame (pkt_len: %x, count: %x)\n",
  474. pkt_len, count);
  475. dev_err(&kaweth->intf->dev, "Packet len & 2047: %x\n",
  476. pkt_len & 2047);
  477. dev_err(&kaweth->intf->dev, "Count 2: %x\n", count2);
  478. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  479. return;
  480. }
  481. if(!(skb = dev_alloc_skb(pkt_len+2))) {
  482. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  483. return;
  484. }
  485. skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
  486. skb_copy_to_linear_data(skb, kaweth->rx_buf + 2, pkt_len);
  487. skb_put(skb, pkt_len);
  488. skb->protocol = eth_type_trans(skb, net);
  489. netif_rx(skb);
  490. net->stats.rx_packets++;
  491. net->stats.rx_bytes += pkt_len;
  492. }
  493. kaweth_resubmit_rx_urb(kaweth, GFP_ATOMIC);
  494. }
  495. /****************************************************************
  496. * kaweth_open
  497. ****************************************************************/
  498. static int kaweth_open(struct net_device *net)
  499. {
  500. struct kaweth_device *kaweth = netdev_priv(net);
  501. int res;
  502. res = usb_autopm_get_interface(kaweth->intf);
  503. if (res) {
  504. dev_err(&kaweth->intf->dev, "Interface cannot be resumed.\n");
  505. return -EIO;
  506. }
  507. res = kaweth_resubmit_rx_urb(kaweth, GFP_KERNEL);
  508. if (res)
  509. goto err_out;
  510. usb_fill_int_urb(
  511. kaweth->irq_urb,
  512. kaweth->dev,
  513. usb_rcvintpipe(kaweth->dev, 3),
  514. kaweth->intbuffer,
  515. INTBUFFERSIZE,
  516. int_callback,
  517. kaweth,
  518. 250); /* overriding the descriptor */
  519. kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
  520. kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  521. res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
  522. if (res) {
  523. usb_kill_urb(kaweth->rx_urb);
  524. goto err_out;
  525. }
  526. kaweth->opened = 1;
  527. netif_start_queue(net);
  528. kaweth_async_set_rx_mode(kaweth, true);
  529. return 0;
  530. err_out:
  531. usb_autopm_put_interface(kaweth->intf);
  532. return -EIO;
  533. }
  534. /****************************************************************
  535. * kaweth_kill_urbs
  536. ****************************************************************/
  537. static void kaweth_kill_urbs(struct kaweth_device *kaweth)
  538. {
  539. usb_kill_urb(kaweth->irq_urb);
  540. usb_kill_urb(kaweth->rx_urb);
  541. usb_kill_urb(kaweth->tx_urb);
  542. cancel_delayed_work_sync(&kaweth->lowmem_work);
  543. /* a scheduled work may have resubmitted,
  544. we hit them again */
  545. usb_kill_urb(kaweth->irq_urb);
  546. usb_kill_urb(kaweth->rx_urb);
  547. }
  548. /****************************************************************
  549. * kaweth_close
  550. ****************************************************************/
  551. static int kaweth_close(struct net_device *net)
  552. {
  553. struct kaweth_device *kaweth = netdev_priv(net);
  554. netif_stop_queue(net);
  555. kaweth->opened = 0;
  556. kaweth->status |= KAWETH_STATUS_CLOSING;
  557. kaweth_kill_urbs(kaweth);
  558. kaweth->status &= ~KAWETH_STATUS_CLOSING;
  559. usb_autopm_put_interface(kaweth->intf);
  560. return 0;
  561. }
  562. static u32 kaweth_get_link(struct net_device *dev)
  563. {
  564. struct kaweth_device *kaweth = netdev_priv(dev);
  565. return kaweth->linkstate;
  566. }
  567. static const struct ethtool_ops ops = {
  568. .get_link = kaweth_get_link
  569. };
  570. /****************************************************************
  571. * kaweth_usb_transmit_complete
  572. ****************************************************************/
  573. static void kaweth_usb_transmit_complete(struct urb *urb)
  574. {
  575. struct kaweth_device *kaweth = urb->context;
  576. struct sk_buff *skb = kaweth->tx_skb;
  577. int status = urb->status;
  578. if (unlikely(status != 0))
  579. if (status != -ENOENT)
  580. dev_dbg(&urb->dev->dev, "%s: TX status %d.\n",
  581. kaweth->net->name, status);
  582. netif_wake_queue(kaweth->net);
  583. dev_kfree_skb_irq(skb);
  584. }
  585. /****************************************************************
  586. * kaweth_start_xmit
  587. ****************************************************************/
  588. static netdev_tx_t kaweth_start_xmit(struct sk_buff *skb,
  589. struct net_device *net)
  590. {
  591. struct kaweth_device *kaweth = netdev_priv(net);
  592. __le16 *private_header;
  593. int res;
  594. spin_lock_irq(&kaweth->device_lock);
  595. kaweth_async_set_rx_mode(kaweth, false);
  596. netif_stop_queue(net);
  597. if (IS_BLOCKED(kaweth->status)) {
  598. goto skip;
  599. }
  600. /* We now decide whether we can put our special header into the sk_buff */
  601. if (skb_cow_head(skb, 2)) {
  602. net->stats.tx_errors++;
  603. netif_start_queue(net);
  604. spin_unlock_irq(&kaweth->device_lock);
  605. dev_kfree_skb_any(skb);
  606. return NETDEV_TX_OK;
  607. }
  608. private_header = __skb_push(skb, 2);
  609. *private_header = cpu_to_le16(skb->len-2);
  610. kaweth->tx_skb = skb;
  611. usb_fill_bulk_urb(kaweth->tx_urb,
  612. kaweth->dev,
  613. usb_sndbulkpipe(kaweth->dev, 2),
  614. private_header,
  615. skb->len,
  616. kaweth_usb_transmit_complete,
  617. kaweth);
  618. kaweth->end = 0;
  619. if((res = usb_submit_urb(kaweth->tx_urb, GFP_ATOMIC)))
  620. {
  621. dev_warn(&net->dev, "kaweth failed tx_urb %d\n", res);
  622. skip:
  623. net->stats.tx_errors++;
  624. netif_start_queue(net);
  625. dev_kfree_skb_irq(skb);
  626. }
  627. else
  628. {
  629. net->stats.tx_packets++;
  630. net->stats.tx_bytes += skb->len;
  631. }
  632. spin_unlock_irq(&kaweth->device_lock);
  633. return NETDEV_TX_OK;
  634. }
  635. /****************************************************************
  636. * kaweth_set_rx_mode
  637. ****************************************************************/
  638. static void kaweth_set_rx_mode(struct net_device *net)
  639. {
  640. struct kaweth_device *kaweth = netdev_priv(net);
  641. __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
  642. KAWETH_PACKET_FILTER_BROADCAST |
  643. KAWETH_PACKET_FILTER_MULTICAST;
  644. netdev_dbg(net, "Setting Rx mode to %d\n", packet_filter_bitmap);
  645. if (net->flags & IFF_PROMISC) {
  646. packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
  647. }
  648. else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
  649. packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
  650. }
  651. kaweth->packet_filter_bitmap = packet_filter_bitmap;
  652. }
  653. /****************************************************************
  654. * kaweth_async_set_rx_mode
  655. ****************************************************************/
  656. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth,
  657. bool may_sleep)
  658. {
  659. int ret;
  660. __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
  661. kaweth->packet_filter_bitmap = 0;
  662. if (packet_filter_bitmap == 0)
  663. return;
  664. if (!may_sleep)
  665. return;
  666. ret = usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
  667. KAWETH_COMMAND_SET_PACKET_FILTER,
  668. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  669. packet_filter_bitmap, 0,
  670. &kaweth->scratch, 0,
  671. KAWETH_CONTROL_TIMEOUT);
  672. if (ret < 0)
  673. dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
  674. ret);
  675. else
  676. netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
  677. packet_filter_bitmap);
  678. }
  679. /****************************************************************
  680. * kaweth_tx_timeout
  681. ****************************************************************/
  682. static void kaweth_tx_timeout(struct net_device *net, unsigned int txqueue)
  683. {
  684. struct kaweth_device *kaweth = netdev_priv(net);
  685. dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
  686. net->stats.tx_errors++;
  687. netif_trans_update(net);
  688. usb_unlink_urb(kaweth->tx_urb);
  689. }
  690. /****************************************************************
  691. * kaweth_suspend
  692. ****************************************************************/
  693. static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
  694. {
  695. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  696. unsigned long flags;
  697. spin_lock_irqsave(&kaweth->device_lock, flags);
  698. kaweth->status |= KAWETH_STATUS_SUSPENDING;
  699. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  700. kaweth_kill_urbs(kaweth);
  701. return 0;
  702. }
  703. /****************************************************************
  704. * kaweth_resume
  705. ****************************************************************/
  706. static int kaweth_resume(struct usb_interface *intf)
  707. {
  708. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  709. unsigned long flags;
  710. spin_lock_irqsave(&kaweth->device_lock, flags);
  711. kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
  712. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  713. if (!kaweth->opened)
  714. return 0;
  715. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  716. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  717. return 0;
  718. }
  719. /****************************************************************
  720. * kaweth_probe
  721. ****************************************************************/
  722. static const struct net_device_ops kaweth_netdev_ops = {
  723. .ndo_open = kaweth_open,
  724. .ndo_stop = kaweth_close,
  725. .ndo_start_xmit = kaweth_start_xmit,
  726. .ndo_tx_timeout = kaweth_tx_timeout,
  727. .ndo_set_rx_mode = kaweth_set_rx_mode,
  728. .ndo_set_mac_address = eth_mac_addr,
  729. .ndo_validate_addr = eth_validate_addr,
  730. };
  731. static int kaweth_probe(
  732. struct usb_interface *intf,
  733. const struct usb_device_id *id /* from id_table */
  734. )
  735. {
  736. struct device *dev = &intf->dev;
  737. struct usb_device *udev = interface_to_usbdev(intf);
  738. struct kaweth_device *kaweth;
  739. struct net_device *netdev;
  740. const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  741. int result = 0;
  742. int rv = -EIO;
  743. static const u8 bulk_ep_addr[] = {
  744. 1 | USB_DIR_IN,
  745. 2 | USB_DIR_OUT,
  746. 0};
  747. static const u8 int_ep_addr[] = {
  748. 3 | USB_DIR_IN,
  749. 0};
  750. dev_dbg(dev,
  751. "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
  752. udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
  753. le16_to_cpu(udev->descriptor.idProduct),
  754. le16_to_cpu(udev->descriptor.bcdDevice));
  755. dev_dbg(dev, "Device at %p\n", udev);
  756. dev_dbg(dev, "Descriptor length: %x type: %x\n",
  757. (int)udev->descriptor.bLength,
  758. (int)udev->descriptor.bDescriptorType);
  759. if (!usb_check_bulk_endpoints(intf, bulk_ep_addr) ||
  760. !usb_check_int_endpoints(intf, int_ep_addr)) {
  761. dev_err(dev, "couldn't find required endpoints\n");
  762. return -ENODEV;
  763. }
  764. netdev = alloc_etherdev(sizeof(*kaweth));
  765. if (!netdev)
  766. return -ENOMEM;
  767. kaweth = netdev_priv(netdev);
  768. kaweth->dev = udev;
  769. kaweth->net = netdev;
  770. kaweth->intf = intf;
  771. spin_lock_init(&kaweth->device_lock);
  772. init_waitqueue_head(&kaweth->term_wait);
  773. dev_dbg(dev, "Resetting.\n");
  774. kaweth_reset(kaweth);
  775. /*
  776. * If high byte of bcdDevice is nonzero, firmware is already
  777. * downloaded. Don't try to do it again, or we'll hang the device.
  778. */
  779. if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
  780. dev_info(dev, "Firmware present in device.\n");
  781. } else {
  782. /* Download the firmware */
  783. dev_info(dev, "Downloading firmware...\n");
  784. kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
  785. if (!kaweth->firmware_buf) {
  786. rv = -ENOMEM;
  787. goto err_free_netdev;
  788. }
  789. if ((result = kaweth_download_firmware(kaweth,
  790. "kaweth/new_code.bin",
  791. 100,
  792. 2)) < 0) {
  793. dev_err(dev, "Error downloading firmware (%d)\n",
  794. result);
  795. goto err_fw;
  796. }
  797. if ((result = kaweth_download_firmware(kaweth,
  798. "kaweth/new_code_fix.bin",
  799. 100,
  800. 3)) < 0) {
  801. dev_err(dev, "Error downloading firmware fix (%d)\n",
  802. result);
  803. goto err_fw;
  804. }
  805. if ((result = kaweth_download_firmware(kaweth,
  806. "kaweth/trigger_code.bin",
  807. 126,
  808. 2)) < 0) {
  809. dev_err(dev, "Error downloading trigger code (%d)\n",
  810. result);
  811. goto err_fw;
  812. }
  813. if ((result = kaweth_download_firmware(kaweth,
  814. "kaweth/trigger_code_fix.bin",
  815. 126,
  816. 3)) < 0) {
  817. dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
  818. goto err_fw;
  819. }
  820. if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
  821. dev_err(dev, "Error triggering firmware (%d)\n", result);
  822. goto err_fw;
  823. }
  824. /* Device will now disappear for a moment... */
  825. dev_info(dev, "Firmware loaded. I'll be back...\n");
  826. err_fw:
  827. free_page((unsigned long)kaweth->firmware_buf);
  828. free_netdev(netdev);
  829. return -EIO;
  830. }
  831. result = kaweth_read_configuration(kaweth);
  832. if(result < 0) {
  833. dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
  834. goto err_free_netdev;
  835. }
  836. dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
  837. dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
  838. dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
  839. dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
  840. if(!memcmp(&kaweth->configuration.hw_addr,
  841. &bcast_addr,
  842. sizeof(bcast_addr))) {
  843. dev_err(dev, "Firmware not functioning properly, no net device created\n");
  844. goto err_free_netdev;
  845. }
  846. if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
  847. dev_dbg(dev, "Error setting URB size\n");
  848. goto err_free_netdev;
  849. }
  850. if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
  851. dev_err(dev, "Error setting SOFS wait\n");
  852. goto err_free_netdev;
  853. }
  854. result = kaweth_set_receive_filter(kaweth,
  855. KAWETH_PACKET_FILTER_DIRECTED |
  856. KAWETH_PACKET_FILTER_BROADCAST |
  857. KAWETH_PACKET_FILTER_MULTICAST);
  858. if(result < 0) {
  859. dev_err(dev, "Error setting receive filter\n");
  860. goto err_free_netdev;
  861. }
  862. dev_dbg(dev, "Initializing net device.\n");
  863. kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  864. if (!kaweth->tx_urb)
  865. goto err_free_netdev;
  866. kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  867. if (!kaweth->rx_urb)
  868. goto err_only_tx;
  869. kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  870. if (!kaweth->irq_urb)
  871. goto err_tx_and_rx;
  872. kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
  873. INTBUFFERSIZE,
  874. GFP_KERNEL,
  875. &kaweth->intbufferhandle);
  876. if (!kaweth->intbuffer)
  877. goto err_tx_and_rx_and_irq;
  878. kaweth->rx_buf = usb_alloc_coherent( kaweth->dev,
  879. KAWETH_BUF_SIZE,
  880. GFP_KERNEL,
  881. &kaweth->rxbufferhandle);
  882. if (!kaweth->rx_buf)
  883. goto err_all_but_rxbuf;
  884. memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
  885. eth_hw_addr_set(netdev, (u8 *)&kaweth->configuration.hw_addr);
  886. netdev->netdev_ops = &kaweth_netdev_ops;
  887. netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
  888. netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
  889. netdev->ethtool_ops = &ops;
  890. /* kaweth is zeroed as part of alloc_netdev */
  891. INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
  892. usb_set_intfdata(intf, kaweth);
  893. SET_NETDEV_DEV(netdev, dev);
  894. if (register_netdev(netdev) != 0) {
  895. dev_err(dev, "Error registering netdev.\n");
  896. goto err_intfdata;
  897. }
  898. dev_info(dev, "kaweth interface created at %s\n",
  899. kaweth->net->name);
  900. return 0;
  901. err_intfdata:
  902. usb_set_intfdata(intf, NULL);
  903. usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  904. err_all_but_rxbuf:
  905. usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  906. err_tx_and_rx_and_irq:
  907. usb_free_urb(kaweth->irq_urb);
  908. err_tx_and_rx:
  909. usb_free_urb(kaweth->rx_urb);
  910. err_only_tx:
  911. usb_free_urb(kaweth->tx_urb);
  912. err_free_netdev:
  913. free_netdev(netdev);
  914. return rv;
  915. }
  916. /****************************************************************
  917. * kaweth_disconnect
  918. ****************************************************************/
  919. static void kaweth_disconnect(struct usb_interface *intf)
  920. {
  921. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  922. struct net_device *netdev;
  923. usb_set_intfdata(intf, NULL);
  924. if (!kaweth) {
  925. dev_warn(&intf->dev, "unregistering non-existent device\n");
  926. return;
  927. }
  928. netdev = kaweth->net;
  929. netdev_dbg(kaweth->net, "Unregistering net device\n");
  930. unregister_netdev(netdev);
  931. usb_free_urb(kaweth->rx_urb);
  932. usb_free_urb(kaweth->tx_urb);
  933. usb_free_urb(kaweth->irq_urb);
  934. usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  935. usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  936. free_netdev(netdev);
  937. }
  938. module_usb_driver(kaweth_driver);