gs_usb.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* CAN driver for Geschwister Schneider USB/CAN devices
  3. * and bytewerk.org candleLight USB CAN interfaces.
  4. *
  5. * Copyright (C) 2013-2016 Geschwister Schneider Technologie-,
  6. * Entwicklungs- und Vertriebs UG (Haftungsbeschränkt).
  7. * Copyright (C) 2016 Hubert Denkmair
  8. * Copyright (c) 2023 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
  9. *
  10. * Many thanks to all socketcan devs!
  11. */
  12. #include <linux/bitfield.h>
  13. #include <linux/clocksource.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/signal.h>
  19. #include <linux/timecounter.h>
  20. #include <linux/units.h>
  21. #include <linux/usb.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/can.h>
  24. #include <linux/can/dev.h>
  25. #include <linux/can/error.h>
  26. #include <linux/can/rx-offload.h>
  27. /* Device specific constants */
  28. #define USB_GS_USB_1_VENDOR_ID 0x1d50
  29. #define USB_GS_USB_1_PRODUCT_ID 0x606f
  30. #define USB_CANDLELIGHT_VENDOR_ID 0x1209
  31. #define USB_CANDLELIGHT_PRODUCT_ID 0x2323
  32. #define USB_CES_CANEXT_FD_VENDOR_ID 0x1cd2
  33. #define USB_CES_CANEXT_FD_PRODUCT_ID 0x606f
  34. #define USB_ABE_CANDEBUGGER_FD_VENDOR_ID 0x16d0
  35. #define USB_ABE_CANDEBUGGER_FD_PRODUCT_ID 0x10b8
  36. #define USB_XYLANTA_SAINT3_VENDOR_ID 0x16d0
  37. #define USB_XYLANTA_SAINT3_PRODUCT_ID 0x0f30
  38. /* Timestamp 32 bit timer runs at 1 MHz (1 µs tick). Worker accounts
  39. * for timer overflow (will be after ~71 minutes)
  40. */
  41. #define GS_USB_TIMESTAMP_TIMER_HZ (1 * HZ_PER_MHZ)
  42. #define GS_USB_TIMESTAMP_WORK_DELAY_SEC 1800
  43. static_assert(GS_USB_TIMESTAMP_WORK_DELAY_SEC <
  44. CYCLECOUNTER_MASK(32) / GS_USB_TIMESTAMP_TIMER_HZ / 2);
  45. /* Device specific constants */
  46. enum gs_usb_breq {
  47. GS_USB_BREQ_HOST_FORMAT = 0,
  48. GS_USB_BREQ_BITTIMING,
  49. GS_USB_BREQ_MODE,
  50. GS_USB_BREQ_BERR,
  51. GS_USB_BREQ_BT_CONST,
  52. GS_USB_BREQ_DEVICE_CONFIG,
  53. GS_USB_BREQ_TIMESTAMP,
  54. GS_USB_BREQ_IDENTIFY,
  55. GS_USB_BREQ_GET_USER_ID,
  56. GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING = GS_USB_BREQ_GET_USER_ID,
  57. GS_USB_BREQ_SET_USER_ID,
  58. GS_USB_BREQ_DATA_BITTIMING,
  59. GS_USB_BREQ_BT_CONST_EXT,
  60. GS_USB_BREQ_SET_TERMINATION,
  61. GS_USB_BREQ_GET_TERMINATION,
  62. GS_USB_BREQ_GET_STATE,
  63. };
  64. enum gs_can_mode {
  65. /* reset a channel. turns it off */
  66. GS_CAN_MODE_RESET = 0,
  67. /* starts a channel */
  68. GS_CAN_MODE_START
  69. };
  70. enum gs_can_state {
  71. GS_CAN_STATE_ERROR_ACTIVE = 0,
  72. GS_CAN_STATE_ERROR_WARNING,
  73. GS_CAN_STATE_ERROR_PASSIVE,
  74. GS_CAN_STATE_BUS_OFF,
  75. GS_CAN_STATE_STOPPED,
  76. GS_CAN_STATE_SLEEPING
  77. };
  78. enum gs_can_identify_mode {
  79. GS_CAN_IDENTIFY_OFF = 0,
  80. GS_CAN_IDENTIFY_ON
  81. };
  82. enum gs_can_termination_state {
  83. GS_CAN_TERMINATION_STATE_OFF = 0,
  84. GS_CAN_TERMINATION_STATE_ON
  85. };
  86. #define GS_USB_TERMINATION_DISABLED CAN_TERMINATION_DISABLED
  87. #define GS_USB_TERMINATION_ENABLED 120
  88. /* data types passed between host and device */
  89. /* The firmware on the original USB2CAN by Geschwister Schneider
  90. * Technologie Entwicklungs- und Vertriebs UG exchanges all data
  91. * between the host and the device in host byte order. This is done
  92. * with the struct gs_host_config::byte_order member, which is sent
  93. * first to indicate the desired byte order.
  94. *
  95. * The widely used open source firmware candleLight doesn't support
  96. * this feature and exchanges the data in little endian byte order.
  97. */
  98. struct gs_host_config {
  99. __le32 byte_order;
  100. } __packed;
  101. struct gs_device_config {
  102. u8 reserved1;
  103. u8 reserved2;
  104. u8 reserved3;
  105. u8 icount;
  106. __le32 sw_version;
  107. __le32 hw_version;
  108. } __packed;
  109. #define GS_CAN_MODE_NORMAL 0
  110. #define GS_CAN_MODE_LISTEN_ONLY BIT(0)
  111. #define GS_CAN_MODE_LOOP_BACK BIT(1)
  112. #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2)
  113. #define GS_CAN_MODE_ONE_SHOT BIT(3)
  114. #define GS_CAN_MODE_HW_TIMESTAMP BIT(4)
  115. /* GS_CAN_FEATURE_IDENTIFY BIT(5) */
  116. /* GS_CAN_FEATURE_USER_ID BIT(6) */
  117. #define GS_CAN_MODE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
  118. #define GS_CAN_MODE_FD BIT(8)
  119. /* GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9) */
  120. /* GS_CAN_FEATURE_BT_CONST_EXT BIT(10) */
  121. /* GS_CAN_FEATURE_TERMINATION BIT(11) */
  122. #define GS_CAN_MODE_BERR_REPORTING BIT(12)
  123. /* GS_CAN_FEATURE_GET_STATE BIT(13) */
  124. struct gs_device_mode {
  125. __le32 mode;
  126. __le32 flags;
  127. } __packed;
  128. struct gs_device_state {
  129. __le32 state;
  130. __le32 rxerr;
  131. __le32 txerr;
  132. } __packed;
  133. struct gs_device_bittiming {
  134. __le32 prop_seg;
  135. __le32 phase_seg1;
  136. __le32 phase_seg2;
  137. __le32 sjw;
  138. __le32 brp;
  139. } __packed;
  140. struct gs_identify_mode {
  141. __le32 mode;
  142. } __packed;
  143. struct gs_device_termination_state {
  144. __le32 state;
  145. } __packed;
  146. #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0)
  147. #define GS_CAN_FEATURE_LOOP_BACK BIT(1)
  148. #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2)
  149. #define GS_CAN_FEATURE_ONE_SHOT BIT(3)
  150. #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4)
  151. #define GS_CAN_FEATURE_IDENTIFY BIT(5)
  152. #define GS_CAN_FEATURE_USER_ID BIT(6)
  153. #define GS_CAN_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE BIT(7)
  154. #define GS_CAN_FEATURE_FD BIT(8)
  155. #define GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX BIT(9)
  156. #define GS_CAN_FEATURE_BT_CONST_EXT BIT(10)
  157. #define GS_CAN_FEATURE_TERMINATION BIT(11)
  158. #define GS_CAN_FEATURE_BERR_REPORTING BIT(12)
  159. #define GS_CAN_FEATURE_GET_STATE BIT(13)
  160. #define GS_CAN_FEATURE_MASK GENMASK(13, 0)
  161. /* internal quirks - keep in GS_CAN_FEATURE space for now */
  162. /* CANtact Pro original firmware:
  163. * BREQ DATA_BITTIMING overlaps with GET_USER_ID
  164. */
  165. #define GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO BIT(31)
  166. struct gs_device_bt_const {
  167. __le32 feature;
  168. __le32 fclk_can;
  169. __le32 tseg1_min;
  170. __le32 tseg1_max;
  171. __le32 tseg2_min;
  172. __le32 tseg2_max;
  173. __le32 sjw_max;
  174. __le32 brp_min;
  175. __le32 brp_max;
  176. __le32 brp_inc;
  177. } __packed;
  178. struct gs_device_bt_const_extended {
  179. __le32 feature;
  180. __le32 fclk_can;
  181. __le32 tseg1_min;
  182. __le32 tseg1_max;
  183. __le32 tseg2_min;
  184. __le32 tseg2_max;
  185. __le32 sjw_max;
  186. __le32 brp_min;
  187. __le32 brp_max;
  188. __le32 brp_inc;
  189. __le32 dtseg1_min;
  190. __le32 dtseg1_max;
  191. __le32 dtseg2_min;
  192. __le32 dtseg2_max;
  193. __le32 dsjw_max;
  194. __le32 dbrp_min;
  195. __le32 dbrp_max;
  196. __le32 dbrp_inc;
  197. } __packed;
  198. #define GS_CAN_FLAG_OVERFLOW BIT(0)
  199. #define GS_CAN_FLAG_FD BIT(1)
  200. #define GS_CAN_FLAG_BRS BIT(2)
  201. #define GS_CAN_FLAG_ESI BIT(3)
  202. struct classic_can {
  203. u8 data[8];
  204. } __packed;
  205. struct classic_can_ts {
  206. u8 data[8];
  207. __le32 timestamp_us;
  208. } __packed;
  209. struct classic_can_quirk {
  210. u8 data[8];
  211. u8 quirk;
  212. } __packed;
  213. struct canfd {
  214. u8 data[64];
  215. } __packed;
  216. struct canfd_ts {
  217. u8 data[64];
  218. __le32 timestamp_us;
  219. } __packed;
  220. struct canfd_quirk {
  221. u8 data[64];
  222. u8 quirk;
  223. } __packed;
  224. struct gs_host_frame {
  225. u32 echo_id;
  226. __le32 can_id;
  227. u8 can_dlc;
  228. u8 channel;
  229. u8 flags;
  230. u8 reserved;
  231. union {
  232. DECLARE_FLEX_ARRAY(struct classic_can, classic_can);
  233. DECLARE_FLEX_ARRAY(struct classic_can_ts, classic_can_ts);
  234. DECLARE_FLEX_ARRAY(struct classic_can_quirk, classic_can_quirk);
  235. DECLARE_FLEX_ARRAY(struct canfd, canfd);
  236. DECLARE_FLEX_ARRAY(struct canfd_ts, canfd_ts);
  237. DECLARE_FLEX_ARRAY(struct canfd_quirk, canfd_quirk);
  238. };
  239. } __packed;
  240. /* The GS USB devices make use of the same flags and masks as in
  241. * linux/can.h and linux/can/error.h, and no additional mapping is necessary.
  242. */
  243. /* Only send a max of GS_MAX_TX_URBS frames per channel at a time. */
  244. #define GS_MAX_TX_URBS 10
  245. /* Only launch a max of GS_MAX_RX_URBS usb requests at a time. */
  246. #define GS_MAX_RX_URBS 30
  247. #define GS_NAPI_WEIGHT 32
  248. struct gs_tx_context {
  249. struct gs_can *dev;
  250. unsigned int echo_id;
  251. };
  252. struct gs_can {
  253. struct can_priv can; /* must be the first member */
  254. struct can_rx_offload offload;
  255. struct gs_usb *parent;
  256. struct net_device *netdev;
  257. struct usb_device *udev;
  258. struct can_bittiming_const bt_const, data_bt_const;
  259. unsigned int channel; /* channel number */
  260. u32 feature;
  261. unsigned int hf_size_tx;
  262. /* This lock prevents a race condition between xmit and receive. */
  263. spinlock_t tx_ctx_lock;
  264. struct gs_tx_context tx_context[GS_MAX_TX_URBS];
  265. struct usb_anchor tx_submitted;
  266. atomic_t active_tx_urbs;
  267. };
  268. /* usb interface struct */
  269. struct gs_usb {
  270. struct usb_anchor rx_submitted;
  271. struct usb_device *udev;
  272. /* time counter for hardware timestamps */
  273. struct cyclecounter cc;
  274. struct timecounter tc;
  275. spinlock_t tc_lock; /* spinlock to guard access tc->cycle_last */
  276. struct delayed_work timestamp;
  277. unsigned int hf_size_rx;
  278. u8 active_channels;
  279. u8 channel_cnt;
  280. unsigned int pipe_in;
  281. unsigned int pipe_out;
  282. struct gs_can *canch[] __counted_by(channel_cnt);
  283. };
  284. /* 'allocate' a tx context.
  285. * returns a valid tx context or NULL if there is no space.
  286. */
  287. static struct gs_tx_context *gs_alloc_tx_context(struct gs_can *dev)
  288. {
  289. int i = 0;
  290. unsigned long flags;
  291. spin_lock_irqsave(&dev->tx_ctx_lock, flags);
  292. for (; i < GS_MAX_TX_URBS; i++) {
  293. if (dev->tx_context[i].echo_id == GS_MAX_TX_URBS) {
  294. dev->tx_context[i].echo_id = i;
  295. spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
  296. return &dev->tx_context[i];
  297. }
  298. }
  299. spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
  300. return NULL;
  301. }
  302. /* releases a tx context
  303. */
  304. static void gs_free_tx_context(struct gs_tx_context *txc)
  305. {
  306. txc->echo_id = GS_MAX_TX_URBS;
  307. }
  308. /* Get a tx context by id.
  309. */
  310. static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev,
  311. unsigned int id)
  312. {
  313. unsigned long flags;
  314. if (id < GS_MAX_TX_URBS) {
  315. spin_lock_irqsave(&dev->tx_ctx_lock, flags);
  316. if (dev->tx_context[id].echo_id == id) {
  317. spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
  318. return &dev->tx_context[id];
  319. }
  320. spin_unlock_irqrestore(&dev->tx_ctx_lock, flags);
  321. }
  322. return NULL;
  323. }
  324. static int gs_cmd_reset(struct gs_can *dev)
  325. {
  326. struct gs_device_mode dm = {
  327. .mode = cpu_to_le32(GS_CAN_MODE_RESET),
  328. };
  329. return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
  330. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  331. dev->channel, 0, &dm, sizeof(dm), 1000,
  332. GFP_KERNEL);
  333. }
  334. static inline int gs_usb_get_timestamp(const struct gs_usb *parent,
  335. u32 *timestamp_p)
  336. {
  337. __le32 timestamp;
  338. int rc;
  339. rc = usb_control_msg_recv(parent->udev, 0, GS_USB_BREQ_TIMESTAMP,
  340. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  341. 0, 0,
  342. &timestamp, sizeof(timestamp),
  343. USB_CTRL_GET_TIMEOUT,
  344. GFP_KERNEL);
  345. if (rc)
  346. return rc;
  347. *timestamp_p = le32_to_cpu(timestamp);
  348. return 0;
  349. }
  350. static u64 gs_usb_timestamp_read(const struct cyclecounter *cc) __must_hold(&dev->tc_lock)
  351. {
  352. struct gs_usb *parent = container_of(cc, struct gs_usb, cc);
  353. u32 timestamp = 0;
  354. int err;
  355. lockdep_assert_held(&parent->tc_lock);
  356. /* drop lock for synchronous USB transfer */
  357. spin_unlock_bh(&parent->tc_lock);
  358. err = gs_usb_get_timestamp(parent, &timestamp);
  359. spin_lock_bh(&parent->tc_lock);
  360. if (err)
  361. dev_err(&parent->udev->dev,
  362. "Error %d while reading timestamp. HW timestamps may be inaccurate.",
  363. err);
  364. return timestamp;
  365. }
  366. static void gs_usb_timestamp_work(struct work_struct *work)
  367. {
  368. struct delayed_work *delayed_work = to_delayed_work(work);
  369. struct gs_usb *parent;
  370. parent = container_of(delayed_work, struct gs_usb, timestamp);
  371. spin_lock_bh(&parent->tc_lock);
  372. timecounter_read(&parent->tc);
  373. spin_unlock_bh(&parent->tc_lock);
  374. schedule_delayed_work(&parent->timestamp,
  375. GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
  376. }
  377. static void gs_usb_skb_set_timestamp(struct gs_can *dev,
  378. struct sk_buff *skb, u32 timestamp)
  379. {
  380. struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
  381. struct gs_usb *parent = dev->parent;
  382. u64 ns;
  383. spin_lock_bh(&parent->tc_lock);
  384. ns = timecounter_cyc2time(&parent->tc, timestamp);
  385. spin_unlock_bh(&parent->tc_lock);
  386. hwtstamps->hwtstamp = ns_to_ktime(ns);
  387. }
  388. static void gs_usb_timestamp_init(struct gs_usb *parent)
  389. {
  390. struct cyclecounter *cc = &parent->cc;
  391. cc->read = gs_usb_timestamp_read;
  392. cc->mask = CYCLECOUNTER_MASK(32);
  393. cc->shift = 32 - bits_per(NSEC_PER_SEC / GS_USB_TIMESTAMP_TIMER_HZ);
  394. cc->mult = clocksource_hz2mult(GS_USB_TIMESTAMP_TIMER_HZ, cc->shift);
  395. spin_lock_init(&parent->tc_lock);
  396. spin_lock_bh(&parent->tc_lock);
  397. timecounter_init(&parent->tc, &parent->cc, ktime_get_real_ns());
  398. spin_unlock_bh(&parent->tc_lock);
  399. INIT_DELAYED_WORK(&parent->timestamp, gs_usb_timestamp_work);
  400. schedule_delayed_work(&parent->timestamp,
  401. GS_USB_TIMESTAMP_WORK_DELAY_SEC * HZ);
  402. }
  403. static void gs_usb_timestamp_stop(struct gs_usb *parent)
  404. {
  405. cancel_delayed_work_sync(&parent->timestamp);
  406. }
  407. static void gs_update_state(struct gs_can *dev, struct can_frame *cf)
  408. {
  409. struct can_device_stats *can_stats = &dev->can.can_stats;
  410. if (cf->can_id & CAN_ERR_RESTARTED) {
  411. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  412. can_stats->restarts++;
  413. } else if (cf->can_id & CAN_ERR_BUSOFF) {
  414. dev->can.state = CAN_STATE_BUS_OFF;
  415. can_stats->bus_off++;
  416. } else if (cf->can_id & CAN_ERR_CRTL) {
  417. if ((cf->data[1] & CAN_ERR_CRTL_TX_WARNING) ||
  418. (cf->data[1] & CAN_ERR_CRTL_RX_WARNING)) {
  419. dev->can.state = CAN_STATE_ERROR_WARNING;
  420. can_stats->error_warning++;
  421. } else if ((cf->data[1] & CAN_ERR_CRTL_TX_PASSIVE) ||
  422. (cf->data[1] & CAN_ERR_CRTL_RX_PASSIVE)) {
  423. dev->can.state = CAN_STATE_ERROR_PASSIVE;
  424. can_stats->error_passive++;
  425. } else {
  426. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  427. }
  428. }
  429. }
  430. static u32 gs_usb_set_timestamp(struct gs_can *dev, struct sk_buff *skb,
  431. const struct gs_host_frame *hf)
  432. {
  433. u32 timestamp;
  434. if (hf->flags & GS_CAN_FLAG_FD)
  435. timestamp = le32_to_cpu(hf->canfd_ts->timestamp_us);
  436. else
  437. timestamp = le32_to_cpu(hf->classic_can_ts->timestamp_us);
  438. if (skb)
  439. gs_usb_skb_set_timestamp(dev, skb, timestamp);
  440. return timestamp;
  441. }
  442. static void gs_usb_rx_offload(struct gs_can *dev, struct sk_buff *skb,
  443. const struct gs_host_frame *hf)
  444. {
  445. struct can_rx_offload *offload = &dev->offload;
  446. int rc;
  447. if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
  448. const u32 ts = gs_usb_set_timestamp(dev, skb, hf);
  449. rc = can_rx_offload_queue_timestamp(offload, skb, ts);
  450. } else {
  451. rc = can_rx_offload_queue_tail(offload, skb);
  452. }
  453. if (rc)
  454. dev->netdev->stats.rx_fifo_errors++;
  455. }
  456. static unsigned int
  457. gs_usb_get_echo_skb(struct gs_can *dev, struct sk_buff *skb,
  458. const struct gs_host_frame *hf)
  459. {
  460. struct can_rx_offload *offload = &dev->offload;
  461. const u32 echo_id = hf->echo_id;
  462. unsigned int len;
  463. if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP) {
  464. const u32 ts = gs_usb_set_timestamp(dev, skb, hf);
  465. len = can_rx_offload_get_echo_skb_queue_timestamp(offload, echo_id,
  466. ts, NULL);
  467. } else {
  468. len = can_rx_offload_get_echo_skb_queue_tail(offload, echo_id,
  469. NULL);
  470. }
  471. return len;
  472. }
  473. static void gs_usb_receive_bulk_callback(struct urb *urb)
  474. {
  475. struct gs_usb *parent = urb->context;
  476. struct gs_can *dev;
  477. struct net_device *netdev;
  478. int rc;
  479. struct net_device_stats *stats;
  480. struct gs_host_frame *hf = urb->transfer_buffer;
  481. struct gs_tx_context *txc;
  482. struct can_frame *cf;
  483. struct canfd_frame *cfd;
  484. struct sk_buff *skb;
  485. BUG_ON(!parent);
  486. switch (urb->status) {
  487. case 0: /* success */
  488. break;
  489. case -ENOENT:
  490. case -ESHUTDOWN:
  491. return;
  492. default:
  493. /* do not resubmit aborted urbs. eg: when device goes down */
  494. return;
  495. }
  496. /* device reports out of range channel id */
  497. if (hf->channel >= parent->channel_cnt)
  498. goto device_detach;
  499. dev = parent->canch[hf->channel];
  500. netdev = dev->netdev;
  501. stats = &netdev->stats;
  502. if (!netif_device_present(netdev))
  503. return;
  504. if (!netif_running(netdev))
  505. goto resubmit_urb;
  506. if (hf->echo_id == -1) { /* normal rx */
  507. if (hf->flags & GS_CAN_FLAG_FD) {
  508. skb = alloc_canfd_skb(netdev, &cfd);
  509. if (!skb)
  510. return;
  511. cfd->can_id = le32_to_cpu(hf->can_id);
  512. cfd->len = can_fd_dlc2len(hf->can_dlc);
  513. if (hf->flags & GS_CAN_FLAG_BRS)
  514. cfd->flags |= CANFD_BRS;
  515. if (hf->flags & GS_CAN_FLAG_ESI)
  516. cfd->flags |= CANFD_ESI;
  517. memcpy(cfd->data, hf->canfd->data, cfd->len);
  518. } else {
  519. skb = alloc_can_skb(netdev, &cf);
  520. if (!skb)
  521. return;
  522. cf->can_id = le32_to_cpu(hf->can_id);
  523. can_frame_set_cc_len(cf, hf->can_dlc, dev->can.ctrlmode);
  524. memcpy(cf->data, hf->classic_can->data, 8);
  525. /* ERROR frames tell us information about the controller */
  526. if (le32_to_cpu(hf->can_id) & CAN_ERR_FLAG)
  527. gs_update_state(dev, cf);
  528. }
  529. gs_usb_rx_offload(dev, skb, hf);
  530. } else { /* echo_id == hf->echo_id */
  531. if (hf->echo_id >= GS_MAX_TX_URBS) {
  532. netdev_err(netdev,
  533. "Unexpected out of range echo id %u\n",
  534. hf->echo_id);
  535. goto resubmit_urb;
  536. }
  537. txc = gs_get_tx_context(dev, hf->echo_id);
  538. /* bad devices send bad echo_ids. */
  539. if (!txc) {
  540. netdev_err(netdev,
  541. "Unexpected unused echo id %u\n",
  542. hf->echo_id);
  543. goto resubmit_urb;
  544. }
  545. skb = dev->can.echo_skb[hf->echo_id];
  546. stats->tx_packets++;
  547. stats->tx_bytes += gs_usb_get_echo_skb(dev, skb, hf);
  548. gs_free_tx_context(txc);
  549. atomic_dec(&dev->active_tx_urbs);
  550. netif_wake_queue(netdev);
  551. }
  552. if (hf->flags & GS_CAN_FLAG_OVERFLOW) {
  553. stats->rx_over_errors++;
  554. stats->rx_errors++;
  555. skb = alloc_can_err_skb(netdev, &cf);
  556. if (!skb)
  557. goto resubmit_urb;
  558. cf->can_id |= CAN_ERR_CRTL;
  559. cf->len = CAN_ERR_DLC;
  560. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  561. gs_usb_rx_offload(dev, skb, hf);
  562. }
  563. can_rx_offload_irq_finish(&dev->offload);
  564. resubmit_urb:
  565. usb_fill_bulk_urb(urb, parent->udev,
  566. parent->pipe_in,
  567. hf, dev->parent->hf_size_rx,
  568. gs_usb_receive_bulk_callback, parent);
  569. rc = usb_submit_urb(urb, GFP_ATOMIC);
  570. /* USB failure take down all interfaces */
  571. if (rc == -ENODEV) {
  572. device_detach:
  573. for (rc = 0; rc < parent->channel_cnt; rc++) {
  574. if (parent->canch[rc])
  575. netif_device_detach(parent->canch[rc]->netdev);
  576. }
  577. }
  578. }
  579. static int gs_usb_set_bittiming(struct net_device *netdev)
  580. {
  581. struct gs_can *dev = netdev_priv(netdev);
  582. struct can_bittiming *bt = &dev->can.bittiming;
  583. struct gs_device_bittiming dbt = {
  584. .prop_seg = cpu_to_le32(bt->prop_seg),
  585. .phase_seg1 = cpu_to_le32(bt->phase_seg1),
  586. .phase_seg2 = cpu_to_le32(bt->phase_seg2),
  587. .sjw = cpu_to_le32(bt->sjw),
  588. .brp = cpu_to_le32(bt->brp),
  589. };
  590. /* request bit timings */
  591. return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_BITTIMING,
  592. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  593. dev->channel, 0, &dbt, sizeof(dbt), 1000,
  594. GFP_KERNEL);
  595. }
  596. static int gs_usb_set_data_bittiming(struct net_device *netdev)
  597. {
  598. struct gs_can *dev = netdev_priv(netdev);
  599. struct can_bittiming *bt = &dev->can.data_bittiming;
  600. struct gs_device_bittiming dbt = {
  601. .prop_seg = cpu_to_le32(bt->prop_seg),
  602. .phase_seg1 = cpu_to_le32(bt->phase_seg1),
  603. .phase_seg2 = cpu_to_le32(bt->phase_seg2),
  604. .sjw = cpu_to_le32(bt->sjw),
  605. .brp = cpu_to_le32(bt->brp),
  606. };
  607. u8 request = GS_USB_BREQ_DATA_BITTIMING;
  608. if (dev->feature & GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO)
  609. request = GS_USB_BREQ_QUIRK_CANTACT_PRO_DATA_BITTIMING;
  610. /* request data bit timings */
  611. return usb_control_msg_send(dev->udev, 0, request,
  612. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  613. dev->channel, 0, &dbt, sizeof(dbt), 1000,
  614. GFP_KERNEL);
  615. }
  616. static void gs_usb_xmit_callback(struct urb *urb)
  617. {
  618. struct gs_tx_context *txc = urb->context;
  619. struct gs_can *dev = txc->dev;
  620. struct net_device *netdev = dev->netdev;
  621. if (urb->status)
  622. netdev_info(netdev, "usb xmit fail %u\n", txc->echo_id);
  623. }
  624. static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb,
  625. struct net_device *netdev)
  626. {
  627. struct gs_can *dev = netdev_priv(netdev);
  628. struct net_device_stats *stats = &dev->netdev->stats;
  629. struct urb *urb;
  630. struct gs_host_frame *hf;
  631. struct can_frame *cf;
  632. struct canfd_frame *cfd;
  633. int rc;
  634. unsigned int idx;
  635. struct gs_tx_context *txc;
  636. if (can_dev_dropped_skb(netdev, skb))
  637. return NETDEV_TX_OK;
  638. /* find an empty context to keep track of transmission */
  639. txc = gs_alloc_tx_context(dev);
  640. if (!txc)
  641. return NETDEV_TX_BUSY;
  642. /* create a URB, and a buffer for it */
  643. urb = usb_alloc_urb(0, GFP_ATOMIC);
  644. if (!urb)
  645. goto nomem_urb;
  646. hf = kmalloc(dev->hf_size_tx, GFP_ATOMIC);
  647. if (!hf)
  648. goto nomem_hf;
  649. idx = txc->echo_id;
  650. if (idx >= GS_MAX_TX_URBS) {
  651. netdev_err(netdev, "Invalid tx context %u\n", idx);
  652. goto badidx;
  653. }
  654. hf->echo_id = idx;
  655. hf->channel = dev->channel;
  656. hf->flags = 0;
  657. hf->reserved = 0;
  658. if (can_is_canfd_skb(skb)) {
  659. cfd = (struct canfd_frame *)skb->data;
  660. hf->can_id = cpu_to_le32(cfd->can_id);
  661. hf->can_dlc = can_fd_len2dlc(cfd->len);
  662. hf->flags |= GS_CAN_FLAG_FD;
  663. if (cfd->flags & CANFD_BRS)
  664. hf->flags |= GS_CAN_FLAG_BRS;
  665. if (cfd->flags & CANFD_ESI)
  666. hf->flags |= GS_CAN_FLAG_ESI;
  667. memcpy(hf->canfd->data, cfd->data, cfd->len);
  668. } else {
  669. cf = (struct can_frame *)skb->data;
  670. hf->can_id = cpu_to_le32(cf->can_id);
  671. hf->can_dlc = can_get_cc_dlc(cf, dev->can.ctrlmode);
  672. memcpy(hf->classic_can->data, cf->data, cf->len);
  673. }
  674. usb_fill_bulk_urb(urb, dev->udev,
  675. dev->parent->pipe_out,
  676. hf, dev->hf_size_tx,
  677. gs_usb_xmit_callback, txc);
  678. urb->transfer_flags |= URB_FREE_BUFFER;
  679. usb_anchor_urb(urb, &dev->tx_submitted);
  680. can_put_echo_skb(skb, netdev, idx, 0);
  681. atomic_inc(&dev->active_tx_urbs);
  682. rc = usb_submit_urb(urb, GFP_ATOMIC);
  683. if (unlikely(rc)) { /* usb send failed */
  684. atomic_dec(&dev->active_tx_urbs);
  685. can_free_echo_skb(netdev, idx, NULL);
  686. gs_free_tx_context(txc);
  687. usb_unanchor_urb(urb);
  688. if (rc == -ENODEV) {
  689. netif_device_detach(netdev);
  690. } else {
  691. netdev_err(netdev, "usb_submit failed (err=%d)\n", rc);
  692. stats->tx_dropped++;
  693. }
  694. } else {
  695. /* Slow down tx path */
  696. if (atomic_read(&dev->active_tx_urbs) >= GS_MAX_TX_URBS)
  697. netif_stop_queue(netdev);
  698. }
  699. /* let usb core take care of this urb */
  700. usb_free_urb(urb);
  701. return NETDEV_TX_OK;
  702. badidx:
  703. kfree(hf);
  704. nomem_hf:
  705. usb_free_urb(urb);
  706. nomem_urb:
  707. gs_free_tx_context(txc);
  708. dev_kfree_skb(skb);
  709. stats->tx_dropped++;
  710. return NETDEV_TX_OK;
  711. }
  712. static int gs_can_open(struct net_device *netdev)
  713. {
  714. struct gs_can *dev = netdev_priv(netdev);
  715. struct gs_usb *parent = dev->parent;
  716. struct gs_device_mode dm = {
  717. .mode = cpu_to_le32(GS_CAN_MODE_START),
  718. };
  719. struct gs_host_frame *hf;
  720. struct urb *urb = NULL;
  721. u32 ctrlmode;
  722. u32 flags = 0;
  723. int rc, i;
  724. rc = open_candev(netdev);
  725. if (rc)
  726. return rc;
  727. ctrlmode = dev->can.ctrlmode;
  728. if (ctrlmode & CAN_CTRLMODE_FD) {
  729. if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
  730. dev->hf_size_tx = struct_size(hf, canfd_quirk, 1);
  731. else
  732. dev->hf_size_tx = struct_size(hf, canfd, 1);
  733. } else {
  734. if (dev->feature & GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX)
  735. dev->hf_size_tx = struct_size(hf, classic_can_quirk, 1);
  736. else
  737. dev->hf_size_tx = struct_size(hf, classic_can, 1);
  738. }
  739. can_rx_offload_enable(&dev->offload);
  740. if (!parent->active_channels) {
  741. if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
  742. gs_usb_timestamp_init(parent);
  743. for (i = 0; i < GS_MAX_RX_URBS; i++) {
  744. u8 *buf;
  745. /* alloc rx urb */
  746. urb = usb_alloc_urb(0, GFP_KERNEL);
  747. if (!urb) {
  748. rc = -ENOMEM;
  749. goto out_usb_kill_anchored_urbs;
  750. }
  751. /* alloc rx buffer */
  752. buf = kmalloc(dev->parent->hf_size_rx,
  753. GFP_KERNEL);
  754. if (!buf) {
  755. rc = -ENOMEM;
  756. goto out_usb_free_urb;
  757. }
  758. /* fill, anchor, and submit rx urb */
  759. usb_fill_bulk_urb(urb,
  760. dev->udev,
  761. dev->parent->pipe_in,
  762. buf,
  763. dev->parent->hf_size_rx,
  764. gs_usb_receive_bulk_callback, parent);
  765. urb->transfer_flags |= URB_FREE_BUFFER;
  766. usb_anchor_urb(urb, &parent->rx_submitted);
  767. rc = usb_submit_urb(urb, GFP_KERNEL);
  768. if (rc) {
  769. if (rc == -ENODEV)
  770. netif_device_detach(dev->netdev);
  771. netdev_err(netdev,
  772. "usb_submit_urb() failed, error %pe\n",
  773. ERR_PTR(rc));
  774. goto out_usb_unanchor_urb;
  775. }
  776. /* Drop reference,
  777. * USB core will take care of freeing it
  778. */
  779. usb_free_urb(urb);
  780. }
  781. }
  782. /* flags */
  783. if (ctrlmode & CAN_CTRLMODE_LOOPBACK)
  784. flags |= GS_CAN_MODE_LOOP_BACK;
  785. if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
  786. flags |= GS_CAN_MODE_LISTEN_ONLY;
  787. if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  788. flags |= GS_CAN_MODE_TRIPLE_SAMPLE;
  789. if (ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  790. flags |= GS_CAN_MODE_ONE_SHOT;
  791. if (ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
  792. flags |= GS_CAN_MODE_BERR_REPORTING;
  793. if (ctrlmode & CAN_CTRLMODE_FD)
  794. flags |= GS_CAN_MODE_FD;
  795. /* if hardware supports timestamps, enable it */
  796. if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
  797. flags |= GS_CAN_MODE_HW_TIMESTAMP;
  798. /* finally start device */
  799. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  800. dm.flags = cpu_to_le32(flags);
  801. rc = usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_MODE,
  802. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  803. dev->channel, 0, &dm, sizeof(dm), 1000,
  804. GFP_KERNEL);
  805. if (rc) {
  806. netdev_err(netdev, "Couldn't start device (err=%d)\n", rc);
  807. dev->can.state = CAN_STATE_STOPPED;
  808. goto out_usb_kill_anchored_urbs;
  809. }
  810. parent->active_channels++;
  811. if (!(dev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
  812. netif_start_queue(netdev);
  813. return 0;
  814. out_usb_unanchor_urb:
  815. usb_unanchor_urb(urb);
  816. out_usb_free_urb:
  817. usb_free_urb(urb);
  818. out_usb_kill_anchored_urbs:
  819. if (!parent->active_channels) {
  820. usb_kill_anchored_urbs(&dev->tx_submitted);
  821. if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
  822. gs_usb_timestamp_stop(parent);
  823. }
  824. can_rx_offload_disable(&dev->offload);
  825. close_candev(netdev);
  826. return rc;
  827. }
  828. static int gs_usb_get_state(const struct net_device *netdev,
  829. struct can_berr_counter *bec,
  830. enum can_state *state)
  831. {
  832. struct gs_can *dev = netdev_priv(netdev);
  833. struct gs_device_state ds;
  834. int rc;
  835. rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_STATE,
  836. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  837. dev->channel, 0,
  838. &ds, sizeof(ds),
  839. USB_CTRL_GET_TIMEOUT,
  840. GFP_KERNEL);
  841. if (rc)
  842. return rc;
  843. if (le32_to_cpu(ds.state) >= CAN_STATE_MAX)
  844. return -EOPNOTSUPP;
  845. *state = le32_to_cpu(ds.state);
  846. bec->txerr = le32_to_cpu(ds.txerr);
  847. bec->rxerr = le32_to_cpu(ds.rxerr);
  848. return 0;
  849. }
  850. static int gs_usb_can_get_berr_counter(const struct net_device *netdev,
  851. struct can_berr_counter *bec)
  852. {
  853. enum can_state state;
  854. return gs_usb_get_state(netdev, bec, &state);
  855. }
  856. static int gs_can_close(struct net_device *netdev)
  857. {
  858. int rc;
  859. struct gs_can *dev = netdev_priv(netdev);
  860. struct gs_usb *parent = dev->parent;
  861. netif_stop_queue(netdev);
  862. /* Stop polling */
  863. parent->active_channels--;
  864. if (!parent->active_channels) {
  865. usb_kill_anchored_urbs(&parent->rx_submitted);
  866. if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
  867. gs_usb_timestamp_stop(parent);
  868. }
  869. /* Stop sending URBs */
  870. usb_kill_anchored_urbs(&dev->tx_submitted);
  871. atomic_set(&dev->active_tx_urbs, 0);
  872. dev->can.state = CAN_STATE_STOPPED;
  873. /* reset the device */
  874. gs_cmd_reset(dev);
  875. /* reset tx contexts */
  876. for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
  877. dev->tx_context[rc].dev = dev;
  878. dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
  879. }
  880. can_rx_offload_disable(&dev->offload);
  881. /* close the netdev */
  882. close_candev(netdev);
  883. return 0;
  884. }
  885. static int gs_can_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  886. {
  887. const struct gs_can *dev = netdev_priv(netdev);
  888. if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
  889. return can_eth_ioctl_hwts(netdev, ifr, cmd);
  890. return -EOPNOTSUPP;
  891. }
  892. static const struct net_device_ops gs_usb_netdev_ops = {
  893. .ndo_open = gs_can_open,
  894. .ndo_stop = gs_can_close,
  895. .ndo_start_xmit = gs_can_start_xmit,
  896. .ndo_change_mtu = can_change_mtu,
  897. .ndo_eth_ioctl = gs_can_eth_ioctl,
  898. };
  899. static int gs_usb_set_identify(struct net_device *netdev, bool do_identify)
  900. {
  901. struct gs_can *dev = netdev_priv(netdev);
  902. struct gs_identify_mode imode;
  903. if (do_identify)
  904. imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_ON);
  905. else
  906. imode.mode = cpu_to_le32(GS_CAN_IDENTIFY_OFF);
  907. return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_IDENTIFY,
  908. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  909. dev->channel, 0, &imode, sizeof(imode), 100,
  910. GFP_KERNEL);
  911. }
  912. /* blink LED's for finding the this interface */
  913. static int gs_usb_set_phys_id(struct net_device *netdev,
  914. enum ethtool_phys_id_state state)
  915. {
  916. const struct gs_can *dev = netdev_priv(netdev);
  917. int rc = 0;
  918. if (!(dev->feature & GS_CAN_FEATURE_IDENTIFY))
  919. return -EOPNOTSUPP;
  920. switch (state) {
  921. case ETHTOOL_ID_ACTIVE:
  922. rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_ON);
  923. break;
  924. case ETHTOOL_ID_INACTIVE:
  925. rc = gs_usb_set_identify(netdev, GS_CAN_IDENTIFY_OFF);
  926. break;
  927. default:
  928. break;
  929. }
  930. return rc;
  931. }
  932. static int gs_usb_get_ts_info(struct net_device *netdev,
  933. struct kernel_ethtool_ts_info *info)
  934. {
  935. struct gs_can *dev = netdev_priv(netdev);
  936. /* report if device supports HW timestamps */
  937. if (dev->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
  938. return can_ethtool_op_get_ts_info_hwts(netdev, info);
  939. return ethtool_op_get_ts_info(netdev, info);
  940. }
  941. static const struct ethtool_ops gs_usb_ethtool_ops = {
  942. .set_phys_id = gs_usb_set_phys_id,
  943. .get_ts_info = gs_usb_get_ts_info,
  944. };
  945. static int gs_usb_get_termination(struct net_device *netdev, u16 *term)
  946. {
  947. struct gs_can *dev = netdev_priv(netdev);
  948. struct gs_device_termination_state term_state;
  949. int rc;
  950. rc = usb_control_msg_recv(dev->udev, 0, GS_USB_BREQ_GET_TERMINATION,
  951. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  952. dev->channel, 0,
  953. &term_state, sizeof(term_state), 1000,
  954. GFP_KERNEL);
  955. if (rc)
  956. return rc;
  957. if (term_state.state == cpu_to_le32(GS_CAN_TERMINATION_STATE_ON))
  958. *term = GS_USB_TERMINATION_ENABLED;
  959. else
  960. *term = GS_USB_TERMINATION_DISABLED;
  961. return 0;
  962. }
  963. static int gs_usb_set_termination(struct net_device *netdev, u16 term)
  964. {
  965. struct gs_can *dev = netdev_priv(netdev);
  966. struct gs_device_termination_state term_state;
  967. if (term == GS_USB_TERMINATION_ENABLED)
  968. term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_ON);
  969. else
  970. term_state.state = cpu_to_le32(GS_CAN_TERMINATION_STATE_OFF);
  971. return usb_control_msg_send(dev->udev, 0, GS_USB_BREQ_SET_TERMINATION,
  972. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  973. dev->channel, 0,
  974. &term_state, sizeof(term_state), 1000,
  975. GFP_KERNEL);
  976. }
  977. static const u16 gs_usb_termination_const[] = {
  978. GS_USB_TERMINATION_DISABLED,
  979. GS_USB_TERMINATION_ENABLED
  980. };
  981. static struct gs_can *gs_make_candev(unsigned int channel,
  982. struct usb_interface *intf,
  983. struct gs_device_config *dconf)
  984. {
  985. struct gs_can *dev;
  986. struct net_device *netdev;
  987. int rc;
  988. struct gs_device_bt_const_extended bt_const_extended;
  989. struct gs_device_bt_const bt_const;
  990. u32 feature;
  991. /* fetch bit timing constants */
  992. rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
  993. GS_USB_BREQ_BT_CONST,
  994. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  995. channel, 0, &bt_const, sizeof(bt_const), 1000,
  996. GFP_KERNEL);
  997. if (rc) {
  998. dev_err(&intf->dev,
  999. "Couldn't get bit timing const for channel %d (%pe)\n",
  1000. channel, ERR_PTR(rc));
  1001. return ERR_PTR(rc);
  1002. }
  1003. /* create netdev */
  1004. netdev = alloc_candev(sizeof(struct gs_can), GS_MAX_TX_URBS);
  1005. if (!netdev) {
  1006. dev_err(&intf->dev, "Couldn't allocate candev\n");
  1007. return ERR_PTR(-ENOMEM);
  1008. }
  1009. dev = netdev_priv(netdev);
  1010. netdev->netdev_ops = &gs_usb_netdev_ops;
  1011. netdev->ethtool_ops = &gs_usb_ethtool_ops;
  1012. netdev->flags |= IFF_ECHO; /* we support full roundtrip echo */
  1013. netdev->dev_id = channel;
  1014. netdev->dev_port = channel;
  1015. /* dev setup */
  1016. strcpy(dev->bt_const.name, KBUILD_MODNAME);
  1017. dev->bt_const.tseg1_min = le32_to_cpu(bt_const.tseg1_min);
  1018. dev->bt_const.tseg1_max = le32_to_cpu(bt_const.tseg1_max);
  1019. dev->bt_const.tseg2_min = le32_to_cpu(bt_const.tseg2_min);
  1020. dev->bt_const.tseg2_max = le32_to_cpu(bt_const.tseg2_max);
  1021. dev->bt_const.sjw_max = le32_to_cpu(bt_const.sjw_max);
  1022. dev->bt_const.brp_min = le32_to_cpu(bt_const.brp_min);
  1023. dev->bt_const.brp_max = le32_to_cpu(bt_const.brp_max);
  1024. dev->bt_const.brp_inc = le32_to_cpu(bt_const.brp_inc);
  1025. dev->udev = interface_to_usbdev(intf);
  1026. dev->netdev = netdev;
  1027. dev->channel = channel;
  1028. init_usb_anchor(&dev->tx_submitted);
  1029. atomic_set(&dev->active_tx_urbs, 0);
  1030. spin_lock_init(&dev->tx_ctx_lock);
  1031. for (rc = 0; rc < GS_MAX_TX_URBS; rc++) {
  1032. dev->tx_context[rc].dev = dev;
  1033. dev->tx_context[rc].echo_id = GS_MAX_TX_URBS;
  1034. }
  1035. /* can setup */
  1036. dev->can.state = CAN_STATE_STOPPED;
  1037. dev->can.clock.freq = le32_to_cpu(bt_const.fclk_can);
  1038. dev->can.bittiming_const = &dev->bt_const;
  1039. dev->can.do_set_bittiming = gs_usb_set_bittiming;
  1040. dev->can.ctrlmode_supported = CAN_CTRLMODE_CC_LEN8_DLC;
  1041. feature = le32_to_cpu(bt_const.feature);
  1042. dev->feature = FIELD_GET(GS_CAN_FEATURE_MASK, feature);
  1043. if (feature & GS_CAN_FEATURE_LISTEN_ONLY)
  1044. dev->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
  1045. if (feature & GS_CAN_FEATURE_LOOP_BACK)
  1046. dev->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
  1047. if (feature & GS_CAN_FEATURE_TRIPLE_SAMPLE)
  1048. dev->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  1049. if (feature & GS_CAN_FEATURE_ONE_SHOT)
  1050. dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
  1051. if (feature & GS_CAN_FEATURE_FD) {
  1052. dev->can.ctrlmode_supported |= CAN_CTRLMODE_FD;
  1053. /* The data bit timing will be overwritten, if
  1054. * GS_CAN_FEATURE_BT_CONST_EXT is set.
  1055. */
  1056. dev->can.data_bittiming_const = &dev->bt_const;
  1057. dev->can.do_set_data_bittiming = gs_usb_set_data_bittiming;
  1058. }
  1059. if (feature & GS_CAN_FEATURE_TERMINATION) {
  1060. rc = gs_usb_get_termination(netdev, &dev->can.termination);
  1061. if (rc) {
  1062. dev->feature &= ~GS_CAN_FEATURE_TERMINATION;
  1063. dev_info(&intf->dev,
  1064. "Disabling termination support for channel %d (%pe)\n",
  1065. channel, ERR_PTR(rc));
  1066. } else {
  1067. dev->can.termination_const = gs_usb_termination_const;
  1068. dev->can.termination_const_cnt = ARRAY_SIZE(gs_usb_termination_const);
  1069. dev->can.do_set_termination = gs_usb_set_termination;
  1070. }
  1071. }
  1072. if (feature & GS_CAN_FEATURE_BERR_REPORTING)
  1073. dev->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
  1074. if (feature & GS_CAN_FEATURE_GET_STATE)
  1075. dev->can.do_get_berr_counter = gs_usb_can_get_berr_counter;
  1076. /* The CANtact Pro from LinkLayer Labs is based on the
  1077. * LPC54616 µC, which is affected by the NXP LPC USB transfer
  1078. * erratum. However, the current firmware (version 2) doesn't
  1079. * set the GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX bit. Set the
  1080. * feature GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX to workaround
  1081. * this issue.
  1082. *
  1083. * For the GS_USB_BREQ_DATA_BITTIMING USB control message the
  1084. * CANtact Pro firmware uses a request value, which is already
  1085. * used by the candleLight firmware for a different purpose
  1086. * (GS_USB_BREQ_GET_USER_ID). Set the feature
  1087. * GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO to workaround this
  1088. * issue.
  1089. */
  1090. if (dev->udev->descriptor.idVendor == cpu_to_le16(USB_GS_USB_1_VENDOR_ID) &&
  1091. dev->udev->descriptor.idProduct == cpu_to_le16(USB_GS_USB_1_PRODUCT_ID) &&
  1092. dev->udev->manufacturer && dev->udev->product &&
  1093. !strcmp(dev->udev->manufacturer, "LinkLayer Labs") &&
  1094. !strcmp(dev->udev->product, "CANtact Pro") &&
  1095. (le32_to_cpu(dconf->sw_version) <= 2))
  1096. dev->feature |= GS_CAN_FEATURE_REQ_USB_QUIRK_LPC546XX |
  1097. GS_CAN_FEATURE_QUIRK_BREQ_CANTACT_PRO;
  1098. /* GS_CAN_FEATURE_IDENTIFY is only supported for sw_version > 1 */
  1099. if (!(le32_to_cpu(dconf->sw_version) > 1 &&
  1100. feature & GS_CAN_FEATURE_IDENTIFY))
  1101. dev->feature &= ~GS_CAN_FEATURE_IDENTIFY;
  1102. /* fetch extended bit timing constants if device has feature
  1103. * GS_CAN_FEATURE_FD and GS_CAN_FEATURE_BT_CONST_EXT
  1104. */
  1105. if (feature & GS_CAN_FEATURE_FD &&
  1106. feature & GS_CAN_FEATURE_BT_CONST_EXT) {
  1107. rc = usb_control_msg_recv(interface_to_usbdev(intf), 0,
  1108. GS_USB_BREQ_BT_CONST_EXT,
  1109. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  1110. channel, 0, &bt_const_extended,
  1111. sizeof(bt_const_extended),
  1112. 1000, GFP_KERNEL);
  1113. if (rc) {
  1114. dev_err(&intf->dev,
  1115. "Couldn't get extended bit timing const for channel %d (%pe)\n",
  1116. channel, ERR_PTR(rc));
  1117. goto out_free_candev;
  1118. }
  1119. strcpy(dev->data_bt_const.name, KBUILD_MODNAME);
  1120. dev->data_bt_const.tseg1_min = le32_to_cpu(bt_const_extended.dtseg1_min);
  1121. dev->data_bt_const.tseg1_max = le32_to_cpu(bt_const_extended.dtseg1_max);
  1122. dev->data_bt_const.tseg2_min = le32_to_cpu(bt_const_extended.dtseg2_min);
  1123. dev->data_bt_const.tseg2_max = le32_to_cpu(bt_const_extended.dtseg2_max);
  1124. dev->data_bt_const.sjw_max = le32_to_cpu(bt_const_extended.dsjw_max);
  1125. dev->data_bt_const.brp_min = le32_to_cpu(bt_const_extended.dbrp_min);
  1126. dev->data_bt_const.brp_max = le32_to_cpu(bt_const_extended.dbrp_max);
  1127. dev->data_bt_const.brp_inc = le32_to_cpu(bt_const_extended.dbrp_inc);
  1128. dev->can.data_bittiming_const = &dev->data_bt_const;
  1129. }
  1130. can_rx_offload_add_manual(netdev, &dev->offload, GS_NAPI_WEIGHT);
  1131. SET_NETDEV_DEV(netdev, &intf->dev);
  1132. rc = register_candev(dev->netdev);
  1133. if (rc) {
  1134. dev_err(&intf->dev,
  1135. "Couldn't register candev for channel %d (%pe)\n",
  1136. channel, ERR_PTR(rc));
  1137. goto out_can_rx_offload_del;
  1138. }
  1139. return dev;
  1140. out_can_rx_offload_del:
  1141. can_rx_offload_del(&dev->offload);
  1142. out_free_candev:
  1143. free_candev(dev->netdev);
  1144. return ERR_PTR(rc);
  1145. }
  1146. static void gs_destroy_candev(struct gs_can *dev)
  1147. {
  1148. unregister_candev(dev->netdev);
  1149. can_rx_offload_del(&dev->offload);
  1150. free_candev(dev->netdev);
  1151. }
  1152. static int gs_usb_probe(struct usb_interface *intf,
  1153. const struct usb_device_id *id)
  1154. {
  1155. struct usb_device *udev = interface_to_usbdev(intf);
  1156. struct usb_endpoint_descriptor *ep_in, *ep_out;
  1157. struct gs_host_frame *hf;
  1158. struct gs_usb *parent;
  1159. struct gs_host_config hconf = {
  1160. .byte_order = cpu_to_le32(0x0000beef),
  1161. };
  1162. struct gs_device_config dconf;
  1163. unsigned int icount, i;
  1164. int rc;
  1165. rc = usb_find_common_endpoints(intf->cur_altsetting,
  1166. &ep_in, &ep_out, NULL, NULL);
  1167. if (rc) {
  1168. dev_err(&intf->dev, "Required endpoints not found\n");
  1169. return rc;
  1170. }
  1171. /* send host config */
  1172. rc = usb_control_msg_send(udev, 0,
  1173. GS_USB_BREQ_HOST_FORMAT,
  1174. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  1175. 1, intf->cur_altsetting->desc.bInterfaceNumber,
  1176. &hconf, sizeof(hconf), 1000,
  1177. GFP_KERNEL);
  1178. if (rc) {
  1179. dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", rc);
  1180. return rc;
  1181. }
  1182. /* read device config */
  1183. rc = usb_control_msg_recv(udev, 0,
  1184. GS_USB_BREQ_DEVICE_CONFIG,
  1185. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  1186. 1, intf->cur_altsetting->desc.bInterfaceNumber,
  1187. &dconf, sizeof(dconf), 1000,
  1188. GFP_KERNEL);
  1189. if (rc) {
  1190. dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n",
  1191. rc);
  1192. return rc;
  1193. }
  1194. icount = dconf.icount + 1;
  1195. dev_info(&intf->dev, "Configuring for %u interfaces\n", icount);
  1196. if (icount > type_max(parent->channel_cnt)) {
  1197. dev_err(&intf->dev,
  1198. "Driver cannot handle more that %u CAN interfaces\n",
  1199. type_max(parent->channel_cnt));
  1200. return -EINVAL;
  1201. }
  1202. parent = kzalloc(struct_size(parent, canch, icount), GFP_KERNEL);
  1203. if (!parent)
  1204. return -ENOMEM;
  1205. parent->channel_cnt = icount;
  1206. init_usb_anchor(&parent->rx_submitted);
  1207. usb_set_intfdata(intf, parent);
  1208. parent->udev = udev;
  1209. /* store the detected endpoints */
  1210. parent->pipe_in = usb_rcvbulkpipe(parent->udev, ep_in->bEndpointAddress);
  1211. parent->pipe_out = usb_sndbulkpipe(parent->udev, ep_out->bEndpointAddress);
  1212. for (i = 0; i < icount; i++) {
  1213. unsigned int hf_size_rx = 0;
  1214. parent->canch[i] = gs_make_candev(i, intf, &dconf);
  1215. if (IS_ERR_OR_NULL(parent->canch[i])) {
  1216. /* save error code to return later */
  1217. rc = PTR_ERR(parent->canch[i]);
  1218. /* on failure destroy previously created candevs */
  1219. icount = i;
  1220. for (i = 0; i < icount; i++)
  1221. gs_destroy_candev(parent->canch[i]);
  1222. usb_kill_anchored_urbs(&parent->rx_submitted);
  1223. kfree(parent);
  1224. return rc;
  1225. }
  1226. parent->canch[i]->parent = parent;
  1227. /* set RX packet size based on FD and if hardware
  1228. * timestamps are supported.
  1229. */
  1230. if (parent->canch[i]->can.ctrlmode_supported & CAN_CTRLMODE_FD) {
  1231. if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
  1232. hf_size_rx = struct_size(hf, canfd_ts, 1);
  1233. else
  1234. hf_size_rx = struct_size(hf, canfd, 1);
  1235. } else {
  1236. if (parent->canch[i]->feature & GS_CAN_FEATURE_HW_TIMESTAMP)
  1237. hf_size_rx = struct_size(hf, classic_can_ts, 1);
  1238. else
  1239. hf_size_rx = struct_size(hf, classic_can, 1);
  1240. }
  1241. parent->hf_size_rx = max(parent->hf_size_rx, hf_size_rx);
  1242. }
  1243. return 0;
  1244. }
  1245. static void gs_usb_disconnect(struct usb_interface *intf)
  1246. {
  1247. struct gs_usb *parent = usb_get_intfdata(intf);
  1248. unsigned int i;
  1249. usb_set_intfdata(intf, NULL);
  1250. if (!parent) {
  1251. dev_err(&intf->dev, "Disconnect (nodata)\n");
  1252. return;
  1253. }
  1254. for (i = 0; i < parent->channel_cnt; i++)
  1255. if (parent->canch[i])
  1256. gs_destroy_candev(parent->canch[i]);
  1257. kfree(parent);
  1258. }
  1259. static const struct usb_device_id gs_usb_table[] = {
  1260. { USB_DEVICE_INTERFACE_NUMBER(USB_GS_USB_1_VENDOR_ID,
  1261. USB_GS_USB_1_PRODUCT_ID, 0) },
  1262. { USB_DEVICE_INTERFACE_NUMBER(USB_CANDLELIGHT_VENDOR_ID,
  1263. USB_CANDLELIGHT_PRODUCT_ID, 0) },
  1264. { USB_DEVICE_INTERFACE_NUMBER(USB_CES_CANEXT_FD_VENDOR_ID,
  1265. USB_CES_CANEXT_FD_PRODUCT_ID, 0) },
  1266. { USB_DEVICE_INTERFACE_NUMBER(USB_ABE_CANDEBUGGER_FD_VENDOR_ID,
  1267. USB_ABE_CANDEBUGGER_FD_PRODUCT_ID, 0) },
  1268. { USB_DEVICE_INTERFACE_NUMBER(USB_XYLANTA_SAINT3_VENDOR_ID,
  1269. USB_XYLANTA_SAINT3_PRODUCT_ID, 0) },
  1270. {} /* Terminating entry */
  1271. };
  1272. MODULE_DEVICE_TABLE(usb, gs_usb_table);
  1273. static struct usb_driver gs_usb_driver = {
  1274. .name = KBUILD_MODNAME,
  1275. .probe = gs_usb_probe,
  1276. .disconnect = gs_usb_disconnect,
  1277. .id_table = gs_usb_table,
  1278. };
  1279. module_usb_driver(gs_usb_driver);
  1280. MODULE_AUTHOR("Maximilian Schneider <mws@schneidersoft.net>");
  1281. MODULE_DESCRIPTION(
  1282. "Socket CAN device driver for Geschwister Schneider Technologie-, "
  1283. "Entwicklungs- und Vertriebs UG. USB2.0 to CAN interfaces\n"
  1284. "and bytewerk.org candleLight USB CAN interfaces.");
  1285. MODULE_LICENSE("GPL v2");