ucan.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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 raw[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. struct ucan_tx_complete_entry_t can_tx_complete_msg[0];
  223. } __aligned(0x4) msg;
  224. } __packed __aligned(0x4);
  225. /* Macros to calculate message lengths */
  226. #define UCAN_OUT_HDR_SIZE offsetof(struct ucan_message_out, msg)
  227. #define UCAN_IN_HDR_SIZE offsetof(struct ucan_message_in, msg)
  228. #define UCAN_IN_LEN(member) (UCAN_OUT_HDR_SIZE + sizeof(member))
  229. struct ucan_priv;
  230. /* Context Information for transmission URBs */
  231. struct ucan_urb_context {
  232. struct ucan_priv *up;
  233. bool allocated;
  234. };
  235. /* Information reported by the USB device */
  236. struct ucan_device_info {
  237. struct can_bittiming_const bittiming_const;
  238. u8 tx_fifo;
  239. };
  240. /* Driver private data */
  241. struct ucan_priv {
  242. /* must be the first member */
  243. struct can_priv can;
  244. /* linux USB device structures */
  245. struct usb_device *udev;
  246. struct usb_interface *intf;
  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 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 int ucan_device_request_in(struct ucan_priv *up,
  364. u8 cmd, u16 subcmd, u16 datalen)
  365. {
  366. return usb_control_msg(up->udev,
  367. usb_rcvctrlpipe(up->udev, 0),
  368. cmd,
  369. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  370. subcmd,
  371. 0,
  372. up->ctl_msg_buffer,
  373. datalen,
  374. UCAN_USB_CTL_PIPE_TIMEOUT);
  375. }
  376. /* Parse the device information structure reported by the device and
  377. * setup private variables accordingly
  378. */
  379. static void ucan_parse_device_info(struct ucan_priv *up,
  380. struct ucan_ctl_cmd_device_info *device_info)
  381. {
  382. struct can_bittiming_const *bittiming =
  383. &up->device_info.bittiming_const;
  384. u16 ctrlmodes;
  385. /* store the data */
  386. up->can.clock.freq = le32_to_cpu(device_info->freq);
  387. up->device_info.tx_fifo = device_info->tx_fifo;
  388. strcpy(bittiming->name, "ucan");
  389. bittiming->tseg1_min = device_info->tseg1_min;
  390. bittiming->tseg1_max = device_info->tseg1_max;
  391. bittiming->tseg2_min = device_info->tseg2_min;
  392. bittiming->tseg2_max = device_info->tseg2_max;
  393. bittiming->sjw_max = device_info->sjw_max;
  394. bittiming->brp_min = le32_to_cpu(device_info->brp_min);
  395. bittiming->brp_max = le32_to_cpu(device_info->brp_max);
  396. bittiming->brp_inc = le16_to_cpu(device_info->brp_inc);
  397. ctrlmodes = le16_to_cpu(device_info->ctrlmodes);
  398. up->can.ctrlmode_supported = 0;
  399. if (ctrlmodes & UCAN_MODE_LOOPBACK)
  400. up->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
  401. if (ctrlmodes & UCAN_MODE_SILENT)
  402. up->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
  403. if (ctrlmodes & UCAN_MODE_3_SAMPLES)
  404. up->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  405. if (ctrlmodes & UCAN_MODE_ONE_SHOT)
  406. up->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
  407. if (ctrlmodes & UCAN_MODE_BERR_REPORT)
  408. up->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
  409. }
  410. /* Handle a CAN error frame that we have received from the device.
  411. * Returns true if the can state has changed.
  412. */
  413. static bool ucan_handle_error_frame(struct ucan_priv *up,
  414. struct ucan_message_in *m,
  415. canid_t canid)
  416. {
  417. enum can_state new_state = up->can.state;
  418. struct net_device_stats *net_stats = &up->netdev->stats;
  419. struct can_device_stats *can_stats = &up->can.can_stats;
  420. if (canid & CAN_ERR_LOSTARB)
  421. can_stats->arbitration_lost++;
  422. if (canid & CAN_ERR_BUSERROR)
  423. can_stats->bus_error++;
  424. if (canid & CAN_ERR_ACK)
  425. net_stats->tx_errors++;
  426. if (canid & CAN_ERR_BUSOFF)
  427. new_state = CAN_STATE_BUS_OFF;
  428. /* controller problems, details in data[1] */
  429. if (canid & CAN_ERR_CRTL) {
  430. u8 d1 = m->msg.can_msg.data[1];
  431. if (d1 & CAN_ERR_CRTL_RX_OVERFLOW)
  432. net_stats->rx_over_errors++;
  433. /* controller state bits: if multiple are set the worst wins */
  434. if (d1 & CAN_ERR_CRTL_ACTIVE)
  435. new_state = CAN_STATE_ERROR_ACTIVE;
  436. if (d1 & (CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING))
  437. new_state = CAN_STATE_ERROR_WARNING;
  438. if (d1 & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE))
  439. new_state = CAN_STATE_ERROR_PASSIVE;
  440. }
  441. /* protocol error, details in data[2] */
  442. if (canid & CAN_ERR_PROT) {
  443. u8 d2 = m->msg.can_msg.data[2];
  444. if (d2 & CAN_ERR_PROT_TX)
  445. net_stats->tx_errors++;
  446. else
  447. net_stats->rx_errors++;
  448. }
  449. /* no state change - we are done */
  450. if (up->can.state == new_state)
  451. return false;
  452. /* we switched into a better state */
  453. if (up->can.state > new_state) {
  454. up->can.state = new_state;
  455. return true;
  456. }
  457. /* we switched into a worse state */
  458. up->can.state = new_state;
  459. switch (new_state) {
  460. case CAN_STATE_BUS_OFF:
  461. can_stats->bus_off++;
  462. can_bus_off(up->netdev);
  463. break;
  464. case CAN_STATE_ERROR_PASSIVE:
  465. can_stats->error_passive++;
  466. break;
  467. case CAN_STATE_ERROR_WARNING:
  468. can_stats->error_warning++;
  469. break;
  470. default:
  471. break;
  472. }
  473. return true;
  474. }
  475. /* Callback on reception of a can frame via the IN endpoint
  476. *
  477. * This function allocates an skb and transferres it to the Linux
  478. * network stack
  479. */
  480. static void ucan_rx_can_msg(struct ucan_priv *up, struct ucan_message_in *m)
  481. {
  482. int len;
  483. canid_t canid;
  484. struct can_frame *cf;
  485. struct sk_buff *skb;
  486. struct net_device_stats *stats = &up->netdev->stats;
  487. /* get the contents of the length field */
  488. len = le16_to_cpu(m->len);
  489. /* check sanity */
  490. if (len < UCAN_IN_HDR_SIZE + sizeof(m->msg.can_msg.id)) {
  491. netdev_warn(up->netdev, "invalid input message len: %d\n", len);
  492. return;
  493. }
  494. /* handle error frames */
  495. canid = le32_to_cpu(m->msg.can_msg.id);
  496. if (canid & CAN_ERR_FLAG) {
  497. bool busstate_changed = ucan_handle_error_frame(up, m, canid);
  498. /* if berr-reporting is off only state changes get through */
  499. if (!(up->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
  500. !busstate_changed)
  501. return;
  502. } else {
  503. canid_t canid_mask;
  504. /* compute the mask for canid */
  505. canid_mask = CAN_RTR_FLAG;
  506. if (canid & CAN_EFF_FLAG)
  507. canid_mask |= CAN_EFF_MASK | CAN_EFF_FLAG;
  508. else
  509. canid_mask |= CAN_SFF_MASK;
  510. if (canid & ~canid_mask)
  511. netdev_warn(up->netdev,
  512. "unexpected bits set (canid %x, mask %x)",
  513. canid, canid_mask);
  514. canid &= canid_mask;
  515. }
  516. /* allocate skb */
  517. skb = alloc_can_skb(up->netdev, &cf);
  518. if (!skb)
  519. return;
  520. /* fill the can frame */
  521. cf->can_id = canid;
  522. /* compute DLC taking RTR_FLAG into account */
  523. cf->len = ucan_can_cc_dlc2len(&m->msg.can_msg, len);
  524. /* copy the payload of non RTR frames */
  525. if (!(cf->can_id & CAN_RTR_FLAG) || (cf->can_id & CAN_ERR_FLAG))
  526. memcpy(cf->data, m->msg.can_msg.data, cf->len);
  527. /* don't count error frames as real packets */
  528. if (!(cf->can_id & CAN_ERR_FLAG)) {
  529. stats->rx_packets++;
  530. if (!(cf->can_id & CAN_RTR_FLAG))
  531. stats->rx_bytes += cf->len;
  532. }
  533. /* pass it to Linux */
  534. netif_rx(skb);
  535. }
  536. /* callback indicating completed transmission */
  537. static void ucan_tx_complete_msg(struct ucan_priv *up,
  538. struct ucan_message_in *m)
  539. {
  540. unsigned long flags;
  541. u16 count, i;
  542. u8 echo_index;
  543. u16 len = le16_to_cpu(m->len);
  544. struct ucan_urb_context *context;
  545. if (len < UCAN_IN_HDR_SIZE || (len % 2 != 0)) {
  546. netdev_err(up->netdev, "invalid tx complete length\n");
  547. return;
  548. }
  549. count = (len - UCAN_IN_HDR_SIZE) / 2;
  550. for (i = 0; i < count; i++) {
  551. /* we did not submit such echo ids */
  552. echo_index = m->msg.can_tx_complete_msg[i].echo_index;
  553. if (echo_index >= up->device_info.tx_fifo) {
  554. up->netdev->stats.tx_errors++;
  555. netdev_err(up->netdev,
  556. "invalid echo_index %d received\n",
  557. echo_index);
  558. continue;
  559. }
  560. /* gather information from the context */
  561. context = &up->context_array[echo_index];
  562. /* Release context and restart queue if necessary.
  563. * Also check if the context was allocated
  564. */
  565. if (!ucan_release_context(up, context))
  566. continue;
  567. spin_lock_irqsave(&up->echo_skb_lock, flags);
  568. if (m->msg.can_tx_complete_msg[i].flags &
  569. UCAN_TX_COMPLETE_SUCCESS) {
  570. /* update statistics */
  571. up->netdev->stats.tx_packets++;
  572. up->netdev->stats.tx_bytes +=
  573. can_get_echo_skb(up->netdev, echo_index, NULL);
  574. } else {
  575. up->netdev->stats.tx_dropped++;
  576. can_free_echo_skb(up->netdev, echo_index, NULL);
  577. }
  578. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  579. }
  580. }
  581. /* callback on reception of a USB message */
  582. static void ucan_read_bulk_callback(struct urb *urb)
  583. {
  584. int ret;
  585. int pos;
  586. struct ucan_priv *up = urb->context;
  587. struct net_device *netdev = up->netdev;
  588. struct ucan_message_in *m;
  589. /* the device is not up and the driver should not receive any
  590. * data on the bulk in pipe
  591. */
  592. if (WARN_ON(!up->context_array)) {
  593. usb_free_coherent(up->udev,
  594. up->in_ep_size,
  595. urb->transfer_buffer,
  596. urb->transfer_dma);
  597. return;
  598. }
  599. /* check URB status */
  600. switch (urb->status) {
  601. case 0:
  602. break;
  603. case -ENOENT:
  604. case -EPIPE:
  605. case -EPROTO:
  606. case -ESHUTDOWN:
  607. case -ETIME:
  608. /* urb is not resubmitted -> free dma data */
  609. usb_free_coherent(up->udev,
  610. up->in_ep_size,
  611. urb->transfer_buffer,
  612. urb->transfer_dma);
  613. netdev_dbg(up->netdev, "not resubmitting urb; status: %d\n",
  614. urb->status);
  615. return;
  616. default:
  617. goto resubmit;
  618. }
  619. /* sanity check */
  620. if (!netif_device_present(netdev))
  621. return;
  622. /* iterate over input */
  623. pos = 0;
  624. while (pos < urb->actual_length) {
  625. int len;
  626. /* check sanity (length of header) */
  627. if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) {
  628. netdev_warn(up->netdev,
  629. "invalid message (short; no hdr; l:%d)\n",
  630. urb->actual_length);
  631. goto resubmit;
  632. }
  633. /* setup the message address */
  634. m = (struct ucan_message_in *)
  635. ((u8 *)urb->transfer_buffer + pos);
  636. len = le16_to_cpu(m->len);
  637. /* check sanity (length of content) */
  638. if (urb->actual_length - pos < len) {
  639. netdev_warn(up->netdev,
  640. "invalid message (short; no data; l:%d)\n",
  641. urb->actual_length);
  642. print_hex_dump(KERN_WARNING,
  643. "raw data: ",
  644. DUMP_PREFIX_ADDRESS,
  645. 16,
  646. 1,
  647. urb->transfer_buffer,
  648. urb->actual_length,
  649. true);
  650. goto resubmit;
  651. }
  652. switch (m->type) {
  653. case UCAN_IN_RX:
  654. ucan_rx_can_msg(up, m);
  655. break;
  656. case UCAN_IN_TX_COMPLETE:
  657. ucan_tx_complete_msg(up, m);
  658. break;
  659. default:
  660. netdev_warn(up->netdev,
  661. "invalid message (type; t:%d)\n",
  662. m->type);
  663. break;
  664. }
  665. /* proceed to next message */
  666. pos += len;
  667. /* align to 4 byte boundary */
  668. pos = round_up(pos, 4);
  669. }
  670. resubmit:
  671. /* resubmit urb when done */
  672. usb_fill_bulk_urb(urb, up->udev,
  673. usb_rcvbulkpipe(up->udev,
  674. up->in_ep_addr),
  675. urb->transfer_buffer,
  676. up->in_ep_size,
  677. ucan_read_bulk_callback,
  678. up);
  679. usb_anchor_urb(urb, &up->rx_urbs);
  680. ret = usb_submit_urb(urb, GFP_ATOMIC);
  681. if (ret < 0) {
  682. netdev_err(up->netdev,
  683. "failed resubmitting read bulk urb: %d\n",
  684. ret);
  685. usb_unanchor_urb(urb);
  686. usb_free_coherent(up->udev,
  687. up->in_ep_size,
  688. urb->transfer_buffer,
  689. urb->transfer_dma);
  690. if (ret == -ENODEV)
  691. netif_device_detach(netdev);
  692. }
  693. }
  694. /* callback after transmission of a USB message */
  695. static void ucan_write_bulk_callback(struct urb *urb)
  696. {
  697. unsigned long flags;
  698. struct ucan_priv *up;
  699. struct ucan_urb_context *context = urb->context;
  700. /* get the urb context */
  701. if (WARN_ON_ONCE(!context))
  702. return;
  703. /* free up our allocated buffer */
  704. usb_free_coherent(urb->dev,
  705. sizeof(struct ucan_message_out),
  706. urb->transfer_buffer,
  707. urb->transfer_dma);
  708. up = context->up;
  709. if (WARN_ON_ONCE(!up))
  710. return;
  711. /* sanity check */
  712. if (!netif_device_present(up->netdev))
  713. return;
  714. /* transmission failed (USB - the device will not send a TX complete) */
  715. if (urb->status) {
  716. netdev_warn(up->netdev,
  717. "failed to transmit USB message to device: %d\n",
  718. urb->status);
  719. /* update counters an cleanup */
  720. spin_lock_irqsave(&up->echo_skb_lock, flags);
  721. can_free_echo_skb(up->netdev, context - up->context_array, NULL);
  722. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  723. up->netdev->stats.tx_dropped++;
  724. /* release context and restart the queue if necessary */
  725. if (!ucan_release_context(up, context))
  726. netdev_err(up->netdev,
  727. "urb failed, failed to release context\n");
  728. }
  729. }
  730. static void ucan_cleanup_rx_urbs(struct ucan_priv *up, struct urb **urbs)
  731. {
  732. int i;
  733. for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
  734. if (urbs[i]) {
  735. usb_unanchor_urb(urbs[i]);
  736. usb_free_coherent(up->udev,
  737. up->in_ep_size,
  738. urbs[i]->transfer_buffer,
  739. urbs[i]->transfer_dma);
  740. usb_free_urb(urbs[i]);
  741. }
  742. }
  743. memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
  744. }
  745. static int ucan_prepare_and_anchor_rx_urbs(struct ucan_priv *up,
  746. struct urb **urbs)
  747. {
  748. int i;
  749. memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
  750. for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
  751. void *buf;
  752. urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
  753. if (!urbs[i])
  754. goto err;
  755. buf = usb_alloc_coherent(up->udev,
  756. up->in_ep_size,
  757. GFP_KERNEL, &urbs[i]->transfer_dma);
  758. if (!buf) {
  759. /* cleanup this urb */
  760. usb_free_urb(urbs[i]);
  761. urbs[i] = NULL;
  762. goto err;
  763. }
  764. usb_fill_bulk_urb(urbs[i], up->udev,
  765. usb_rcvbulkpipe(up->udev,
  766. up->in_ep_addr),
  767. buf,
  768. up->in_ep_size,
  769. ucan_read_bulk_callback,
  770. up);
  771. urbs[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  772. usb_anchor_urb(urbs[i], &up->rx_urbs);
  773. }
  774. return 0;
  775. err:
  776. /* cleanup other unsubmitted urbs */
  777. ucan_cleanup_rx_urbs(up, urbs);
  778. return -ENOMEM;
  779. }
  780. /* Submits rx urbs with the semantic: Either submit all, or cleanup
  781. * everything. I case of errors submitted urbs are killed and all urbs in
  782. * the array are freed. I case of no errors every entry in the urb
  783. * array is set to NULL.
  784. */
  785. static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs)
  786. {
  787. int i, ret;
  788. /* Iterate over all urbs to submit. On success remove the urb
  789. * from the list.
  790. */
  791. for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
  792. ret = usb_submit_urb(urbs[i], GFP_KERNEL);
  793. if (ret) {
  794. netdev_err(up->netdev,
  795. "could not submit urb; code: %d\n",
  796. ret);
  797. goto err;
  798. }
  799. /* Anchor URB and drop reference, USB core will take
  800. * care of freeing it
  801. */
  802. usb_free_urb(urbs[i]);
  803. urbs[i] = NULL;
  804. }
  805. return 0;
  806. err:
  807. /* Cleanup unsubmitted urbs */
  808. ucan_cleanup_rx_urbs(up, urbs);
  809. /* Kill urbs that are already submitted */
  810. usb_kill_anchored_urbs(&up->rx_urbs);
  811. return ret;
  812. }
  813. /* Open the network device */
  814. static int ucan_open(struct net_device *netdev)
  815. {
  816. int ret, ret_cleanup;
  817. u16 ctrlmode;
  818. struct urb *urbs[UCAN_MAX_RX_URBS];
  819. struct ucan_priv *up = netdev_priv(netdev);
  820. ret = ucan_alloc_context_array(up);
  821. if (ret)
  822. return ret;
  823. /* Allocate and prepare IN URBS - allocated and anchored
  824. * urbs are stored in urbs[] for clean
  825. */
  826. ret = ucan_prepare_and_anchor_rx_urbs(up, urbs);
  827. if (ret)
  828. goto err_contexts;
  829. /* Check the control mode */
  830. ctrlmode = 0;
  831. if (up->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  832. ctrlmode |= UCAN_MODE_LOOPBACK;
  833. if (up->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  834. ctrlmode |= UCAN_MODE_SILENT;
  835. if (up->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  836. ctrlmode |= UCAN_MODE_3_SAMPLES;
  837. if (up->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  838. ctrlmode |= UCAN_MODE_ONE_SHOT;
  839. /* Enable this in any case - filtering is down within the
  840. * receive path
  841. */
  842. ctrlmode |= UCAN_MODE_BERR_REPORT;
  843. up->ctl_msg_buffer->cmd_start.mode = cpu_to_le16(ctrlmode);
  844. /* Driver is ready to receive data - start the USB device */
  845. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2);
  846. if (ret < 0) {
  847. netdev_err(up->netdev,
  848. "could not start device, code: %d\n",
  849. ret);
  850. goto err_reset;
  851. }
  852. /* Call CAN layer open */
  853. ret = open_candev(netdev);
  854. if (ret)
  855. goto err_stop;
  856. /* Driver is ready to receive data. Submit RX URBS */
  857. ret = ucan_submit_rx_urbs(up, urbs);
  858. if (ret)
  859. goto err_stop;
  860. up->can.state = CAN_STATE_ERROR_ACTIVE;
  861. /* Start the network queue */
  862. netif_start_queue(netdev);
  863. return 0;
  864. err_stop:
  865. /* The device have started already stop it */
  866. ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
  867. if (ret_cleanup < 0)
  868. netdev_err(up->netdev,
  869. "could not stop device, code: %d\n",
  870. ret_cleanup);
  871. err_reset:
  872. /* The device might have received data, reset it for
  873. * consistent state
  874. */
  875. ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  876. if (ret_cleanup < 0)
  877. netdev_err(up->netdev,
  878. "could not reset device, code: %d\n",
  879. ret_cleanup);
  880. /* clean up unsubmitted urbs */
  881. ucan_cleanup_rx_urbs(up, urbs);
  882. err_contexts:
  883. ucan_release_context_array(up);
  884. return ret;
  885. }
  886. static struct urb *ucan_prepare_tx_urb(struct ucan_priv *up,
  887. struct ucan_urb_context *context,
  888. struct can_frame *cf,
  889. u8 echo_index)
  890. {
  891. int mlen;
  892. struct urb *urb;
  893. struct ucan_message_out *m;
  894. /* create a URB, and a buffer for it, and copy the data to the URB */
  895. urb = usb_alloc_urb(0, GFP_ATOMIC);
  896. if (!urb) {
  897. netdev_err(up->netdev, "no memory left for URBs\n");
  898. return NULL;
  899. }
  900. m = usb_alloc_coherent(up->udev,
  901. sizeof(struct ucan_message_out),
  902. GFP_ATOMIC,
  903. &urb->transfer_dma);
  904. if (!m) {
  905. netdev_err(up->netdev, "no memory left for USB buffer\n");
  906. usb_free_urb(urb);
  907. return NULL;
  908. }
  909. /* build the USB message */
  910. m->type = UCAN_OUT_TX;
  911. m->msg.can_msg.id = cpu_to_le32(cf->can_id);
  912. if (cf->can_id & CAN_RTR_FLAG) {
  913. mlen = UCAN_OUT_HDR_SIZE +
  914. offsetof(struct ucan_can_msg, dlc) +
  915. sizeof(m->msg.can_msg.dlc);
  916. m->msg.can_msg.dlc = cf->len;
  917. } else {
  918. mlen = UCAN_OUT_HDR_SIZE +
  919. sizeof(m->msg.can_msg.id) + cf->len;
  920. memcpy(m->msg.can_msg.data, cf->data, cf->len);
  921. }
  922. m->len = cpu_to_le16(mlen);
  923. m->subtype = echo_index;
  924. /* build the urb */
  925. usb_fill_bulk_urb(urb, up->udev,
  926. usb_sndbulkpipe(up->udev,
  927. up->out_ep_addr),
  928. m, mlen, ucan_write_bulk_callback, context);
  929. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  930. return urb;
  931. }
  932. static void ucan_clean_up_tx_urb(struct ucan_priv *up, struct urb *urb)
  933. {
  934. usb_free_coherent(up->udev, sizeof(struct ucan_message_out),
  935. urb->transfer_buffer, urb->transfer_dma);
  936. usb_free_urb(urb);
  937. }
  938. /* callback when Linux needs to send a can frame */
  939. static netdev_tx_t ucan_start_xmit(struct sk_buff *skb,
  940. struct net_device *netdev)
  941. {
  942. unsigned long flags;
  943. int ret;
  944. u8 echo_index;
  945. struct urb *urb;
  946. struct ucan_urb_context *context;
  947. struct ucan_priv *up = netdev_priv(netdev);
  948. struct can_frame *cf = (struct can_frame *)skb->data;
  949. /* check skb */
  950. if (can_dev_dropped_skb(netdev, skb))
  951. return NETDEV_TX_OK;
  952. /* allocate a context and slow down tx path, if fifo state is low */
  953. context = ucan_alloc_context(up);
  954. echo_index = context - up->context_array;
  955. if (WARN_ON_ONCE(!context))
  956. return NETDEV_TX_BUSY;
  957. /* prepare urb for transmission */
  958. urb = ucan_prepare_tx_urb(up, context, cf, echo_index);
  959. if (!urb)
  960. goto drop;
  961. /* put the skb on can loopback stack */
  962. spin_lock_irqsave(&up->echo_skb_lock, flags);
  963. can_put_echo_skb(skb, up->netdev, echo_index, 0);
  964. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  965. /* transmit it */
  966. usb_anchor_urb(urb, &up->tx_urbs);
  967. ret = usb_submit_urb(urb, GFP_ATOMIC);
  968. /* cleanup urb */
  969. if (ret) {
  970. /* on error, clean up */
  971. usb_unanchor_urb(urb);
  972. ucan_clean_up_tx_urb(up, urb);
  973. if (!ucan_release_context(up, context))
  974. netdev_err(up->netdev,
  975. "xmit err: failed to release context\n");
  976. /* remove the skb from the echo stack - this also
  977. * frees the skb
  978. */
  979. spin_lock_irqsave(&up->echo_skb_lock, flags);
  980. can_free_echo_skb(up->netdev, echo_index, NULL);
  981. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  982. if (ret == -ENODEV) {
  983. netif_device_detach(up->netdev);
  984. } else {
  985. netdev_warn(up->netdev,
  986. "xmit err: failed to submit urb %d\n",
  987. ret);
  988. up->netdev->stats.tx_dropped++;
  989. }
  990. return NETDEV_TX_OK;
  991. }
  992. netif_trans_update(netdev);
  993. /* release ref, as we do not need the urb anymore */
  994. usb_free_urb(urb);
  995. return NETDEV_TX_OK;
  996. drop:
  997. if (!ucan_release_context(up, context))
  998. netdev_err(up->netdev,
  999. "xmit drop: failed to release context\n");
  1000. dev_kfree_skb(skb);
  1001. up->netdev->stats.tx_dropped++;
  1002. return NETDEV_TX_OK;
  1003. }
  1004. /* Device goes down
  1005. *
  1006. * Clean up used resources
  1007. */
  1008. static int ucan_close(struct net_device *netdev)
  1009. {
  1010. int ret;
  1011. struct ucan_priv *up = netdev_priv(netdev);
  1012. up->can.state = CAN_STATE_STOPPED;
  1013. /* stop sending data */
  1014. usb_kill_anchored_urbs(&up->tx_urbs);
  1015. /* stop receiving data */
  1016. usb_kill_anchored_urbs(&up->rx_urbs);
  1017. /* stop and reset can device */
  1018. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
  1019. if (ret < 0)
  1020. netdev_err(up->netdev,
  1021. "could not stop device, code: %d\n",
  1022. ret);
  1023. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  1024. if (ret < 0)
  1025. netdev_err(up->netdev,
  1026. "could not reset device, code: %d\n",
  1027. ret);
  1028. netif_stop_queue(netdev);
  1029. ucan_release_context_array(up);
  1030. close_candev(up->netdev);
  1031. return 0;
  1032. }
  1033. /* CAN driver callbacks */
  1034. static const struct net_device_ops ucan_netdev_ops = {
  1035. .ndo_open = ucan_open,
  1036. .ndo_stop = ucan_close,
  1037. .ndo_start_xmit = ucan_start_xmit,
  1038. .ndo_change_mtu = can_change_mtu,
  1039. };
  1040. static const struct ethtool_ops ucan_ethtool_ops = {
  1041. .get_ts_info = ethtool_op_get_ts_info,
  1042. };
  1043. /* Request to set bittiming
  1044. *
  1045. * This function generates an USB set bittiming message and transmits
  1046. * it to the device
  1047. */
  1048. static int ucan_set_bittiming(struct net_device *netdev)
  1049. {
  1050. int ret;
  1051. struct ucan_priv *up = netdev_priv(netdev);
  1052. struct ucan_ctl_cmd_set_bittiming *cmd_set_bittiming;
  1053. cmd_set_bittiming = &up->ctl_msg_buffer->cmd_set_bittiming;
  1054. cmd_set_bittiming->tq = cpu_to_le32(up->can.bittiming.tq);
  1055. cmd_set_bittiming->brp = cpu_to_le16(up->can.bittiming.brp);
  1056. cmd_set_bittiming->sample_point =
  1057. cpu_to_le16(up->can.bittiming.sample_point);
  1058. cmd_set_bittiming->prop_seg = up->can.bittiming.prop_seg;
  1059. cmd_set_bittiming->phase_seg1 = up->can.bittiming.phase_seg1;
  1060. cmd_set_bittiming->phase_seg2 = up->can.bittiming.phase_seg2;
  1061. cmd_set_bittiming->sjw = up->can.bittiming.sjw;
  1062. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0,
  1063. sizeof(*cmd_set_bittiming));
  1064. return (ret < 0) ? ret : 0;
  1065. }
  1066. /* Restart the device to get it out of BUS-OFF state.
  1067. * Called when the user runs "ip link set can1 type can restart".
  1068. */
  1069. static int ucan_set_mode(struct net_device *netdev, enum can_mode mode)
  1070. {
  1071. int ret;
  1072. unsigned long flags;
  1073. struct ucan_priv *up = netdev_priv(netdev);
  1074. switch (mode) {
  1075. case CAN_MODE_START:
  1076. netdev_dbg(up->netdev, "restarting device\n");
  1077. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0);
  1078. up->can.state = CAN_STATE_ERROR_ACTIVE;
  1079. /* check if queue can be restarted,
  1080. * up->available_tx_urbs must be protected by the
  1081. * lock
  1082. */
  1083. spin_lock_irqsave(&up->context_lock, flags);
  1084. if (up->available_tx_urbs > 0)
  1085. netif_wake_queue(up->netdev);
  1086. spin_unlock_irqrestore(&up->context_lock, flags);
  1087. return ret;
  1088. default:
  1089. return -EOPNOTSUPP;
  1090. }
  1091. }
  1092. /* Probe the device, reset it and gather general device information */
  1093. static int ucan_probe(struct usb_interface *intf,
  1094. const struct usb_device_id *id)
  1095. {
  1096. int ret;
  1097. int i;
  1098. u32 protocol_version;
  1099. struct usb_device *udev;
  1100. struct net_device *netdev;
  1101. struct usb_host_interface *iface_desc;
  1102. struct ucan_priv *up;
  1103. struct usb_endpoint_descriptor *ep;
  1104. u16 in_ep_size;
  1105. u16 out_ep_size;
  1106. u8 in_ep_addr;
  1107. u8 out_ep_addr;
  1108. union ucan_ctl_payload *ctl_msg_buffer;
  1109. char firmware_str[sizeof(union ucan_ctl_payload) + 1];
  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->intf = intf;
  1277. up->netdev = netdev;
  1278. up->intf_index = iface_desc->desc.bInterfaceNumber;
  1279. up->in_ep_addr = in_ep_addr;
  1280. up->out_ep_addr = out_ep_addr;
  1281. up->in_ep_size = in_ep_size;
  1282. up->ctl_msg_buffer = ctl_msg_buffer;
  1283. up->context_array = NULL;
  1284. up->available_tx_urbs = 0;
  1285. up->can.state = CAN_STATE_STOPPED;
  1286. up->can.bittiming_const = &up->device_info.bittiming_const;
  1287. up->can.do_set_bittiming = ucan_set_bittiming;
  1288. up->can.do_set_mode = &ucan_set_mode;
  1289. spin_lock_init(&up->context_lock);
  1290. spin_lock_init(&up->echo_skb_lock);
  1291. netdev->netdev_ops = &ucan_netdev_ops;
  1292. netdev->ethtool_ops = &ucan_ethtool_ops;
  1293. usb_set_intfdata(intf, up);
  1294. SET_NETDEV_DEV(netdev, &intf->dev);
  1295. /* parse device information
  1296. * the data retrieved in Stage 2 is still available in
  1297. * up->ctl_msg_buffer
  1298. */
  1299. ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info);
  1300. /* just print some device information - if available */
  1301. ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0,
  1302. sizeof(union ucan_ctl_payload));
  1303. if (ret > 0) {
  1304. /* copy string while ensuring zero termination */
  1305. strncpy(firmware_str, up->ctl_msg_buffer->raw,
  1306. sizeof(union ucan_ctl_payload));
  1307. firmware_str[sizeof(union ucan_ctl_payload)] = '\0';
  1308. } else {
  1309. strcpy(firmware_str, "unknown");
  1310. }
  1311. /* device is compatible, reset it */
  1312. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  1313. if (ret < 0)
  1314. goto err_free_candev;
  1315. init_usb_anchor(&up->rx_urbs);
  1316. init_usb_anchor(&up->tx_urbs);
  1317. up->can.state = CAN_STATE_STOPPED;
  1318. /* register the device */
  1319. ret = register_candev(netdev);
  1320. if (ret)
  1321. goto err_free_candev;
  1322. /* initialisation complete, log device info */
  1323. netdev_info(up->netdev, "registered device\n");
  1324. netdev_info(up->netdev, "firmware string: %s\n", firmware_str);
  1325. /* success */
  1326. return 0;
  1327. err_free_candev:
  1328. free_candev(netdev);
  1329. return ret;
  1330. err_firmware_needs_update:
  1331. dev_err(&udev->dev,
  1332. "%s: probe failed; try to update the device firmware\n",
  1333. UCAN_DRIVER_NAME);
  1334. return -ENODEV;
  1335. }
  1336. /* disconnect the device */
  1337. static void ucan_disconnect(struct usb_interface *intf)
  1338. {
  1339. struct ucan_priv *up = usb_get_intfdata(intf);
  1340. usb_set_intfdata(intf, NULL);
  1341. if (up) {
  1342. unregister_netdev(up->netdev);
  1343. free_candev(up->netdev);
  1344. }
  1345. }
  1346. static struct usb_device_id ucan_table[] = {
  1347. /* Mule (soldered onto compute modules) */
  1348. {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425a, 0)},
  1349. /* Seal (standalone USB stick) */
  1350. {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425b, 0)},
  1351. {} /* Terminating entry */
  1352. };
  1353. MODULE_DEVICE_TABLE(usb, ucan_table);
  1354. /* driver callbacks */
  1355. static struct usb_driver ucan_driver = {
  1356. .name = UCAN_DRIVER_NAME,
  1357. .probe = ucan_probe,
  1358. .disconnect = ucan_disconnect,
  1359. .id_table = ucan_table,
  1360. };
  1361. module_usb_driver(ucan_driver);
  1362. MODULE_LICENSE("GPL v2");
  1363. MODULE_AUTHOR("Martin Elshuber <[email protected]>");
  1364. MODULE_AUTHOR("Jakob Unterwurzacher <[email protected]>");
  1365. MODULE_DESCRIPTION("Driver for Theobroma Systems UCAN devices");