2 * Microcontroller Message Bus
5 * Copyright (c) 2009-2010 Michael Buesch <mb@bu3sch.de>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
20 #include <linux/version.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/miscdevice.h>
25 #include <linux/spi/spi.h>
26 #include <linux/spi/spi_gpio.h>
27 #include <linux/spi/spi_bitbang.h>
28 #include <linux/gpio.h>
29 #include <linux/gfp.h>
30 #include <linux/delay.h>
31 #include <linux/crc16.h>
32 #include <linux/sched.h>
34 #include <asm/uaccess.h>
42 MODULE_LICENSE("GPL");
43 MODULE_DESCRIPTION("Microcontroller Message Bus");
44 MODULE_AUTHOR("Michael Buesch");
52 unsigned int chunk_size
;
53 unsigned int msg_delay_usec
;
54 unsigned int gpio_reset
;
57 /* Misc character device driver */
58 struct miscdevice mdev
;
59 struct file_operations mdev_fops
;
62 struct spi_device
*sdev
;
65 struct spi_gpio_platform_data spi_gpio_pdata
;
66 struct platform_device spi_gpio_pdev
;
69 #define UCMB_MAX_MSG_DELAY (10 * 1000 * 1000) /* 10 seconds */
72 struct ucmb_message_hdr
{
73 __le16 magic
; /* UCMB_MAGIC */
74 __le16 len
; /* Payload length (excluding header and footer) */
75 } __attribute__((packed
));
77 struct ucmb_message_footer
{
78 __le16 crc
; /* CRC of the header + payload. */
79 } __attribute__((packed
));
82 __le16 magic
; /* UCMB_MAGIC */
83 __le16 code
; /* enum ucmb_status_code */
84 } __attribute__((packed
));
86 #define UCMB_MAGIC 0x1337
88 enum ucmb_status_code
{
90 UCMB_STAT_EPROTO
, /* Protocol format error */
91 UCMB_STAT_ENOMEM
, /* Out of memory */
92 UCMB_STAT_E2BIG
, /* Message too big */
93 UCMB_STAT_ECRC
, /* CRC error */
97 static int ucmb_spi_busnum_count
= 1337;
98 static int ucmb_pdev_id_count
;
101 static int __devinit
ucmb_spi_probe(struct spi_device
*sdev
)
106 static int __devexit
ucmb_spi_remove(struct spi_device
*sdev
)
111 static struct spi_driver ucmb_spi_driver
= {
114 .bus
= &spi_bus_type
,
115 .owner
= THIS_MODULE
,
117 .probe
= ucmb_spi_probe
,
118 .remove
= __devexit_p(ucmb_spi_remove
),
121 static void ucmb_toggle_reset_line(struct ucmb
*ucmb
, bool active
)
123 if (ucmb
->reset_activelow
)
125 gpio_set_value(ucmb
->gpio_reset
, active
);
128 static int ucmb_reset_microcontroller(struct ucmb
*ucmb
)
130 if (ucmb
->gpio_reset
== UCMB_NO_RESET
)
133 ucmb_toggle_reset_line(ucmb
, 1);
135 ucmb_toggle_reset_line(ucmb
, 0);
141 static int ucmb_status_code_to_errno(enum ucmb_status_code code
)
146 case UCMB_STAT_EPROTO
:
148 case UCMB_STAT_ENOMEM
:
150 case UCMB_STAT_E2BIG
:
158 static inline struct ucmb
* filp_to_ucmb(struct file
*filp
)
160 return container_of(filp
->f_op
, struct ucmb
, mdev_fops
);
163 static int ucmb_open(struct inode
*inode
, struct file
*filp
)
165 struct ucmb
*ucmb
= filp_to_ucmb(filp
);
168 mutex_lock(&ucmb
->mutex
);
175 ucmb
->msg_delay_usec
= 0;
178 mutex_unlock(&ucmb
->mutex
);
183 static int ucmb_release(struct inode
*inode
, struct file
*filp
)
185 struct ucmb
*ucmb
= filp_to_ucmb(filp
);
187 mutex_lock(&ucmb
->mutex
);
188 WARN_ON(!ucmb
->is_open
);
190 mutex_unlock(&ucmb
->mutex
);
195 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
196 static long ucmb_ioctl(struct file
*filp
,
197 unsigned int cmd
, unsigned long arg
)
199 static int ucmb_ioctl(struct inode
*inode
, struct file
*filp
,
200 unsigned int cmd
, unsigned long arg
)
203 struct ucmb
*ucmb
= filp_to_ucmb(filp
);
206 mutex_lock(&ucmb
->mutex
);
208 case UCMB_IOCTL_RESETUC
:
209 ret
= ucmb_reset_microcontroller(ucmb
);
211 case UCMB_IOCTL_GMSGDELAY
:
212 if (put_user(ucmb
->msg_delay_usec
, (unsigned int __user
*)arg
)) {
217 case UCMB_IOCTL_SMSGDELAY
: {
218 unsigned int msg_delay_usec
;
220 if (get_user(msg_delay_usec
, (unsigned int __user
*)arg
)) {
224 if (msg_delay_usec
> UCMB_MAX_MSG_DELAY
) {
228 ucmb
->msg_delay_usec
= msg_delay_usec
;
234 mutex_unlock(&ucmb
->mutex
);
239 static int ucmb_spi_write(struct ucmb
*ucmb
, const void *_buf
, size_t size
)
241 const u8
*buf
= _buf
;
242 size_t i
, chunk_size
, current_size
;
245 chunk_size
= ucmb
->chunk_size
? : size
;
246 for (i
= 0; i
< size
; i
+= chunk_size
) {
247 current_size
= chunk_size
;
248 if (i
+ current_size
> size
)
249 current_size
= size
- i
;
250 err
= spi_write(ucmb
->sdev
, buf
+ i
, current_size
);
253 if (ucmb
->chunk_size
&& need_resched())
260 static int ucmb_spi_read(struct ucmb
*ucmb
, void *_buf
, size_t size
)
263 size_t i
, chunk_size
, current_size
;
266 chunk_size
= ucmb
->chunk_size
? : size
;
267 for (i
= 0; i
< size
; i
+= chunk_size
) {
268 current_size
= chunk_size
;
269 if (i
+ current_size
> size
)
270 current_size
= size
- i
;
271 err
= spi_read(ucmb
->sdev
, buf
+ i
, current_size
);
274 if (ucmb
->chunk_size
&& need_resched())
281 static ssize_t
ucmb_read(struct file
*filp
, char __user
*user_buf
,
282 size_t size
, loff_t
*offp
)
284 struct ucmb
*ucmb
= filp_to_ucmb(filp
);
287 struct ucmb_message_hdr hdr
;
288 struct ucmb_message_footer footer
;
289 struct ucmb_status status
= { .magic
= cpu_to_le16(UCMB_MAGIC
), };
292 mutex_lock(&ucmb
->mutex
);
294 size
= min_t(size_t, size
, PAGE_SIZE
);
297 buf
= (char *)__get_free_page(GFP_KERNEL
);
301 err
= ucmb_spi_read(ucmb
, &hdr
, sizeof(hdr
));
305 printk(KERN_DEBUG PFX
"Received message header 0x%04X 0x%04X\n",
306 le16_to_cpu(hdr
.magic
), le16_to_cpu(hdr
.len
));
309 if (hdr
.magic
!= cpu_to_le16(UCMB_MAGIC
))
312 if (size
< le16_to_cpu(hdr
.len
))
314 size
= le16_to_cpu(hdr
.len
);
315 err
= ucmb_spi_read(ucmb
, buf
, size
);
318 err
= ucmb_spi_read(ucmb
, &footer
, sizeof(footer
));
322 crc
= crc16(crc
, (u8
*)&hdr
, sizeof(hdr
));
323 crc
= crc16(crc
, buf
, size
);
325 if (crc
!= le16_to_cpu(footer
.crc
)) {
327 status
.code
= UCMB_STAT_ECRC
;
328 goto out_send_status
;
331 if (copy_to_user(user_buf
, buf
, size
)) {
333 status
.code
= UCMB_STAT_ENOMEM
;
334 goto out_send_status
;
337 status
.code
= UCMB_STAT_OK
;
341 res
= ucmb_spi_write(ucmb
, &status
, sizeof(status
));
345 free_page((unsigned long)buf
);
347 mutex_unlock(&ucmb
->mutex
);
349 return err
? err
: size
;
352 static ssize_t
ucmb_write(struct file
*filp
, const char __user
*user_buf
,
353 size_t size
, loff_t
*offp
)
355 struct ucmb
*ucmb
= filp_to_ucmb(filp
);
358 struct ucmb_message_hdr hdr
= { .magic
= cpu_to_le16(UCMB_MAGIC
), };
359 struct ucmb_message_footer footer
= { .crc
= 0xFFFF, };
360 struct ucmb_status status
;
362 mutex_lock(&ucmb
->mutex
);
365 buf
= (char *)__get_free_page(GFP_KERNEL
);
369 size
= min_t(size_t, PAGE_SIZE
, size
);
371 if (copy_from_user(buf
, user_buf
, size
))
373 hdr
.len
= cpu_to_le16(size
);
375 footer
.crc
= crc16(footer
.crc
, (u8
*)&hdr
, sizeof(hdr
));
376 footer
.crc
= crc16(footer
.crc
, buf
, size
);
377 footer
.crc
^= 0xFFFF;
379 err
= ucmb_spi_write(ucmb
, &hdr
, sizeof(hdr
));
382 err
= ucmb_spi_write(ucmb
, buf
, size
);
385 err
= ucmb_spi_write(ucmb
, &footer
, sizeof(footer
));
389 if (ucmb
->msg_delay_usec
) {
390 /* The microcontroller deserves some time to process the message. */
391 if (ucmb
->msg_delay_usec
>= 1000000) {
392 ssleep(ucmb
->msg_delay_usec
/ 1000000);
393 msleep(DIV_ROUND_UP(ucmb
->msg_delay_usec
% 1000000, 1000));
394 } else if (ucmb
->msg_delay_usec
>= 1000) {
395 msleep(DIV_ROUND_UP(ucmb
->msg_delay_usec
, 1000));
397 udelay(ucmb
->msg_delay_usec
);
400 /* Get the status code. */
401 err
= ucmb_spi_read(ucmb
, &status
, sizeof(status
));
405 printk(KERN_DEBUG PFX
"Sent message. Status report: 0x%04X 0x%04X\n",
406 le16_to_cpu(status
.magic
), le16_to_cpu(status
.code
));
409 if (status
.magic
!= cpu_to_le16(UCMB_MAGIC
))
411 err
= ucmb_status_code_to_errno(le16_to_cpu(status
.code
));
416 free_page((unsigned long)buf
);
418 mutex_unlock(&ucmb
->mutex
);
420 return err
? err
: size
;
423 static int __devinit
ucmb_probe(struct platform_device
*pdev
)
425 struct ucmb_platform_data
*pdata
;
428 const int bus_num
= ucmb_spi_busnum_count
++;
429 struct spi_bitbang
*bb
;
431 pdata
= pdev
->dev
.platform_data
;
435 ucmb
= kzalloc(sizeof(struct ucmb
), GFP_KERNEL
);
438 mutex_init(&ucmb
->mutex
);
439 ucmb
->gpio_reset
= pdata
->gpio_reset
;
440 ucmb
->reset_activelow
= pdata
->reset_activelow
;
441 ucmb
->chunk_size
= pdata
->chunk_size
;
443 #ifdef CONFIG_PREEMPT
444 /* A preemptible kernel does not need to sleep between
445 * chunks, because it can sleep at desire (if IRQs are enabled).
446 * So transmit/receive it all in one go. */
447 ucmb
->chunk_size
= 0;
450 /* Create the SPI GPIO bus master. */
452 #ifdef CONFIG_SPI_GPIO_MODULE
453 err
= request_module("spi_gpio");
455 printk(KERN_WARNING PFX
"Failed to request spi_gpio module\n");
456 #endif /* CONFIG_SPI_GPIO_MODULE */
458 ucmb
->spi_gpio_pdata
.sck
= pdata
->gpio_sck
;
459 ucmb
->spi_gpio_pdata
.mosi
= pdata
->gpio_mosi
;
460 ucmb
->spi_gpio_pdata
.miso
= pdata
->gpio_miso
;
461 ucmb
->spi_gpio_pdata
.num_chipselect
= 1;
463 ucmb
->spi_gpio_pdev
.name
= "spi_gpio";
464 ucmb
->spi_gpio_pdev
.id
= bus_num
;
465 ucmb
->spi_gpio_pdev
.dev
.platform_data
= &ucmb
->spi_gpio_pdata
;
467 err
= platform_device_register(&ucmb
->spi_gpio_pdev
);
469 printk(KERN_ERR PFX
"Failed to register SPI-GPIO platform device\n");
472 bb
= platform_get_drvdata(&ucmb
->spi_gpio_pdev
);
473 if (!bb
|| !bb
->master
) {
474 printk(KERN_ERR PFX
"No bitbanged master device found.\n");
475 goto err_unreg_spi_gpio_pdev
;
478 /* Create the SPI device. */
480 ucmb
->sdev
= spi_alloc_device(bb
->master
);
482 printk(KERN_ERR PFX
"Failed to allocate SPI device\n");
483 goto err_unreg_spi_gpio_pdev
;
485 ucmb
->sdev
->max_speed_hz
= pdata
->max_speed_hz
;
486 ucmb
->sdev
->chip_select
= 0;
487 ucmb
->sdev
->mode
= pdata
->mode
;
488 strlcpy(ucmb
->sdev
->modalias
, "ucmb", /* We are the SPI driver. */
489 sizeof(ucmb
->sdev
->modalias
));
490 ucmb
->sdev
->controller_data
= (void *)pdata
->gpio_cs
;
491 err
= spi_add_device(ucmb
->sdev
);
493 printk(KERN_ERR PFX
"Failed to add SPI device\n");
494 goto err_free_spi_device
;
497 /* Initialize the RESET line. */
499 if (pdata
->gpio_reset
!= UCMB_NO_RESET
) {
500 err
= gpio_request(pdata
->gpio_reset
, pdata
->name
);
503 "Failed to request RESET GPIO line\n");
504 goto err_unreg_spi_device
;
506 err
= gpio_direction_output(pdata
->gpio_reset
,
507 pdata
->reset_activelow
);
510 "Failed to set RESET GPIO direction\n");
511 goto err_free_reset_gpio
;
513 ucmb_reset_microcontroller(ucmb
);
516 /* Create the Misc char device. */
518 ucmb
->mdev
.minor
= MISC_DYNAMIC_MINOR
;
519 ucmb
->mdev
.name
= pdata
->name
;
520 ucmb
->mdev
.parent
= &pdev
->dev
;
521 ucmb
->mdev_fops
.open
= ucmb_open
;
522 ucmb
->mdev_fops
.release
= ucmb_release
;
523 ucmb
->mdev_fops
.read
= ucmb_read
;
524 ucmb
->mdev_fops
.write
= ucmb_write
;
525 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
526 ucmb
->mdev_fops
.unlocked_ioctl
= ucmb_ioctl
;
528 ucmb
->mdev_fops
.ioctl
= ucmb_ioctl
;
530 ucmb
->mdev
.fops
= &ucmb
->mdev_fops
;
532 err
= misc_register(&ucmb
->mdev
);
534 printk(KERN_ERR PFX
"Failed to register miscdev %s\n",
536 goto err_free_reset_gpio
;
539 platform_set_drvdata(pdev
, ucmb
);
541 printk(KERN_INFO PFX
"Registered message bus \"%s\"\n", pdata
->name
);
546 if (pdata
->gpio_reset
!= UCMB_NO_RESET
)
547 gpio_free(pdata
->gpio_reset
);
548 err_unreg_spi_device
:
549 spi_unregister_device(ucmb
->sdev
);
551 spi_dev_put(ucmb
->sdev
);
552 err_unreg_spi_gpio_pdev
:
553 platform_device_unregister(&ucmb
->spi_gpio_pdev
);
560 static int __devexit
ucmb_remove(struct platform_device
*pdev
)
562 struct ucmb
*ucmb
= platform_get_drvdata(pdev
);
565 err
= misc_deregister(&ucmb
->mdev
);
567 printk(KERN_ERR PFX
"Failed to unregister miscdev %s\n",
570 if (ucmb
->gpio_reset
!= UCMB_NO_RESET
)
571 gpio_free(ucmb
->gpio_reset
);
572 spi_unregister_device(ucmb
->sdev
);
573 spi_dev_put(ucmb
->sdev
);
574 platform_device_unregister(&ucmb
->spi_gpio_pdev
);
577 platform_set_drvdata(pdev
, NULL
);
582 static struct platform_driver ucmb_driver
= {
585 .owner
= THIS_MODULE
,
588 .remove
= __devexit_p(ucmb_remove
),
591 int ucmb_device_register(struct ucmb_platform_data
*pdata
)
593 struct platform_device
*pdev
;
596 pdev
= platform_device_alloc("ucmb", ucmb_pdev_id_count
++);
598 printk(KERN_ERR PFX
"Failed to allocate platform device.\n");
601 err
= platform_device_add_data(pdev
, pdata
, sizeof(*pdata
));
603 printk(KERN_ERR PFX
"Failed to add platform data.\n");
604 platform_device_put(pdev
);
607 err
= platform_device_add(pdev
);
609 printk(KERN_ERR PFX
"Failed to register platform device.\n");
610 platform_device_put(pdev
);
617 EXPORT_SYMBOL(ucmb_device_register
);
619 void ucmb_device_unregister(struct ucmb_platform_data
*pdata
)
623 platform_device_unregister(pdata
->pdev
);
624 platform_device_put(pdata
->pdev
);
627 EXPORT_SYMBOL(ucmb_device_unregister
);
629 static int ucmb_modinit(void)
633 printk(KERN_INFO
"Microcontroller message bus driver\n");
635 err
= spi_register_driver(&ucmb_spi_driver
);
637 printk(KERN_ERR PFX
"Failed to register SPI driver\n");
640 err
= platform_driver_register(&ucmb_driver
);
642 printk(KERN_ERR PFX
"Failed to register platform driver\n");
643 spi_unregister_driver(&ucmb_spi_driver
);
649 subsys_initcall(ucmb_modinit
);
651 static void ucmb_modexit(void)
653 platform_driver_unregister(&ucmb_driver
);
654 spi_unregister_driver(&ucmb_spi_driver
);
656 module_exit(ucmb_modexit
);