ucan.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Driver for Theobroma Systems UCAN devices, Protocol Version 3
  3. *
  4. * Copyright (C) 2018 Theobroma Systems Design und Consulting GmbH
  5. *
  6. *
  7. * General Description:
  8. *
  9. * The USB Device uses three Endpoints:
  10. *
  11. * CONTROL Endpoint: Is used the setup the device (start, stop,
  12. * info, configure).
  13. *
  14. * IN Endpoint: The device sends CAN Frame Messages and Device
  15. * Information using the IN endpoint.
  16. *
  17. * OUT Endpoint: The driver sends configuration requests, and CAN
  18. * Frames on the out endpoint.
  19. *
  20. * Error Handling:
  21. *
  22. * If error reporting is turned on the device encodes error into CAN
  23. * error frames (see uapi/linux/can/error.h) and sends it using the
  24. * IN Endpoint. The driver updates statistics and forward it.
  25. */
  26. #include <linux/can.h>
  27. #include <linux/can/dev.h>
  28. #include <linux/can/error.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/module.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/signal.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/slab.h>
  35. #include <linux/usb.h>
  36. #define UCAN_DRIVER_NAME "ucan"
  37. #define UCAN_MAX_RX_URBS 8
  38. /* the CAN controller needs a while to enable/disable the bus */
  39. #define UCAN_USB_CTL_PIPE_TIMEOUT 1000
  40. /* this driver currently supports protocol version 3 only */
  41. #define UCAN_PROTOCOL_VERSION_MIN 3
  42. #define UCAN_PROTOCOL_VERSION_MAX 3
  43. /* UCAN Message Definitions
  44. * ------------------------
  45. *
  46. * ucan_message_out_t and ucan_message_in_t define the messages
  47. * transmitted on the OUT and IN endpoint.
  48. *
  49. * Multibyte fields are transmitted with little endianness
  50. *
  51. * INTR Endpoint: a single uint32_t storing the current space in the fifo
  52. *
  53. * OUT Endpoint: single message of type ucan_message_out_t is
  54. * transmitted on the out endpoint
  55. *
  56. * IN Endpoint: multiple messages ucan_message_in_t concateted in
  57. * the following way:
  58. *
  59. * m[n].len <=> the length if message n(including the header in bytes)
  60. * m[n] is is aligned to a 4 byte boundary, hence
  61. * offset(m[0]) := 0;
  62. * offset(m[n+1]) := offset(m[n]) + (m[n].len + 3) & 3
  63. *
  64. * this implies that
  65. * offset(m[n]) % 4 <=> 0
  66. */
  67. /* Device Global Commands */
  68. enum {
  69. UCAN_DEVICE_GET_FW_STRING = 0,
  70. };
  71. /* UCAN Commands */
  72. enum {
  73. /* start the can transceiver - val defines the operation mode */
  74. UCAN_COMMAND_START = 0,
  75. /* cancel pending transmissions and stop the can transceiver */
  76. UCAN_COMMAND_STOP = 1,
  77. /* send can transceiver into low-power sleep mode */
  78. UCAN_COMMAND_SLEEP = 2,
  79. /* wake up can transceiver from low-power sleep mode */
  80. UCAN_COMMAND_WAKEUP = 3,
  81. /* reset the can transceiver */
  82. UCAN_COMMAND_RESET = 4,
  83. /* get piece of info from the can transceiver - subcmd defines what
  84. * piece
  85. */
  86. UCAN_COMMAND_GET = 5,
  87. /* clear or disable hardware filter - subcmd defines which of the two */
  88. UCAN_COMMAND_FILTER = 6,
  89. /* Setup bittiming */
  90. UCAN_COMMAND_SET_BITTIMING = 7,
  91. /* recover from bus-off state */
  92. UCAN_COMMAND_RESTART = 8,
  93. };
  94. /* UCAN_COMMAND_START and UCAN_COMMAND_GET_INFO operation modes (bitmap).
  95. * Undefined bits must be set to 0.
  96. */
  97. enum {
  98. UCAN_MODE_LOOPBACK = BIT(0),
  99. UCAN_MODE_SILENT = BIT(1),
  100. UCAN_MODE_3_SAMPLES = BIT(2),
  101. UCAN_MODE_ONE_SHOT = BIT(3),
  102. UCAN_MODE_BERR_REPORT = BIT(4),
  103. };
  104. /* UCAN_COMMAND_GET subcommands */
  105. enum {
  106. UCAN_COMMAND_GET_INFO = 0,
  107. UCAN_COMMAND_GET_PROTOCOL_VERSION = 1,
  108. };
  109. /* UCAN_COMMAND_FILTER subcommands */
  110. enum {
  111. UCAN_FILTER_CLEAR = 0,
  112. UCAN_FILTER_DISABLE = 1,
  113. UCAN_FILTER_ENABLE = 2,
  114. };
  115. /* OUT endpoint message types */
  116. enum {
  117. UCAN_OUT_TX = 2, /* transmit a CAN frame */
  118. };
  119. /* IN endpoint message types */
  120. enum {
  121. UCAN_IN_TX_COMPLETE = 1, /* CAN frame transmission completed */
  122. UCAN_IN_RX = 2, /* CAN frame received */
  123. };
  124. struct ucan_ctl_cmd_start {
  125. __le16 mode; /* OR-ing any of UCAN_MODE_* */
  126. } __packed;
  127. struct ucan_ctl_cmd_set_bittiming {
  128. __le32 tq; /* Time quanta (TQ) in nanoseconds */
  129. __le16 brp; /* TQ Prescaler */
  130. __le16 sample_point; /* Samplepoint on tenth percent */
  131. u8 prop_seg; /* Propagation segment in TQs */
  132. u8 phase_seg1; /* Phase buffer segment 1 in TQs */
  133. u8 phase_seg2; /* Phase buffer segment 2 in TQs */
  134. u8 sjw; /* Synchronisation jump width in TQs */
  135. } __packed;
  136. struct ucan_ctl_cmd_device_info {
  137. __le32 freq; /* Clock Frequency for tq generation */
  138. u8 tx_fifo; /* Size of the transmission fifo */
  139. u8 sjw_max; /* can_bittiming fields... */
  140. u8 tseg1_min;
  141. u8 tseg1_max;
  142. u8 tseg2_min;
  143. u8 tseg2_max;
  144. __le16 brp_inc;
  145. __le32 brp_min;
  146. __le32 brp_max; /* ...can_bittiming fields */
  147. __le16 ctrlmodes; /* supported control modes */
  148. __le16 hwfilter; /* Number of HW filter banks */
  149. __le16 rxmboxes; /* Number of receive Mailboxes */
  150. } __packed;
  151. struct ucan_ctl_cmd_get_protocol_version {
  152. __le32 version;
  153. } __packed;
  154. union ucan_ctl_payload {
  155. /* Setup Bittiming
  156. * bmRequest == UCAN_COMMAND_START
  157. */
  158. struct ucan_ctl_cmd_start cmd_start;
  159. /* Setup Bittiming
  160. * bmRequest == UCAN_COMMAND_SET_BITTIMING
  161. */
  162. struct ucan_ctl_cmd_set_bittiming cmd_set_bittiming;
  163. /* Get Device Information
  164. * bmRequest == UCAN_COMMAND_GET; wValue = UCAN_COMMAND_GET_INFO
  165. */
  166. struct ucan_ctl_cmd_device_info cmd_get_device_info;
  167. /* Get Protocol Version
  168. * bmRequest == UCAN_COMMAND_GET;
  169. * wValue = UCAN_COMMAND_GET_PROTOCOL_VERSION
  170. */
  171. struct ucan_ctl_cmd_get_protocol_version cmd_get_protocol_version;
  172. u8 fw_str[128];
  173. } __packed;
  174. enum {
  175. UCAN_TX_COMPLETE_SUCCESS = BIT(0),
  176. };
  177. /* Transmission Complete within ucan_message_in */
  178. struct ucan_tx_complete_entry_t {
  179. u8 echo_index;
  180. u8 flags;
  181. } __packed __aligned(0x2);
  182. /* CAN Data message format within ucan_message_in/out */
  183. struct ucan_can_msg {
  184. /* note DLC is computed by
  185. * msg.len - sizeof (msg.len)
  186. * - sizeof (msg.type)
  187. * - sizeof (msg.can_msg.id)
  188. */
  189. __le32 id;
  190. union {
  191. u8 data[CAN_MAX_DLEN]; /* Data of CAN frames */
  192. u8 dlc; /* RTR dlc */
  193. };
  194. } __packed;
  195. /* OUT Endpoint, outbound messages */
  196. struct ucan_message_out {
  197. __le16 len; /* Length of the content include header */
  198. u8 type; /* UCAN_OUT_TX and friends */
  199. u8 subtype; /* command sub type */
  200. union {
  201. /* Transmit CAN frame
  202. * (type == UCAN_TX) && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
  203. * subtype stores the echo id
  204. */
  205. struct ucan_can_msg can_msg;
  206. } msg;
  207. } __packed __aligned(0x4);
  208. /* IN Endpoint, inbound messages */
  209. struct ucan_message_in {
  210. __le16 len; /* Length of the content include header */
  211. u8 type; /* UCAN_IN_RX and friends */
  212. u8 subtype; /* command sub type */
  213. union {
  214. /* CAN Frame received
  215. * (type == UCAN_IN_RX)
  216. * && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
  217. */
  218. struct ucan_can_msg can_msg;
  219. /* CAN transmission complete
  220. * (type == UCAN_IN_TX_COMPLETE)
  221. */
  222. DECLARE_FLEX_ARRAY(struct ucan_tx_complete_entry_t,
  223. can_tx_complete_msg);
  224. } __aligned(0x4) msg;
  225. } __packed __aligned(0x4);
  226. /* Macros to calculate message lengths */
  227. #define UCAN_OUT_HDR_SIZE offsetof(struct ucan_message_out, msg)
  228. #define UCAN_IN_HDR_SIZE offsetof(struct ucan_message_in, msg)
  229. #define UCAN_IN_LEN(member) (UCAN_OUT_HDR_SIZE + sizeof(member))
  230. struct ucan_priv;
  231. /* Context Information for transmission URBs */
  232. struct ucan_urb_context {
  233. struct ucan_priv *up;
  234. bool allocated;
  235. };
  236. /* Information reported by the USB device */
  237. struct ucan_device_info {
  238. struct can_bittiming_const bittiming_const;
  239. u8 tx_fifo;
  240. };
  241. /* Driver private data */
  242. struct ucan_priv {
  243. /* must be the first member */
  244. struct can_priv can;
  245. /* linux USB device structures */
  246. struct usb_device *udev;
  247. struct net_device *netdev;
  248. /* lock for can->echo_skb (used around
  249. * can_put/get/free_echo_skb
  250. */
  251. spinlock_t echo_skb_lock;
  252. /* usb device information */
  253. u8 intf_index;
  254. u8 in_ep_addr;
  255. u8 out_ep_addr;
  256. u16 in_ep_size;
  257. /* transmission and reception buffers */
  258. struct usb_anchor rx_urbs;
  259. struct usb_anchor tx_urbs;
  260. union ucan_ctl_payload *ctl_msg_buffer;
  261. struct ucan_device_info device_info;
  262. /* transmission control information and locks */
  263. spinlock_t context_lock;
  264. unsigned int available_tx_urbs;
  265. struct ucan_urb_context *context_array;
  266. };
  267. static u8 ucan_can_cc_dlc2len(struct ucan_can_msg *msg, u16 len)
  268. {
  269. if (le32_to_cpu(msg->id) & CAN_RTR_FLAG)
  270. return can_cc_dlc2len(msg->dlc);
  271. else
  272. return can_cc_dlc2len(len - (UCAN_IN_HDR_SIZE + sizeof(msg->id)));
  273. }
  274. static void ucan_release_context_array(struct ucan_priv *up)
  275. {
  276. if (!up->context_array)
  277. return;
  278. /* lock is not needed because, driver is currently opening or closing */
  279. up->available_tx_urbs = 0;
  280. kfree(up->context_array);
  281. up->context_array = NULL;
  282. }
  283. static int ucan_alloc_context_array(struct ucan_priv *up)
  284. {
  285. int i;
  286. /* release contexts if any */
  287. ucan_release_context_array(up);
  288. up->context_array = kcalloc(up->device_info.tx_fifo,
  289. sizeof(*up->context_array),
  290. GFP_KERNEL);
  291. if (!up->context_array) {
  292. netdev_err(up->netdev,
  293. "Not enough memory to allocate tx contexts\n");
  294. return -ENOMEM;
  295. }
  296. for (i = 0; i < up->device_info.tx_fifo; i++) {
  297. up->context_array[i].allocated = false;
  298. up->context_array[i].up = up;
  299. }
  300. /* lock is not needed because, driver is currently opening */
  301. up->available_tx_urbs = up->device_info.tx_fifo;
  302. return 0;
  303. }
  304. static struct ucan_urb_context *ucan_alloc_context(struct ucan_priv *up)
  305. {
  306. int i;
  307. unsigned long flags;
  308. struct ucan_urb_context *ret = NULL;
  309. if (WARN_ON_ONCE(!up->context_array))
  310. return NULL;
  311. /* execute context operation atomically */
  312. spin_lock_irqsave(&up->context_lock, flags);
  313. for (i = 0; i < up->device_info.tx_fifo; i++) {
  314. if (!up->context_array[i].allocated) {
  315. /* update context */
  316. ret = &up->context_array[i];
  317. up->context_array[i].allocated = true;
  318. /* stop queue if necessary */
  319. up->available_tx_urbs--;
  320. if (!up->available_tx_urbs)
  321. netif_stop_queue(up->netdev);
  322. break;
  323. }
  324. }
  325. spin_unlock_irqrestore(&up->context_lock, flags);
  326. return ret;
  327. }
  328. static bool ucan_release_context(struct ucan_priv *up,
  329. struct ucan_urb_context *ctx)
  330. {
  331. unsigned long flags;
  332. bool ret = false;
  333. if (WARN_ON_ONCE(!up->context_array))
  334. return false;
  335. /* execute context operation atomically */
  336. spin_lock_irqsave(&up->context_lock, flags);
  337. /* context was not allocated, maybe the device sent garbage */
  338. if (ctx->allocated) {
  339. ctx->allocated = false;
  340. /* check if the queue needs to be woken */
  341. if (!up->available_tx_urbs)
  342. netif_wake_queue(up->netdev);
  343. up->available_tx_urbs++;
  344. ret = true;
  345. }
  346. spin_unlock_irqrestore(&up->context_lock, flags);
  347. return ret;
  348. }
  349. static int ucan_ctrl_command_out(struct ucan_priv *up,
  350. u8 cmd, u16 subcmd, u16 datalen)
  351. {
  352. return usb_control_msg(up->udev,
  353. usb_sndctrlpipe(up->udev, 0),
  354. cmd,
  355. USB_DIR_OUT | USB_TYPE_VENDOR |
  356. USB_RECIP_INTERFACE,
  357. subcmd,
  358. up->intf_index,
  359. up->ctl_msg_buffer,
  360. datalen,
  361. UCAN_USB_CTL_PIPE_TIMEOUT);
  362. }
  363. static void ucan_get_fw_str(struct ucan_priv *up, char *fw_str, size_t size)
  364. {
  365. int ret;
  366. ret = usb_control_msg(up->udev, usb_rcvctrlpipe(up->udev, 0),
  367. UCAN_DEVICE_GET_FW_STRING,
  368. USB_DIR_IN | USB_TYPE_VENDOR |
  369. USB_RECIP_DEVICE,
  370. 0, 0, fw_str, size - 1,
  371. UCAN_USB_CTL_PIPE_TIMEOUT);
  372. if (ret > 0)
  373. fw_str[ret] = '\0';
  374. else
  375. strscpy(fw_str, "unknown", size);
  376. }
  377. /* Parse the device information structure reported by the device and
  378. * setup private variables accordingly
  379. */
  380. static void ucan_parse_device_info(struct ucan_priv *up,
  381. struct ucan_ctl_cmd_device_info *device_info)
  382. {
  383. struct can_bittiming_const *bittiming =
  384. &up->device_info.bittiming_const;
  385. u16 ctrlmodes;
  386. /* store the data */
  387. up->can.clock.freq = le32_to_cpu(device_info->freq);
  388. up->device_info.tx_fifo = device_info->tx_fifo;
  389. strcpy(bittiming->name, "ucan");
  390. bittiming->tseg1_min = device_info->tseg1_min;
  391. bittiming->tseg1_max = device_info->tseg1_max;
  392. bittiming->tseg2_min = device_info->tseg2_min;
  393. bittiming->tseg2_max = device_info->tseg2_max;
  394. bittiming->sjw_max = device_info->sjw_max;
  395. bittiming->brp_min = le32_to_cpu(device_info->brp_min);
  396. bittiming->brp_max = le32_to_cpu(device_info->brp_max);
  397. bittiming->brp_inc = le16_to_cpu(device_info->brp_inc);
  398. ctrlmodes = le16_to_cpu(device_info->ctrlmodes);
  399. up->can.ctrlmode_supported = 0;
  400. if (ctrlmodes & UCAN_MODE_LOOPBACK)
  401. up->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
  402. if (ctrlmodes & UCAN_MODE_SILENT)
  403. up->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
  404. if (ctrlmodes & UCAN_MODE_3_SAMPLES)
  405. up->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  406. if (ctrlmodes & UCAN_MODE_ONE_SHOT)
  407. up->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
  408. if (ctrlmodes & UCAN_MODE_BERR_REPORT)
  409. up->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
  410. }
  411. /* Handle a CAN error frame that we have received from the device.
  412. * Returns true if the can state has changed.
  413. */
  414. static bool ucan_handle_error_frame(struct ucan_priv *up,
  415. struct ucan_message_in *m,
  416. canid_t canid)
  417. {
  418. enum can_state new_state = up->can.state;
  419. struct net_device_stats *net_stats = &up->netdev->stats;
  420. struct can_device_stats *can_stats = &up->can.can_stats;
  421. if (canid & CAN_ERR_LOSTARB)
  422. can_stats->arbitration_lost++;
  423. if (canid & CAN_ERR_BUSERROR)
  424. can_stats->bus_error++;
  425. if (canid & CAN_ERR_ACK)
  426. net_stats->tx_errors++;
  427. if (canid & CAN_ERR_BUSOFF)
  428. new_state = CAN_STATE_BUS_OFF;
  429. /* controller problems, details in data[1] */
  430. if (canid & CAN_ERR_CRTL) {
  431. u8 d1 = m->msg.can_msg.data[1];
  432. if (d1 & CAN_ERR_CRTL_RX_OVERFLOW)
  433. net_stats->rx_over_errors++;
  434. /* controller state bits: if multiple are set the worst wins */
  435. if (d1 & CAN_ERR_CRTL_ACTIVE)
  436. new_state = CAN_STATE_ERROR_ACTIVE;
  437. if (d1 & (CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING))
  438. new_state = CAN_STATE_ERROR_WARNING;
  439. if (d1 & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE))
  440. new_state = CAN_STATE_ERROR_PASSIVE;
  441. }
  442. /* protocol error, details in data[2] */
  443. if (canid & CAN_ERR_PROT) {
  444. u8 d2 = m->msg.can_msg.data[2];
  445. if (d2 & CAN_ERR_PROT_TX)
  446. net_stats->tx_errors++;
  447. else
  448. net_stats->rx_errors++;
  449. }
  450. /* no state change - we are done */
  451. if (up->can.state == new_state)
  452. return false;
  453. /* we switched into a better state */
  454. if (up->can.state > new_state) {
  455. up->can.state = new_state;
  456. return true;
  457. }
  458. /* we switched into a worse state */
  459. up->can.state = new_state;
  460. switch (new_state) {
  461. case CAN_STATE_BUS_OFF:
  462. can_stats->bus_off++;
  463. can_bus_off(up->netdev);
  464. break;
  465. case CAN_STATE_ERROR_PASSIVE:
  466. can_stats->error_passive++;
  467. break;
  468. case CAN_STATE_ERROR_WARNING:
  469. can_stats->error_warning++;
  470. break;
  471. default:
  472. break;
  473. }
  474. return true;
  475. }
  476. /* Callback on reception of a can frame via the IN endpoint
  477. *
  478. * This function allocates an skb and transferres it to the Linux
  479. * network stack
  480. */
  481. static void ucan_rx_can_msg(struct ucan_priv *up, struct ucan_message_in *m)
  482. {
  483. int len;
  484. canid_t canid;
  485. struct can_frame *cf;
  486. struct sk_buff *skb;
  487. struct net_device_stats *stats = &up->netdev->stats;
  488. /* get the contents of the length field */
  489. len = le16_to_cpu(m->len);
  490. /* check sanity */
  491. if (len < UCAN_IN_HDR_SIZE + sizeof(m->msg.can_msg.id)) {
  492. netdev_warn(up->netdev, "invalid input message len: %d\n", len);
  493. return;
  494. }
  495. /* handle error frames */
  496. canid = le32_to_cpu(m->msg.can_msg.id);
  497. if (canid & CAN_ERR_FLAG) {
  498. bool busstate_changed = ucan_handle_error_frame(up, m, canid);
  499. /* if berr-reporting is off only state changes get through */
  500. if (!(up->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
  501. !busstate_changed)
  502. return;
  503. } else {
  504. canid_t canid_mask;
  505. /* compute the mask for canid */
  506. canid_mask = CAN_RTR_FLAG;
  507. if (canid & CAN_EFF_FLAG)
  508. canid_mask |= CAN_EFF_MASK | CAN_EFF_FLAG;
  509. else
  510. canid_mask |= CAN_SFF_MASK;
  511. if (canid & ~canid_mask)
  512. netdev_warn(up->netdev,
  513. "unexpected bits set (canid %x, mask %x)",
  514. canid, canid_mask);
  515. canid &= canid_mask;
  516. }
  517. /* allocate skb */
  518. skb = alloc_can_skb(up->netdev, &cf);
  519. if (!skb)
  520. return;
  521. /* fill the can frame */
  522. cf->can_id = canid;
  523. /* compute DLC taking RTR_FLAG into account */
  524. cf->len = ucan_can_cc_dlc2len(&m->msg.can_msg, len);
  525. /* copy the payload of non RTR frames */
  526. if (!(cf->can_id & CAN_RTR_FLAG) || (cf->can_id & CAN_ERR_FLAG))
  527. memcpy(cf->data, m->msg.can_msg.data, cf->len);
  528. /* don't count error frames as real packets */
  529. if (!(cf->can_id & CAN_ERR_FLAG)) {
  530. stats->rx_packets++;
  531. if (!(cf->can_id & CAN_RTR_FLAG))
  532. stats->rx_bytes += cf->len;
  533. }
  534. /* pass it to Linux */
  535. netif_rx(skb);
  536. }
  537. /* callback indicating completed transmission */
  538. static void ucan_tx_complete_msg(struct ucan_priv *up,
  539. struct ucan_message_in *m)
  540. {
  541. unsigned long flags;
  542. u16 count, i;
  543. u8 echo_index;
  544. u16 len = le16_to_cpu(m->len);
  545. struct ucan_urb_context *context;
  546. if (len < UCAN_IN_HDR_SIZE || (len % 2 != 0)) {
  547. netdev_err(up->netdev, "invalid tx complete length\n");
  548. return;
  549. }
  550. count = (len - UCAN_IN_HDR_SIZE) / 2;
  551. for (i = 0; i < count; i++) {
  552. /* we did not submit such echo ids */
  553. echo_index = m->msg.can_tx_complete_msg[i].echo_index;
  554. if (echo_index >= up->device_info.tx_fifo) {
  555. up->netdev->stats.tx_errors++;
  556. netdev_err(up->netdev,
  557. "invalid echo_index %d received\n",
  558. echo_index);
  559. continue;
  560. }
  561. /* gather information from the context */
  562. context = &up->context_array[echo_index];
  563. /* Release context and restart queue if necessary.
  564. * Also check if the context was allocated
  565. */
  566. if (!ucan_release_context(up, context))
  567. continue;
  568. spin_lock_irqsave(&up->echo_skb_lock, flags);
  569. if (m->msg.can_tx_complete_msg[i].flags &
  570. UCAN_TX_COMPLETE_SUCCESS) {
  571. /* update statistics */
  572. up->netdev->stats.tx_packets++;
  573. up->netdev->stats.tx_bytes +=
  574. can_get_echo_skb(up->netdev, echo_index, NULL);
  575. } else {
  576. up->netdev->stats.tx_dropped++;
  577. can_free_echo_skb(up->netdev, echo_index, NULL);
  578. }
  579. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  580. }
  581. }
  582. /* callback on reception of a USB message */
  583. static void ucan_read_bulk_callback(struct urb *urb)
  584. {
  585. int ret;
  586. int pos;
  587. struct ucan_priv *up = urb->context;
  588. struct net_device *netdev = up->netdev;
  589. struct ucan_message_in *m;
  590. /* the device is not up and the driver should not receive any
  591. * data on the bulk in pipe
  592. */
  593. if (WARN_ON(!up->context_array)) {
  594. usb_free_coherent(up->udev,
  595. up->in_ep_size,
  596. urb->transfer_buffer,
  597. urb->transfer_dma);
  598. return;
  599. }
  600. /* check URB status */
  601. switch (urb->status) {
  602. case 0:
  603. break;
  604. case -ENOENT:
  605. case -EPIPE:
  606. case -EPROTO:
  607. case -ESHUTDOWN:
  608. case -ETIME:
  609. /* urb is not resubmitted -> free dma data */
  610. usb_free_coherent(up->udev,
  611. up->in_ep_size,
  612. urb->transfer_buffer,
  613. urb->transfer_dma);
  614. netdev_dbg(up->netdev, "not resubmitting urb; status: %d\n",
  615. urb->status);
  616. return;
  617. default:
  618. goto resubmit;
  619. }
  620. /* sanity check */
  621. if (!netif_device_present(netdev))
  622. return;
  623. /* iterate over input */
  624. pos = 0;
  625. while (pos < urb->actual_length) {
  626. int len;
  627. /* check sanity (length of header) */
  628. if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) {
  629. netdev_warn(up->netdev,
  630. "invalid message (short; no hdr; l:%d)\n",
  631. urb->actual_length);
  632. goto resubmit;
  633. }
  634. /* setup the message address */
  635. m = (struct ucan_message_in *)
  636. ((u8 *)urb->transfer_buffer + pos);
  637. len = le16_to_cpu(m->len);
  638. /* check sanity (length of content) */
  639. if (urb->actual_length - pos < len) {
  640. netdev_warn(up->netdev,
  641. "invalid message (short; no data; l:%d)\n",
  642. urb->actual_length);
  643. print_hex_dump(KERN_WARNING,
  644. "raw data: ",
  645. DUMP_PREFIX_ADDRESS,
  646. 16,
  647. 1,
  648. urb->transfer_buffer,
  649. urb->actual_length,
  650. true);
  651. goto resubmit;
  652. }
  653. switch (m->type) {
  654. case UCAN_IN_RX:
  655. ucan_rx_can_msg(up, m);
  656. break;
  657. case UCAN_IN_TX_COMPLETE:
  658. ucan_tx_complete_msg(up, m);
  659. break;
  660. default:
  661. netdev_warn(up->netdev,
  662. "invalid message (type; t:%d)\n",
  663. m->type);
  664. break;
  665. }
  666. /* proceed to next message */
  667. pos += len;
  668. /* align to 4 byte boundary */
  669. pos = round_up(pos, 4);
  670. }
  671. resubmit:
  672. /* resubmit urb when done */
  673. usb_fill_bulk_urb(urb, up->udev,
  674. usb_rcvbulkpipe(up->udev,
  675. up->in_ep_addr),
  676. urb->transfer_buffer,
  677. up->in_ep_size,
  678. ucan_read_bulk_callback,
  679. up);
  680. usb_anchor_urb(urb, &up->rx_urbs);
  681. ret = usb_submit_urb(urb, GFP_ATOMIC);
  682. if (ret < 0) {
  683. netdev_err(up->netdev,
  684. "failed resubmitting read bulk urb: %d\n",
  685. ret);
  686. usb_unanchor_urb(urb);
  687. usb_free_coherent(up->udev,
  688. up->in_ep_size,
  689. urb->transfer_buffer,
  690. urb->transfer_dma);
  691. if (ret == -ENODEV)
  692. netif_device_detach(netdev);
  693. }
  694. }
  695. /* callback after transmission of a USB message */
  696. static void ucan_write_bulk_callback(struct urb *urb)
  697. {
  698. unsigned long flags;
  699. struct ucan_priv *up;
  700. struct ucan_urb_context *context = urb->context;
  701. /* get the urb context */
  702. if (WARN_ON_ONCE(!context))
  703. return;
  704. /* free up our allocated buffer */
  705. usb_free_coherent(urb->dev,
  706. sizeof(struct ucan_message_out),
  707. urb->transfer_buffer,
  708. urb->transfer_dma);
  709. up = context->up;
  710. if (WARN_ON_ONCE(!up))
  711. return;
  712. /* sanity check */
  713. if (!netif_device_present(up->netdev))
  714. return;
  715. /* transmission failed (USB - the device will not send a TX complete) */
  716. if (urb->status) {
  717. netdev_warn(up->netdev,
  718. "failed to transmit USB message to device: %d\n",
  719. urb->status);
  720. /* update counters an cleanup */
  721. spin_lock_irqsave(&up->echo_skb_lock, flags);
  722. can_free_echo_skb(up->netdev, context - up->context_array, NULL);
  723. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  724. up->netdev->stats.tx_dropped++;
  725. /* release context and restart the queue if necessary */
  726. if (!ucan_release_context(up, context))
  727. netdev_err(up->netdev,
  728. "urb failed, failed to release context\n");
  729. }
  730. }
  731. static void ucan_cleanup_rx_urbs(struct ucan_priv *up, struct urb **urbs)
  732. {
  733. int i;
  734. for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
  735. if (urbs[i]) {
  736. usb_unanchor_urb(urbs[i]);
  737. usb_free_coherent(up->udev,
  738. up->in_ep_size,
  739. urbs[i]->transfer_buffer,
  740. urbs[i]->transfer_dma);
  741. usb_free_urb(urbs[i]);
  742. }
  743. }
  744. memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
  745. }
  746. static int ucan_prepare_and_anchor_rx_urbs(struct ucan_priv *up,
  747. struct urb **urbs)
  748. {
  749. int i;
  750. memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
  751. for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
  752. void *buf;
  753. urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
  754. if (!urbs[i])
  755. goto err;
  756. buf = usb_alloc_coherent(up->udev,
  757. up->in_ep_size,
  758. GFP_KERNEL, &urbs[i]->transfer_dma);
  759. if (!buf) {
  760. /* cleanup this urb */
  761. usb_free_urb(urbs[i]);
  762. urbs[i] = NULL;
  763. goto err;
  764. }
  765. usb_fill_bulk_urb(urbs[i], up->udev,
  766. usb_rcvbulkpipe(up->udev,
  767. up->in_ep_addr),
  768. buf,
  769. up->in_ep_size,
  770. ucan_read_bulk_callback,
  771. up);
  772. urbs[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  773. usb_anchor_urb(urbs[i], &up->rx_urbs);
  774. }
  775. return 0;
  776. err:
  777. /* cleanup other unsubmitted urbs */
  778. ucan_cleanup_rx_urbs(up, urbs);
  779. return -ENOMEM;
  780. }
  781. /* Submits rx urbs with the semantic: Either submit all, or cleanup
  782. * everything. I case of errors submitted urbs are killed and all urbs in
  783. * the array are freed. I case of no errors every entry in the urb
  784. * array is set to NULL.
  785. */
  786. static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs)
  787. {
  788. int i, ret;
  789. /* Iterate over all urbs to submit. On success remove the urb
  790. * from the list.
  791. */
  792. for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
  793. ret = usb_submit_urb(urbs[i], GFP_KERNEL);
  794. if (ret) {
  795. netdev_err(up->netdev,
  796. "could not submit urb; code: %d\n",
  797. ret);
  798. goto err;
  799. }
  800. /* Anchor URB and drop reference, USB core will take
  801. * care of freeing it
  802. */
  803. usb_free_urb(urbs[i]);
  804. urbs[i] = NULL;
  805. }
  806. return 0;
  807. err:
  808. /* Cleanup unsubmitted urbs */
  809. ucan_cleanup_rx_urbs(up, urbs);
  810. /* Kill urbs that are already submitted */
  811. usb_kill_anchored_urbs(&up->rx_urbs);
  812. return ret;
  813. }
  814. /* Open the network device */
  815. static int ucan_open(struct net_device *netdev)
  816. {
  817. int ret, ret_cleanup;
  818. u16 ctrlmode;
  819. struct urb *urbs[UCAN_MAX_RX_URBS];
  820. struct ucan_priv *up = netdev_priv(netdev);
  821. ret = ucan_alloc_context_array(up);
  822. if (ret)
  823. return ret;
  824. /* Allocate and prepare IN URBS - allocated and anchored
  825. * urbs are stored in urbs[] for clean
  826. */
  827. ret = ucan_prepare_and_anchor_rx_urbs(up, urbs);
  828. if (ret)
  829. goto err_contexts;
  830. /* Check the control mode */
  831. ctrlmode = 0;
  832. if (up->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  833. ctrlmode |= UCAN_MODE_LOOPBACK;
  834. if (up->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  835. ctrlmode |= UCAN_MODE_SILENT;
  836. if (up->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  837. ctrlmode |= UCAN_MODE_3_SAMPLES;
  838. if (up->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  839. ctrlmode |= UCAN_MODE_ONE_SHOT;
  840. /* Enable this in any case - filtering is down within the
  841. * receive path
  842. */
  843. ctrlmode |= UCAN_MODE_BERR_REPORT;
  844. up->ctl_msg_buffer->cmd_start.mode = cpu_to_le16(ctrlmode);
  845. /* Driver is ready to receive data - start the USB device */
  846. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2);
  847. if (ret < 0) {
  848. netdev_err(up->netdev,
  849. "could not start device, code: %d\n",
  850. ret);
  851. goto err_reset;
  852. }
  853. /* Call CAN layer open */
  854. ret = open_candev(netdev);
  855. if (ret)
  856. goto err_stop;
  857. /* Driver is ready to receive data. Submit RX URBS */
  858. ret = ucan_submit_rx_urbs(up, urbs);
  859. if (ret)
  860. goto err_stop;
  861. up->can.state = CAN_STATE_ERROR_ACTIVE;
  862. /* Start the network queue */
  863. netif_start_queue(netdev);
  864. return 0;
  865. err_stop:
  866. /* The device have started already stop it */
  867. ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
  868. if (ret_cleanup < 0)
  869. netdev_err(up->netdev,
  870. "could not stop device, code: %d\n",
  871. ret_cleanup);
  872. err_reset:
  873. /* The device might have received data, reset it for
  874. * consistent state
  875. */
  876. ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  877. if (ret_cleanup < 0)
  878. netdev_err(up->netdev,
  879. "could not reset device, code: %d\n",
  880. ret_cleanup);
  881. /* clean up unsubmitted urbs */
  882. ucan_cleanup_rx_urbs(up, urbs);
  883. err_contexts:
  884. ucan_release_context_array(up);
  885. return ret;
  886. }
  887. static struct urb *ucan_prepare_tx_urb(struct ucan_priv *up,
  888. struct ucan_urb_context *context,
  889. struct can_frame *cf,
  890. u8 echo_index)
  891. {
  892. int mlen;
  893. struct urb *urb;
  894. struct ucan_message_out *m;
  895. /* create a URB, and a buffer for it, and copy the data to the URB */
  896. urb = usb_alloc_urb(0, GFP_ATOMIC);
  897. if (!urb) {
  898. netdev_err(up->netdev, "no memory left for URBs\n");
  899. return NULL;
  900. }
  901. m = usb_alloc_coherent(up->udev,
  902. sizeof(struct ucan_message_out),
  903. GFP_ATOMIC,
  904. &urb->transfer_dma);
  905. if (!m) {
  906. netdev_err(up->netdev, "no memory left for USB buffer\n");
  907. usb_free_urb(urb);
  908. return NULL;
  909. }
  910. /* build the USB message */
  911. m->type = UCAN_OUT_TX;
  912. m->msg.can_msg.id = cpu_to_le32(cf->can_id);
  913. if (cf->can_id & CAN_RTR_FLAG) {
  914. mlen = UCAN_OUT_HDR_SIZE +
  915. offsetof(struct ucan_can_msg, dlc) +
  916. sizeof(m->msg.can_msg.dlc);
  917. m->msg.can_msg.dlc = cf->len;
  918. } else {
  919. mlen = UCAN_OUT_HDR_SIZE +
  920. sizeof(m->msg.can_msg.id) + cf->len;
  921. memcpy(m->msg.can_msg.data, cf->data, cf->len);
  922. }
  923. m->len = cpu_to_le16(mlen);
  924. m->subtype = echo_index;
  925. /* build the urb */
  926. usb_fill_bulk_urb(urb, up->udev,
  927. usb_sndbulkpipe(up->udev,
  928. up->out_ep_addr),
  929. m, mlen, ucan_write_bulk_callback, context);
  930. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  931. return urb;
  932. }
  933. static void ucan_clean_up_tx_urb(struct ucan_priv *up, struct urb *urb)
  934. {
  935. usb_free_coherent(up->udev, sizeof(struct ucan_message_out),
  936. urb->transfer_buffer, urb->transfer_dma);
  937. usb_free_urb(urb);
  938. }
  939. /* callback when Linux needs to send a can frame */
  940. static netdev_tx_t ucan_start_xmit(struct sk_buff *skb,
  941. struct net_device *netdev)
  942. {
  943. unsigned long flags;
  944. int ret;
  945. u8 echo_index;
  946. struct urb *urb;
  947. struct ucan_urb_context *context;
  948. struct ucan_priv *up = netdev_priv(netdev);
  949. struct can_frame *cf = (struct can_frame *)skb->data;
  950. /* check skb */
  951. if (can_dev_dropped_skb(netdev, skb))
  952. return NETDEV_TX_OK;
  953. /* allocate a context and slow down tx path, if fifo state is low */
  954. context = ucan_alloc_context(up);
  955. echo_index = context - up->context_array;
  956. if (WARN_ON_ONCE(!context))
  957. return NETDEV_TX_BUSY;
  958. /* prepare urb for transmission */
  959. urb = ucan_prepare_tx_urb(up, context, cf, echo_index);
  960. if (!urb)
  961. goto drop;
  962. /* put the skb on can loopback stack */
  963. spin_lock_irqsave(&up->echo_skb_lock, flags);
  964. can_put_echo_skb(skb, up->netdev, echo_index, 0);
  965. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  966. /* transmit it */
  967. usb_anchor_urb(urb, &up->tx_urbs);
  968. ret = usb_submit_urb(urb, GFP_ATOMIC);
  969. /* cleanup urb */
  970. if (ret) {
  971. /* on error, clean up */
  972. usb_unanchor_urb(urb);
  973. ucan_clean_up_tx_urb(up, urb);
  974. if (!ucan_release_context(up, context))
  975. netdev_err(up->netdev,
  976. "xmit err: failed to release context\n");
  977. /* remove the skb from the echo stack - this also
  978. * frees the skb
  979. */
  980. spin_lock_irqsave(&up->echo_skb_lock, flags);
  981. can_free_echo_skb(up->netdev, echo_index, NULL);
  982. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  983. if (ret == -ENODEV) {
  984. netif_device_detach(up->netdev);
  985. } else {
  986. netdev_warn(up->netdev,
  987. "xmit err: failed to submit urb %d\n",
  988. ret);
  989. up->netdev->stats.tx_dropped++;
  990. }
  991. return NETDEV_TX_OK;
  992. }
  993. netif_trans_update(netdev);
  994. /* release ref, as we do not need the urb anymore */
  995. usb_free_urb(urb);
  996. return NETDEV_TX_OK;
  997. drop:
  998. if (!ucan_release_context(up, context))
  999. netdev_err(up->netdev,
  1000. "xmit drop: failed to release context\n");
  1001. dev_kfree_skb(skb);
  1002. up->netdev->stats.tx_dropped++;
  1003. return NETDEV_TX_OK;
  1004. }
  1005. /* Device goes down
  1006. *
  1007. * Clean up used resources
  1008. */
  1009. static int ucan_close(struct net_device *netdev)
  1010. {
  1011. int ret;
  1012. struct ucan_priv *up = netdev_priv(netdev);
  1013. up->can.state = CAN_STATE_STOPPED;
  1014. /* stop sending data */
  1015. usb_kill_anchored_urbs(&up->tx_urbs);
  1016. /* stop receiving data */
  1017. usb_kill_anchored_urbs(&up->rx_urbs);
  1018. /* stop and reset can device */
  1019. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
  1020. if (ret < 0)
  1021. netdev_err(up->netdev,
  1022. "could not stop device, code: %d\n",
  1023. ret);
  1024. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  1025. if (ret < 0)
  1026. netdev_err(up->netdev,
  1027. "could not reset device, code: %d\n",
  1028. ret);
  1029. netif_stop_queue(netdev);
  1030. ucan_release_context_array(up);
  1031. close_candev(up->netdev);
  1032. return 0;
  1033. }
  1034. /* CAN driver callbacks */
  1035. static const struct net_device_ops ucan_netdev_ops = {
  1036. .ndo_open = ucan_open,
  1037. .ndo_stop = ucan_close,
  1038. .ndo_start_xmit = ucan_start_xmit,
  1039. .ndo_change_mtu = can_change_mtu,
  1040. };
  1041. static const struct ethtool_ops ucan_ethtool_ops = {
  1042. .get_ts_info = ethtool_op_get_ts_info,
  1043. };
  1044. /* Request to set bittiming
  1045. *
  1046. * This function generates an USB set bittiming message and transmits
  1047. * it to the device
  1048. */
  1049. static int ucan_set_bittiming(struct net_device *netdev)
  1050. {
  1051. int ret;
  1052. struct ucan_priv *up = netdev_priv(netdev);
  1053. struct ucan_ctl_cmd_set_bittiming *cmd_set_bittiming;
  1054. cmd_set_bittiming = &up->ctl_msg_buffer->cmd_set_bittiming;
  1055. cmd_set_bittiming->tq = cpu_to_le32(up->can.bittiming.tq);
  1056. cmd_set_bittiming->brp = cpu_to_le16(up->can.bittiming.brp);
  1057. cmd_set_bittiming->sample_point =
  1058. cpu_to_le16(up->can.bittiming.sample_point);
  1059. cmd_set_bittiming->prop_seg = up->can.bittiming.prop_seg;
  1060. cmd_set_bittiming->phase_seg1 = up->can.bittiming.phase_seg1;
  1061. cmd_set_bittiming->phase_seg2 = up->can.bittiming.phase_seg2;
  1062. cmd_set_bittiming->sjw = up->can.bittiming.sjw;
  1063. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0,
  1064. sizeof(*cmd_set_bittiming));
  1065. return (ret < 0) ? ret : 0;
  1066. }
  1067. /* Restart the device to get it out of BUS-OFF state.
  1068. * Called when the user runs "ip link set can1 type can restart".
  1069. */
  1070. static int ucan_set_mode(struct net_device *netdev, enum can_mode mode)
  1071. {
  1072. int ret;
  1073. unsigned long flags;
  1074. struct ucan_priv *up = netdev_priv(netdev);
  1075. switch (mode) {
  1076. case CAN_MODE_START:
  1077. netdev_dbg(up->netdev, "restarting device\n");
  1078. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0);
  1079. up->can.state = CAN_STATE_ERROR_ACTIVE;
  1080. /* check if queue can be restarted,
  1081. * up->available_tx_urbs must be protected by the
  1082. * lock
  1083. */
  1084. spin_lock_irqsave(&up->context_lock, flags);
  1085. if (up->available_tx_urbs > 0)
  1086. netif_wake_queue(up->netdev);
  1087. spin_unlock_irqrestore(&up->context_lock, flags);
  1088. return ret;
  1089. default:
  1090. return -EOPNOTSUPP;
  1091. }
  1092. }
  1093. /* Probe the device, reset it and gather general device information */
  1094. static int ucan_probe(struct usb_interface *intf,
  1095. const struct usb_device_id *id)
  1096. {
  1097. int ret;
  1098. int i;
  1099. u32 protocol_version;
  1100. struct usb_device *udev;
  1101. struct net_device *netdev;
  1102. struct usb_host_interface *iface_desc;
  1103. struct ucan_priv *up;
  1104. struct usb_endpoint_descriptor *ep;
  1105. u16 in_ep_size;
  1106. u16 out_ep_size;
  1107. u8 in_ep_addr;
  1108. u8 out_ep_addr;
  1109. union ucan_ctl_payload *ctl_msg_buffer;
  1110. udev = interface_to_usbdev(intf);
  1111. /* Stage 1 - Interface Parsing
  1112. * ---------------------------
  1113. *
  1114. * Identifie the device USB interface descriptor and its
  1115. * endpoints. Probing is aborted on errors.
  1116. */
  1117. /* check if the interface is sane */
  1118. iface_desc = intf->cur_altsetting;
  1119. if (!iface_desc)
  1120. return -ENODEV;
  1121. dev_info(&udev->dev,
  1122. "%s: probing device on interface #%d\n",
  1123. UCAN_DRIVER_NAME,
  1124. iface_desc->desc.bInterfaceNumber);
  1125. /* interface sanity check */
  1126. if (iface_desc->desc.bNumEndpoints != 2) {
  1127. dev_err(&udev->dev,
  1128. "%s: invalid EP count (%d)",
  1129. UCAN_DRIVER_NAME, iface_desc->desc.bNumEndpoints);
  1130. goto err_firmware_needs_update;
  1131. }
  1132. /* check interface endpoints */
  1133. in_ep_addr = 0;
  1134. out_ep_addr = 0;
  1135. in_ep_size = 0;
  1136. out_ep_size = 0;
  1137. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  1138. ep = &iface_desc->endpoint[i].desc;
  1139. if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != 0) &&
  1140. ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  1141. USB_ENDPOINT_XFER_BULK)) {
  1142. /* In Endpoint */
  1143. in_ep_addr = ep->bEndpointAddress;
  1144. in_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
  1145. in_ep_size = le16_to_cpu(ep->wMaxPacketSize);
  1146. } else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
  1147. 0) &&
  1148. ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  1149. USB_ENDPOINT_XFER_BULK)) {
  1150. /* Out Endpoint */
  1151. out_ep_addr = ep->bEndpointAddress;
  1152. out_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
  1153. out_ep_size = le16_to_cpu(ep->wMaxPacketSize);
  1154. }
  1155. }
  1156. /* check if interface is sane */
  1157. if (!in_ep_addr || !out_ep_addr) {
  1158. dev_err(&udev->dev, "%s: invalid endpoint configuration\n",
  1159. UCAN_DRIVER_NAME);
  1160. goto err_firmware_needs_update;
  1161. }
  1162. if (in_ep_size < sizeof(struct ucan_message_in)) {
  1163. dev_err(&udev->dev, "%s: invalid in_ep MaxPacketSize\n",
  1164. UCAN_DRIVER_NAME);
  1165. goto err_firmware_needs_update;
  1166. }
  1167. if (out_ep_size < sizeof(struct ucan_message_out)) {
  1168. dev_err(&udev->dev, "%s: invalid out_ep MaxPacketSize\n",
  1169. UCAN_DRIVER_NAME);
  1170. goto err_firmware_needs_update;
  1171. }
  1172. /* Stage 2 - Device Identification
  1173. * -------------------------------
  1174. *
  1175. * The device interface seems to be a ucan device. Do further
  1176. * compatibility checks. On error probing is aborted, on
  1177. * success this stage leaves the ctl_msg_buffer with the
  1178. * reported contents of a GET_INFO command (supported
  1179. * bittimings, tx_fifo depth). This information is used in
  1180. * Stage 3 for the final driver initialisation.
  1181. */
  1182. /* Prepare Memory for control transfers */
  1183. ctl_msg_buffer = devm_kzalloc(&udev->dev,
  1184. sizeof(union ucan_ctl_payload),
  1185. GFP_KERNEL);
  1186. if (!ctl_msg_buffer) {
  1187. dev_err(&udev->dev,
  1188. "%s: failed to allocate control pipe memory\n",
  1189. UCAN_DRIVER_NAME);
  1190. return -ENOMEM;
  1191. }
  1192. /* get protocol version
  1193. *
  1194. * note: ucan_ctrl_command_* wrappers cannot be used yet
  1195. * because `up` is initialised in Stage 3
  1196. */
  1197. ret = usb_control_msg(udev,
  1198. usb_rcvctrlpipe(udev, 0),
  1199. UCAN_COMMAND_GET,
  1200. USB_DIR_IN | USB_TYPE_VENDOR |
  1201. USB_RECIP_INTERFACE,
  1202. UCAN_COMMAND_GET_PROTOCOL_VERSION,
  1203. iface_desc->desc.bInterfaceNumber,
  1204. ctl_msg_buffer,
  1205. sizeof(union ucan_ctl_payload),
  1206. UCAN_USB_CTL_PIPE_TIMEOUT);
  1207. /* older firmware version do not support this command - those
  1208. * are not supported by this drive
  1209. */
  1210. if (ret != 4) {
  1211. dev_err(&udev->dev,
  1212. "%s: could not read protocol version, ret=%d\n",
  1213. UCAN_DRIVER_NAME, ret);
  1214. if (ret >= 0)
  1215. ret = -EINVAL;
  1216. goto err_firmware_needs_update;
  1217. }
  1218. /* this driver currently supports protocol version 3 only */
  1219. protocol_version =
  1220. le32_to_cpu(ctl_msg_buffer->cmd_get_protocol_version.version);
  1221. if (protocol_version < UCAN_PROTOCOL_VERSION_MIN ||
  1222. protocol_version > UCAN_PROTOCOL_VERSION_MAX) {
  1223. dev_err(&udev->dev,
  1224. "%s: device protocol version %d is not supported\n",
  1225. UCAN_DRIVER_NAME, protocol_version);
  1226. goto err_firmware_needs_update;
  1227. }
  1228. /* request the device information and store it in ctl_msg_buffer
  1229. *
  1230. * note: ucan_ctrl_command_* wrappers cannot be used yet
  1231. * because `up` is initialised in Stage 3
  1232. */
  1233. ret = usb_control_msg(udev,
  1234. usb_rcvctrlpipe(udev, 0),
  1235. UCAN_COMMAND_GET,
  1236. USB_DIR_IN | USB_TYPE_VENDOR |
  1237. USB_RECIP_INTERFACE,
  1238. UCAN_COMMAND_GET_INFO,
  1239. iface_desc->desc.bInterfaceNumber,
  1240. ctl_msg_buffer,
  1241. sizeof(ctl_msg_buffer->cmd_get_device_info),
  1242. UCAN_USB_CTL_PIPE_TIMEOUT);
  1243. if (ret < 0) {
  1244. dev_err(&udev->dev, "%s: failed to retrieve device info\n",
  1245. UCAN_DRIVER_NAME);
  1246. goto err_firmware_needs_update;
  1247. }
  1248. if (ret < sizeof(ctl_msg_buffer->cmd_get_device_info)) {
  1249. dev_err(&udev->dev, "%s: device reported invalid device info\n",
  1250. UCAN_DRIVER_NAME);
  1251. goto err_firmware_needs_update;
  1252. }
  1253. if (ctl_msg_buffer->cmd_get_device_info.tx_fifo == 0) {
  1254. dev_err(&udev->dev,
  1255. "%s: device reported invalid tx-fifo size\n",
  1256. UCAN_DRIVER_NAME);
  1257. goto err_firmware_needs_update;
  1258. }
  1259. /* Stage 3 - Driver Initialisation
  1260. * -------------------------------
  1261. *
  1262. * Register device to Linux, prepare private structures and
  1263. * reset the device.
  1264. */
  1265. /* allocate driver resources */
  1266. netdev = alloc_candev(sizeof(struct ucan_priv),
  1267. ctl_msg_buffer->cmd_get_device_info.tx_fifo);
  1268. if (!netdev) {
  1269. dev_err(&udev->dev,
  1270. "%s: cannot allocate candev\n", UCAN_DRIVER_NAME);
  1271. return -ENOMEM;
  1272. }
  1273. up = netdev_priv(netdev);
  1274. /* initialize data */
  1275. up->udev = udev;
  1276. up->netdev = netdev;
  1277. up->intf_index = iface_desc->desc.bInterfaceNumber;
  1278. up->in_ep_addr = in_ep_addr;
  1279. up->out_ep_addr = out_ep_addr;
  1280. up->in_ep_size = in_ep_size;
  1281. up->ctl_msg_buffer = ctl_msg_buffer;
  1282. up->context_array = NULL;
  1283. up->available_tx_urbs = 0;
  1284. up->can.state = CAN_STATE_STOPPED;
  1285. up->can.bittiming_const = &up->device_info.bittiming_const;
  1286. up->can.do_set_bittiming = ucan_set_bittiming;
  1287. up->can.do_set_mode = &ucan_set_mode;
  1288. spin_lock_init(&up->context_lock);
  1289. spin_lock_init(&up->echo_skb_lock);
  1290. netdev->netdev_ops = &ucan_netdev_ops;
  1291. netdev->ethtool_ops = &ucan_ethtool_ops;
  1292. usb_set_intfdata(intf, up);
  1293. SET_NETDEV_DEV(netdev, &intf->dev);
  1294. /* parse device information
  1295. * the data retrieved in Stage 2 is still available in
  1296. * up->ctl_msg_buffer
  1297. */
  1298. ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info);
  1299. /* device is compatible, reset it */
  1300. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  1301. if (ret < 0)
  1302. goto err_free_candev;
  1303. init_usb_anchor(&up->rx_urbs);
  1304. init_usb_anchor(&up->tx_urbs);
  1305. up->can.state = CAN_STATE_STOPPED;
  1306. /* register the device */
  1307. ret = register_candev(netdev);
  1308. if (ret)
  1309. goto err_free_candev;
  1310. /* initialisation complete, log device info */
  1311. netdev_info(up->netdev, "registered device\n");
  1312. ucan_get_fw_str(up, up->ctl_msg_buffer->fw_str,
  1313. sizeof(up->ctl_msg_buffer->fw_str));
  1314. netdev_info(up->netdev, "firmware string: %s\n",
  1315. up->ctl_msg_buffer->fw_str);
  1316. /* success */
  1317. return 0;
  1318. err_free_candev:
  1319. free_candev(netdev);
  1320. return ret;
  1321. err_firmware_needs_update:
  1322. dev_err(&udev->dev,
  1323. "%s: probe failed; try to update the device firmware\n",
  1324. UCAN_DRIVER_NAME);
  1325. return -ENODEV;
  1326. }
  1327. /* disconnect the device */
  1328. static void ucan_disconnect(struct usb_interface *intf)
  1329. {
  1330. struct ucan_priv *up = usb_get_intfdata(intf);
  1331. usb_set_intfdata(intf, NULL);
  1332. if (up) {
  1333. unregister_candev(up->netdev);
  1334. free_candev(up->netdev);
  1335. }
  1336. }
  1337. static struct usb_device_id ucan_table[] = {
  1338. /* Mule (soldered onto compute modules) */
  1339. {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425a, 0)},
  1340. /* Seal (standalone USB stick) */
  1341. {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425b, 0)},
  1342. {} /* Terminating entry */
  1343. };
  1344. MODULE_DEVICE_TABLE(usb, ucan_table);
  1345. /* driver callbacks */
  1346. static struct usb_driver ucan_driver = {
  1347. .name = UCAN_DRIVER_NAME,
  1348. .probe = ucan_probe,
  1349. .disconnect = ucan_disconnect,
  1350. .id_table = ucan_table,
  1351. };
  1352. module_usb_driver(ucan_driver);
  1353. MODULE_LICENSE("GPL v2");
  1354. MODULE_AUTHOR("Martin Elshuber <martin.elshuber@theobroma-systems.com>");
  1355. MODULE_AUTHOR("Jakob Unterwurzacher <jakob.unterwurzacher@theobroma-systems.com>");
  1356. MODULE_DESCRIPTION("Driver for Theobroma Systems UCAN devices");