ucan.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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. #define UCAN_DRIVER_NAME "ucan"
  36. #define UCAN_MAX_RX_URBS 8
  37. /* the CAN controller needs a while to enable/disable the bus */
  38. #define UCAN_USB_CTL_PIPE_TIMEOUT 1000
  39. /* this driver currently supports protocol version 3 only */
  40. #define UCAN_PROTOCOL_VERSION_MIN 3
  41. #define UCAN_PROTOCOL_VERSION_MAX 3
  42. /* UCAN Message Definitions
  43. * ------------------------
  44. *
  45. * ucan_message_out_t and ucan_message_in_t define the messages
  46. * transmitted on the OUT and IN endpoint.
  47. *
  48. * Multibyte fields are transmitted with little endianness
  49. *
  50. * INTR Endpoint: a single uint32_t storing the current space in the fifo
  51. *
  52. * OUT Endpoint: single message of type ucan_message_out_t is
  53. * transmitted on the out endpoint
  54. *
  55. * IN Endpoint: multiple messages ucan_message_in_t concateted in
  56. * the following way:
  57. *
  58. * m[n].len <=> the length if message n(including the header in bytes)
  59. * m[n] is is aligned to a 4 byte boundary, hence
  60. * offset(m[0]) := 0;
  61. * offset(m[n+1]) := offset(m[n]) + (m[n].len + 3) & 3
  62. *
  63. * this implies that
  64. * offset(m[n]) % 4 <=> 0
  65. */
  66. /* Device Global Commands */
  67. enum {
  68. UCAN_DEVICE_GET_FW_STRING = 0,
  69. };
  70. /* UCAN Commands */
  71. enum {
  72. /* start the can transceiver - val defines the operation mode */
  73. UCAN_COMMAND_START = 0,
  74. /* cancel pending transmissions and stop the can transceiver */
  75. UCAN_COMMAND_STOP = 1,
  76. /* send can transceiver into low-power sleep mode */
  77. UCAN_COMMAND_SLEEP = 2,
  78. /* wake up can transceiver from low-power sleep mode */
  79. UCAN_COMMAND_WAKEUP = 3,
  80. /* reset the can transceiver */
  81. UCAN_COMMAND_RESET = 4,
  82. /* get piece of info from the can transceiver - subcmd defines what
  83. * piece
  84. */
  85. UCAN_COMMAND_GET = 5,
  86. /* clear or disable hardware filter - subcmd defines which of the two */
  87. UCAN_COMMAND_FILTER = 6,
  88. /* Setup bittiming */
  89. UCAN_COMMAND_SET_BITTIMING = 7,
  90. /* recover from bus-off state */
  91. UCAN_COMMAND_RESTART = 8,
  92. };
  93. /* UCAN_COMMAND_START and UCAN_COMMAND_GET_INFO operation modes (bitmap).
  94. * Undefined bits must be set to 0.
  95. */
  96. enum {
  97. UCAN_MODE_LOOPBACK = BIT(0),
  98. UCAN_MODE_SILENT = BIT(1),
  99. UCAN_MODE_3_SAMPLES = BIT(2),
  100. UCAN_MODE_ONE_SHOT = BIT(3),
  101. UCAN_MODE_BERR_REPORT = BIT(4),
  102. };
  103. /* UCAN_COMMAND_GET subcommands */
  104. enum {
  105. UCAN_COMMAND_GET_INFO = 0,
  106. UCAN_COMMAND_GET_PROTOCOL_VERSION = 1,
  107. };
  108. /* UCAN_COMMAND_FILTER subcommands */
  109. enum {
  110. UCAN_FILTER_CLEAR = 0,
  111. UCAN_FILTER_DISABLE = 1,
  112. UCAN_FILTER_ENABLE = 2,
  113. };
  114. /* OUT endpoint message types */
  115. enum {
  116. UCAN_OUT_TX = 2, /* transmit a CAN frame */
  117. };
  118. /* IN endpoint message types */
  119. enum {
  120. UCAN_IN_TX_COMPLETE = 1, /* CAN frame transmission completed */
  121. UCAN_IN_RX = 2, /* CAN frame received */
  122. };
  123. struct ucan_ctl_cmd_start {
  124. __le16 mode; /* OR-ing any of UCAN_MODE_* */
  125. } __packed;
  126. struct ucan_ctl_cmd_set_bittiming {
  127. __le32 tq; /* Time quanta (TQ) in nanoseconds */
  128. __le16 brp; /* TQ Prescaler */
  129. __le16 sample_point; /* Samplepoint on tenth percent */
  130. u8 prop_seg; /* Propagation segment in TQs */
  131. u8 phase_seg1; /* Phase buffer segment 1 in TQs */
  132. u8 phase_seg2; /* Phase buffer segment 2 in TQs */
  133. u8 sjw; /* Synchronisation jump width in TQs */
  134. } __packed;
  135. struct ucan_ctl_cmd_device_info {
  136. __le32 freq; /* Clock Frequency for tq generation */
  137. u8 tx_fifo; /* Size of the transmission fifo */
  138. u8 sjw_max; /* can_bittiming fields... */
  139. u8 tseg1_min;
  140. u8 tseg1_max;
  141. u8 tseg2_min;
  142. u8 tseg2_max;
  143. __le16 brp_inc;
  144. __le32 brp_min;
  145. __le32 brp_max; /* ...can_bittiming fields */
  146. __le16 ctrlmodes; /* supported control modes */
  147. __le16 hwfilter; /* Number of HW filter banks */
  148. __le16 rxmboxes; /* Number of receive Mailboxes */
  149. } __packed;
  150. struct ucan_ctl_cmd_get_protocol_version {
  151. __le32 version;
  152. } __packed;
  153. union ucan_ctl_payload {
  154. /* Setup Bittiming
  155. * bmRequest == UCAN_COMMAND_START
  156. */
  157. struct ucan_ctl_cmd_start cmd_start;
  158. /* Setup Bittiming
  159. * bmRequest == UCAN_COMMAND_SET_BITTIMING
  160. */
  161. struct ucan_ctl_cmd_set_bittiming cmd_set_bittiming;
  162. /* Get Device Information
  163. * bmRequest == UCAN_COMMAND_GET; wValue = UCAN_COMMAND_GET_INFO
  164. */
  165. struct ucan_ctl_cmd_device_info cmd_get_device_info;
  166. /* Get Protocol Version
  167. * bmRequest == UCAN_COMMAND_GET;
  168. * wValue = UCAN_COMMAND_GET_PROTOCOL_VERSION
  169. */
  170. struct ucan_ctl_cmd_get_protocol_version cmd_get_protocol_version;
  171. u8 raw[128];
  172. } __packed;
  173. enum {
  174. UCAN_TX_COMPLETE_SUCCESS = BIT(0),
  175. };
  176. /* Transmission Complete within ucan_message_in */
  177. struct ucan_tx_complete_entry_t {
  178. u8 echo_index;
  179. u8 flags;
  180. } __packed __aligned(0x2);
  181. /* CAN Data message format within ucan_message_in/out */
  182. struct ucan_can_msg {
  183. /* note DLC is computed by
  184. * msg.len - sizeof (msg.len)
  185. * - sizeof (msg.type)
  186. * - sizeof (msg.can_msg.id)
  187. */
  188. __le32 id;
  189. union {
  190. u8 data[CAN_MAX_DLEN]; /* Data of CAN frames */
  191. u8 dlc; /* RTR dlc */
  192. };
  193. } __packed;
  194. /* OUT Endpoint, outbound messages */
  195. struct ucan_message_out {
  196. __le16 len; /* Length of the content include header */
  197. u8 type; /* UCAN_OUT_TX and friends */
  198. u8 subtype; /* command sub type */
  199. union {
  200. /* Transmit CAN frame
  201. * (type == UCAN_TX) && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
  202. * subtype stores the echo id
  203. */
  204. struct ucan_can_msg can_msg;
  205. } msg;
  206. } __packed __aligned(0x4);
  207. /* IN Endpoint, inbound messages */
  208. struct ucan_message_in {
  209. __le16 len; /* Length of the content include header */
  210. u8 type; /* UCAN_IN_RX and friends */
  211. u8 subtype; /* command sub type */
  212. union {
  213. /* CAN Frame received
  214. * (type == UCAN_IN_RX)
  215. * && ((msg.can_msg.id & CAN_RTR_FLAG) == 0)
  216. */
  217. struct ucan_can_msg can_msg;
  218. /* CAN transmission complete
  219. * (type == UCAN_IN_TX_COMPLETE)
  220. */
  221. struct ucan_tx_complete_entry_t can_tx_complete_msg[0];
  222. } __aligned(0x4) msg;
  223. } __packed;
  224. /* Macros to calculate message lengths */
  225. #define UCAN_OUT_HDR_SIZE offsetof(struct ucan_message_out, msg)
  226. #define UCAN_IN_HDR_SIZE offsetof(struct ucan_message_in, msg)
  227. #define UCAN_IN_LEN(member) (UCAN_OUT_HDR_SIZE + sizeof(member))
  228. struct ucan_priv;
  229. /* Context Information for transmission URBs */
  230. struct ucan_urb_context {
  231. struct ucan_priv *up;
  232. u8 dlc;
  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_get_can_dlc(struct ucan_can_msg *msg, u16 len)
  268. {
  269. if (le32_to_cpu(msg->id) & CAN_RTR_FLAG)
  270. return get_can_dlc(msg->dlc);
  271. else
  272. return get_can_dlc(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->can_dlc = ucan_get_can_dlc(&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->can_dlc);
  527. /* don't count error frames as real packets */
  528. stats->rx_packets++;
  529. stats->rx_bytes += cf->can_dlc;
  530. /* pass it to Linux */
  531. netif_rx(skb);
  532. }
  533. /* callback indicating completed transmission */
  534. static void ucan_tx_complete_msg(struct ucan_priv *up,
  535. struct ucan_message_in *m)
  536. {
  537. unsigned long flags;
  538. u16 count, i;
  539. u8 echo_index, dlc;
  540. u16 len = le16_to_cpu(m->len);
  541. struct ucan_urb_context *context;
  542. if (len < UCAN_IN_HDR_SIZE || (len % 2 != 0)) {
  543. netdev_err(up->netdev, "invalid tx complete length\n");
  544. return;
  545. }
  546. count = (len - UCAN_IN_HDR_SIZE) / 2;
  547. for (i = 0; i < count; i++) {
  548. /* we did not submit such echo ids */
  549. echo_index = m->msg.can_tx_complete_msg[i].echo_index;
  550. if (echo_index >= up->device_info.tx_fifo) {
  551. up->netdev->stats.tx_errors++;
  552. netdev_err(up->netdev,
  553. "invalid echo_index %d received\n",
  554. echo_index);
  555. continue;
  556. }
  557. /* gather information from the context */
  558. context = &up->context_array[echo_index];
  559. dlc = READ_ONCE(context->dlc);
  560. /* Release context and restart queue if necessary.
  561. * Also check if the context was allocated
  562. */
  563. if (!ucan_release_context(up, context))
  564. continue;
  565. spin_lock_irqsave(&up->echo_skb_lock, flags);
  566. if (m->msg.can_tx_complete_msg[i].flags &
  567. UCAN_TX_COMPLETE_SUCCESS) {
  568. /* update statistics */
  569. up->netdev->stats.tx_packets++;
  570. up->netdev->stats.tx_bytes += dlc;
  571. can_get_echo_skb(up->netdev, echo_index);
  572. } else {
  573. up->netdev->stats.tx_dropped++;
  574. can_free_echo_skb(up->netdev, echo_index);
  575. }
  576. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  577. }
  578. }
  579. /* callback on reception of a USB message */
  580. static void ucan_read_bulk_callback(struct urb *urb)
  581. {
  582. int ret;
  583. int pos;
  584. struct ucan_priv *up = urb->context;
  585. struct net_device *netdev = up->netdev;
  586. struct ucan_message_in *m;
  587. /* the device is not up and the driver should not receive any
  588. * data on the bulk in pipe
  589. */
  590. if (WARN_ON(!up->context_array)) {
  591. usb_free_coherent(up->udev,
  592. up->in_ep_size,
  593. urb->transfer_buffer,
  594. urb->transfer_dma);
  595. return;
  596. }
  597. /* check URB status */
  598. switch (urb->status) {
  599. case 0:
  600. break;
  601. case -ENOENT:
  602. case -EPIPE:
  603. case -EPROTO:
  604. case -ESHUTDOWN:
  605. case -ETIME:
  606. /* urb is not resubmitted -> free dma data */
  607. usb_free_coherent(up->udev,
  608. up->in_ep_size,
  609. urb->transfer_buffer,
  610. urb->transfer_dma);
  611. netdev_dbg(up->netdev, "not resubmitting urb; status: %d\n",
  612. urb->status);
  613. return;
  614. default:
  615. goto resubmit;
  616. }
  617. /* sanity check */
  618. if (!netif_device_present(netdev))
  619. return;
  620. /* iterate over input */
  621. pos = 0;
  622. while (pos < urb->actual_length) {
  623. int len;
  624. /* check sanity (length of header) */
  625. if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) {
  626. netdev_warn(up->netdev,
  627. "invalid message (short; no hdr; l:%d)\n",
  628. urb->actual_length);
  629. goto resubmit;
  630. }
  631. /* setup the message address */
  632. m = (struct ucan_message_in *)
  633. ((u8 *)urb->transfer_buffer + pos);
  634. len = le16_to_cpu(m->len);
  635. /* check sanity (length of content) */
  636. if (urb->actual_length - pos < len) {
  637. netdev_warn(up->netdev,
  638. "invalid message (short; no data; l:%d)\n",
  639. urb->actual_length);
  640. print_hex_dump(KERN_WARNING,
  641. "raw data: ",
  642. DUMP_PREFIX_ADDRESS,
  643. 16,
  644. 1,
  645. urb->transfer_buffer,
  646. urb->actual_length,
  647. true);
  648. goto resubmit;
  649. }
  650. switch (m->type) {
  651. case UCAN_IN_RX:
  652. ucan_rx_can_msg(up, m);
  653. break;
  654. case UCAN_IN_TX_COMPLETE:
  655. ucan_tx_complete_msg(up, m);
  656. break;
  657. default:
  658. netdev_warn(up->netdev,
  659. "invalid message (type; t:%d)\n",
  660. m->type);
  661. break;
  662. }
  663. /* proceed to next message */
  664. pos += len;
  665. /* align to 4 byte boundary */
  666. pos = round_up(pos, 4);
  667. }
  668. resubmit:
  669. /* resubmit urb when done */
  670. usb_fill_bulk_urb(urb, up->udev,
  671. usb_rcvbulkpipe(up->udev,
  672. up->in_ep_addr),
  673. urb->transfer_buffer,
  674. up->in_ep_size,
  675. ucan_read_bulk_callback,
  676. up);
  677. usb_anchor_urb(urb, &up->rx_urbs);
  678. ret = usb_submit_urb(urb, GFP_ATOMIC);
  679. if (ret < 0) {
  680. netdev_err(up->netdev,
  681. "failed resubmitting read bulk urb: %d\n",
  682. ret);
  683. usb_unanchor_urb(urb);
  684. usb_free_coherent(up->udev,
  685. up->in_ep_size,
  686. urb->transfer_buffer,
  687. urb->transfer_dma);
  688. if (ret == -ENODEV)
  689. netif_device_detach(netdev);
  690. }
  691. }
  692. /* callback after transmission of a USB message */
  693. static void ucan_write_bulk_callback(struct urb *urb)
  694. {
  695. unsigned long flags;
  696. struct ucan_priv *up;
  697. struct ucan_urb_context *context = urb->context;
  698. /* get the urb context */
  699. if (WARN_ON_ONCE(!context))
  700. return;
  701. /* free up our allocated buffer */
  702. usb_free_coherent(urb->dev,
  703. sizeof(struct ucan_message_out),
  704. urb->transfer_buffer,
  705. urb->transfer_dma);
  706. up = context->up;
  707. if (WARN_ON_ONCE(!up))
  708. return;
  709. /* sanity check */
  710. if (!netif_device_present(up->netdev))
  711. return;
  712. /* transmission failed (USB - the device will not send a TX complete) */
  713. if (urb->status) {
  714. netdev_warn(up->netdev,
  715. "failed to transmit USB message to device: %d\n",
  716. urb->status);
  717. /* update counters an cleanup */
  718. spin_lock_irqsave(&up->echo_skb_lock, flags);
  719. can_free_echo_skb(up->netdev, context - up->context_array);
  720. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  721. up->netdev->stats.tx_dropped++;
  722. /* release context and restart the queue if necessary */
  723. if (!ucan_release_context(up, context))
  724. netdev_err(up->netdev,
  725. "urb failed, failed to release context\n");
  726. }
  727. }
  728. static void ucan_cleanup_rx_urbs(struct ucan_priv *up, struct urb **urbs)
  729. {
  730. int i;
  731. for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
  732. if (urbs[i]) {
  733. usb_unanchor_urb(urbs[i]);
  734. usb_free_coherent(up->udev,
  735. up->in_ep_size,
  736. urbs[i]->transfer_buffer,
  737. urbs[i]->transfer_dma);
  738. usb_free_urb(urbs[i]);
  739. }
  740. }
  741. memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
  742. }
  743. static int ucan_prepare_and_anchor_rx_urbs(struct ucan_priv *up,
  744. struct urb **urbs)
  745. {
  746. int i;
  747. memset(urbs, 0, sizeof(*urbs) * UCAN_MAX_RX_URBS);
  748. for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
  749. void *buf;
  750. urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
  751. if (!urbs[i])
  752. goto err;
  753. buf = usb_alloc_coherent(up->udev,
  754. up->in_ep_size,
  755. GFP_KERNEL, &urbs[i]->transfer_dma);
  756. if (!buf) {
  757. /* cleanup this urb */
  758. usb_free_urb(urbs[i]);
  759. urbs[i] = NULL;
  760. goto err;
  761. }
  762. usb_fill_bulk_urb(urbs[i], up->udev,
  763. usb_rcvbulkpipe(up->udev,
  764. up->in_ep_addr),
  765. buf,
  766. up->in_ep_size,
  767. ucan_read_bulk_callback,
  768. up);
  769. urbs[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  770. usb_anchor_urb(urbs[i], &up->rx_urbs);
  771. }
  772. return 0;
  773. err:
  774. /* cleanup other unsubmitted urbs */
  775. ucan_cleanup_rx_urbs(up, urbs);
  776. return -ENOMEM;
  777. }
  778. /* Submits rx urbs with the semantic: Either submit all, or cleanup
  779. * everything. I case of errors submitted urbs are killed and all urbs in
  780. * the array are freed. I case of no errors every entry in the urb
  781. * array is set to NULL.
  782. */
  783. static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs)
  784. {
  785. int i, ret;
  786. /* Iterate over all urbs to submit. On success remove the urb
  787. * from the list.
  788. */
  789. for (i = 0; i < UCAN_MAX_RX_URBS; i++) {
  790. ret = usb_submit_urb(urbs[i], GFP_KERNEL);
  791. if (ret) {
  792. netdev_err(up->netdev,
  793. "could not submit urb; code: %d\n",
  794. ret);
  795. goto err;
  796. }
  797. /* Anchor URB and drop reference, USB core will take
  798. * care of freeing it
  799. */
  800. usb_free_urb(urbs[i]);
  801. urbs[i] = NULL;
  802. }
  803. return 0;
  804. err:
  805. /* Cleanup unsubmitted urbs */
  806. ucan_cleanup_rx_urbs(up, urbs);
  807. /* Kill urbs that are already submitted */
  808. usb_kill_anchored_urbs(&up->rx_urbs);
  809. return ret;
  810. }
  811. /* Open the network device */
  812. static int ucan_open(struct net_device *netdev)
  813. {
  814. int ret, ret_cleanup;
  815. u16 ctrlmode;
  816. struct urb *urbs[UCAN_MAX_RX_URBS];
  817. struct ucan_priv *up = netdev_priv(netdev);
  818. ret = ucan_alloc_context_array(up);
  819. if (ret)
  820. return ret;
  821. /* Allocate and prepare IN URBS - allocated and anchored
  822. * urbs are stored in urbs[] for clean
  823. */
  824. ret = ucan_prepare_and_anchor_rx_urbs(up, urbs);
  825. if (ret)
  826. goto err_contexts;
  827. /* Check the control mode */
  828. ctrlmode = 0;
  829. if (up->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  830. ctrlmode |= UCAN_MODE_LOOPBACK;
  831. if (up->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  832. ctrlmode |= UCAN_MODE_SILENT;
  833. if (up->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  834. ctrlmode |= UCAN_MODE_3_SAMPLES;
  835. if (up->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  836. ctrlmode |= UCAN_MODE_ONE_SHOT;
  837. /* Enable this in any case - filtering is down within the
  838. * receive path
  839. */
  840. ctrlmode |= UCAN_MODE_BERR_REPORT;
  841. up->ctl_msg_buffer->cmd_start.mode = cpu_to_le16(ctrlmode);
  842. /* Driver is ready to receive data - start the USB device */
  843. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2);
  844. if (ret < 0) {
  845. netdev_err(up->netdev,
  846. "could not start device, code: %d\n",
  847. ret);
  848. goto err_reset;
  849. }
  850. /* Call CAN layer open */
  851. ret = open_candev(netdev);
  852. if (ret)
  853. goto err_stop;
  854. /* Driver is ready to receive data. Submit RX URBS */
  855. ret = ucan_submit_rx_urbs(up, urbs);
  856. if (ret)
  857. goto err_stop;
  858. up->can.state = CAN_STATE_ERROR_ACTIVE;
  859. /* Start the network queue */
  860. netif_start_queue(netdev);
  861. return 0;
  862. err_stop:
  863. /* The device have started already stop it */
  864. ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
  865. if (ret_cleanup < 0)
  866. netdev_err(up->netdev,
  867. "could not stop device, code: %d\n",
  868. ret_cleanup);
  869. err_reset:
  870. /* The device might have received data, reset it for
  871. * consistent state
  872. */
  873. ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  874. if (ret_cleanup < 0)
  875. netdev_err(up->netdev,
  876. "could not reset device, code: %d\n",
  877. ret_cleanup);
  878. /* clean up unsubmitted urbs */
  879. ucan_cleanup_rx_urbs(up, urbs);
  880. err_contexts:
  881. ucan_release_context_array(up);
  882. return ret;
  883. }
  884. static struct urb *ucan_prepare_tx_urb(struct ucan_priv *up,
  885. struct ucan_urb_context *context,
  886. struct can_frame *cf,
  887. u8 echo_index)
  888. {
  889. int mlen;
  890. struct urb *urb;
  891. struct ucan_message_out *m;
  892. /* create a URB, and a buffer for it, and copy the data to the URB */
  893. urb = usb_alloc_urb(0, GFP_ATOMIC);
  894. if (!urb) {
  895. netdev_err(up->netdev, "no memory left for URBs\n");
  896. return NULL;
  897. }
  898. m = usb_alloc_coherent(up->udev,
  899. sizeof(struct ucan_message_out),
  900. GFP_ATOMIC,
  901. &urb->transfer_dma);
  902. if (!m) {
  903. netdev_err(up->netdev, "no memory left for USB buffer\n");
  904. usb_free_urb(urb);
  905. return NULL;
  906. }
  907. /* build the USB message */
  908. m->type = UCAN_OUT_TX;
  909. m->msg.can_msg.id = cpu_to_le32(cf->can_id);
  910. if (cf->can_id & CAN_RTR_FLAG) {
  911. mlen = UCAN_OUT_HDR_SIZE +
  912. offsetof(struct ucan_can_msg, dlc) +
  913. sizeof(m->msg.can_msg.dlc);
  914. m->msg.can_msg.dlc = cf->can_dlc;
  915. } else {
  916. mlen = UCAN_OUT_HDR_SIZE +
  917. sizeof(m->msg.can_msg.id) + cf->can_dlc;
  918. memcpy(m->msg.can_msg.data, cf->data, cf->can_dlc);
  919. }
  920. m->len = cpu_to_le16(mlen);
  921. context->dlc = cf->can_dlc;
  922. m->subtype = echo_index;
  923. /* build the urb */
  924. usb_fill_bulk_urb(urb, up->udev,
  925. usb_sndbulkpipe(up->udev,
  926. up->out_ep_addr),
  927. m, mlen, ucan_write_bulk_callback, context);
  928. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  929. return urb;
  930. }
  931. static void ucan_clean_up_tx_urb(struct ucan_priv *up, struct urb *urb)
  932. {
  933. usb_free_coherent(up->udev, sizeof(struct ucan_message_out),
  934. urb->transfer_buffer, urb->transfer_dma);
  935. usb_free_urb(urb);
  936. }
  937. /* callback when Linux needs to send a can frame */
  938. static netdev_tx_t ucan_start_xmit(struct sk_buff *skb,
  939. struct net_device *netdev)
  940. {
  941. unsigned long flags;
  942. int ret;
  943. u8 echo_index;
  944. struct urb *urb;
  945. struct ucan_urb_context *context;
  946. struct ucan_priv *up = netdev_priv(netdev);
  947. struct can_frame *cf = (struct can_frame *)skb->data;
  948. /* check skb */
  949. if (can_dropped_invalid_skb(netdev, skb))
  950. return NETDEV_TX_OK;
  951. /* allocate a context and slow down tx path, if fifo state is low */
  952. context = ucan_alloc_context(up);
  953. echo_index = context - up->context_array;
  954. if (WARN_ON_ONCE(!context))
  955. return NETDEV_TX_BUSY;
  956. /* prepare urb for transmission */
  957. urb = ucan_prepare_tx_urb(up, context, cf, echo_index);
  958. if (!urb)
  959. goto drop;
  960. /* put the skb on can loopback stack */
  961. spin_lock_irqsave(&up->echo_skb_lock, flags);
  962. can_put_echo_skb(skb, up->netdev, echo_index);
  963. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  964. /* transmit it */
  965. usb_anchor_urb(urb, &up->tx_urbs);
  966. ret = usb_submit_urb(urb, GFP_ATOMIC);
  967. /* cleanup urb */
  968. if (ret) {
  969. /* on error, clean up */
  970. usb_unanchor_urb(urb);
  971. ucan_clean_up_tx_urb(up, urb);
  972. if (!ucan_release_context(up, context))
  973. netdev_err(up->netdev,
  974. "xmit err: failed to release context\n");
  975. /* remove the skb from the echo stack - this also
  976. * frees the skb
  977. */
  978. spin_lock_irqsave(&up->echo_skb_lock, flags);
  979. can_free_echo_skb(up->netdev, echo_index);
  980. spin_unlock_irqrestore(&up->echo_skb_lock, flags);
  981. if (ret == -ENODEV) {
  982. netif_device_detach(up->netdev);
  983. } else {
  984. netdev_warn(up->netdev,
  985. "xmit err: failed to submit urb %d\n",
  986. ret);
  987. up->netdev->stats.tx_dropped++;
  988. }
  989. return NETDEV_TX_OK;
  990. }
  991. netif_trans_update(netdev);
  992. /* release ref, as we do not need the urb anymore */
  993. usb_free_urb(urb);
  994. return NETDEV_TX_OK;
  995. drop:
  996. if (!ucan_release_context(up, context))
  997. netdev_err(up->netdev,
  998. "xmit drop: failed to release context\n");
  999. dev_kfree_skb(skb);
  1000. up->netdev->stats.tx_dropped++;
  1001. return NETDEV_TX_OK;
  1002. }
  1003. /* Device goes down
  1004. *
  1005. * Clean up used resources
  1006. */
  1007. static int ucan_close(struct net_device *netdev)
  1008. {
  1009. int ret;
  1010. struct ucan_priv *up = netdev_priv(netdev);
  1011. up->can.state = CAN_STATE_STOPPED;
  1012. /* stop sending data */
  1013. usb_kill_anchored_urbs(&up->tx_urbs);
  1014. /* stop receiving data */
  1015. usb_kill_anchored_urbs(&up->rx_urbs);
  1016. /* stop and reset can device */
  1017. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0);
  1018. if (ret < 0)
  1019. netdev_err(up->netdev,
  1020. "could not stop device, code: %d\n",
  1021. ret);
  1022. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  1023. if (ret < 0)
  1024. netdev_err(up->netdev,
  1025. "could not reset device, code: %d\n",
  1026. ret);
  1027. netif_stop_queue(netdev);
  1028. ucan_release_context_array(up);
  1029. close_candev(up->netdev);
  1030. return 0;
  1031. }
  1032. /* CAN driver callbacks */
  1033. static const struct net_device_ops ucan_netdev_ops = {
  1034. .ndo_open = ucan_open,
  1035. .ndo_stop = ucan_close,
  1036. .ndo_start_xmit = ucan_start_xmit,
  1037. .ndo_change_mtu = can_change_mtu,
  1038. };
  1039. /* Request to set bittiming
  1040. *
  1041. * This function generates an USB set bittiming message and transmits
  1042. * it to the device
  1043. */
  1044. static int ucan_set_bittiming(struct net_device *netdev)
  1045. {
  1046. int ret;
  1047. struct ucan_priv *up = netdev_priv(netdev);
  1048. struct ucan_ctl_cmd_set_bittiming *cmd_set_bittiming;
  1049. cmd_set_bittiming = &up->ctl_msg_buffer->cmd_set_bittiming;
  1050. cmd_set_bittiming->tq = cpu_to_le32(up->can.bittiming.tq);
  1051. cmd_set_bittiming->brp = cpu_to_le16(up->can.bittiming.brp);
  1052. cmd_set_bittiming->sample_point =
  1053. cpu_to_le16(up->can.bittiming.sample_point);
  1054. cmd_set_bittiming->prop_seg = up->can.bittiming.prop_seg;
  1055. cmd_set_bittiming->phase_seg1 = up->can.bittiming.phase_seg1;
  1056. cmd_set_bittiming->phase_seg2 = up->can.bittiming.phase_seg2;
  1057. cmd_set_bittiming->sjw = up->can.bittiming.sjw;
  1058. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0,
  1059. sizeof(*cmd_set_bittiming));
  1060. return (ret < 0) ? ret : 0;
  1061. }
  1062. /* Restart the device to get it out of BUS-OFF state.
  1063. * Called when the user runs "ip link set can1 type can restart".
  1064. */
  1065. static int ucan_set_mode(struct net_device *netdev, enum can_mode mode)
  1066. {
  1067. int ret;
  1068. unsigned long flags;
  1069. struct ucan_priv *up = netdev_priv(netdev);
  1070. switch (mode) {
  1071. case CAN_MODE_START:
  1072. netdev_dbg(up->netdev, "restarting device\n");
  1073. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0);
  1074. up->can.state = CAN_STATE_ERROR_ACTIVE;
  1075. /* check if queue can be restarted,
  1076. * up->available_tx_urbs must be protected by the
  1077. * lock
  1078. */
  1079. spin_lock_irqsave(&up->context_lock, flags);
  1080. if (up->available_tx_urbs > 0)
  1081. netif_wake_queue(up->netdev);
  1082. spin_unlock_irqrestore(&up->context_lock, flags);
  1083. return ret;
  1084. default:
  1085. return -EOPNOTSUPP;
  1086. }
  1087. }
  1088. /* Probe the device, reset it and gather general device information */
  1089. static int ucan_probe(struct usb_interface *intf,
  1090. const struct usb_device_id *id)
  1091. {
  1092. int ret;
  1093. int i;
  1094. u32 protocol_version;
  1095. struct usb_device *udev;
  1096. struct net_device *netdev;
  1097. struct usb_host_interface *iface_desc;
  1098. struct ucan_priv *up;
  1099. struct usb_endpoint_descriptor *ep;
  1100. u16 in_ep_size;
  1101. u16 out_ep_size;
  1102. u8 in_ep_addr;
  1103. u8 out_ep_addr;
  1104. union ucan_ctl_payload *ctl_msg_buffer;
  1105. char firmware_str[sizeof(union ucan_ctl_payload) + 1];
  1106. udev = interface_to_usbdev(intf);
  1107. /* Stage 1 - Interface Parsing
  1108. * ---------------------------
  1109. *
  1110. * Identifie the device USB interface descriptor and its
  1111. * endpoints. Probing is aborted on errors.
  1112. */
  1113. /* check if the interface is sane */
  1114. iface_desc = intf->cur_altsetting;
  1115. if (!iface_desc)
  1116. return -ENODEV;
  1117. dev_info(&udev->dev,
  1118. "%s: probing device on interface #%d\n",
  1119. UCAN_DRIVER_NAME,
  1120. iface_desc->desc.bInterfaceNumber);
  1121. /* interface sanity check */
  1122. if (iface_desc->desc.bNumEndpoints != 2) {
  1123. dev_err(&udev->dev,
  1124. "%s: invalid EP count (%d)",
  1125. UCAN_DRIVER_NAME, iface_desc->desc.bNumEndpoints);
  1126. goto err_firmware_needs_update;
  1127. }
  1128. /* check interface endpoints */
  1129. in_ep_addr = 0;
  1130. out_ep_addr = 0;
  1131. in_ep_size = 0;
  1132. out_ep_size = 0;
  1133. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  1134. ep = &iface_desc->endpoint[i].desc;
  1135. if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != 0) &&
  1136. ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  1137. USB_ENDPOINT_XFER_BULK)) {
  1138. /* In Endpoint */
  1139. in_ep_addr = ep->bEndpointAddress;
  1140. in_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
  1141. in_ep_size = le16_to_cpu(ep->wMaxPacketSize);
  1142. } else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
  1143. 0) &&
  1144. ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  1145. USB_ENDPOINT_XFER_BULK)) {
  1146. /* Out Endpoint */
  1147. out_ep_addr = ep->bEndpointAddress;
  1148. out_ep_addr &= USB_ENDPOINT_NUMBER_MASK;
  1149. out_ep_size = le16_to_cpu(ep->wMaxPacketSize);
  1150. }
  1151. }
  1152. /* check if interface is sane */
  1153. if (!in_ep_addr || !out_ep_addr) {
  1154. dev_err(&udev->dev, "%s: invalid endpoint configuration\n",
  1155. UCAN_DRIVER_NAME);
  1156. goto err_firmware_needs_update;
  1157. }
  1158. if (in_ep_size < sizeof(struct ucan_message_in)) {
  1159. dev_err(&udev->dev, "%s: invalid in_ep MaxPacketSize\n",
  1160. UCAN_DRIVER_NAME);
  1161. goto err_firmware_needs_update;
  1162. }
  1163. if (out_ep_size < sizeof(struct ucan_message_out)) {
  1164. dev_err(&udev->dev, "%s: invalid out_ep MaxPacketSize\n",
  1165. UCAN_DRIVER_NAME);
  1166. goto err_firmware_needs_update;
  1167. }
  1168. /* Stage 2 - Device Identification
  1169. * -------------------------------
  1170. *
  1171. * The device interface seems to be a ucan device. Do further
  1172. * compatibility checks. On error probing is aborted, on
  1173. * success this stage leaves the ctl_msg_buffer with the
  1174. * reported contents of a GET_INFO command (supported
  1175. * bittimings, tx_fifo depth). This information is used in
  1176. * Stage 3 for the final driver initialisation.
  1177. */
  1178. /* Prepare Memory for control transferes */
  1179. ctl_msg_buffer = devm_kzalloc(&udev->dev,
  1180. sizeof(union ucan_ctl_payload),
  1181. GFP_KERNEL);
  1182. if (!ctl_msg_buffer) {
  1183. dev_err(&udev->dev,
  1184. "%s: failed to allocate control pipe memory\n",
  1185. UCAN_DRIVER_NAME);
  1186. return -ENOMEM;
  1187. }
  1188. /* get protocol version
  1189. *
  1190. * note: ucan_ctrl_command_* wrappers cannot be used yet
  1191. * because `up` is initialised in Stage 3
  1192. */
  1193. ret = usb_control_msg(udev,
  1194. usb_rcvctrlpipe(udev, 0),
  1195. UCAN_COMMAND_GET,
  1196. USB_DIR_IN | USB_TYPE_VENDOR |
  1197. USB_RECIP_INTERFACE,
  1198. UCAN_COMMAND_GET_PROTOCOL_VERSION,
  1199. iface_desc->desc.bInterfaceNumber,
  1200. ctl_msg_buffer,
  1201. sizeof(union ucan_ctl_payload),
  1202. UCAN_USB_CTL_PIPE_TIMEOUT);
  1203. /* older firmware version do not support this command - those
  1204. * are not supported by this drive
  1205. */
  1206. if (ret != 4) {
  1207. dev_err(&udev->dev,
  1208. "%s: could not read protocol version, ret=%d\n",
  1209. UCAN_DRIVER_NAME, ret);
  1210. if (ret >= 0)
  1211. ret = -EINVAL;
  1212. goto err_firmware_needs_update;
  1213. }
  1214. /* this driver currently supports protocol version 3 only */
  1215. protocol_version =
  1216. le32_to_cpu(ctl_msg_buffer->cmd_get_protocol_version.version);
  1217. if (protocol_version < UCAN_PROTOCOL_VERSION_MIN ||
  1218. protocol_version > UCAN_PROTOCOL_VERSION_MAX) {
  1219. dev_err(&udev->dev,
  1220. "%s: device protocol version %d is not supported\n",
  1221. UCAN_DRIVER_NAME, protocol_version);
  1222. goto err_firmware_needs_update;
  1223. }
  1224. /* request the device information and store it in ctl_msg_buffer
  1225. *
  1226. * note: ucan_ctrl_command_* wrappers connot be used yet
  1227. * because `up` is initialised in Stage 3
  1228. */
  1229. ret = usb_control_msg(udev,
  1230. usb_rcvctrlpipe(udev, 0),
  1231. UCAN_COMMAND_GET,
  1232. USB_DIR_IN | USB_TYPE_VENDOR |
  1233. USB_RECIP_INTERFACE,
  1234. UCAN_COMMAND_GET_INFO,
  1235. iface_desc->desc.bInterfaceNumber,
  1236. ctl_msg_buffer,
  1237. sizeof(ctl_msg_buffer->cmd_get_device_info),
  1238. UCAN_USB_CTL_PIPE_TIMEOUT);
  1239. if (ret < 0) {
  1240. dev_err(&udev->dev, "%s: failed to retrieve device info\n",
  1241. UCAN_DRIVER_NAME);
  1242. goto err_firmware_needs_update;
  1243. }
  1244. if (ret < sizeof(ctl_msg_buffer->cmd_get_device_info)) {
  1245. dev_err(&udev->dev, "%s: device reported invalid device info\n",
  1246. UCAN_DRIVER_NAME);
  1247. goto err_firmware_needs_update;
  1248. }
  1249. if (ctl_msg_buffer->cmd_get_device_info.tx_fifo == 0) {
  1250. dev_err(&udev->dev,
  1251. "%s: device reported invalid tx-fifo size\n",
  1252. UCAN_DRIVER_NAME);
  1253. goto err_firmware_needs_update;
  1254. }
  1255. /* Stage 3 - Driver Initialisation
  1256. * -------------------------------
  1257. *
  1258. * Register device to Linux, prepare private structures and
  1259. * reset the device.
  1260. */
  1261. /* allocate driver resources */
  1262. netdev = alloc_candev(sizeof(struct ucan_priv),
  1263. ctl_msg_buffer->cmd_get_device_info.tx_fifo);
  1264. if (!netdev) {
  1265. dev_err(&udev->dev,
  1266. "%s: cannot allocate candev\n", UCAN_DRIVER_NAME);
  1267. return -ENOMEM;
  1268. }
  1269. up = netdev_priv(netdev);
  1270. /* initialze data */
  1271. up->udev = udev;
  1272. up->intf = intf;
  1273. up->netdev = netdev;
  1274. up->intf_index = iface_desc->desc.bInterfaceNumber;
  1275. up->in_ep_addr = in_ep_addr;
  1276. up->out_ep_addr = out_ep_addr;
  1277. up->in_ep_size = in_ep_size;
  1278. up->ctl_msg_buffer = ctl_msg_buffer;
  1279. up->context_array = NULL;
  1280. up->available_tx_urbs = 0;
  1281. up->can.state = CAN_STATE_STOPPED;
  1282. up->can.bittiming_const = &up->device_info.bittiming_const;
  1283. up->can.do_set_bittiming = ucan_set_bittiming;
  1284. up->can.do_set_mode = &ucan_set_mode;
  1285. spin_lock_init(&up->context_lock);
  1286. spin_lock_init(&up->echo_skb_lock);
  1287. netdev->netdev_ops = &ucan_netdev_ops;
  1288. usb_set_intfdata(intf, up);
  1289. SET_NETDEV_DEV(netdev, &intf->dev);
  1290. /* parse device information
  1291. * the data retrieved in Stage 2 is still available in
  1292. * up->ctl_msg_buffer
  1293. */
  1294. ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info);
  1295. /* just print some device information - if available */
  1296. ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0,
  1297. sizeof(union ucan_ctl_payload));
  1298. if (ret > 0) {
  1299. /* copy string while ensuring zero terminiation */
  1300. strncpy(firmware_str, up->ctl_msg_buffer->raw,
  1301. sizeof(union ucan_ctl_payload));
  1302. firmware_str[sizeof(union ucan_ctl_payload)] = '\0';
  1303. } else {
  1304. strcpy(firmware_str, "unknown");
  1305. }
  1306. /* device is compatible, reset it */
  1307. ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0);
  1308. if (ret < 0)
  1309. goto err_free_candev;
  1310. init_usb_anchor(&up->rx_urbs);
  1311. init_usb_anchor(&up->tx_urbs);
  1312. up->can.state = CAN_STATE_STOPPED;
  1313. /* register the device */
  1314. ret = register_candev(netdev);
  1315. if (ret)
  1316. goto err_free_candev;
  1317. /* initialisation complete, log device info */
  1318. netdev_info(up->netdev, "registered device\n");
  1319. netdev_info(up->netdev, "firmware string: %s\n", firmware_str);
  1320. /* success */
  1321. return 0;
  1322. err_free_candev:
  1323. free_candev(netdev);
  1324. return ret;
  1325. err_firmware_needs_update:
  1326. dev_err(&udev->dev,
  1327. "%s: probe failed; try to update the device firmware\n",
  1328. UCAN_DRIVER_NAME);
  1329. return -ENODEV;
  1330. }
  1331. /* disconnect the device */
  1332. static void ucan_disconnect(struct usb_interface *intf)
  1333. {
  1334. struct ucan_priv *up = usb_get_intfdata(intf);
  1335. usb_set_intfdata(intf, NULL);
  1336. if (up) {
  1337. unregister_netdev(up->netdev);
  1338. free_candev(up->netdev);
  1339. }
  1340. }
  1341. static struct usb_device_id ucan_table[] = {
  1342. /* Mule (soldered onto compute modules) */
  1343. {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425a, 0)},
  1344. /* Seal (standalone USB stick) */
  1345. {USB_DEVICE_INTERFACE_NUMBER(0x2294, 0x425b, 0)},
  1346. {} /* Terminating entry */
  1347. };
  1348. MODULE_DEVICE_TABLE(usb, ucan_table);
  1349. /* driver callbacks */
  1350. static struct usb_driver ucan_driver = {
  1351. .name = UCAN_DRIVER_NAME,
  1352. .probe = ucan_probe,
  1353. .disconnect = ucan_disconnect,
  1354. .id_table = ucan_table,
  1355. };
  1356. module_usb_driver(ucan_driver);
  1357. MODULE_LICENSE("GPL v2");
  1358. MODULE_AUTHOR("Martin Elshuber <martin.elshuber@theobroma-systems.com>");
  1359. MODULE_AUTHOR("Jakob Unterwurzacher <jakob.unterwurzacher@theobroma-systems.com>");
  1360. MODULE_DESCRIPTION("Driver for Theobroma Systems UCAN devices");