ems_usb.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /*
  2. * CAN driver for EMS Dr. Thomas Wuensche CPC-USB/ARM7
  3. *
  4. * Copyright (C) 2004-2009 EMS Dr. Thomas Wuensche
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/signal.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/usb.h>
  25. #include <linux/can.h>
  26. #include <linux/can/dev.h>
  27. #include <linux/can/error.h>
  28. MODULE_AUTHOR("Sebastian Haas <haas@ems-wuensche.com>");
  29. MODULE_DESCRIPTION("CAN driver for EMS Dr. Thomas Wuensche CAN/USB interfaces");
  30. MODULE_LICENSE("GPL v2");
  31. /* Control-Values for CPC_Control() Command Subject Selection */
  32. #define CONTR_CAN_MESSAGE 0x04
  33. #define CONTR_CAN_STATE 0x0C
  34. #define CONTR_BUS_ERROR 0x1C
  35. /* Control Command Actions */
  36. #define CONTR_CONT_OFF 0
  37. #define CONTR_CONT_ON 1
  38. #define CONTR_ONCE 2
  39. /* Messages from CPC to PC */
  40. #define CPC_MSG_TYPE_CAN_FRAME 1 /* CAN data frame */
  41. #define CPC_MSG_TYPE_RTR_FRAME 8 /* CAN remote frame */
  42. #define CPC_MSG_TYPE_CAN_PARAMS 12 /* Actual CAN parameters */
  43. #define CPC_MSG_TYPE_CAN_STATE 14 /* CAN state message */
  44. #define CPC_MSG_TYPE_EXT_CAN_FRAME 16 /* Extended CAN data frame */
  45. #define CPC_MSG_TYPE_EXT_RTR_FRAME 17 /* Extended remote frame */
  46. #define CPC_MSG_TYPE_CONTROL 19 /* change interface behavior */
  47. #define CPC_MSG_TYPE_CONFIRM 20 /* command processed confirmation */
  48. #define CPC_MSG_TYPE_OVERRUN 21 /* overrun events */
  49. #define CPC_MSG_TYPE_CAN_FRAME_ERROR 23 /* detected bus errors */
  50. #define CPC_MSG_TYPE_ERR_COUNTER 25 /* RX/TX error counter */
  51. /* Messages from the PC to the CPC interface */
  52. #define CPC_CMD_TYPE_CAN_FRAME 1 /* CAN data frame */
  53. #define CPC_CMD_TYPE_CONTROL 3 /* control of interface behavior */
  54. #define CPC_CMD_TYPE_CAN_PARAMS 6 /* set CAN parameters */
  55. #define CPC_CMD_TYPE_RTR_FRAME 13 /* CAN remote frame */
  56. #define CPC_CMD_TYPE_CAN_STATE 14 /* CAN state message */
  57. #define CPC_CMD_TYPE_EXT_CAN_FRAME 15 /* Extended CAN data frame */
  58. #define CPC_CMD_TYPE_EXT_RTR_FRAME 16 /* Extended CAN remote frame */
  59. #define CPC_CMD_TYPE_CAN_EXIT 200 /* exit the CAN */
  60. #define CPC_CMD_TYPE_INQ_ERR_COUNTER 25 /* request the CAN error counters */
  61. #define CPC_CMD_TYPE_CLEAR_MSG_QUEUE 8 /* clear CPC_MSG queue */
  62. #define CPC_CMD_TYPE_CLEAR_CMD_QUEUE 28 /* clear CPC_CMD queue */
  63. #define CPC_CC_TYPE_SJA1000 2 /* Philips basic CAN controller */
  64. #define CPC_CAN_ECODE_ERRFRAME 0x01 /* Ecode type */
  65. /* Overrun types */
  66. #define CPC_OVR_EVENT_CAN 0x01
  67. #define CPC_OVR_EVENT_CANSTATE 0x02
  68. #define CPC_OVR_EVENT_BUSERROR 0x04
  69. /*
  70. * If the CAN controller lost a message we indicate it with the highest bit
  71. * set in the count field.
  72. */
  73. #define CPC_OVR_HW 0x80
  74. /* Size of the "struct ems_cpc_msg" without the union */
  75. #define CPC_MSG_HEADER_LEN 11
  76. #define CPC_CAN_MSG_MIN_SIZE 5
  77. /* Define these values to match your devices */
  78. #define USB_CPCUSB_VENDOR_ID 0x12D6
  79. #define USB_CPCUSB_ARM7_PRODUCT_ID 0x0444
  80. /* Mode register NXP LPC2119/SJA1000 CAN Controller */
  81. #define SJA1000_MOD_NORMAL 0x00
  82. #define SJA1000_MOD_RM 0x01
  83. /* ECC register NXP LPC2119/SJA1000 CAN Controller */
  84. #define SJA1000_ECC_SEG 0x1F
  85. #define SJA1000_ECC_DIR 0x20
  86. #define SJA1000_ECC_ERR 0x06
  87. #define SJA1000_ECC_BIT 0x00
  88. #define SJA1000_ECC_FORM 0x40
  89. #define SJA1000_ECC_STUFF 0x80
  90. #define SJA1000_ECC_MASK 0xc0
  91. /* Status register content */
  92. #define SJA1000_SR_BS 0x80
  93. #define SJA1000_SR_ES 0x40
  94. #define SJA1000_DEFAULT_OUTPUT_CONTROL 0xDA
  95. /*
  96. * The device actually uses a 16MHz clock to generate the CAN clock
  97. * but it expects SJA1000 bit settings based on 8MHz (is internally
  98. * converted).
  99. */
  100. #define EMS_USB_ARM7_CLOCK 8000000
  101. /*
  102. * CAN-Message representation in a CPC_MSG. Message object type is
  103. * CPC_MSG_TYPE_CAN_FRAME or CPC_MSG_TYPE_RTR_FRAME or
  104. * CPC_MSG_TYPE_EXT_CAN_FRAME or CPC_MSG_TYPE_EXT_RTR_FRAME.
  105. */
  106. struct cpc_can_msg {
  107. u32 id;
  108. u8 length;
  109. u8 msg[8];
  110. };
  111. /* Representation of the CAN parameters for the SJA1000 controller */
  112. struct cpc_sja1000_params {
  113. u8 mode;
  114. u8 acc_code0;
  115. u8 acc_code1;
  116. u8 acc_code2;
  117. u8 acc_code3;
  118. u8 acc_mask0;
  119. u8 acc_mask1;
  120. u8 acc_mask2;
  121. u8 acc_mask3;
  122. u8 btr0;
  123. u8 btr1;
  124. u8 outp_contr;
  125. };
  126. /* CAN params message representation */
  127. struct cpc_can_params {
  128. u8 cc_type;
  129. /* Will support M16C CAN controller in the future */
  130. union {
  131. struct cpc_sja1000_params sja1000;
  132. } cc_params;
  133. };
  134. /* Structure for confirmed message handling */
  135. struct cpc_confirm {
  136. u8 error; /* error code */
  137. };
  138. /* Structure for overrun conditions */
  139. struct cpc_overrun {
  140. u8 event;
  141. u8 count;
  142. };
  143. /* SJA1000 CAN errors (compatible to NXP LPC2119) */
  144. struct cpc_sja1000_can_error {
  145. u8 ecc;
  146. u8 rxerr;
  147. u8 txerr;
  148. };
  149. /* structure for CAN error conditions */
  150. struct cpc_can_error {
  151. u8 ecode;
  152. struct {
  153. u8 cc_type;
  154. /* Other controllers may also provide error code capture regs */
  155. union {
  156. struct cpc_sja1000_can_error sja1000;
  157. } regs;
  158. } cc;
  159. };
  160. /*
  161. * Structure containing RX/TX error counter. This structure is used to request
  162. * the values of the CAN controllers TX and RX error counter.
  163. */
  164. struct cpc_can_err_counter {
  165. u8 rx;
  166. u8 tx;
  167. };
  168. /* Main message type used between library and application */
  169. struct __packed ems_cpc_msg {
  170. u8 type; /* type of message */
  171. u8 length; /* length of data within union 'msg' */
  172. u8 msgid; /* confirmation handle */
  173. u32 ts_sec; /* timestamp in seconds */
  174. u32 ts_nsec; /* timestamp in nano seconds */
  175. union {
  176. u8 generic[64];
  177. struct cpc_can_msg can_msg;
  178. struct cpc_can_params can_params;
  179. struct cpc_confirm confirmation;
  180. struct cpc_overrun overrun;
  181. struct cpc_can_error error;
  182. struct cpc_can_err_counter err_counter;
  183. u8 can_state;
  184. } msg;
  185. };
  186. /*
  187. * Table of devices that work with this driver
  188. * NOTE: This driver supports only CPC-USB/ARM7 (LPC2119) yet.
  189. */
  190. static struct usb_device_id ems_usb_table[] = {
  191. {USB_DEVICE(USB_CPCUSB_VENDOR_ID, USB_CPCUSB_ARM7_PRODUCT_ID)},
  192. {} /* Terminating entry */
  193. };
  194. MODULE_DEVICE_TABLE(usb, ems_usb_table);
  195. #define RX_BUFFER_SIZE 64
  196. #define CPC_HEADER_SIZE 4
  197. #define INTR_IN_BUFFER_SIZE 4
  198. #define MAX_RX_URBS 10
  199. #define MAX_TX_URBS 10
  200. struct ems_usb;
  201. struct ems_tx_urb_context {
  202. struct ems_usb *dev;
  203. u32 echo_index;
  204. u8 dlc;
  205. };
  206. struct ems_usb {
  207. struct can_priv can; /* must be the first member */
  208. int open_time;
  209. struct sk_buff *echo_skb[MAX_TX_URBS];
  210. struct usb_device *udev;
  211. struct net_device *netdev;
  212. atomic_t active_tx_urbs;
  213. struct usb_anchor tx_submitted;
  214. struct ems_tx_urb_context tx_contexts[MAX_TX_URBS];
  215. struct usb_anchor rx_submitted;
  216. struct urb *intr_urb;
  217. u8 *tx_msg_buffer;
  218. u8 *intr_in_buffer;
  219. unsigned int free_slots; /* remember number of available slots */
  220. struct ems_cpc_msg active_params; /* active controller parameters */
  221. };
  222. static void ems_usb_read_interrupt_callback(struct urb *urb)
  223. {
  224. struct ems_usb *dev = urb->context;
  225. struct net_device *netdev = dev->netdev;
  226. int err;
  227. if (!netif_device_present(netdev))
  228. return;
  229. switch (urb->status) {
  230. case 0:
  231. dev->free_slots = dev->intr_in_buffer[1];
  232. break;
  233. case -ECONNRESET: /* unlink */
  234. case -ENOENT:
  235. case -ESHUTDOWN:
  236. return;
  237. default:
  238. dev_info(netdev->dev.parent, "Rx interrupt aborted %d\n",
  239. urb->status);
  240. break;
  241. }
  242. err = usb_submit_urb(urb, GFP_ATOMIC);
  243. if (err == -ENODEV)
  244. netif_device_detach(netdev);
  245. else if (err)
  246. dev_err(netdev->dev.parent,
  247. "failed resubmitting intr urb: %d\n", err);
  248. }
  249. static void ems_usb_rx_can_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
  250. {
  251. struct can_frame *cf;
  252. struct sk_buff *skb;
  253. int i;
  254. struct net_device_stats *stats = &dev->netdev->stats;
  255. skb = alloc_can_skb(dev->netdev, &cf);
  256. if (skb == NULL)
  257. return;
  258. cf->can_id = le32_to_cpu(msg->msg.can_msg.id);
  259. cf->can_dlc = get_can_dlc(msg->msg.can_msg.length & 0xF);
  260. if (msg->type == CPC_MSG_TYPE_EXT_CAN_FRAME ||
  261. msg->type == CPC_MSG_TYPE_EXT_RTR_FRAME)
  262. cf->can_id |= CAN_EFF_FLAG;
  263. if (msg->type == CPC_MSG_TYPE_RTR_FRAME ||
  264. msg->type == CPC_MSG_TYPE_EXT_RTR_FRAME) {
  265. cf->can_id |= CAN_RTR_FLAG;
  266. } else {
  267. for (i = 0; i < cf->can_dlc; i++)
  268. cf->data[i] = msg->msg.can_msg.msg[i];
  269. }
  270. netif_rx(skb);
  271. stats->rx_packets++;
  272. stats->rx_bytes += cf->can_dlc;
  273. }
  274. static void ems_usb_rx_err(struct ems_usb *dev, struct ems_cpc_msg *msg)
  275. {
  276. struct can_frame *cf;
  277. struct sk_buff *skb;
  278. struct net_device_stats *stats = &dev->netdev->stats;
  279. skb = alloc_can_err_skb(dev->netdev, &cf);
  280. if (skb == NULL)
  281. return;
  282. if (msg->type == CPC_MSG_TYPE_CAN_STATE) {
  283. u8 state = msg->msg.can_state;
  284. if (state & SJA1000_SR_BS) {
  285. dev->can.state = CAN_STATE_BUS_OFF;
  286. cf->can_id |= CAN_ERR_BUSOFF;
  287. can_bus_off(dev->netdev);
  288. } else if (state & SJA1000_SR_ES) {
  289. dev->can.state = CAN_STATE_ERROR_WARNING;
  290. dev->can.can_stats.error_warning++;
  291. } else {
  292. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  293. dev->can.can_stats.error_passive++;
  294. }
  295. } else if (msg->type == CPC_MSG_TYPE_CAN_FRAME_ERROR) {
  296. u8 ecc = msg->msg.error.cc.regs.sja1000.ecc;
  297. u8 txerr = msg->msg.error.cc.regs.sja1000.txerr;
  298. u8 rxerr = msg->msg.error.cc.regs.sja1000.rxerr;
  299. /* bus error interrupt */
  300. dev->can.can_stats.bus_error++;
  301. stats->rx_errors++;
  302. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  303. switch (ecc & SJA1000_ECC_MASK) {
  304. case SJA1000_ECC_BIT:
  305. cf->data[2] |= CAN_ERR_PROT_BIT;
  306. break;
  307. case SJA1000_ECC_FORM:
  308. cf->data[2] |= CAN_ERR_PROT_FORM;
  309. break;
  310. case SJA1000_ECC_STUFF:
  311. cf->data[2] |= CAN_ERR_PROT_STUFF;
  312. break;
  313. default:
  314. cf->data[2] |= CAN_ERR_PROT_UNSPEC;
  315. cf->data[3] = ecc & SJA1000_ECC_SEG;
  316. break;
  317. }
  318. /* Error occured during transmission? */
  319. if ((ecc & SJA1000_ECC_DIR) == 0)
  320. cf->data[2] |= CAN_ERR_PROT_TX;
  321. if (dev->can.state == CAN_STATE_ERROR_WARNING ||
  322. dev->can.state == CAN_STATE_ERROR_PASSIVE) {
  323. cf->data[1] = (txerr > rxerr) ?
  324. CAN_ERR_CRTL_TX_PASSIVE : CAN_ERR_CRTL_RX_PASSIVE;
  325. }
  326. } else if (msg->type == CPC_MSG_TYPE_OVERRUN) {
  327. cf->can_id |= CAN_ERR_CRTL;
  328. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  329. stats->rx_over_errors++;
  330. stats->rx_errors++;
  331. }
  332. netif_rx(skb);
  333. stats->rx_packets++;
  334. stats->rx_bytes += cf->can_dlc;
  335. }
  336. /*
  337. * callback for bulk IN urb
  338. */
  339. static void ems_usb_read_bulk_callback(struct urb *urb)
  340. {
  341. struct ems_usb *dev = urb->context;
  342. struct net_device *netdev;
  343. int retval;
  344. netdev = dev->netdev;
  345. if (!netif_device_present(netdev))
  346. return;
  347. switch (urb->status) {
  348. case 0: /* success */
  349. break;
  350. case -ENOENT:
  351. return;
  352. default:
  353. dev_info(netdev->dev.parent, "Rx URB aborted (%d)\n",
  354. urb->status);
  355. goto resubmit_urb;
  356. }
  357. if (urb->actual_length > CPC_HEADER_SIZE) {
  358. struct ems_cpc_msg *msg;
  359. u8 *ibuf = urb->transfer_buffer;
  360. u8 msg_count, again, start;
  361. msg_count = ibuf[0] & ~0x80;
  362. again = ibuf[0] & 0x80;
  363. start = CPC_HEADER_SIZE;
  364. while (msg_count) {
  365. msg = (struct ems_cpc_msg *)&ibuf[start];
  366. switch (msg->type) {
  367. case CPC_MSG_TYPE_CAN_STATE:
  368. /* Process CAN state changes */
  369. ems_usb_rx_err(dev, msg);
  370. break;
  371. case CPC_MSG_TYPE_CAN_FRAME:
  372. case CPC_MSG_TYPE_EXT_CAN_FRAME:
  373. case CPC_MSG_TYPE_RTR_FRAME:
  374. case CPC_MSG_TYPE_EXT_RTR_FRAME:
  375. ems_usb_rx_can_msg(dev, msg);
  376. break;
  377. case CPC_MSG_TYPE_CAN_FRAME_ERROR:
  378. /* Process errorframe */
  379. ems_usb_rx_err(dev, msg);
  380. break;
  381. case CPC_MSG_TYPE_OVERRUN:
  382. /* Message lost while receiving */
  383. ems_usb_rx_err(dev, msg);
  384. break;
  385. }
  386. start += CPC_MSG_HEADER_LEN + msg->length;
  387. msg_count--;
  388. if (start > urb->transfer_buffer_length) {
  389. dev_err(netdev->dev.parent, "format error\n");
  390. break;
  391. }
  392. }
  393. }
  394. resubmit_urb:
  395. usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
  396. urb->transfer_buffer, RX_BUFFER_SIZE,
  397. ems_usb_read_bulk_callback, dev);
  398. retval = usb_submit_urb(urb, GFP_ATOMIC);
  399. if (retval == -ENODEV)
  400. netif_device_detach(netdev);
  401. else if (retval)
  402. dev_err(netdev->dev.parent,
  403. "failed resubmitting read bulk urb: %d\n", retval);
  404. }
  405. /*
  406. * callback for bulk IN urb
  407. */
  408. static void ems_usb_write_bulk_callback(struct urb *urb)
  409. {
  410. struct ems_tx_urb_context *context = urb->context;
  411. struct ems_usb *dev;
  412. struct net_device *netdev;
  413. BUG_ON(!context);
  414. dev = context->dev;
  415. netdev = dev->netdev;
  416. /* free up our allocated buffer */
  417. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  418. urb->transfer_buffer, urb->transfer_dma);
  419. atomic_dec(&dev->active_tx_urbs);
  420. if (!netif_device_present(netdev))
  421. return;
  422. if (urb->status)
  423. dev_info(netdev->dev.parent, "Tx URB aborted (%d)\n",
  424. urb->status);
  425. netdev->trans_start = jiffies;
  426. /* transmission complete interrupt */
  427. netdev->stats.tx_packets++;
  428. netdev->stats.tx_bytes += context->dlc;
  429. can_get_echo_skb(netdev, context->echo_index);
  430. /* Release context */
  431. context->echo_index = MAX_TX_URBS;
  432. if (netif_queue_stopped(netdev))
  433. netif_wake_queue(netdev);
  434. }
  435. /*
  436. * Send the given CPC command synchronously
  437. */
  438. static int ems_usb_command_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
  439. {
  440. int actual_length;
  441. /* Copy payload */
  442. memcpy(&dev->tx_msg_buffer[CPC_HEADER_SIZE], msg,
  443. msg->length + CPC_MSG_HEADER_LEN);
  444. /* Clear header */
  445. memset(&dev->tx_msg_buffer[0], 0, CPC_HEADER_SIZE);
  446. return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
  447. &dev->tx_msg_buffer[0],
  448. msg->length + CPC_MSG_HEADER_LEN + CPC_HEADER_SIZE,
  449. &actual_length, 1000);
  450. }
  451. /*
  452. * Change CAN controllers' mode register
  453. */
  454. static int ems_usb_write_mode(struct ems_usb *dev, u8 mode)
  455. {
  456. dev->active_params.msg.can_params.cc_params.sja1000.mode = mode;
  457. return ems_usb_command_msg(dev, &dev->active_params);
  458. }
  459. /*
  460. * Send a CPC_Control command to change behaviour when interface receives a CAN
  461. * message, bus error or CAN state changed notifications.
  462. */
  463. static int ems_usb_control_cmd(struct ems_usb *dev, u8 val)
  464. {
  465. struct ems_cpc_msg cmd;
  466. cmd.type = CPC_CMD_TYPE_CONTROL;
  467. cmd.length = CPC_MSG_HEADER_LEN + 1;
  468. cmd.msgid = 0;
  469. cmd.msg.generic[0] = val;
  470. return ems_usb_command_msg(dev, &cmd);
  471. }
  472. /*
  473. * Start interface
  474. */
  475. static int ems_usb_start(struct ems_usb *dev)
  476. {
  477. struct net_device *netdev = dev->netdev;
  478. int err, i;
  479. dev->intr_in_buffer[0] = 0;
  480. dev->free_slots = 15; /* initial size */
  481. for (i = 0; i < MAX_RX_URBS; i++) {
  482. struct urb *urb = NULL;
  483. u8 *buf = NULL;
  484. /* create a URB, and a buffer for it */
  485. urb = usb_alloc_urb(0, GFP_KERNEL);
  486. if (!urb) {
  487. dev_err(netdev->dev.parent,
  488. "No memory left for URBs\n");
  489. return -ENOMEM;
  490. }
  491. buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
  492. &urb->transfer_dma);
  493. if (!buf) {
  494. dev_err(netdev->dev.parent,
  495. "No memory left for USB buffer\n");
  496. usb_free_urb(urb);
  497. return -ENOMEM;
  498. }
  499. usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
  500. buf, RX_BUFFER_SIZE,
  501. ems_usb_read_bulk_callback, dev);
  502. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  503. usb_anchor_urb(urb, &dev->rx_submitted);
  504. err = usb_submit_urb(urb, GFP_KERNEL);
  505. if (err) {
  506. if (err == -ENODEV)
  507. netif_device_detach(dev->netdev);
  508. usb_unanchor_urb(urb);
  509. usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
  510. urb->transfer_dma);
  511. break;
  512. }
  513. /* Drop reference, USB core will take care of freeing it */
  514. usb_free_urb(urb);
  515. }
  516. /* Did we submit any URBs */
  517. if (i == 0) {
  518. dev_warn(netdev->dev.parent, "couldn't setup read URBs\n");
  519. return err;
  520. }
  521. /* Warn if we've couldn't transmit all the URBs */
  522. if (i < MAX_RX_URBS)
  523. dev_warn(netdev->dev.parent, "rx performance may be slow\n");
  524. /* Setup and start interrupt URB */
  525. usb_fill_int_urb(dev->intr_urb, dev->udev,
  526. usb_rcvintpipe(dev->udev, 1),
  527. dev->intr_in_buffer,
  528. INTR_IN_BUFFER_SIZE,
  529. ems_usb_read_interrupt_callback, dev, 1);
  530. err = usb_submit_urb(dev->intr_urb, GFP_KERNEL);
  531. if (err) {
  532. if (err == -ENODEV)
  533. netif_device_detach(dev->netdev);
  534. dev_warn(netdev->dev.parent, "intr URB submit failed: %d\n",
  535. err);
  536. return err;
  537. }
  538. /* CPC-USB will transfer received message to host */
  539. err = ems_usb_control_cmd(dev, CONTR_CAN_MESSAGE | CONTR_CONT_ON);
  540. if (err)
  541. goto failed;
  542. /* CPC-USB will transfer CAN state changes to host */
  543. err = ems_usb_control_cmd(dev, CONTR_CAN_STATE | CONTR_CONT_ON);
  544. if (err)
  545. goto failed;
  546. /* CPC-USB will transfer bus errors to host */
  547. err = ems_usb_control_cmd(dev, CONTR_BUS_ERROR | CONTR_CONT_ON);
  548. if (err)
  549. goto failed;
  550. err = ems_usb_write_mode(dev, SJA1000_MOD_NORMAL);
  551. if (err)
  552. goto failed;
  553. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  554. return 0;
  555. failed:
  556. if (err == -ENODEV)
  557. netif_device_detach(dev->netdev);
  558. dev_warn(netdev->dev.parent, "couldn't submit control: %d\n", err);
  559. return err;
  560. }
  561. static void unlink_all_urbs(struct ems_usb *dev)
  562. {
  563. int i;
  564. usb_unlink_urb(dev->intr_urb);
  565. usb_kill_anchored_urbs(&dev->rx_submitted);
  566. usb_kill_anchored_urbs(&dev->tx_submitted);
  567. atomic_set(&dev->active_tx_urbs, 0);
  568. for (i = 0; i < MAX_TX_URBS; i++)
  569. dev->tx_contexts[i].echo_index = MAX_TX_URBS;
  570. }
  571. static int ems_usb_open(struct net_device *netdev)
  572. {
  573. struct ems_usb *dev = netdev_priv(netdev);
  574. int err;
  575. err = ems_usb_write_mode(dev, SJA1000_MOD_RM);
  576. if (err)
  577. return err;
  578. /* common open */
  579. err = open_candev(netdev);
  580. if (err)
  581. return err;
  582. /* finally start device */
  583. err = ems_usb_start(dev);
  584. if (err) {
  585. if (err == -ENODEV)
  586. netif_device_detach(dev->netdev);
  587. dev_warn(netdev->dev.parent, "couldn't start device: %d\n",
  588. err);
  589. close_candev(netdev);
  590. return err;
  591. }
  592. dev->open_time = jiffies;
  593. netif_start_queue(netdev);
  594. return 0;
  595. }
  596. static netdev_tx_t ems_usb_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  597. {
  598. struct ems_usb *dev = netdev_priv(netdev);
  599. struct ems_tx_urb_context *context = NULL;
  600. struct net_device_stats *stats = &netdev->stats;
  601. struct can_frame *cf = (struct can_frame *)skb->data;
  602. struct ems_cpc_msg *msg;
  603. struct urb *urb;
  604. u8 *buf;
  605. int i, err;
  606. size_t size = CPC_HEADER_SIZE + CPC_MSG_HEADER_LEN
  607. + sizeof(struct cpc_can_msg);
  608. if (can_dropped_invalid_skb(netdev, skb))
  609. return NETDEV_TX_OK;
  610. /* create a URB, and a buffer for it, and copy the data to the URB */
  611. urb = usb_alloc_urb(0, GFP_ATOMIC);
  612. if (!urb) {
  613. dev_err(netdev->dev.parent, "No memory left for URBs\n");
  614. goto nomem;
  615. }
  616. buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, &urb->transfer_dma);
  617. if (!buf) {
  618. dev_err(netdev->dev.parent, "No memory left for USB buffer\n");
  619. usb_free_urb(urb);
  620. goto nomem;
  621. }
  622. msg = (struct ems_cpc_msg *)&buf[CPC_HEADER_SIZE];
  623. msg->msg.can_msg.id = cf->can_id & CAN_ERR_MASK;
  624. msg->msg.can_msg.length = cf->can_dlc;
  625. if (cf->can_id & CAN_RTR_FLAG) {
  626. msg->type = cf->can_id & CAN_EFF_FLAG ?
  627. CPC_CMD_TYPE_EXT_RTR_FRAME : CPC_CMD_TYPE_RTR_FRAME;
  628. msg->length = CPC_CAN_MSG_MIN_SIZE;
  629. } else {
  630. msg->type = cf->can_id & CAN_EFF_FLAG ?
  631. CPC_CMD_TYPE_EXT_CAN_FRAME : CPC_CMD_TYPE_CAN_FRAME;
  632. for (i = 0; i < cf->can_dlc; i++)
  633. msg->msg.can_msg.msg[i] = cf->data[i];
  634. msg->length = CPC_CAN_MSG_MIN_SIZE + cf->can_dlc;
  635. }
  636. /* Respect byte order */
  637. msg->msg.can_msg.id = cpu_to_le32(msg->msg.can_msg.id);
  638. for (i = 0; i < MAX_TX_URBS; i++) {
  639. if (dev->tx_contexts[i].echo_index == MAX_TX_URBS) {
  640. context = &dev->tx_contexts[i];
  641. break;
  642. }
  643. }
  644. /*
  645. * May never happen! When this happens we'd more URBs in flight as
  646. * allowed (MAX_TX_URBS).
  647. */
  648. if (!context) {
  649. usb_unanchor_urb(urb);
  650. usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
  651. dev_warn(netdev->dev.parent, "couldn't find free context\n");
  652. return NETDEV_TX_BUSY;
  653. }
  654. context->dev = dev;
  655. context->echo_index = i;
  656. context->dlc = cf->can_dlc;
  657. usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
  658. size, ems_usb_write_bulk_callback, context);
  659. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  660. usb_anchor_urb(urb, &dev->tx_submitted);
  661. can_put_echo_skb(skb, netdev, context->echo_index);
  662. atomic_inc(&dev->active_tx_urbs);
  663. err = usb_submit_urb(urb, GFP_ATOMIC);
  664. if (unlikely(err)) {
  665. can_free_echo_skb(netdev, context->echo_index);
  666. usb_unanchor_urb(urb);
  667. usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
  668. dev_kfree_skb(skb);
  669. atomic_dec(&dev->active_tx_urbs);
  670. if (err == -ENODEV) {
  671. netif_device_detach(netdev);
  672. } else {
  673. dev_warn(netdev->dev.parent, "failed tx_urb %d\n", err);
  674. stats->tx_dropped++;
  675. }
  676. } else {
  677. netdev->trans_start = jiffies;
  678. /* Slow down tx path */
  679. if (atomic_read(&dev->active_tx_urbs) >= MAX_TX_URBS ||
  680. dev->free_slots < 5) {
  681. netif_stop_queue(netdev);
  682. }
  683. }
  684. /*
  685. * Release our reference to this URB, the USB core will eventually free
  686. * it entirely.
  687. */
  688. usb_free_urb(urb);
  689. return NETDEV_TX_OK;
  690. nomem:
  691. dev_kfree_skb(skb);
  692. stats->tx_dropped++;
  693. return NETDEV_TX_OK;
  694. }
  695. static int ems_usb_close(struct net_device *netdev)
  696. {
  697. struct ems_usb *dev = netdev_priv(netdev);
  698. /* Stop polling */
  699. unlink_all_urbs(dev);
  700. netif_stop_queue(netdev);
  701. /* Set CAN controller to reset mode */
  702. if (ems_usb_write_mode(dev, SJA1000_MOD_RM))
  703. dev_warn(netdev->dev.parent, "couldn't stop device");
  704. close_candev(netdev);
  705. dev->open_time = 0;
  706. return 0;
  707. }
  708. static const struct net_device_ops ems_usb_netdev_ops = {
  709. .ndo_open = ems_usb_open,
  710. .ndo_stop = ems_usb_close,
  711. .ndo_start_xmit = ems_usb_start_xmit,
  712. };
  713. static struct can_bittiming_const ems_usb_bittiming_const = {
  714. .name = "ems_usb",
  715. .tseg1_min = 1,
  716. .tseg1_max = 16,
  717. .tseg2_min = 1,
  718. .tseg2_max = 8,
  719. .sjw_max = 4,
  720. .brp_min = 1,
  721. .brp_max = 64,
  722. .brp_inc = 1,
  723. };
  724. static int ems_usb_set_mode(struct net_device *netdev, enum can_mode mode)
  725. {
  726. struct ems_usb *dev = netdev_priv(netdev);
  727. if (!dev->open_time)
  728. return -EINVAL;
  729. switch (mode) {
  730. case CAN_MODE_START:
  731. if (ems_usb_write_mode(dev, SJA1000_MOD_NORMAL))
  732. dev_warn(netdev->dev.parent, "couldn't start device");
  733. if (netif_queue_stopped(netdev))
  734. netif_wake_queue(netdev);
  735. break;
  736. default:
  737. return -EOPNOTSUPP;
  738. }
  739. return 0;
  740. }
  741. static int ems_usb_set_bittiming(struct net_device *netdev)
  742. {
  743. struct ems_usb *dev = netdev_priv(netdev);
  744. struct can_bittiming *bt = &dev->can.bittiming;
  745. u8 btr0, btr1;
  746. btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
  747. btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
  748. (((bt->phase_seg2 - 1) & 0x7) << 4);
  749. if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  750. btr1 |= 0x80;
  751. dev_info(netdev->dev.parent, "setting BTR0=0x%02x BTR1=0x%02x\n",
  752. btr0, btr1);
  753. dev->active_params.msg.can_params.cc_params.sja1000.btr0 = btr0;
  754. dev->active_params.msg.can_params.cc_params.sja1000.btr1 = btr1;
  755. return ems_usb_command_msg(dev, &dev->active_params);
  756. }
  757. static void init_params_sja1000(struct ems_cpc_msg *msg)
  758. {
  759. struct cpc_sja1000_params *sja1000 =
  760. &msg->msg.can_params.cc_params.sja1000;
  761. msg->type = CPC_CMD_TYPE_CAN_PARAMS;
  762. msg->length = sizeof(struct cpc_can_params);
  763. msg->msgid = 0;
  764. msg->msg.can_params.cc_type = CPC_CC_TYPE_SJA1000;
  765. /* Acceptance filter open */
  766. sja1000->acc_code0 = 0x00;
  767. sja1000->acc_code1 = 0x00;
  768. sja1000->acc_code2 = 0x00;
  769. sja1000->acc_code3 = 0x00;
  770. /* Acceptance filter open */
  771. sja1000->acc_mask0 = 0xFF;
  772. sja1000->acc_mask1 = 0xFF;
  773. sja1000->acc_mask2 = 0xFF;
  774. sja1000->acc_mask3 = 0xFF;
  775. sja1000->btr0 = 0;
  776. sja1000->btr1 = 0;
  777. sja1000->outp_contr = SJA1000_DEFAULT_OUTPUT_CONTROL;
  778. sja1000->mode = SJA1000_MOD_RM;
  779. }
  780. /*
  781. * probe function for new CPC-USB devices
  782. */
  783. static int ems_usb_probe(struct usb_interface *intf,
  784. const struct usb_device_id *id)
  785. {
  786. struct net_device *netdev;
  787. struct ems_usb *dev;
  788. int i, err = -ENOMEM;
  789. netdev = alloc_candev(sizeof(struct ems_usb), MAX_TX_URBS);
  790. if (!netdev) {
  791. dev_err(&intf->dev, "ems_usb: Couldn't alloc candev\n");
  792. return -ENOMEM;
  793. }
  794. dev = netdev_priv(netdev);
  795. dev->udev = interface_to_usbdev(intf);
  796. dev->netdev = netdev;
  797. dev->can.state = CAN_STATE_STOPPED;
  798. dev->can.clock.freq = EMS_USB_ARM7_CLOCK;
  799. dev->can.bittiming_const = &ems_usb_bittiming_const;
  800. dev->can.do_set_bittiming = ems_usb_set_bittiming;
  801. dev->can.do_set_mode = ems_usb_set_mode;
  802. dev->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
  803. netdev->netdev_ops = &ems_usb_netdev_ops;
  804. netdev->flags |= IFF_ECHO; /* we support local echo */
  805. init_usb_anchor(&dev->rx_submitted);
  806. init_usb_anchor(&dev->tx_submitted);
  807. atomic_set(&dev->active_tx_urbs, 0);
  808. for (i = 0; i < MAX_TX_URBS; i++)
  809. dev->tx_contexts[i].echo_index = MAX_TX_URBS;
  810. dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  811. if (!dev->intr_urb) {
  812. dev_err(&intf->dev, "Couldn't alloc intr URB\n");
  813. goto cleanup_candev;
  814. }
  815. dev->intr_in_buffer = kzalloc(INTR_IN_BUFFER_SIZE, GFP_KERNEL);
  816. if (!dev->intr_in_buffer) {
  817. dev_err(&intf->dev, "Couldn't alloc Intr buffer\n");
  818. goto cleanup_intr_urb;
  819. }
  820. dev->tx_msg_buffer = kzalloc(CPC_HEADER_SIZE +
  821. sizeof(struct ems_cpc_msg), GFP_KERNEL);
  822. if (!dev->tx_msg_buffer) {
  823. dev_err(&intf->dev, "Couldn't alloc Tx buffer\n");
  824. goto cleanup_intr_in_buffer;
  825. }
  826. usb_set_intfdata(intf, dev);
  827. SET_NETDEV_DEV(netdev, &intf->dev);
  828. init_params_sja1000(&dev->active_params);
  829. err = ems_usb_command_msg(dev, &dev->active_params);
  830. if (err) {
  831. dev_err(netdev->dev.parent,
  832. "couldn't initialize controller: %d\n", err);
  833. goto cleanup_tx_msg_buffer;
  834. }
  835. err = register_candev(netdev);
  836. if (err) {
  837. dev_err(netdev->dev.parent,
  838. "couldn't register CAN device: %d\n", err);
  839. goto cleanup_tx_msg_buffer;
  840. }
  841. return 0;
  842. cleanup_tx_msg_buffer:
  843. kfree(dev->tx_msg_buffer);
  844. cleanup_intr_in_buffer:
  845. kfree(dev->intr_in_buffer);
  846. cleanup_intr_urb:
  847. usb_free_urb(dev->intr_urb);
  848. cleanup_candev:
  849. free_candev(netdev);
  850. return err;
  851. }
  852. /*
  853. * called by the usb core when the device is removed from the system
  854. */
  855. static void ems_usb_disconnect(struct usb_interface *intf)
  856. {
  857. struct ems_usb *dev = usb_get_intfdata(intf);
  858. usb_set_intfdata(intf, NULL);
  859. if (dev) {
  860. unregister_netdev(dev->netdev);
  861. free_candev(dev->netdev);
  862. unlink_all_urbs(dev);
  863. usb_free_urb(dev->intr_urb);
  864. kfree(dev->intr_in_buffer);
  865. }
  866. }
  867. /* usb specific object needed to register this driver with the usb subsystem */
  868. static struct usb_driver ems_usb_driver = {
  869. .name = "ems_usb",
  870. .probe = ems_usb_probe,
  871. .disconnect = ems_usb_disconnect,
  872. .id_table = ems_usb_table,
  873. };
  874. static int __init ems_usb_init(void)
  875. {
  876. int err;
  877. printk(KERN_INFO "CPC-USB kernel driver loaded\n");
  878. /* register this driver with the USB subsystem */
  879. err = usb_register(&ems_usb_driver);
  880. if (err) {
  881. err("usb_register failed. Error number %d\n", err);
  882. return err;
  883. }
  884. return 0;
  885. }
  886. static void __exit ems_usb_exit(void)
  887. {
  888. /* deregister this driver with the USB subsystem */
  889. usb_deregister(&ems_usb_driver);
  890. }
  891. module_init(ems_usb_init);
  892. module_exit(ems_usb_exit);