ucmb: Fix compile on 2.6.36
[openwrt/svn-archive/archive.git] / utils / ucmb / driver / ucmb.c
1 /*
2 * Microcontroller Message Bus
3 * Linux kernel driver
4 *
5 * Copyright (c) 2009-2010 Michael Buesch <mb@bu3sch.de>
6 *
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.
11 *
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.
16 */
17
18 #include "ucmb.h"
19
20 #include <linux/version.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/miscdevice.h>
24 #include <linux/fs.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>
33
34 #include <asm/uaccess.h>
35
36
37 #define PFX "ucmb: "
38
39 #undef DEBUG
40
41
42 MODULE_LICENSE("GPL");
43 MODULE_DESCRIPTION("Microcontroller Message Bus");
44 MODULE_AUTHOR("Michael Buesch");
45
46
47 struct ucmb {
48 struct mutex mutex;
49
50 bool is_open;
51
52 unsigned int chunk_size;
53 unsigned int msg_delay_usec;
54 unsigned int gpio_reset;
55 bool reset_activelow;
56
57 /* Misc character device driver */
58 struct miscdevice mdev;
59 struct file_operations mdev_fops;
60
61 /* SPI driver */
62 struct spi_device *sdev;
63
64 /* SPI-GPIO driver */
65 struct spi_gpio_platform_data spi_gpio_pdata;
66 struct platform_device spi_gpio_pdev;
67 };
68
69 #define UCMB_MAX_MSG_DELAY (10 * 1000 * 1000) /* 10 seconds */
70
71
72 struct ucmb_message_hdr {
73 __le16 magic; /* UCMB_MAGIC */
74 __le16 len; /* Payload length (excluding header and footer) */
75 } __attribute__((packed));
76
77 struct ucmb_message_footer {
78 __le16 crc; /* CRC of the header + payload. */
79 } __attribute__((packed));
80
81 struct ucmb_status {
82 __le16 magic; /* UCMB_MAGIC */
83 __le16 code; /* enum ucmb_status_code */
84 } __attribute__((packed));
85
86 #define UCMB_MAGIC 0x1337
87
88 enum ucmb_status_code {
89 UCMB_STAT_OK = 0,
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 */
94 };
95
96
97 static int ucmb_spi_busnum_count = 1337;
98 static int ucmb_pdev_id_count;
99
100
101 static int __devinit ucmb_spi_probe(struct spi_device *sdev)
102 {
103 return 0;
104 }
105
106 static int __devexit ucmb_spi_remove(struct spi_device *sdev)
107 {
108 return 0;
109 }
110
111 static struct spi_driver ucmb_spi_driver = {
112 .driver = {
113 .name = "ucmb",
114 .bus = &spi_bus_type,
115 .owner = THIS_MODULE,
116 },
117 .probe = ucmb_spi_probe,
118 .remove = __devexit_p(ucmb_spi_remove),
119 };
120
121 static void ucmb_toggle_reset_line(struct ucmb *ucmb, bool active)
122 {
123 if (ucmb->reset_activelow)
124 active = !active;
125 gpio_set_value(ucmb->gpio_reset, active);
126 }
127
128 static int ucmb_reset_microcontroller(struct ucmb *ucmb)
129 {
130 if (ucmb->gpio_reset == UCMB_NO_RESET)
131 return -ENODEV;
132
133 ucmb_toggle_reset_line(ucmb, 1);
134 msleep(50);
135 ucmb_toggle_reset_line(ucmb, 0);
136 msleep(50);
137
138 return 0;
139 }
140
141 static int ucmb_status_code_to_errno(enum ucmb_status_code code)
142 {
143 switch (code) {
144 case UCMB_STAT_OK:
145 return 0;
146 case UCMB_STAT_EPROTO:
147 return -EPROTO;
148 case UCMB_STAT_ENOMEM:
149 return -ENOMEM;
150 case UCMB_STAT_E2BIG:
151 return -E2BIG;
152 case UCMB_STAT_ECRC:
153 return -EBADMSG;
154 }
155 return -EBUSY;
156 }
157
158 static inline struct ucmb * filp_to_ucmb(struct file *filp)
159 {
160 return container_of(filp->f_op, struct ucmb, mdev_fops);
161 }
162
163 static int ucmb_open(struct inode *inode, struct file *filp)
164 {
165 struct ucmb *ucmb = filp_to_ucmb(filp);
166 int err = 0;
167
168 mutex_lock(&ucmb->mutex);
169
170 if (ucmb->is_open) {
171 err = -EBUSY;
172 goto out_unlock;
173 }
174 ucmb->is_open = 1;
175 ucmb->msg_delay_usec = 0;
176
177 out_unlock:
178 mutex_unlock(&ucmb->mutex);
179
180 return err;
181 }
182
183 static int ucmb_release(struct inode *inode, struct file *filp)
184 {
185 struct ucmb *ucmb = filp_to_ucmb(filp);
186
187 mutex_lock(&ucmb->mutex);
188 WARN_ON(!ucmb->is_open);
189 ucmb->is_open = 0;
190 mutex_unlock(&ucmb->mutex);
191
192 return 0;
193 }
194
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)
198 #else
199 static int ucmb_ioctl(struct inode *inode, struct file *filp,
200 unsigned int cmd, unsigned long arg)
201 #endif
202 {
203 struct ucmb *ucmb = filp_to_ucmb(filp);
204 int ret = 0;
205
206 mutex_lock(&ucmb->mutex);
207 switch (cmd) {
208 case UCMB_IOCTL_RESETUC:
209 ret = ucmb_reset_microcontroller(ucmb);
210 break;
211 case UCMB_IOCTL_GMSGDELAY:
212 if (put_user(ucmb->msg_delay_usec, (unsigned int __user *)arg)) {
213 ret = -EFAULT;
214 break;
215 }
216 break;
217 case UCMB_IOCTL_SMSGDELAY: {
218 unsigned int msg_delay_usec;
219
220 if (get_user(msg_delay_usec, (unsigned int __user *)arg)) {
221 ret = -EFAULT;
222 break;
223 }
224 if (msg_delay_usec > UCMB_MAX_MSG_DELAY) {
225 ret = -E2BIG;
226 break;
227 }
228 ucmb->msg_delay_usec = msg_delay_usec;
229 break;
230 }
231 default:
232 ret = -EINVAL;
233 }
234 mutex_unlock(&ucmb->mutex);
235
236 return ret;
237 }
238
239 static int ucmb_spi_write(struct ucmb *ucmb, const void *_buf, size_t size)
240 {
241 const u8 *buf = _buf;
242 size_t i, chunk_size, current_size;
243 int err = 0;
244
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);
251 if (err)
252 goto out;
253 if (ucmb->chunk_size && need_resched())
254 msleep(1);
255 }
256 out:
257 return err;
258 }
259
260 static int ucmb_spi_read(struct ucmb *ucmb, void *_buf, size_t size)
261 {
262 u8 *buf = _buf;
263 size_t i, chunk_size, current_size;
264 int err = 0;
265
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);
272 if (err)
273 goto out;
274 if (ucmb->chunk_size && need_resched())
275 msleep(1);
276 }
277 out:
278 return err;
279 }
280
281 static ssize_t ucmb_read(struct file *filp, char __user *user_buf,
282 size_t size, loff_t *offp)
283 {
284 struct ucmb *ucmb = filp_to_ucmb(filp);
285 u8 *buf;
286 int res, err;
287 struct ucmb_message_hdr hdr;
288 struct ucmb_message_footer footer;
289 struct ucmb_status status = { .magic = cpu_to_le16(UCMB_MAGIC), };
290 u16 crc = 0xFFFF;
291
292 mutex_lock(&ucmb->mutex);
293
294 size = min_t(size_t, size, PAGE_SIZE);
295
296 err = -ENOMEM;
297 buf = (char *)__get_free_page(GFP_KERNEL);
298 if (!buf)
299 goto out;
300
301 err = ucmb_spi_read(ucmb, &hdr, sizeof(hdr));
302 if (err)
303 goto out_free;
304 #ifdef DEBUG
305 printk(KERN_DEBUG PFX "Received message header 0x%04X 0x%04X\n",
306 le16_to_cpu(hdr.magic), le16_to_cpu(hdr.len));
307 #endif
308 err = -EPROTO;
309 if (hdr.magic != cpu_to_le16(UCMB_MAGIC))
310 goto out_free;
311 err = -ENOBUFS;
312 if (size < le16_to_cpu(hdr.len))
313 goto out_free;
314 size = le16_to_cpu(hdr.len);
315 err = ucmb_spi_read(ucmb, buf, size);
316 if (err)
317 goto out_free;
318 err = ucmb_spi_read(ucmb, &footer, sizeof(footer));
319 if (err)
320 goto out_free;
321
322 crc = crc16(crc, (u8 *)&hdr, sizeof(hdr));
323 crc = crc16(crc, buf, size);
324 crc ^= 0xFFFF;
325 if (crc != le16_to_cpu(footer.crc)) {
326 err = -EPROTO;
327 status.code = UCMB_STAT_ECRC;
328 goto out_send_status;
329 }
330
331 if (copy_to_user(user_buf, buf, size)) {
332 err = -EFAULT;
333 status.code = UCMB_STAT_ENOMEM;
334 goto out_send_status;
335 }
336
337 status.code = UCMB_STAT_OK;
338 err = 0;
339
340 out_send_status:
341 res = ucmb_spi_write(ucmb, &status, sizeof(status));
342 if (res && !err)
343 err = res;
344 out_free:
345 free_page((unsigned long)buf);
346 out:
347 mutex_unlock(&ucmb->mutex);
348
349 return err ? err : size;
350 }
351
352 static ssize_t ucmb_write(struct file *filp, const char __user *user_buf,
353 size_t size, loff_t *offp)
354 {
355 struct ucmb *ucmb = filp_to_ucmb(filp);
356 u8 *buf;
357 int err;
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;
361
362 mutex_lock(&ucmb->mutex);
363
364 err = -ENOMEM;
365 buf = (char *)__get_free_page(GFP_KERNEL);
366 if (!buf)
367 goto out;
368
369 size = min_t(size_t, PAGE_SIZE, size);
370 err = -EFAULT;
371 if (copy_from_user(buf, user_buf, size))
372 goto out_free;
373 hdr.len = cpu_to_le16(size);
374
375 footer.crc = crc16(footer.crc, (u8 *)&hdr, sizeof(hdr));
376 footer.crc = crc16(footer.crc, buf, size);
377 footer.crc ^= 0xFFFF;
378
379 err = ucmb_spi_write(ucmb, &hdr, sizeof(hdr));
380 if (err)
381 goto out_free;
382 err = ucmb_spi_write(ucmb, buf, size);
383 if (err)
384 goto out_free;
385 err = ucmb_spi_write(ucmb, &footer, sizeof(footer));
386 if (err)
387 goto out_free;
388
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));
396 } else
397 udelay(ucmb->msg_delay_usec);
398 }
399
400 /* Get the status code. */
401 err = ucmb_spi_read(ucmb, &status, sizeof(status));
402 if (err)
403 goto out_free;
404 #ifdef DEBUG
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));
407 #endif
408 err = -EPROTO;
409 if (status.magic != cpu_to_le16(UCMB_MAGIC))
410 goto out_free;
411 err = ucmb_status_code_to_errno(le16_to_cpu(status.code));
412 if (err)
413 goto out_free;
414
415 out_free:
416 free_page((unsigned long)buf);
417 out:
418 mutex_unlock(&ucmb->mutex);
419
420 return err ? err : size;
421 }
422
423 static int __devinit ucmb_probe(struct platform_device *pdev)
424 {
425 struct ucmb_platform_data *pdata;
426 struct ucmb *ucmb;
427 int err;
428 const int bus_num = ucmb_spi_busnum_count++;
429 struct spi_bitbang *bb;
430
431 pdata = pdev->dev.platform_data;
432 if (!pdata)
433 return -ENXIO;
434
435 ucmb = kzalloc(sizeof(struct ucmb), GFP_KERNEL);
436 if (!ucmb)
437 return -ENOMEM;
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;
442
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;
448 #endif
449
450 /* Create the SPI GPIO bus master. */
451
452 #ifdef CONFIG_SPI_GPIO_MODULE
453 err = request_module("spi_gpio");
454 if (err)
455 printk(KERN_WARNING PFX "Failed to request spi_gpio module\n");
456 #endif /* CONFIG_SPI_GPIO_MODULE */
457
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;
462
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;
466
467 err = platform_device_register(&ucmb->spi_gpio_pdev);
468 if (err) {
469 printk(KERN_ERR PFX "Failed to register SPI-GPIO platform device\n");
470 goto err_free_ucmb;
471 }
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;
476 }
477
478 /* Create the SPI device. */
479
480 ucmb->sdev = spi_alloc_device(bb->master);
481 if (!ucmb->sdev) {
482 printk(KERN_ERR PFX "Failed to allocate SPI device\n");
483 goto err_unreg_spi_gpio_pdev;
484 }
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);
492 if (err) {
493 printk(KERN_ERR PFX "Failed to add SPI device\n");
494 goto err_free_spi_device;
495 }
496
497 /* Initialize the RESET line. */
498
499 if (pdata->gpio_reset != UCMB_NO_RESET) {
500 err = gpio_request(pdata->gpio_reset, pdata->name);
501 if (err) {
502 printk(KERN_ERR PFX
503 "Failed to request RESET GPIO line\n");
504 goto err_unreg_spi_device;
505 }
506 err = gpio_direction_output(pdata->gpio_reset,
507 pdata->reset_activelow);
508 if (err) {
509 printk(KERN_ERR PFX
510 "Failed to set RESET GPIO direction\n");
511 goto err_free_reset_gpio;
512 }
513 ucmb_reset_microcontroller(ucmb);
514 }
515
516 /* Create the Misc char device. */
517
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;
527 #else
528 ucmb->mdev_fops.ioctl = ucmb_ioctl;
529 #endif
530 ucmb->mdev.fops = &ucmb->mdev_fops;
531
532 err = misc_register(&ucmb->mdev);
533 if (err) {
534 printk(KERN_ERR PFX "Failed to register miscdev %s\n",
535 ucmb->mdev.name);
536 goto err_free_reset_gpio;
537 }
538
539 platform_set_drvdata(pdev, ucmb);
540
541 printk(KERN_INFO PFX "Registered message bus \"%s\"\n", pdata->name);
542
543 return 0;
544
545 err_free_reset_gpio:
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);
550 err_free_spi_device:
551 spi_dev_put(ucmb->sdev);
552 err_unreg_spi_gpio_pdev:
553 platform_device_unregister(&ucmb->spi_gpio_pdev);
554 err_free_ucmb:
555 kfree(ucmb);
556
557 return err;
558 }
559
560 static int __devexit ucmb_remove(struct platform_device *pdev)
561 {
562 struct ucmb *ucmb = platform_get_drvdata(pdev);
563 int err;
564
565 err = misc_deregister(&ucmb->mdev);
566 if (err) {
567 printk(KERN_ERR PFX "Failed to unregister miscdev %s\n",
568 ucmb->mdev.name);
569 }
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);
575
576 kfree(ucmb);
577 platform_set_drvdata(pdev, NULL);
578
579 return 0;
580 }
581
582 static struct platform_driver ucmb_driver = {
583 .driver = {
584 .name = "ucmb",
585 .owner = THIS_MODULE,
586 },
587 .probe = ucmb_probe,
588 .remove = __devexit_p(ucmb_remove),
589 };
590
591 int ucmb_device_register(struct ucmb_platform_data *pdata)
592 {
593 struct platform_device *pdev;
594 int err;
595
596 pdev = platform_device_alloc("ucmb", ucmb_pdev_id_count++);
597 if (!pdev) {
598 printk(KERN_ERR PFX "Failed to allocate platform device.\n");
599 return -ENOMEM;
600 }
601 err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
602 if (err) {
603 printk(KERN_ERR PFX "Failed to add platform data.\n");
604 platform_device_put(pdev);
605 return err;
606 }
607 err = platform_device_add(pdev);
608 if (err) {
609 printk(KERN_ERR PFX "Failed to register platform device.\n");
610 platform_device_put(pdev);
611 return err;
612 }
613 pdata->pdev = pdev;
614
615 return 0;
616 }
617 EXPORT_SYMBOL(ucmb_device_register);
618
619 void ucmb_device_unregister(struct ucmb_platform_data *pdata)
620 {
621 if (!pdata->pdev)
622 return;
623 platform_device_unregister(pdata->pdev);
624 platform_device_put(pdata->pdev);
625 pdata->pdev = NULL;
626 }
627 EXPORT_SYMBOL(ucmb_device_unregister);
628
629 static int ucmb_modinit(void)
630 {
631 int err;
632
633 printk(KERN_INFO "Microcontroller message bus driver\n");
634
635 err = spi_register_driver(&ucmb_spi_driver);
636 if (err) {
637 printk(KERN_ERR PFX "Failed to register SPI driver\n");
638 return err;
639 }
640 err = platform_driver_register(&ucmb_driver);
641 if (err) {
642 printk(KERN_ERR PFX "Failed to register platform driver\n");
643 spi_unregister_driver(&ucmb_spi_driver);
644 return err;
645 }
646
647 return 0;
648 }
649 subsys_initcall(ucmb_modinit);
650
651 static void ucmb_modexit(void)
652 {
653 platform_driver_unregister(&ucmb_driver);
654 spi_unregister_driver(&ucmb_spi_driver);
655 }
656 module_exit(ucmb_modexit);