1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2008-2010
4  *
5  * - Kurt Van Dijck, EIA Electronics
6  */
7 
8 #include <linux/ethtool.h>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <asm/io.h>
12 
13 #include "softing.h"
14 
15 #define TX_ECHO_SKB_MAX (((TXMAX+1)/2)-1)
16 
17 /*
18  * test is a specific CAN netdev
19  * is online (ie. up 'n running, not sleeping, not busoff
20  */
21 static inline int canif_is_active(struct net_device *netdev)
22 {
23 	struct can_priv *can = netdev_priv(netdev);
24 
25 	if (!netif_running(netdev))
26 		return 0;
27 	return (can->state <= CAN_STATE_ERROR_PASSIVE);
28 }
29 
30 /* reset DPRAM */
31 static inline void softing_set_reset_dpram(struct softing *card)
32 {
33 	if (card->pdat->generation >= 2) {
34 		spin_lock_bh(&card->spin);
35 		iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) & ~1,
36 				&card->dpram[DPRAM_V2_RESET]);
37 		spin_unlock_bh(&card->spin);
38 	}
39 }
40 
41 static inline void softing_clr_reset_dpram(struct softing *card)
42 {
43 	if (card->pdat->generation >= 2) {
44 		spin_lock_bh(&card->spin);
45 		iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) | 1,
46 				&card->dpram[DPRAM_V2_RESET]);
47 		spin_unlock_bh(&card->spin);
48 	}
49 }
50 
51 /* trigger the tx queue-ing */
52 static netdev_tx_t softing_netdev_start_xmit(struct sk_buff *skb,
53 		struct net_device *dev)
54 {
55 	struct softing_priv *priv = netdev_priv(dev);
56 	struct softing *card = priv->card;
57 	int ret;
58 	uint8_t *ptr;
59 	uint8_t fifo_wr, fifo_rd;
60 	struct can_frame *cf = (struct can_frame *)skb->data;
61 	uint8_t buf[DPRAM_TX_SIZE];
62 
63 	if (can_dev_dropped_skb(dev, skb))
64 		return NETDEV_TX_OK;
65 
66 	spin_lock(&card->spin);
67 
68 	ret = NETDEV_TX_BUSY;
69 	if (!card->fw.up ||
70 			(card->tx.pending >= TXMAX) ||
71 			(priv->tx.pending >= TX_ECHO_SKB_MAX))
72 		goto xmit_done;
73 	fifo_wr = ioread8(&card->dpram[DPRAM_TX_WR]);
74 	fifo_rd = ioread8(&card->dpram[DPRAM_TX_RD]);
75 	if (fifo_wr == fifo_rd)
76 		/* fifo full */
77 		goto xmit_done;
78 	memset(buf, 0, sizeof(buf));
79 	ptr = buf;
80 	*ptr = CMD_TX;
81 	if (cf->can_id & CAN_RTR_FLAG)
82 		*ptr |= CMD_RTR;
83 	if (cf->can_id & CAN_EFF_FLAG)
84 		*ptr |= CMD_XTD;
85 	if (priv->index)
86 		*ptr |= CMD_BUS2;
87 	++ptr;
88 	*ptr++ = cf->len;
89 	*ptr++ = (cf->can_id >> 0);
90 	*ptr++ = (cf->can_id >> 8);
91 	if (cf->can_id & CAN_EFF_FLAG) {
92 		*ptr++ = (cf->can_id >> 16);
93 		*ptr++ = (cf->can_id >> 24);
94 	} else {
95 		/* increment 1, not 2 as you might think */
96 		ptr += 1;
97 	}
98 	if (!(cf->can_id & CAN_RTR_FLAG))
99 		memcpy(ptr, &cf->data[0], cf->len);
100 	memcpy_toio(&card->dpram[DPRAM_TX + DPRAM_TX_SIZE * fifo_wr],
101 			buf, DPRAM_TX_SIZE);
102 	if (++fifo_wr >= DPRAM_TX_CNT)
103 		fifo_wr = 0;
104 	iowrite8(fifo_wr, &card->dpram[DPRAM_TX_WR]);
105 	card->tx.last_bus = priv->index;
106 	++card->tx.pending;
107 	++priv->tx.pending;
108 	can_put_echo_skb(skb, dev, priv->tx.echo_put, 0);
109 	++priv->tx.echo_put;
110 	if (priv->tx.echo_put >= TX_ECHO_SKB_MAX)
111 		priv->tx.echo_put = 0;
112 	/* can_put_echo_skb() saves the skb, safe to return TX_OK */
113 	ret = NETDEV_TX_OK;
114 xmit_done:
115 	spin_unlock(&card->spin);
116 	if (card->tx.pending >= TXMAX) {
117 		int j;
118 		for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
119 			if (card->net[j])
120 				netif_stop_queue(card->net[j]);
121 		}
122 	}
123 	if (ret != NETDEV_TX_OK)
124 		netif_stop_queue(dev);
125 
126 	return ret;
127 }
128 
129 /*
130  * shortcut for skb delivery
131  */
132 int softing_netdev_rx(struct net_device *netdev, const struct can_frame *msg,
133 		ktime_t ktime)
134 {
135 	struct sk_buff *skb;
136 	struct can_frame *cf;
137 
138 	skb = alloc_can_skb(netdev, &cf);
139 	if (!skb)
140 		return -ENOMEM;
141 	memcpy(cf, msg, sizeof(*msg));
142 	skb->tstamp = ktime;
143 	return netif_rx(skb);
144 }
145 
146 /*
147  * softing_handle_1
148  * pop 1 entry from the DPRAM queue, and process
149  */
150 static int softing_handle_1(struct softing *card)
151 {
152 	struct net_device *netdev;
153 	struct softing_priv *priv;
154 	ktime_t ktime;
155 	struct can_frame msg;
156 	int cnt = 0, lost_msg;
157 	uint8_t fifo_rd, fifo_wr, cmd;
158 	uint8_t *ptr;
159 	uint32_t tmp_u32;
160 	uint8_t buf[DPRAM_RX_SIZE];
161 
162 	memset(&msg, 0, sizeof(msg));
163 	/* test for lost msgs */
164 	lost_msg = ioread8(&card->dpram[DPRAM_RX_LOST]);
165 	if (lost_msg) {
166 		int j;
167 		/* reset condition */
168 		iowrite8(0, &card->dpram[DPRAM_RX_LOST]);
169 		/* prepare msg */
170 		msg.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
171 		msg.len = CAN_ERR_DLC;
172 		msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
173 		/*
174 		 * service to all buses, we don't know which it was applicable
175 		 * but only service buses that are online
176 		 */
177 		for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
178 			netdev = card->net[j];
179 			if (!netdev)
180 				continue;
181 			if (!canif_is_active(netdev))
182 				/* a dead bus has no overflows */
183 				continue;
184 			++netdev->stats.rx_over_errors;
185 			softing_netdev_rx(netdev, &msg, 0);
186 		}
187 		/* prepare for other use */
188 		memset(&msg, 0, sizeof(msg));
189 		++cnt;
190 	}
191 
192 	fifo_rd = ioread8(&card->dpram[DPRAM_RX_RD]);
193 	fifo_wr = ioread8(&card->dpram[DPRAM_RX_WR]);
194 
195 	if (++fifo_rd >= DPRAM_RX_CNT)
196 		fifo_rd = 0;
197 	if (fifo_wr == fifo_rd)
198 		return cnt;
199 
200 	memcpy_fromio(buf, &card->dpram[DPRAM_RX + DPRAM_RX_SIZE*fifo_rd],
201 			DPRAM_RX_SIZE);
202 	mb();
203 	/* trigger dual port RAM */
204 	iowrite8(fifo_rd, &card->dpram[DPRAM_RX_RD]);
205 
206 	ptr = buf;
207 	cmd = *ptr++;
208 	if (cmd == 0xff)
209 		/* not quite useful, probably the card has got out */
210 		return 0;
211 	netdev = card->net[0];
212 	if (cmd & CMD_BUS2)
213 		netdev = card->net[1];
214 	priv = netdev_priv(netdev);
215 
216 	if (cmd & CMD_ERR) {
217 		uint8_t can_state, state;
218 
219 		state = *ptr++;
220 
221 		msg.can_id = CAN_ERR_FLAG;
222 		msg.len = CAN_ERR_DLC;
223 
224 		if (state & SF_MASK_BUSOFF) {
225 			can_state = CAN_STATE_BUS_OFF;
226 			msg.can_id |= CAN_ERR_BUSOFF;
227 			state = STATE_BUSOFF;
228 		} else if (state & SF_MASK_EPASSIVE) {
229 			can_state = CAN_STATE_ERROR_PASSIVE;
230 			msg.can_id |= CAN_ERR_CRTL;
231 			msg.data[1] = CAN_ERR_CRTL_TX_PASSIVE;
232 			state = STATE_EPASSIVE;
233 		} else {
234 			can_state = CAN_STATE_ERROR_ACTIVE;
235 			msg.can_id |= CAN_ERR_CRTL;
236 			state = STATE_EACTIVE;
237 		}
238 		/* update DPRAM */
239 		iowrite8(state, &card->dpram[priv->index ?
240 				DPRAM_INFO_BUSSTATE2 : DPRAM_INFO_BUSSTATE]);
241 		/* timestamp */
242 		tmp_u32 = le32_to_cpup((void *)ptr);
243 		ktime = softing_raw2ktime(card, tmp_u32);
244 
245 		++netdev->stats.rx_errors;
246 		/* update internal status */
247 		if (can_state != priv->can.state) {
248 			priv->can.state = can_state;
249 			if (can_state == CAN_STATE_ERROR_PASSIVE)
250 				++priv->can.can_stats.error_passive;
251 			else if (can_state == CAN_STATE_BUS_OFF) {
252 				/* this calls can_close_cleanup() */
253 				++priv->can.can_stats.bus_off;
254 				can_bus_off(netdev);
255 				netif_stop_queue(netdev);
256 			}
257 			/* trigger socketcan */
258 			softing_netdev_rx(netdev, &msg, ktime);
259 		}
260 
261 	} else {
262 		if (cmd & CMD_RTR)
263 			msg.can_id |= CAN_RTR_FLAG;
264 		msg.len = can_cc_dlc2len(*ptr++);
265 		if (cmd & CMD_XTD) {
266 			msg.can_id |= CAN_EFF_FLAG;
267 			msg.can_id |= le32_to_cpup((void *)ptr);
268 			ptr += 4;
269 		} else {
270 			msg.can_id |= le16_to_cpup((void *)ptr);
271 			ptr += 2;
272 		}
273 		/* timestamp */
274 		tmp_u32 = le32_to_cpup((void *)ptr);
275 		ptr += 4;
276 		ktime = softing_raw2ktime(card, tmp_u32);
277 		if (!(msg.can_id & CAN_RTR_FLAG))
278 			memcpy(&msg.data[0], ptr, 8);
279 		/* update socket */
280 		if (cmd & CMD_ACK) {
281 			/* acknowledge, was tx msg */
282 			struct sk_buff *skb;
283 			skb = priv->can.echo_skb[priv->tx.echo_get];
284 			if (skb)
285 				skb->tstamp = ktime;
286 			++netdev->stats.tx_packets;
287 			netdev->stats.tx_bytes +=
288 				can_get_echo_skb(netdev, priv->tx.echo_get,
289 						 NULL);
290 			++priv->tx.echo_get;
291 			if (priv->tx.echo_get >= TX_ECHO_SKB_MAX)
292 				priv->tx.echo_get = 0;
293 			if (priv->tx.pending)
294 				--priv->tx.pending;
295 			if (card->tx.pending)
296 				--card->tx.pending;
297 		} else {
298 			int ret;
299 
300 			ret = softing_netdev_rx(netdev, &msg, ktime);
301 			if (ret == NET_RX_SUCCESS) {
302 				++netdev->stats.rx_packets;
303 				if (!(msg.can_id & CAN_RTR_FLAG))
304 					netdev->stats.rx_bytes += msg.len;
305 			} else {
306 				++netdev->stats.rx_dropped;
307 			}
308 		}
309 	}
310 	++cnt;
311 	return cnt;
312 }
313 
314 /*
315  * real interrupt handler
316  */
317 static irqreturn_t softing_irq_thread(int irq, void *dev_id)
318 {
319 	struct softing *card = (struct softing *)dev_id;
320 	struct net_device *netdev;
321 	struct softing_priv *priv;
322 	int j, offset, work_done;
323 
324 	work_done = 0;
325 	spin_lock_bh(&card->spin);
326 	while (softing_handle_1(card) > 0) {
327 		++card->irq.svc_count;
328 		++work_done;
329 	}
330 	spin_unlock_bh(&card->spin);
331 	/* resume tx queue's */
332 	offset = card->tx.last_bus;
333 	for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
334 		if (card->tx.pending >= TXMAX)
335 			break;
336 		netdev = card->net[(j + offset + 1) % card->pdat->nbus];
337 		if (!netdev)
338 			continue;
339 		priv = netdev_priv(netdev);
340 		if (!canif_is_active(netdev))
341 			/* it makes no sense to wake dead buses */
342 			continue;
343 		if (priv->tx.pending >= TX_ECHO_SKB_MAX)
344 			continue;
345 		++work_done;
346 		netif_wake_queue(netdev);
347 	}
348 	return work_done ? IRQ_HANDLED : IRQ_NONE;
349 }
350 
351 /*
352  * interrupt routines:
353  * schedule the 'real interrupt handler'
354  */
355 static irqreturn_t softing_irq_v2(int irq, void *dev_id)
356 {
357 	struct softing *card = (struct softing *)dev_id;
358 	uint8_t ir;
359 
360 	ir = ioread8(&card->dpram[DPRAM_V2_IRQ_TOHOST]);
361 	iowrite8(0, &card->dpram[DPRAM_V2_IRQ_TOHOST]);
362 	return (1 == ir) ? IRQ_WAKE_THREAD : IRQ_NONE;
363 }
364 
365 static irqreturn_t softing_irq_v1(int irq, void *dev_id)
366 {
367 	struct softing *card = (struct softing *)dev_id;
368 	uint8_t ir;
369 
370 	ir = ioread8(&card->dpram[DPRAM_IRQ_TOHOST]);
371 	iowrite8(0, &card->dpram[DPRAM_IRQ_TOHOST]);
372 	return ir ? IRQ_WAKE_THREAD : IRQ_NONE;
373 }
374 
375 /*
376  * netdev/candev interoperability
377  */
378 static int softing_netdev_open(struct net_device *ndev)
379 {
380 	int ret;
381 
382 	/* check or determine and set bittime */
383 	ret = open_candev(ndev);
384 	if (ret)
385 		return ret;
386 
387 	ret = softing_startstop(ndev, 1);
388 	if (ret < 0)
389 		close_candev(ndev);
390 
391 	return ret;
392 }
393 
394 static int softing_netdev_stop(struct net_device *ndev)
395 {
396 	netif_stop_queue(ndev);
397 
398 	/* softing cycle does close_candev() */
399 	return softing_startstop(ndev, 0);
400 }
401 
402 static int softing_candev_set_mode(struct net_device *ndev, enum can_mode mode)
403 {
404 	int ret;
405 
406 	switch (mode) {
407 	case CAN_MODE_START:
408 		/* softing_startstop does close_candev() */
409 		ret = softing_startstop(ndev, 1);
410 		return ret;
411 	case CAN_MODE_STOP:
412 	case CAN_MODE_SLEEP:
413 		return -EOPNOTSUPP;
414 	}
415 	return 0;
416 }
417 
418 /*
419  * Softing device management helpers
420  */
421 int softing_enable_irq(struct softing *card, int enable)
422 {
423 	int ret;
424 
425 	if (!card->irq.nr) {
426 		return 0;
427 	} else if (card->irq.requested && !enable) {
428 		free_irq(card->irq.nr, card);
429 		card->irq.requested = 0;
430 	} else if (!card->irq.requested && enable) {
431 		ret = request_threaded_irq(card->irq.nr,
432 				(card->pdat->generation >= 2) ?
433 					softing_irq_v2 : softing_irq_v1,
434 				softing_irq_thread, IRQF_SHARED,
435 				dev_name(&card->pdev->dev), card);
436 		if (ret) {
437 			dev_alert(&card->pdev->dev,
438 					"request_threaded_irq(%u) failed\n",
439 					card->irq.nr);
440 			return ret;
441 		}
442 		card->irq.requested = 1;
443 	}
444 	return 0;
445 }
446 
447 static void softing_card_shutdown(struct softing *card)
448 {
449 	int fw_up = 0;
450 
451 	if (mutex_lock_interruptible(&card->fw.lock)) {
452 		/* return -ERESTARTSYS */;
453 	}
454 	fw_up = card->fw.up;
455 	card->fw.up = 0;
456 
457 	if (card->irq.requested && card->irq.nr) {
458 		free_irq(card->irq.nr, card);
459 		card->irq.requested = 0;
460 	}
461 	if (fw_up) {
462 		if (card->pdat->enable_irq)
463 			card->pdat->enable_irq(card->pdev, 0);
464 		softing_set_reset_dpram(card);
465 		if (card->pdat->reset)
466 			card->pdat->reset(card->pdev, 1);
467 	}
468 	mutex_unlock(&card->fw.lock);
469 }
470 
471 static int softing_card_boot(struct softing *card)
472 {
473 	int ret, j;
474 	static const uint8_t stream[] = {
475 		0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
476 	unsigned char back[sizeof(stream)];
477 
478 	if (mutex_lock_interruptible(&card->fw.lock))
479 		return -ERESTARTSYS;
480 	if (card->fw.up) {
481 		mutex_unlock(&card->fw.lock);
482 		return 0;
483 	}
484 	/* reset board */
485 	if (card->pdat->enable_irq)
486 		card->pdat->enable_irq(card->pdev, 1);
487 	/* boot card */
488 	softing_set_reset_dpram(card);
489 	if (card->pdat->reset)
490 		card->pdat->reset(card->pdev, 1);
491 	for (j = 0; (j + sizeof(stream)) < card->dpram_size;
492 			j += sizeof(stream)) {
493 
494 		memcpy_toio(&card->dpram[j], stream, sizeof(stream));
495 		/* flush IO cache */
496 		mb();
497 		memcpy_fromio(back, &card->dpram[j], sizeof(stream));
498 
499 		if (!memcmp(back, stream, sizeof(stream)))
500 			continue;
501 		/* memory is not equal */
502 		dev_alert(&card->pdev->dev, "dpram failed at 0x%04x\n", j);
503 		ret = -EIO;
504 		goto failed;
505 	}
506 	wmb();
507 	/* load boot firmware */
508 	ret = softing_load_fw(card->pdat->boot.fw, card, card->dpram,
509 				card->dpram_size,
510 				card->pdat->boot.offs - card->pdat->boot.addr);
511 	if (ret < 0)
512 		goto failed;
513 	/* load loader firmware */
514 	ret = softing_load_fw(card->pdat->load.fw, card, card->dpram,
515 				card->dpram_size,
516 				card->pdat->load.offs - card->pdat->load.addr);
517 	if (ret < 0)
518 		goto failed;
519 
520 	if (card->pdat->reset)
521 		card->pdat->reset(card->pdev, 0);
522 	softing_clr_reset_dpram(card);
523 	ret = softing_bootloader_command(card, 0, "card boot");
524 	if (ret < 0)
525 		goto failed;
526 	ret = softing_load_app_fw(card->pdat->app.fw, card);
527 	if (ret < 0)
528 		goto failed;
529 
530 	ret = softing_chip_poweron(card);
531 	if (ret < 0)
532 		goto failed;
533 
534 	card->fw.up = 1;
535 	mutex_unlock(&card->fw.lock);
536 	return 0;
537 failed:
538 	card->fw.up = 0;
539 	if (card->pdat->enable_irq)
540 		card->pdat->enable_irq(card->pdev, 0);
541 	softing_set_reset_dpram(card);
542 	if (card->pdat->reset)
543 		card->pdat->reset(card->pdev, 1);
544 	mutex_unlock(&card->fw.lock);
545 	return ret;
546 }
547 
548 /*
549  * netdev sysfs
550  */
551 static ssize_t show_chip(struct device *dev, struct device_attribute *attr,
552 		char *buf)
553 {
554 	struct net_device *ndev = to_net_dev(dev);
555 	struct softing_priv *priv = netdev2softing(ndev);
556 
557 	return sprintf(buf, "%i\n", priv->chip);
558 }
559 
560 static ssize_t show_output(struct device *dev, struct device_attribute *attr,
561 		char *buf)
562 {
563 	struct net_device *ndev = to_net_dev(dev);
564 	struct softing_priv *priv = netdev2softing(ndev);
565 
566 	return sprintf(buf, "0x%02x\n", priv->output);
567 }
568 
569 static ssize_t store_output(struct device *dev, struct device_attribute *attr,
570 		const char *buf, size_t count)
571 {
572 	struct net_device *ndev = to_net_dev(dev);
573 	struct softing_priv *priv = netdev2softing(ndev);
574 	struct softing *card = priv->card;
575 	unsigned long val;
576 	int ret;
577 
578 	ret = kstrtoul(buf, 0, &val);
579 	if (ret < 0)
580 		return ret;
581 	val &= 0xFF;
582 
583 	ret = mutex_lock_interruptible(&card->fw.lock);
584 	if (ret)
585 		return -ERESTARTSYS;
586 	if (netif_running(ndev)) {
587 		mutex_unlock(&card->fw.lock);
588 		return -EBUSY;
589 	}
590 	priv->output = val;
591 	mutex_unlock(&card->fw.lock);
592 	return count;
593 }
594 
595 static const DEVICE_ATTR(chip, 0444, show_chip, NULL);
596 static const DEVICE_ATTR(output, 0644, show_output, store_output);
597 
598 static const struct attribute *const netdev_sysfs_attrs[] = {
599 	&dev_attr_chip.attr,
600 	&dev_attr_output.attr,
601 	NULL,
602 };
603 static const struct attribute_group netdev_sysfs_group = {
604 	.name = NULL,
605 	.attrs = (struct attribute **)netdev_sysfs_attrs,
606 };
607 
608 static const struct net_device_ops softing_netdev_ops = {
609 	.ndo_open = softing_netdev_open,
610 	.ndo_stop = softing_netdev_stop,
611 	.ndo_start_xmit	= softing_netdev_start_xmit,
612 	.ndo_change_mtu = can_change_mtu,
613 };
614 
615 static const struct ethtool_ops softing_ethtool_ops = {
616 	.get_ts_info = ethtool_op_get_ts_info,
617 };
618 
619 static const struct can_bittiming_const softing_btr_const = {
620 	.name = KBUILD_MODNAME,
621 	.tseg1_min = 1,
622 	.tseg1_max = 16,
623 	.tseg2_min = 1,
624 	.tseg2_max = 8,
625 	.sjw_max = 4, /* overruled */
626 	.brp_min = 1,
627 	.brp_max = 32, /* overruled */
628 	.brp_inc = 1,
629 };
630 
631 
632 static struct net_device *softing_netdev_create(struct softing *card,
633 						uint16_t chip_id)
634 {
635 	struct net_device *netdev;
636 	struct softing_priv *priv;
637 
638 	netdev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
639 	if (!netdev) {
640 		dev_alert(&card->pdev->dev, "alloc_candev failed\n");
641 		return NULL;
642 	}
643 	priv = netdev_priv(netdev);
644 	priv->netdev = netdev;
645 	priv->card = card;
646 	memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const));
647 	priv->btr_const.brp_max = card->pdat->max_brp;
648 	priv->btr_const.sjw_max = card->pdat->max_sjw;
649 	priv->can.bittiming_const = &priv->btr_const;
650 	priv->can.clock.freq = 8000000;
651 	priv->chip = chip_id;
652 	priv->output = softing_default_output(netdev);
653 	SET_NETDEV_DEV(netdev, &card->pdev->dev);
654 
655 	netdev->flags |= IFF_ECHO;
656 	netdev->netdev_ops = &softing_netdev_ops;
657 	netdev->ethtool_ops = &softing_ethtool_ops;
658 	priv->can.do_set_mode = softing_candev_set_mode;
659 	priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
660 
661 	return netdev;
662 }
663 
664 static int softing_netdev_register(struct net_device *netdev)
665 {
666 	int ret;
667 
668 	ret = register_candev(netdev);
669 	if (ret) {
670 		dev_alert(&netdev->dev, "register failed\n");
671 		return ret;
672 	}
673 	if (sysfs_create_group(&netdev->dev.kobj, &netdev_sysfs_group) < 0)
674 		netdev_alert(netdev, "sysfs group failed\n");
675 
676 	return 0;
677 }
678 
679 static void softing_netdev_cleanup(struct net_device *netdev)
680 {
681 	sysfs_remove_group(&netdev->dev.kobj, &netdev_sysfs_group);
682 	unregister_candev(netdev);
683 	free_candev(netdev);
684 }
685 
686 /*
687  * sysfs for Platform device
688  */
689 #define DEV_ATTR_RO(name, member) \
690 static ssize_t show_##name(struct device *dev, \
691 		struct device_attribute *attr, char *buf) \
692 { \
693 	struct softing *card = dev_get_drvdata(dev); \
694 	return sprintf(buf, "%u\n", card->member); \
695 } \
696 static DEVICE_ATTR(name, 0444, show_##name, NULL)
697 
698 #define DEV_ATTR_RO_STR(name, member) \
699 static ssize_t show_##name(struct device *dev, \
700 		struct device_attribute *attr, char *buf) \
701 { \
702 	struct softing *card = dev_get_drvdata(dev); \
703 	return sprintf(buf, "%s\n", card->member); \
704 } \
705 static DEVICE_ATTR(name, 0444, show_##name, NULL)
706 
707 DEV_ATTR_RO(serial, id.serial);
708 DEV_ATTR_RO_STR(firmware, pdat->app.fw);
709 DEV_ATTR_RO(firmware_version, id.fw_version);
710 DEV_ATTR_RO_STR(hardware, pdat->name);
711 DEV_ATTR_RO(hardware_version, id.hw_version);
712 DEV_ATTR_RO(license, id.license);
713 
714 static struct attribute *softing_pdev_attrs[] = {
715 	&dev_attr_serial.attr,
716 	&dev_attr_firmware.attr,
717 	&dev_attr_firmware_version.attr,
718 	&dev_attr_hardware.attr,
719 	&dev_attr_hardware_version.attr,
720 	&dev_attr_license.attr,
721 	NULL,
722 };
723 
724 static const struct attribute_group softing_pdev_group = {
725 	.name = NULL,
726 	.attrs = softing_pdev_attrs,
727 };
728 
729 /*
730  * platform driver
731  */
732 static void softing_pdev_remove(struct platform_device *pdev)
733 {
734 	struct softing *card = platform_get_drvdata(pdev);
735 	int j;
736 
737 	/* first, disable card*/
738 	softing_card_shutdown(card);
739 
740 	for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
741 		if (!card->net[j])
742 			continue;
743 		softing_netdev_cleanup(card->net[j]);
744 		card->net[j] = NULL;
745 	}
746 	sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group);
747 
748 	iounmap(card->dpram);
749 	kfree(card);
750 }
751 
752 static int softing_pdev_probe(struct platform_device *pdev)
753 {
754 	const struct softing_platform_data *pdat = dev_get_platdata(&pdev->dev);
755 	struct softing *card;
756 	struct net_device *netdev;
757 	struct softing_priv *priv;
758 	struct resource *pres;
759 	int ret;
760 	int j;
761 
762 	if (!pdat) {
763 		dev_warn(&pdev->dev, "no platform data\n");
764 		return -EINVAL;
765 	}
766 	if (pdat->nbus > ARRAY_SIZE(card->net)) {
767 		dev_warn(&pdev->dev, "%u nets??\n", pdat->nbus);
768 		return -EINVAL;
769 	}
770 
771 	card = kzalloc(sizeof(*card), GFP_KERNEL);
772 	if (!card)
773 		return -ENOMEM;
774 	card->pdat = pdat;
775 	card->pdev = pdev;
776 	platform_set_drvdata(pdev, card);
777 	mutex_init(&card->fw.lock);
778 	spin_lock_init(&card->spin);
779 
780 	ret = -EINVAL;
781 	pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
782 	if (!pres)
783 		goto platform_resource_failed;
784 	card->dpram_phys = pres->start;
785 	card->dpram_size = resource_size(pres);
786 	card->dpram = ioremap(card->dpram_phys, card->dpram_size);
787 	if (!card->dpram) {
788 		dev_alert(&card->pdev->dev, "dpram ioremap failed\n");
789 		goto ioremap_failed;
790 	}
791 
792 	pres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
793 	if (pres)
794 		card->irq.nr = pres->start;
795 
796 	/* reset card */
797 	ret = softing_card_boot(card);
798 	if (ret < 0) {
799 		dev_alert(&pdev->dev, "failed to boot\n");
800 		goto boot_failed;
801 	}
802 
803 	/* only now, the chip's are known */
804 	card->id.freq = card->pdat->freq;
805 
806 	ret = sysfs_create_group(&pdev->dev.kobj, &softing_pdev_group);
807 	if (ret < 0) {
808 		dev_alert(&card->pdev->dev, "sysfs failed\n");
809 		goto sysfs_failed;
810 	}
811 
812 	for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
813 		card->net[j] = netdev =
814 			softing_netdev_create(card, card->id.chip[j]);
815 		if (!netdev) {
816 			dev_alert(&pdev->dev, "failed to make can[%i]", j);
817 			ret = -ENOMEM;
818 			goto netdev_failed;
819 		}
820 		netdev->dev_id = j;
821 		priv = netdev_priv(card->net[j]);
822 		priv->index = j;
823 		ret = softing_netdev_register(netdev);
824 		if (ret) {
825 			free_candev(netdev);
826 			card->net[j] = NULL;
827 			dev_alert(&card->pdev->dev,
828 					"failed to register can[%i]\n", j);
829 			goto netdev_failed;
830 		}
831 	}
832 	dev_info(&card->pdev->dev, "%s ready.\n", card->pdat->name);
833 	return 0;
834 
835 netdev_failed:
836 	for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
837 		if (!card->net[j])
838 			continue;
839 		softing_netdev_cleanup(card->net[j]);
840 	}
841 	sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group);
842 sysfs_failed:
843 	softing_card_shutdown(card);
844 boot_failed:
845 	iounmap(card->dpram);
846 ioremap_failed:
847 platform_resource_failed:
848 	kfree(card);
849 	return ret;
850 }
851 
852 static struct platform_driver softing_driver = {
853 	.driver = {
854 		.name = KBUILD_MODNAME,
855 	},
856 	.probe = softing_pdev_probe,
857 	.remove_new = softing_pdev_remove,
858 };
859 
860 module_platform_driver(softing_driver);
861 
862 MODULE_ALIAS("platform:softing");
863 MODULE_DESCRIPTION("Softing DPRAM CAN driver");
864 MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>");
865 MODULE_LICENSE("GPL v2");
866