kaweth.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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 <[email protected]>
  12. * Inspired by, and much credit goes to Michael Rothwell
  13. * <[email protected]> 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 <[email protected]>, Stephane Alnet <[email protected]>, Brad Hards <[email protected]> and Oliver Neukum <[email protected]>");
  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. netif_stop_queue(net);
  646. if (net->flags & IFF_PROMISC) {
  647. packet_filter_bitmap |= KAWETH_PACKET_FILTER_PROMISCUOUS;
  648. }
  649. else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
  650. packet_filter_bitmap |= KAWETH_PACKET_FILTER_ALL_MULTICAST;
  651. }
  652. kaweth->packet_filter_bitmap = packet_filter_bitmap;
  653. netif_wake_queue(net);
  654. }
  655. /****************************************************************
  656. * kaweth_async_set_rx_mode
  657. ****************************************************************/
  658. static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth,
  659. bool may_sleep)
  660. {
  661. int ret;
  662. __u16 packet_filter_bitmap = kaweth->packet_filter_bitmap;
  663. kaweth->packet_filter_bitmap = 0;
  664. if (packet_filter_bitmap == 0)
  665. return;
  666. if (!may_sleep)
  667. return;
  668. ret = usb_control_msg(kaweth->dev, usb_sndctrlpipe(kaweth->dev, 0),
  669. KAWETH_COMMAND_SET_PACKET_FILTER,
  670. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  671. packet_filter_bitmap, 0,
  672. &kaweth->scratch, 0,
  673. KAWETH_CONTROL_TIMEOUT);
  674. if (ret < 0)
  675. dev_err(&kaweth->intf->dev, "Failed to set Rx mode: %d\n",
  676. ret);
  677. else
  678. netdev_dbg(kaweth->net, "Set Rx mode to %d\n",
  679. packet_filter_bitmap);
  680. }
  681. /****************************************************************
  682. * kaweth_tx_timeout
  683. ****************************************************************/
  684. static void kaweth_tx_timeout(struct net_device *net, unsigned int txqueue)
  685. {
  686. struct kaweth_device *kaweth = netdev_priv(net);
  687. dev_warn(&net->dev, "%s: Tx timed out. Resetting.\n", net->name);
  688. net->stats.tx_errors++;
  689. netif_trans_update(net);
  690. usb_unlink_urb(kaweth->tx_urb);
  691. }
  692. /****************************************************************
  693. * kaweth_suspend
  694. ****************************************************************/
  695. static int kaweth_suspend(struct usb_interface *intf, pm_message_t message)
  696. {
  697. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  698. unsigned long flags;
  699. spin_lock_irqsave(&kaweth->device_lock, flags);
  700. kaweth->status |= KAWETH_STATUS_SUSPENDING;
  701. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  702. kaweth_kill_urbs(kaweth);
  703. return 0;
  704. }
  705. /****************************************************************
  706. * kaweth_resume
  707. ****************************************************************/
  708. static int kaweth_resume(struct usb_interface *intf)
  709. {
  710. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  711. unsigned long flags;
  712. spin_lock_irqsave(&kaweth->device_lock, flags);
  713. kaweth->status &= ~KAWETH_STATUS_SUSPENDING;
  714. spin_unlock_irqrestore(&kaweth->device_lock, flags);
  715. if (!kaweth->opened)
  716. return 0;
  717. kaweth_resubmit_rx_urb(kaweth, GFP_NOIO);
  718. kaweth_resubmit_int_urb(kaweth, GFP_NOIO);
  719. return 0;
  720. }
  721. /****************************************************************
  722. * kaweth_probe
  723. ****************************************************************/
  724. static const struct net_device_ops kaweth_netdev_ops = {
  725. .ndo_open = kaweth_open,
  726. .ndo_stop = kaweth_close,
  727. .ndo_start_xmit = kaweth_start_xmit,
  728. .ndo_tx_timeout = kaweth_tx_timeout,
  729. .ndo_set_rx_mode = kaweth_set_rx_mode,
  730. .ndo_set_mac_address = eth_mac_addr,
  731. .ndo_validate_addr = eth_validate_addr,
  732. };
  733. static int kaweth_probe(
  734. struct usb_interface *intf,
  735. const struct usb_device_id *id /* from id_table */
  736. )
  737. {
  738. struct device *dev = &intf->dev;
  739. struct usb_device *udev = interface_to_usbdev(intf);
  740. struct kaweth_device *kaweth;
  741. struct net_device *netdev;
  742. const eth_addr_t bcast_addr = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  743. int result = 0;
  744. int rv = -EIO;
  745. dev_dbg(dev,
  746. "Kawasaki Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x\n",
  747. udev->devnum, le16_to_cpu(udev->descriptor.idVendor),
  748. le16_to_cpu(udev->descriptor.idProduct),
  749. le16_to_cpu(udev->descriptor.bcdDevice));
  750. dev_dbg(dev, "Device at %p\n", udev);
  751. dev_dbg(dev, "Descriptor length: %x type: %x\n",
  752. (int)udev->descriptor.bLength,
  753. (int)udev->descriptor.bDescriptorType);
  754. netdev = alloc_etherdev(sizeof(*kaweth));
  755. if (!netdev)
  756. return -ENOMEM;
  757. kaweth = netdev_priv(netdev);
  758. kaweth->dev = udev;
  759. kaweth->net = netdev;
  760. kaweth->intf = intf;
  761. spin_lock_init(&kaweth->device_lock);
  762. init_waitqueue_head(&kaweth->term_wait);
  763. dev_dbg(dev, "Resetting.\n");
  764. kaweth_reset(kaweth);
  765. /*
  766. * If high byte of bcdDevice is nonzero, firmware is already
  767. * downloaded. Don't try to do it again, or we'll hang the device.
  768. */
  769. if (le16_to_cpu(udev->descriptor.bcdDevice) >> 8) {
  770. dev_info(dev, "Firmware present in device.\n");
  771. } else {
  772. /* Download the firmware */
  773. dev_info(dev, "Downloading firmware...\n");
  774. kaweth->firmware_buf = (__u8 *)__get_free_page(GFP_KERNEL);
  775. if (!kaweth->firmware_buf) {
  776. rv = -ENOMEM;
  777. goto err_free_netdev;
  778. }
  779. if ((result = kaweth_download_firmware(kaweth,
  780. "kaweth/new_code.bin",
  781. 100,
  782. 2)) < 0) {
  783. dev_err(dev, "Error downloading firmware (%d)\n",
  784. result);
  785. goto err_fw;
  786. }
  787. if ((result = kaweth_download_firmware(kaweth,
  788. "kaweth/new_code_fix.bin",
  789. 100,
  790. 3)) < 0) {
  791. dev_err(dev, "Error downloading firmware fix (%d)\n",
  792. result);
  793. goto err_fw;
  794. }
  795. if ((result = kaweth_download_firmware(kaweth,
  796. "kaweth/trigger_code.bin",
  797. 126,
  798. 2)) < 0) {
  799. dev_err(dev, "Error downloading trigger code (%d)\n",
  800. result);
  801. goto err_fw;
  802. }
  803. if ((result = kaweth_download_firmware(kaweth,
  804. "kaweth/trigger_code_fix.bin",
  805. 126,
  806. 3)) < 0) {
  807. dev_err(dev, "Error downloading trigger code fix (%d)\n", result);
  808. goto err_fw;
  809. }
  810. if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
  811. dev_err(dev, "Error triggering firmware (%d)\n", result);
  812. goto err_fw;
  813. }
  814. /* Device will now disappear for a moment... */
  815. dev_info(dev, "Firmware loaded. I'll be back...\n");
  816. err_fw:
  817. free_page((unsigned long)kaweth->firmware_buf);
  818. free_netdev(netdev);
  819. return -EIO;
  820. }
  821. result = kaweth_read_configuration(kaweth);
  822. if(result < 0) {
  823. dev_err(dev, "Error reading configuration (%d), no net device created\n", result);
  824. goto err_free_netdev;
  825. }
  826. dev_info(dev, "Statistics collection: %x\n", kaweth->configuration.statistics_mask);
  827. dev_info(dev, "Multicast filter limit: %x\n", kaweth->configuration.max_multicast_filters & ((1 << 15) - 1));
  828. dev_info(dev, "MTU: %d\n", le16_to_cpu(kaweth->configuration.segment_size));
  829. dev_info(dev, "Read MAC address %pM\n", kaweth->configuration.hw_addr);
  830. if(!memcmp(&kaweth->configuration.hw_addr,
  831. &bcast_addr,
  832. sizeof(bcast_addr))) {
  833. dev_err(dev, "Firmware not functioning properly, no net device created\n");
  834. goto err_free_netdev;
  835. }
  836. if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
  837. dev_dbg(dev, "Error setting URB size\n");
  838. goto err_free_netdev;
  839. }
  840. if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
  841. dev_err(dev, "Error setting SOFS wait\n");
  842. goto err_free_netdev;
  843. }
  844. result = kaweth_set_receive_filter(kaweth,
  845. KAWETH_PACKET_FILTER_DIRECTED |
  846. KAWETH_PACKET_FILTER_BROADCAST |
  847. KAWETH_PACKET_FILTER_MULTICAST);
  848. if(result < 0) {
  849. dev_err(dev, "Error setting receive filter\n");
  850. goto err_free_netdev;
  851. }
  852. dev_dbg(dev, "Initializing net device.\n");
  853. kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  854. if (!kaweth->tx_urb)
  855. goto err_free_netdev;
  856. kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  857. if (!kaweth->rx_urb)
  858. goto err_only_tx;
  859. kaweth->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  860. if (!kaweth->irq_urb)
  861. goto err_tx_and_rx;
  862. kaweth->intbuffer = usb_alloc_coherent( kaweth->dev,
  863. INTBUFFERSIZE,
  864. GFP_KERNEL,
  865. &kaweth->intbufferhandle);
  866. if (!kaweth->intbuffer)
  867. goto err_tx_and_rx_and_irq;
  868. kaweth->rx_buf = usb_alloc_coherent( kaweth->dev,
  869. KAWETH_BUF_SIZE,
  870. GFP_KERNEL,
  871. &kaweth->rxbufferhandle);
  872. if (!kaweth->rx_buf)
  873. goto err_all_but_rxbuf;
  874. memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
  875. eth_hw_addr_set(netdev, (u8 *)&kaweth->configuration.hw_addr);
  876. netdev->netdev_ops = &kaweth_netdev_ops;
  877. netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
  878. netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
  879. netdev->ethtool_ops = &ops;
  880. /* kaweth is zeroed as part of alloc_netdev */
  881. INIT_DELAYED_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl);
  882. usb_set_intfdata(intf, kaweth);
  883. SET_NETDEV_DEV(netdev, dev);
  884. if (register_netdev(netdev) != 0) {
  885. dev_err(dev, "Error registering netdev.\n");
  886. goto err_intfdata;
  887. }
  888. dev_info(dev, "kaweth interface created at %s\n",
  889. kaweth->net->name);
  890. return 0;
  891. err_intfdata:
  892. usb_set_intfdata(intf, NULL);
  893. usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  894. err_all_but_rxbuf:
  895. usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  896. err_tx_and_rx_and_irq:
  897. usb_free_urb(kaweth->irq_urb);
  898. err_tx_and_rx:
  899. usb_free_urb(kaweth->rx_urb);
  900. err_only_tx:
  901. usb_free_urb(kaweth->tx_urb);
  902. err_free_netdev:
  903. free_netdev(netdev);
  904. return rv;
  905. }
  906. /****************************************************************
  907. * kaweth_disconnect
  908. ****************************************************************/
  909. static void kaweth_disconnect(struct usb_interface *intf)
  910. {
  911. struct kaweth_device *kaweth = usb_get_intfdata(intf);
  912. struct net_device *netdev;
  913. usb_set_intfdata(intf, NULL);
  914. if (!kaweth) {
  915. dev_warn(&intf->dev, "unregistering non-existent device\n");
  916. return;
  917. }
  918. netdev = kaweth->net;
  919. netdev_dbg(kaweth->net, "Unregistering net device\n");
  920. unregister_netdev(netdev);
  921. usb_free_urb(kaweth->rx_urb);
  922. usb_free_urb(kaweth->tx_urb);
  923. usb_free_urb(kaweth->irq_urb);
  924. usb_free_coherent(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
  925. usb_free_coherent(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
  926. free_netdev(netdev);
  927. }
  928. module_usb_driver(kaweth_driver);