ucan.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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/module.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/signal.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/slab.h>
  34. #include <linux/usb.h>
  35. #include <linux/can.h>
  36. #include <linux/can/dev.h>
  37. #include <linux/can/error.h>
  38. #define UCAN_DRIVER_NAME "ucan"
  39. #define UCAN_MAX_RX_URBS 8
  40. /* the CAN controller needs a while to enable/disable the bus */
  41. #define UCAN_USB_CTL_PIPE_TIMEOUT 1000
  42. /* this driver currently supports protocol version 3 only */
  43. #define UCAN_PROTOCOL_VERSION_MIN 3
  44. #define UCAN_PROTOCOL_VERSION_MAX 3
  45. /* UCAN Message Definitions
  46. * ------------------------
  47. *
  48. * ucan_message_out_t and ucan_message_in_t define the messages
  49. * transmitted on the OUT and IN endpoint.
  50. *
  51. * Multibyte fields are transmitted with little endianness
  52. *
  53. * INTR Endpoint: a single uint32_t storing the current space in the fifo
  54. *
  55. * OUT Endpoint: single message of type ucan_message_out_t is
  56. * transmitted on the out endpoint
  57. *
  58. * IN Endpoint: multiple messages ucan_message_in_t concateted in
  59. * the following way:
  60. *
  61. * m[n].len <=> the length if message n(including the header in bytes)
  62. * m[n] is is aligned to a 4 byte boundary, hence
  63. * offset(m[0]) := 0;
  64. * offset(m[n+1]) := offset(m[n]) + (m[n].len + 3) & 3
  65. *
  66. * this implies that
  67. * offset(m[n]) % 4 <=> 0
  68. */
  69. /* Device Global Commands */
  70. enum {
  71. UCAN_DEVICE_GET_FW_STRING = 0,
  72. };
  73. /* UCAN Commands */
  74. enum {
  75. /* start the can transceiver - val defines the operation mode */
  76. UCAN_COMMAND_START = 0,
  77. /* cancel pending transmissions and stop the can transceiver */
  78. UCAN_COMMAND_STOP = 1,
  79. /* send can transceiver into low-power sleep mode */
  80. UCAN_COMMAND_SLEEP = 2,
  81. /* wake up can transceiver from low-power sleep mode */
  82. UCAN_COMMAND_WAKEUP = 3,
  83. /* reset the can transceiver */
  84. UCAN_COMMAND_RESET = 4,
  85. /* get piece of info from the can transceiver - subcmd defines what
  86. * piece
  87. */
  88. UCAN_COMMAND_GET = 5,
  89. /* clear or disable hardware filter - subcmd defines which of the two */
  90. UCAN_COMMAND_FILTER = 6,
  91. /* Setup bittiming */
  92. UCAN_COMMAND_SET_BITTIMING = 7,
  93. /* recover from bus-off state */
  94. UCAN_COMMAND_RESTART = 8,
  95. };
  96. /* UCAN_COMMAND_START and UCAN_COMMAND_GET_INFO operation modes (bitmap).
  97. * Undefined bits must be set to 0.
  98. */
  99. enum {
  100. UCAN_MODE_LOOPBACK = BIT(0),
  101. UCAN_MODE_SILENT = BIT(1),
  102. UCAN_MODE_3_SAMPLES = BIT(2),
  103. UCAN_MODE_ONE_SHOT = BIT(3),
  104. UCAN_MODE_BERR_REPORT = BIT(4),
  105. };
  106. /* UCAN_COMMAND_GET subcommands */
  107. enum {
  108. UCAN_COMMAND_GET_INFO = 0,
  109. UCAN_COMMAND_GET_PROTOCOL_VERSION = 1,
  110. };
  111. /* UCAN_COMMAND_FILTER subcommands */
  112. enum {
  113. UCAN_FILTER_CLEAR = 0,
  114. UCAN_FILTER_DISABLE = 1,
  115. UCAN_FILTER_ENABLE = 2,
  116. };
  117. /* OUT endpoint message types */
  118. enum {
  119. UCAN_OUT_TX = 2, /* transmit a CAN frame */
  120. };
  121. /* IN endpoint message types */
  122. enum {
  123. UCAN_IN_TX_COMPLETE = 1, /* CAN frame transmission completed */
  124. UCAN_IN_RX = 2, /* CAN frame received */
  125. };
  126. struct ucan_ctl_cmd_start {
  127. __le16 mode; /* OR-ing any of UCAN_MODE_* */
  128. } __packed;
  129. struct ucan_ctl_cmd_set_bittiming {
  130. __le32 tq; /* Time quanta (TQ) in nanoseconds */
  131. __le16 brp; /* TQ Prescaler */
  132. __le16 sample_point; /* Samplepoint on tenth percent */
  133. u8 prop_seg; /* Propagation segment in TQs */
  134. u8 phase_seg1; /* Phase buffer segment 1 in TQs */
  135. u8 phase_seg2; /* Phase buffer segment 2 in TQs */
  136. u8 sjw; /* Synchronisation jump width in TQs */
  137. } __packed;
  138. struct ucan_ctl_cmd_device_info {
  139. __le32 freq; /* Clock Frequency for tq generation */
  140. u8 tx_fifo; /* Size of the transmission fifo */
  141. u8 sjw_max; /* can_bittiming fields... */
  142. u8 tseg1_min;
  143. u8 tseg1_max;
  144. u8 tseg2_min;
  145. u8 tseg2_max;
  146. __le16 brp_inc;
  147. __le32 brp_min;
  148. __le32 brp_max; /* ...can_bittiming fields */
  149. __le16 ctrlmodes; /* supported control modes */
  150. __le16 hwfilter; /* Number of HW filter banks */
  151. __le16 rxmboxes; /* Number of receive Mailboxes */
  152. } __packed;
  153. struct ucan_ctl_cmd_get_protocol_version {
  154. __le32 version;
  155. } __packed;
  156. union ucan_ctl_payload {
  157. /* Setup Bittiming
  158. * bmRequest == UCAN_COMMAND_START
  159. */
  160. struct ucan_ctl_cmd_start cmd_start;
  161. /* Setup Bittiming
  162. * bmRequest == UCAN_COMMAND_SET_BITTIMING
  163. */
  164. struct ucan_ctl_cmd_set_bittiming cmd_set_bittiming;
  165. /* Get Device Information
  166. * bmRequest == UCAN_COMMAND_GET; wValue = UCAN_COMMAND_GET_INFO
  167. */
  168. struct ucan_ctl_cmd_device_info cmd_get_device_info;
  169. /* Get Protocol Version
  170. * bmRequest == UCAN_COMMAND_GET;
  171. * wValue = UCAN_COMMAND_GET_PROTOCOL_VERSION
  172. */
  173. struct ucan_ctl_cmd_get_protocol_version cmd_get_protocol_version;
  174. u8 raw[128];
  175. } __packed;
  176. enum {
  177. UCAN_TX_COMPLETE_SUCCESS = BIT(0),
  178. };
  179. /* Transmission Complete within ucan_message_in */
  180. struct ucan_tx_complete_entry_t {
  181. u8 echo_index;
  182. u8 flags;
  183. } __packed __aligned(0x2);
  184. /* CAN Data message format within ucan_message_in/out */
  185. struct ucan_can_msg {
  186. /* note DLC is computed by
  187. * msg.len - sizeof (msg.len)
  188. * - sizeof (msg.type)
  189. * - sizeof (msg.can_msg.id)
  190. */
  191. __le32 id;
  192. union {
  193. u8 data[CAN_MAX_DLEN]; /* Data of CAN frames */
  194. u8 dlc; /* RTR dlc */
  195. };
  196. } __packed;
  197. /* OUT Endpoint, outbound messages */
  198. struct ucan_message_out {
  199. __le16 len; /* Length of the content include header */
  200. u8 type; /* UCAN_OUT_TX and friends */
  201. u8 subtype; /* command sub type */
  202. union {
  203. /* Transmit CAN frame
  204. * (type == UCAN_TX) && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
  205. * subtype stores the echo id
  206. */
  207. struct ucan_can_msg can_msg;
  208. } msg;
  209. } __packed __aligned(0x4);
  210. /* IN Endpoint, inbound messages */
  211. struct ucan_message_in {
  212. __le16 len; /* Length of the content include header */
  213. u8 type; /* UCAN_IN_RX and friends */
  214. u8 subtype; /* command sub type */
  215. union {
  216. /* CAN Frame received
  217. * (type == UCAN_IN_RX)
  218. * && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
  219. */
  220. struct ucan_can_msg can_msg;
  221. /* CAN transmission complete
  222. * (type == UCAN_IN_TX_COMPLETE)
  223. */
  224. struct ucan_tx_complete_entry_t can_tx_complete_msg[0];
  225. } __aligned(0x4) msg;
  226. } __packed;
  227. /* Macros to calculate message lengths */
  228. #define UCAN_OUT_HDR_SIZE offsetof(struct ucan_message_out, msg)
  229. #define UCAN_IN_HDR_SIZE offsetof(struct ucan_message_in, msg)
  230. #define UCAN_IN_LEN(member) (UCAN_OUT_HDR_SIZE + sizeof(member))
  231. struct ucan_priv;
  232. /* Context Information for transmission URBs */
  233. struct ucan_urb_context {
  234. struct ucan_priv *up;
  235. u8 dlc;
  236. bool allocated;
  237. };
  238. /* Information reported by the USB device */
  239. struct ucan_device_info {
  240. struct can_bittiming_const bittiming_const;
  241. u8 tx_fifo;
  242. };
  243. /* Driver private data */
  244. struct ucan_priv {
  245. /* must be the first member */
  246. struct can_priv can;
  247. /* linux USB device structures */
  248. struct usb_device *udev;
  249. struct usb_interface *intf;
  250. struct net_device *netdev;
  251. /* lock for can->echo_skb (used around
  252. * can_put/get/free_echo_skb
  253. */
  254. spinlock_t echo_skb_lock;
  255. /* usb device information information */
  256. u8 intf_index;
  257. u8 in_ep_addr;
  258. u8 out_ep_addr;
  259. u16 in_ep_size;
  260. /* transmission and reception buffers */
  261. struct usb_anchor rx_urbs;
  262. struct usb_anchor tx_urbs;
  263. union ucan_ctl_payload *ctl_msg_buffer;
  264. struct ucan_device_info device_info;
  265. /* transmission control information and locks */
  266. spinlock_t context_lock;
  267. unsigned int available_tx_urbs;
  268. struct ucan_urb_context *context_array;
  269. };
  270. static u8 ucan_get_can_dlc(struct ucan_can_msg *msg, u16 len)
  271. {
  272. if (le32_to_cpu(msg->id) & CAN_RTR_FLAG)
  273. return get_can_dlc(msg->dlc);
  274. else
  275. return get_can_dlc(len - (UCAN_IN_HDR_SIZE + sizeof(msg->id)));
  276. }
  277. static void ucan_release_context_array(struct ucan_priv *up)
  278. {
  279. if (!up->context_array)
  280. return;
  281. /* lock is not needed because, driver is currently opening or closing */
  282. up->available_tx_urbs = 0;
  283. kfree(up->context_array);
  284. up->context_array = NULL;
  285. }
  286. static int ucan_alloc_context_array(struct ucan_priv *up)
  287. {
  288. int i;
  289. /* release contexts if any */
  290. ucan_release_context_array(up);
  291. up->context_array = kcalloc(up->device_info.tx_fifo,
  292. sizeof(*up->context_array),
  293. GFP_KERNEL);
  294. if (!up->context_array) {
  295. netdev_err(up->netdev,
  296. "Not enough memory to allocate tx contexts\n");
  297. return -ENOMEM;
  298. }
  299. for (i = 0; i < up->device_info.tx_fifo; i++) {
  300. up->context_array[i].allocated = false;
  301. up->context_array[i].up = up;
  302. }
  303. /* lock is not needed because, driver is currently opening */
  304. up->available_tx_urbs = up->device_info.tx_fifo;
  305. return 0;
  306. }
  307. static struct ucan_urb_context *ucan_alloc_context(struct ucan_priv *up)
  308. {
  309. int i;
  310. unsigned long flags;
  311. struct ucan_urb_context *ret = NULL;
  312. if (WARN_ON_ONCE(!up->context_array))
  313. return NULL;
  314. /* execute context operation atomically */
  315. spin_lock_irqsave(&up->context_lock, flags);
  316. for (i = 0; i < up->device_info.tx_fifo; i++) {
  317. if (!up->context_array[i].allocated) {
  318. /* update context */
  319. ret = &up->context_array[i];
  320. up->context_array[i].allocated = true;
  321. /* stop queue if necessary */
  322. up->available_tx_urbs--;
  323. if (!up->available_tx_urbs)
  324. netif_stop_queue(up->netdev);
  325. break;
  326. }
  327. }
  328. spin_unlock_irqrestore(&up->context_lock, flags);
  329. return ret;
  330. }
  331. static bool ucan_release_context(struct ucan_priv *up,
  332. struct ucan_urb_context *ctx)
  333. {
  334. unsigned long flags;
  335. bool ret = false;
  336. if (WARN_ON_ONCE(!up->context_array))
  337. return false;
  338. /* execute context operation atomically */
  339. spin_lock_irqsave(&up->context_lock, flags);
  340. /* context was not allocated, maybe the device sent garbage */
  341. if (ctx->allocated) {
  342. ctx->allocated = false;
  343. /* check if the queue needs to be woken */
  344. if (!up->available_tx_urbs)
  345. netif_wake_queue(up->netdev);
  346. up->available_tx_urbs++;
  347. ret = true;
  348. }
  349. spin_unlock_irqrestore(&up->context_lock, flags);
  350. return ret;
  351. }
  352. static int ucan_ctrl_command_out(struct ucan_priv *up,
  353. u8 cmd, u16 subcmd, u16 datalen)
  354. {
  355. return usb_control_msg(up->udev,
  356. usb_sndctrlpipe(up->udev, 0),
  357. cmd,
  358. USB_DIR_OUT | USB_TYPE_VENDOR |
  359. USB_RECIP_INTERFACE,
  360. subcmd,
  361. up->intf_index,
  362. up->ctl_msg_buffer,
  363. datalen,
  364. UCAN_USB_CTL_PIPE_TIMEOUT);
  365. }
  366. static int ucan_device_request_in(struct ucan_priv *up,
  367. u8 cmd, u16 subcmd, u16 datalen)
  368. {
  369. return usb_control_msg(up->udev,
  370. usb_rcvctrlpipe(up->udev, 0),
  371. cmd,
  372. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  373. subcmd,
  374. 0,
  375. up->ctl_msg_buffer,
  376. datalen,
  377. UCAN_USB_CTL_PIPE_TIMEOUT);
  378. }
  379. /* Parse the device information structure reported by the device and
  380. * setup private variables accordingly
  381. */
  382. static void ucan_parse_device_info(struct ucan_priv *up,
  383. struct ucan_ctl_cmd_device_info *device_info)
  384. {
  385. struct can_bittiming_const *bittiming =
  386. &up->device_info.bittiming_const;
  387. u16 ctrlmodes;
  388. /* store the data */
  389. up->can.clock.freq = le32_to_cpu(device_info->freq);
  390. up->device_info.tx_fifo = device_info->tx_fifo;
  391. strcpy(bittiming->name, "ucan");
  392. bittiming->tseg1_min = device_info->tseg1_min;
  393. bittiming->tseg1_max = device_info->tseg1_max;
  394. bittiming->tseg2_min = device_info->tseg2_min;
  395. bittiming->tseg2_max = device_info->tseg2_max;
  396. bittiming->sjw_max = device_info->sjw_max;
  397. bittiming->brp_min = le32_to_cpu(device_info->brp_min);
  398. bittiming->brp_max = le32_to_cpu(device_info->brp_max);
  399. bittiming->brp_inc = le16_to_cpu(device_info->brp_inc);
  400. ctrlmodes = le16_to_cpu(device_info->ctrlmodes);
  401. up->can.ctrlmode_supported = 0;
  402. if (ctrlmodes & UCAN_MODE_LOOPBACK)
  403. up->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK;
  404. if (ctrlmodes & UCAN_MODE_SILENT)
  405. up->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY;
  406. if (ctrlmodes & UCAN_MODE_3_SAMPLES)
  407. up->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  408. if (ctrlmodes & UCAN_MODE_ONE_SHOT)
  409. up->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
  410. if (ctrlmodes & UCAN_MODE_BERR_REPORT)
  411. up->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING;
  412. }
  413. /* Handle a CAN error frame that we have received from the device.
  414. * Returns true if the can state has changed.
  415. */
  416. static bool ucan_handle_error_frame(struct ucan_priv *up,
  417. struct ucan_message_in *m,
  418. canid_t canid)
  419. {
  420. enum can_state new_state = up->can.state;
  421. struct net_device_stats *net_stats = &up->netdev->stats;
  422. struct can_device_stats *can_stats = &up->can.can_stats;
  423. if (canid & CAN_ERR_LOSTARB)
  424. can_stats->arbitration_lost++;
  425. if (canid & CAN_ERR_BUSERROR)
  426. can_stats->bus_error++;
  427. if (canid & CAN_ERR_ACK)
  428. net_stats->tx_errors++;
  429. if (canid & CAN_ERR_BUSOFF)
  430. new_state = CAN_STATE_BUS_OFF;
  431. /* controller problems, details in data[1] */
  432. if (canid & CAN_ERR_CRTL) {
  433. u8 d1 = m->msg.can_msg.data[1];
  434. if (d1 & CAN_ERR_CRTL_RX_OVERFLOW)
  435. net_stats->rx_over_errors++;
  436. /* controller state bits: if multiple are set the worst wins */
  437. if (d1 & CAN_ERR_CRTL_ACTIVE)
  438. new_state = CAN_STATE_ERROR_ACTIVE;
  439. if (d1 & (CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING))
  440. new_state = CAN_STATE_ERROR_WARNING;
  441. if (d1 & (CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE))
  442. new_state = CAN_STATE_ERROR_PASSIVE;
  443. }
  444. /* protocol error, details in data[2] */
  445. if (canid & CAN_ERR_PROT) {
  446. u8 d2 = m->msg.can_msg.data[2];
  447. if (d2 & CAN_ERR_PROT_TX)
  448. net_stats->tx_errors++;
  449. else
  450. net_stats->rx_errors++;
  451. }
  452. /* no state change - we are done */
  453. if (up->can.state == new_state)
  454. return false;
  455. /* we switched into a better state */
  456. if (up->can.state > new_state) {
  457. up->can.state = new_state;
  458. return true;
  459. }
  460. /* we switched into a worse state */
  461. up->can.state = new_state;
  462. switch (new_state) {
  463. case CAN_STATE_BUS_OFF:
  464. can_stats->bus_off++;
  465. can_bus_off(up->netdev);
  466. break;
  467. case CAN_STATE_ERROR_PASSIVE:
  468. can_stats->error_passive++;
  469. break;
  470. case CAN_STATE_ERROR_WARNING:
  471. can_stats->error_warning++;
  472. break;
  473. default:
  474. break;
  475. }
  476. return true;
  477. }
  478. /* Callback on reception of a can frame via the IN endpoint
  479. *
  480. * This function allocates an skb and transferres it to the Linux
  481. * network stack
  482. */
  483. static void ucan_rx_can_msg(struct ucan_priv *up, struct ucan_message_in *m)
  484. {
  485. int len;
  486. canid_t canid;
  487. struct can_frame *cf;
  488. struct sk_buff *skb;
  489. struct net_device_stats *stats = &up->netdev->stats;
  490. /* get the contents of the length field */
  491. len = le16_to_cpu(m->len);
  492. /* check sanity */
  493. if (len < UCAN_IN_HDR_SIZE + sizeof(m->msg.can_msg.id)) {
  494. netdev_warn(up->netdev, "invalid input message len: %d\n", len);
  495. return;
  496. }
  497. /* handle error frames */
  498. canid = le32_to_cpu(m->msg.can_msg.id);
  499. if (canid & CAN_ERR_FLAG) {
  500. bool busstate_changed = ucan_handle_error_frame(up, m, canid);
  501. /* if berr-reporting is off only state changes get through */
  502. if (!(up->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
  503. !busstate_changed)
  504. return;
  505. } else {
  506. canid_t canid_mask;
  507. /* compute the mask for canid */
  508. canid_mask = CAN_RTR_FLAG;
  509. if (canid & CAN_EFF_FLAG)
  510. canid_mask |= CAN_EFF_MASK | CAN_EFF_FLAG;
  511. else
  512. canid_mask |= CAN_SFF_MASK;
  513. if (canid & ~canid_mask)
  514. netdev_warn(up->netdev,
  515. "unexpected bits set (canid %x, mask %x)",
  516. canid, canid_mask);
  517. canid &= canid_mask;
  518. }
  519. /* allocate skb */
  520. skb = alloc_can_skb(up->netdev, &cf);
  521. if (!skb)
  522. return;
  523. /* fill the can frame */
  524. cf->can_id = canid;
  525. /* compute DLC taking RTR_FLAG into account */
  526. cf->can_dlc = ucan_get_can_dlc(&m->msg.can_msg, len);
  527. /* copy the payload of non RTR frames */
  528. if (!(cf->can_id & CAN_RTR_FLAG) || (cf->can_id & CAN_ERR_FLAG))
  529. memcpy(cf->data, m->msg.can_msg.data, cf->can_dlc);
  530. /* don't count error frames as real packets */
  531. stats->rx_packets++;
  532. stats->rx_bytes += cf->can_dlc;
  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, dlc;
  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. dlc = READ_ONCE(context->dlc);
  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 += dlc;
  574. can_get_echo_skb(up->netdev, echo_index);
  575. } else {
  576. up->netdev->stats.tx_dropped++;
  577. can_free_echo_skb(up->netdev, echo_index);
  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 resumbmitting 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_KERNEL);
  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);
  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->can_dlc;
  918. } else {
  919. mlen = UCAN_OUT_HDR_SIZE +
  920. sizeof(m->msg.can_msg.id) + cf->can_dlc;
  921. memcpy(m->msg.can_msg.data, cf->data, cf->can_dlc);
  922. }
  923. m->len = cpu_to_le16(mlen);
  924. context->dlc = cf->can_dlc;
  925. m->subtype = echo_index;
  926. /* build the urb */
  927. usb_fill_bulk_urb(urb, up->udev,
  928. usb_sndbulkpipe(up->udev,
  929. up->out_ep_addr),
  930. m, mlen, ucan_write_bulk_callback, context);
  931. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  932. return urb;
  933. }
  934. static void ucan_clean_up_tx_urb(struct ucan_priv *up, struct urb *urb)
  935. {
  936. usb_free_coherent(up->udev, sizeof(struct ucan_message_out),
  937. urb->transfer_buffer, urb->transfer_dma);
  938. usb_free_urb(urb);
  939. }
  940. /* callback when Linux needs to send a can frame */
  941. static netdev_tx_t ucan_start_xmit(struct sk_buff *skb,
  942. struct net_device *netdev)
  943. {
  944. unsigned long flags;
  945. int ret;
  946. u8 echo_index;
  947. struct urb *urb;
  948. struct ucan_urb_context *context;
  949. struct ucan_priv *up = netdev_priv(netdev);
  950. struct can_frame *cf = (struct can_frame *)skb->data;
  951. /* check skb */
  952. if (can_dropped_invalid_skb(netdev, skb))
  953. return NETDEV_TX_OK;
  954. /* allocate a context and slow down tx path, if fifo state is low */
  955. context = ucan_alloc_context(up);
  956. echo_index = context - up->context_array;
  957. if (WARN_ON_ONCE(!context))
  958. return NETDEV_TX_BUSY;
  959. /* prepare urb for transmission */
  960. urb = ucan_prepare_tx_urb(up, context, cf, echo_index);
  961. if (!urb)
  962. goto drop;
  963. /* put the skb on can loopback stack */
  964. spin_lock_irqsave(&up->echo_skb_lock, flags);
  965. can_put_echo_skb(skb, up->netdev, echo_index);
  966. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  967. /* transmit it */
  968. usb_anchor_urb(urb, &up->tx_urbs);
  969. ret = usb_submit_urb(urb, GFP_ATOMIC);
  970. /* cleanup urb */
  971. if (ret) {
  972. /* on error, clean up */
  973. usb_unanchor_urb(urb);
  974. ucan_clean_up_tx_urb(up, urb);
  975. if (!ucan_release_context(up, context))
  976. netdev_err(up->netdev,
  977. "xmit err: failed to release context\n");
  978. /* remove the skb from the echo stack - this also
  979. * frees the skb
  980. */
  981. spin_lock_irqsave(&up->echo_skb_lock, flags);
  982. can_free_echo_skb(up->netdev, echo_index);
  983. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  984. if (ret == -ENODEV) {
  985. netif_device_detach(up->netdev);
  986. } else {
  987. netdev_warn(up->netdev,
  988. "xmit err: failed to submit urb %d\n",
  989. ret);
  990. up->netdev->stats.tx_dropped++;
  991. }
  992. return NETDEV_TX_OK;
  993. }
  994. netif_trans_update(netdev);
  995. /* release ref, as we do not need the urb anymore */
  996. usb_free_urb(urb);
  997. return NETDEV_TX_OK;
  998. drop:
  999. if (!ucan_release_context(up, context))
  1000. netdev_err(up->netdev,
  1001. "xmit drop: failed to release context\n");
  1002. dev_kfree_skb(skb);
  1003. up->netdev->stats.tx_dropped++;
  1004. return NETDEV_TX_OK;
  1005. }
  1006. /* Device goes down
  1007. *
  1008. * Clean up used resources
  1009. */
  1010. static int ucan_close(struct net_device *netdev)
  1011. {
  1012. int ret;
  1013. struct ucan_priv *up = netdev_priv(netdev);
  1014. up->can.state = CAN_STATE_STOPPED;
  1015. /* stop sending data */
  1016. usb_kill_anchored_urbs(&up->tx_urbs);
  1017. /* stop receiving data */
  1018. usb_kill_anchored_urbs(&up->rx_urbs);
  1019. /* stop and reset can device */
  1020. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
  1021. if (ret < 0)
  1022. netdev_err(up->netdev,
  1023. "could not stop device, code: %d\n",
  1024. ret);
  1025. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  1026. if (ret < 0)
  1027. netdev_err(up->netdev,
  1028. "could not reset device, code: %d\n",
  1029. ret);
  1030. netif_stop_queue(netdev);
  1031. ucan_release_context_array(up);
  1032. close_candev(up->netdev);
  1033. return 0;
  1034. }
  1035. /* CAN driver callbacks */
  1036. static const struct net_device_ops ucan_netdev_ops = {
  1037. .ndo_open = ucan_open,
  1038. .ndo_stop = ucan_close,
  1039. .ndo_start_xmit = ucan_start_xmit,
  1040. .ndo_change_mtu = can_change_mtu,
  1041. };
  1042. /* Request to set bittiming
  1043. *
  1044. * This function generates an USB set bittiming message and transmits
  1045. * it to the device
  1046. */
  1047. static int ucan_set_bittiming(struct net_device *netdev)
  1048. {
  1049. int ret;
  1050. struct ucan_priv *up = netdev_priv(netdev);
  1051. struct ucan_ctl_cmd_set_bittiming *cmd_set_bittiming;
  1052. cmd_set_bittiming = &up->ctl_msg_buffer->cmd_set_bittiming;
  1053. cmd_set_bittiming->tq = cpu_to_le32(up->can.bittiming.tq);
  1054. cmd_set_bittiming->brp = cpu_to_le16(up->can.bittiming.brp);
  1055. cmd_set_bittiming->sample_point =
  1056. cpu_to_le16(up->can.bittiming.sample_point);
  1057. cmd_set_bittiming->prop_seg = up->can.bittiming.prop_seg;
  1058. cmd_set_bittiming->phase_seg1 = up->can.bittiming.phase_seg1;
  1059. cmd_set_bittiming->phase_seg2 = up->can.bittiming.phase_seg2;
  1060. cmd_set_bittiming->sjw = up->can.bittiming.sjw;
  1061. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0,
  1062. sizeof(*cmd_set_bittiming));
  1063. return (ret < 0) ? ret : 0;
  1064. }
  1065. /* Restart the device to get it out of BUS-OFF state.
  1066. * Called when the user runs "ip link set can1 type can restart".
  1067. */
  1068. static int ucan_set_mode(struct net_device *netdev, enum can_mode mode)
  1069. {
  1070. int ret;
  1071. unsigned long flags;
  1072. struct ucan_priv *up = netdev_priv(netdev);
  1073. switch (mode) {
  1074. case CAN_MODE_START:
  1075. netdev_dbg(up->netdev, "restarting device\n");
  1076. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0);
  1077. up->can.state = CAN_STATE_ERROR_ACTIVE;
  1078. /* check if queue can be restarted,
  1079. * up->available_tx_urbs must be protected by the
  1080. * lock
  1081. */
  1082. spin_lock_irqsave(&up->context_lock, flags);
  1083. if (up->available_tx_urbs > 0)
  1084. netif_wake_queue(up->netdev);
  1085. spin_unlock_irqrestore(&up->context_lock, flags);
  1086. return ret;
  1087. default:
  1088. return -EOPNOTSUPP;
  1089. }
  1090. }
  1091. /* Probe the device, reset it and gather general device information */
  1092. static int ucan_probe(struct usb_interface *intf,
  1093. const struct usb_device_id *id)
  1094. {
  1095. int ret;
  1096. int i;
  1097. u32 protocol_version;
  1098. struct usb_device *udev;
  1099. struct net_device *netdev;
  1100. struct usb_host_interface *iface_desc;
  1101. struct ucan_priv *up;
  1102. struct usb_endpoint_descriptor *ep;
  1103. u16 in_ep_size;
  1104. u16 out_ep_size;
  1105. u8 in_ep_addr;
  1106. u8 out_ep_addr;
  1107. union ucan_ctl_payload *ctl_msg_buffer;
  1108. char firmware_str[sizeof(union ucan_ctl_payload) + 1];
  1109. udev = interface_to_usbdev(intf);
  1110. /* Stage 1 - Interface Parsing
  1111. * ---------------------------
  1112. *
  1113. * Identifie the device USB interface descriptor and its
  1114. * endpoints. Probing is aborted on errors.
  1115. */
  1116. /* check if the interface is sane */
  1117. iface_desc = intf->cur_altsetting;
  1118. if (!iface_desc)
  1119. return -ENODEV;
  1120. dev_info(&udev->dev,
  1121. "%s: probing device on interface #%d\n",
  1122. UCAN_DRIVER_NAME,
  1123. iface_desc->desc.bInterfaceNumber);
  1124. /* interface sanity check */
  1125. if (iface_desc->desc.bNumEndpoints != 2) {
  1126. dev_err(&udev->dev,
  1127. "%s: invalid EP count (%d)",
  1128. UCAN_DRIVER_NAME, iface_desc->desc.bNumEndpoints);
  1129. goto err_firmware_needs_update;
  1130. }
  1131. /* check interface endpoints */
  1132. in_ep_addr = 0;
  1133. out_ep_addr = 0;
  1134. in_ep_size = 0;
  1135. out_ep_size = 0;
  1136. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  1137. ep = &iface_desc->endpoint[i].desc;
  1138. if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != 0) &&
  1139. ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  1140. USB_ENDPOINT_XFER_BULK)) {
  1141. /* In Endpoint */
  1142. in_ep_addr = ep->bEndpointAddress;
  1143. in_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
  1144. in_ep_size = le16_to_cpu(ep->wMaxPacketSize);
  1145. } else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
  1146. 0) &&
  1147. ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  1148. USB_ENDPOINT_XFER_BULK)) {
  1149. /* Out Endpoint */
  1150. out_ep_addr = ep->bEndpointAddress;
  1151. out_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
  1152. out_ep_size = le16_to_cpu(ep->wMaxPacketSize);
  1153. }
  1154. }
  1155. /* check if interface is sane */
  1156. if (!in_ep_addr || !out_ep_addr) {
  1157. dev_err(&udev->dev, "%s: invalid endpoint configuration\n",
  1158. UCAN_DRIVER_NAME);
  1159. goto err_firmware_needs_update;
  1160. }
  1161. if (in_ep_size < sizeof(struct ucan_message_in)) {
  1162. dev_err(&udev->dev, "%s: invalid in_ep MaxPacketSize\n",
  1163. UCAN_DRIVER_NAME);
  1164. goto err_firmware_needs_update;
  1165. }
  1166. if (out_ep_size < sizeof(struct ucan_message_out)) {
  1167. dev_err(&udev->dev, "%s: invalid out_ep MaxPacketSize\n",
  1168. UCAN_DRIVER_NAME);
  1169. goto err_firmware_needs_update;
  1170. }
  1171. /* Stage 2 - Device Identification
  1172. * -------------------------------
  1173. *
  1174. * The device interface seems to be a ucan device. Do further
  1175. * compatibility checks. On error probing is aborted, on
  1176. * success this stage leaves the ctl_msg_buffer with the
  1177. * reported contents of a GET_INFO command (supported
  1178. * bittimings, tx_fifo depth). This information is used in
  1179. * Stage 3 for the final driver initialisation.
  1180. */
  1181. /* Prepare Memory for control transferes */
  1182. ctl_msg_buffer = devm_kzalloc(&udev->dev,
  1183. sizeof(union ucan_ctl_payload),
  1184. GFP_KERNEL);
  1185. if (!ctl_msg_buffer) {
  1186. dev_err(&udev->dev,
  1187. "%s: failed to allocate control pipe memory\n",
  1188. UCAN_DRIVER_NAME);
  1189. return -ENOMEM;
  1190. }
  1191. /* get protocol version
  1192. *
  1193. * note: ucan_ctrl_command_* wrappers cannot be used yet
  1194. * because `up` is initialised in Stage 3
  1195. */
  1196. ret = usb_control_msg(udev,
  1197. usb_rcvctrlpipe(udev, 0),
  1198. UCAN_COMMAND_GET,
  1199. USB_DIR_IN | USB_TYPE_VENDOR |
  1200. USB_RECIP_INTERFACE,
  1201. UCAN_COMMAND_GET_PROTOCOL_VERSION,
  1202. iface_desc->desc.bInterfaceNumber,
  1203. ctl_msg_buffer,
  1204. sizeof(union ucan_ctl_payload),
  1205. UCAN_USB_CTL_PIPE_TIMEOUT);
  1206. /* older firmware version do not support this command - those
  1207. * are not supported by this drive
  1208. */
  1209. if (ret != 4) {
  1210. dev_err(&udev->dev,
  1211. "%s: could not read protocol version, ret=%d\n",
  1212. UCAN_DRIVER_NAME, ret);
  1213. if (ret >= 0)
  1214. ret = -EINVAL;
  1215. goto err_firmware_needs_update;
  1216. }
  1217. /* this driver currently supports protocol version 3 only */
  1218. protocol_version =
  1219. le32_to_cpu(ctl_msg_buffer->cmd_get_protocol_version.version);
  1220. if (protocol_version < UCAN_PROTOCOL_VERSION_MIN ||
  1221. protocol_version > UCAN_PROTOCOL_VERSION_MAX) {
  1222. dev_err(&udev->dev,
  1223. "%s: device protocol version %d is not supported\n",
  1224. UCAN_DRIVER_NAME, protocol_version);
  1225. goto err_firmware_needs_update;
  1226. }
  1227. /* request the device information and store it in ctl_msg_buffer
  1228. *
  1229. * note: ucan_ctrl_command_* wrappers connot be used yet
  1230. * because `up` is initialised in Stage 3
  1231. */
  1232. ret = usb_control_msg(udev,
  1233. usb_rcvctrlpipe(udev, 0),
  1234. UCAN_COMMAND_GET,
  1235. USB_DIR_IN | USB_TYPE_VENDOR |
  1236. USB_RECIP_INTERFACE,
  1237. UCAN_COMMAND_GET_INFO,
  1238. iface_desc->desc.bInterfaceNumber,
  1239. ctl_msg_buffer,
  1240. sizeof(ctl_msg_buffer->cmd_get_device_info),
  1241. UCAN_USB_CTL_PIPE_TIMEOUT);
  1242. if (ret < 0) {
  1243. dev_err(&udev->dev, "%s: failed to retrieve device info\n",
  1244. UCAN_DRIVER_NAME);
  1245. goto err_firmware_needs_update;
  1246. }
  1247. if (ret < sizeof(ctl_msg_buffer->cmd_get_device_info)) {
  1248. dev_err(&udev->dev, "%s: device reported invalid device info\n",
  1249. UCAN_DRIVER_NAME);
  1250. goto err_firmware_needs_update;
  1251. }
  1252. if (ctl_msg_buffer->cmd_get_device_info.tx_fifo == 0) {
  1253. dev_err(&udev->dev,
  1254. "%s: device reported invalid tx-fifo size\n",
  1255. UCAN_DRIVER_NAME);
  1256. goto err_firmware_needs_update;
  1257. }
  1258. /* Stage 3 - Driver Initialisation
  1259. * -------------------------------
  1260. *
  1261. * Register device to Linux, prepare private structures and
  1262. * reset the device.
  1263. */
  1264. /* allocate driver resources */
  1265. netdev = alloc_candev(sizeof(struct ucan_priv),
  1266. ctl_msg_buffer->cmd_get_device_info.tx_fifo);
  1267. if (!netdev) {
  1268. dev_err(&udev->dev,
  1269. "%s: cannot allocate candev\n", UCAN_DRIVER_NAME);
  1270. return -ENOMEM;
  1271. }
  1272. up = netdev_priv(netdev);
  1273. /* initialze data */
  1274. up->udev = udev;
  1275. up->intf = intf;
  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. usb_set_intfdata(intf, up);
  1292. SET_NETDEV_DEV(netdev, &intf->dev);
  1293. /* parse device information
  1294. * the data retrieved in Stage 2 is still available in
  1295. * up->ctl_msg_buffer
  1296. */
  1297. ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info);
  1298. /* just print some device information - if available */
  1299. ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0,
  1300. sizeof(union ucan_ctl_payload));
  1301. if (ret > 0) {
  1302. /* copy string while ensuring zero terminiation */
  1303. strncpy(firmware_str, up->ctl_msg_buffer->raw,
  1304. sizeof(union ucan_ctl_payload));
  1305. firmware_str[sizeof(union ucan_ctl_payload)] = '\0';
  1306. } else {
  1307. strcpy(firmware_str, "unknown");
  1308. }
  1309. /* device is compatible, reset it */
  1310. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  1311. if (ret < 0)
  1312. goto err_free_candev;
  1313. init_usb_anchor(&up->rx_urbs);
  1314. init_usb_anchor(&up->tx_urbs);
  1315. up->can.state = CAN_STATE_STOPPED;
  1316. /* register the device */
  1317. ret = register_candev(netdev);
  1318. if (ret)
  1319. goto err_free_candev;
  1320. /* initialisation complete, log device info */
  1321. netdev_info(up->netdev, "registered device\n");
  1322. netdev_info(up->netdev, "firmware string: %s\n", firmware_str);
  1323. /* success */
  1324. return 0;
  1325. err_free_candev:
  1326. free_candev(netdev);
  1327. return ret;
  1328. err_firmware_needs_update:
  1329. dev_err(&udev->dev,
  1330. "%s: probe failed; try to update the device firmware\n",
  1331. UCAN_DRIVER_NAME);
  1332. return -ENODEV;
  1333. }
  1334. /* disconnect the device */
  1335. static void ucan_disconnect(struct usb_interface *intf)
  1336. {
  1337. struct usb_device *udev;
  1338. struct ucan_priv *up = usb_get_intfdata(intf);
  1339. udev = interface_to_usbdev(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 <martin.elshuber@theobroma-systems.com>");
  1364. MODULE_AUTHOR("Jakob Unterwurzacher <jakob.unterwurzacher@theobroma-systems.com>");
  1365. MODULE_DESCRIPTION("Driver for Theobroma Systems UCAN devices");