[package] fix ucmb compilation (#4711)
[openwrt/svn-archive/archive.git] / utils / ucmb / driver / ucmb.c
1 /*
2 * Microcontroller Message Bus
3 * Linux kernel driver
4 *
5 * Copyright (c) 2009 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/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/miscdevice.h>
23 #include <linux/fs.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spi/spi_gpio.h>
26 #include <linux/spi/spi_bitbang.h>
27 #include <linux/gpio.h>
28 #include <linux/gfp.h>
29 #include <linux/delay.h>
30 #include <linux/crc16.h>
31
32 #include <asm/uaccess.h>
33
34
35 #define PFX "ucmb: "
36
37 #undef DEBUG
38
39
40 MODULE_LICENSE("GPL");
41 MODULE_DESCRIPTION("Microcontroller Message Bus");
42 MODULE_AUTHOR("Michael Buesch");
43
44
45 struct ucmb {
46 struct mutex mutex;
47
48 bool is_open;
49
50 unsigned int msg_delay_usec;
51 unsigned int gpio_reset;
52 bool reset_activelow;
53
54 /* Misc character device driver */
55 struct miscdevice mdev;
56 struct file_operations mdev_fops;
57
58 /* SPI driver */
59 struct spi_device *sdev;
60
61 /* SPI-GPIO driver */
62 struct spi_gpio_platform_data spi_gpio_pdata;
63 struct platform_device spi_gpio_pdev;
64 };
65
66 #define UCMB_MAX_MSG_DELAY (10 * 1000 * 1000) /* 10 seconds */
67
68
69 struct ucmb_message_hdr {
70 __le16 magic; /* UCMB_MAGIC */
71 __le16 len; /* Payload length (excluding header and footer) */
72 } __attribute__((packed));
73
74 struct ucmb_message_footer {
75 __le16 crc; /* CRC of the header + payload. */
76 } __attribute__((packed));
77
78 struct ucmb_status {
79 __le16 magic; /* UCMB_MAGIC */
80 __le16 code; /* enum ucmb_status_code */
81 } __attribute__((packed));
82
83 #define UCMB_MAGIC 0x1337
84
85 enum ucmb_status_code {
86 UCMB_STAT_OK = 0,
87 UCMB_STAT_EPROTO, /* Protocol format error */
88 UCMB_STAT_ENOMEM, /* Out of memory */
89 UCMB_STAT_E2BIG, /* Message too big */
90 UCMB_STAT_ECRC, /* CRC error */
91 };
92
93
94 static int ucmb_spi_busnum_count = 1337;
95 static int ucmb_pdev_id_count;
96
97
98 static int __devinit ucmb_spi_probe(struct spi_device *sdev)
99 {
100 return 0;
101 }
102
103 static int __devexit ucmb_spi_remove(struct spi_device *sdev)
104 {
105 return 0;
106 }
107
108 static struct spi_driver ucmb_spi_driver = {
109 .driver = {
110 .name = "ucmb",
111 .bus = &spi_bus_type,
112 .owner = THIS_MODULE,
113 },
114 .probe = ucmb_spi_probe,
115 .remove = __devexit_p(ucmb_spi_remove),
116 };
117
118 static void ucmb_toggle_reset_line(struct ucmb *ucmb, bool active)
119 {
120 if (ucmb->reset_activelow)
121 active = !active;
122 gpio_set_value(ucmb->gpio_reset, active);
123 }
124
125 static int ucmb_reset_microcontroller(struct ucmb *ucmb)
126 {
127 if (ucmb->gpio_reset == UCMB_NO_RESET)
128 return -ENODEV;
129
130 ucmb_toggle_reset_line(ucmb, 1);
131 msleep(50);
132 ucmb_toggle_reset_line(ucmb, 0);
133 msleep(50);
134
135 return 0;
136 }
137
138 static int ucmb_status_code_to_errno(enum ucmb_status_code code)
139 {
140 switch (code) {
141 case UCMB_STAT_OK:
142 return 0;
143 case UCMB_STAT_EPROTO:
144 return -EPROTO;
145 case UCMB_STAT_ENOMEM:
146 return -ENOMEM;
147 case UCMB_STAT_E2BIG:
148 return -E2BIG;
149 case UCMB_STAT_ECRC:
150 return -EBADMSG;
151 }
152 return -EBUSY;
153 }
154
155 static inline struct ucmb * filp_to_ucmb(struct file *filp)
156 {
157 return container_of(filp->f_op, struct ucmb, mdev_fops);
158 }
159
160 static int ucmb_open(struct inode *inode, struct file *filp)
161 {
162 struct ucmb *ucmb = filp_to_ucmb(filp);
163 int err = 0;
164
165 mutex_lock(&ucmb->mutex);
166
167 if (ucmb->is_open) {
168 err = -EBUSY;
169 goto out_unlock;
170 }
171 ucmb->is_open = 1;
172 ucmb->msg_delay_usec = 0;
173
174 out_unlock:
175 mutex_unlock(&ucmb->mutex);
176
177 return err;
178 }
179
180 static int ucmb_release(struct inode *inode, struct file *filp)
181 {
182 struct ucmb *ucmb = filp_to_ucmb(filp);
183
184 mutex_lock(&ucmb->mutex);
185 WARN_ON(!ucmb->is_open);
186 ucmb->is_open = 0;
187 mutex_unlock(&ucmb->mutex);
188
189 return 0;
190 }
191
192 static int ucmb_ioctl(struct inode *inode, struct file *filp,
193 unsigned int cmd, unsigned long arg)
194 {
195 struct ucmb *ucmb = filp_to_ucmb(filp);
196 int ret = 0;
197
198 mutex_lock(&ucmb->mutex);
199 switch (cmd) {
200 case UCMB_IOCTL_RESETUC:
201 ret = ucmb_reset_microcontroller(ucmb);
202 break;
203 case UCMB_IOCTL_GMSGDELAY:
204 if (put_user(ucmb->msg_delay_usec, (unsigned int __user *)arg)) {
205 ret = -EFAULT;
206 break;
207 }
208 break;
209 case UCMB_IOCTL_SMSGDELAY: {
210 unsigned int msg_delay_usec;
211
212 if (get_user(msg_delay_usec, (unsigned int __user *)arg)) {
213 ret = -EFAULT;
214 break;
215 }
216 if (msg_delay_usec > UCMB_MAX_MSG_DELAY) {
217 ret = -E2BIG;
218 break;
219 }
220 ucmb->msg_delay_usec = msg_delay_usec;
221 break;
222 }
223 default:
224 ret = -EINVAL;
225 }
226 mutex_unlock(&ucmb->mutex);
227
228 return ret;
229 }
230
231 static ssize_t ucmb_read(struct file *filp, char __user *user_buf,
232 size_t size, loff_t *offp)
233 {
234 struct ucmb *ucmb = filp_to_ucmb(filp);
235 u8 *buf;
236 int res, err;
237 struct ucmb_message_hdr hdr;
238 struct ucmb_message_footer footer;
239 struct ucmb_status status = { .magic = cpu_to_le16(UCMB_MAGIC), };
240 u16 crc = 0xFFFF;
241
242 mutex_lock(&ucmb->mutex);
243
244 size = min_t(size_t, size, PAGE_SIZE);
245
246 err = -ENOMEM;
247 buf = (char *)__get_free_page(GFP_KERNEL);
248 if (!buf)
249 goto out;
250
251 err = spi_read(ucmb->sdev, (u8 *)&hdr, sizeof(hdr));
252 if (err)
253 goto out_free;
254 #ifdef DEBUG
255 printk(KERN_DEBUG PFX "Received message header 0x%04X 0x%04X\n",
256 le16_to_cpu(hdr.magic), le16_to_cpu(hdr.len));
257 #endif
258 err = -EPROTO;
259 if (hdr.magic != cpu_to_le16(UCMB_MAGIC))
260 goto out_free;
261 err = -ENOBUFS;
262 if (size < le16_to_cpu(hdr.len))
263 goto out_free;
264 size = le16_to_cpu(hdr.len);
265 err = spi_read(ucmb->sdev, buf, size);
266 if (err)
267 goto out_free;
268 err = spi_read(ucmb->sdev, (u8 *)&footer, sizeof(footer));
269 if (err)
270 goto out_free;
271
272 crc = crc16(crc, (u8 *)&hdr, sizeof(hdr));
273 crc = crc16(crc, buf, size);
274 crc ^= 0xFFFF;
275 if (crc != le16_to_cpu(footer.crc)) {
276 err = -EPROTO;
277 status.code = UCMB_STAT_ECRC;
278 goto out_send_status;
279 }
280
281 if (copy_to_user(user_buf, buf, size)) {
282 err = -EFAULT;
283 status.code = UCMB_STAT_ENOMEM;
284 goto out_send_status;
285 }
286
287 status.code = UCMB_STAT_OK;
288 err = 0;
289
290 out_send_status:
291 res = spi_write(ucmb->sdev, (u8 *)&status, sizeof(status));
292 if (res && !err)
293 err = res;
294 out_free:
295 free_page((unsigned long)buf);
296 out:
297 mutex_unlock(&ucmb->mutex);
298
299 return err ? err : size;
300 }
301
302 static ssize_t ucmb_write(struct file *filp, const char __user *user_buf,
303 size_t size, loff_t *offp)
304 {
305 struct ucmb *ucmb = filp_to_ucmb(filp);
306 u8 *buf;
307 int err;
308 struct ucmb_message_hdr hdr = { .magic = cpu_to_le16(UCMB_MAGIC), };
309 struct ucmb_message_footer footer = { .crc = 0xFFFF, };
310 struct ucmb_status status;
311 struct spi_transfer spi_hdr_xfer;
312 struct spi_transfer spi_footer_xfer;
313 struct spi_transfer spi_data_xfer;
314 struct spi_message spi_msg;
315
316 mutex_lock(&ucmb->mutex);
317
318 err = -ENOMEM;
319 buf = (char *)__get_free_page(GFP_KERNEL);
320 if (!buf)
321 goto out;
322
323 size = min_t(size_t, PAGE_SIZE, size);
324 err = -EFAULT;
325 if (copy_from_user(buf, user_buf, size))
326 goto out_free;
327 hdr.len = cpu_to_le16(size);
328
329 footer.crc = crc16(footer.crc, (u8 *)&hdr, sizeof(hdr));
330 footer.crc = crc16(footer.crc, buf, size);
331 footer.crc ^= 0xFFFF;
332
333 spi_message_init(&spi_msg);
334
335 memset(&spi_hdr_xfer, 0, sizeof(spi_hdr_xfer));
336 spi_hdr_xfer.tx_buf = &hdr;
337 spi_hdr_xfer.len = sizeof(hdr);
338 spi_message_add_tail(&spi_hdr_xfer, &spi_msg);
339
340 memset(&spi_data_xfer, 0, sizeof(spi_data_xfer));
341 spi_data_xfer.tx_buf = buf;
342 spi_data_xfer.len = size;
343 spi_message_add_tail(&spi_data_xfer, &spi_msg);
344
345 memset(&spi_footer_xfer, 0, sizeof(spi_footer_xfer));
346 spi_footer_xfer.tx_buf = &footer;
347 spi_footer_xfer.len = sizeof(footer);
348 spi_message_add_tail(&spi_footer_xfer, &spi_msg);
349
350 /* Send the message, including header. */
351 err = spi_sync(ucmb->sdev, &spi_msg);
352 if (err)
353 goto out_free;
354
355 if (ucmb->msg_delay_usec) {
356 /* The microcontroller deserves some time to process the message. */
357 if (ucmb->msg_delay_usec >= 1000000) {
358 ssleep(ucmb->msg_delay_usec / 1000000);
359 msleep(DIV_ROUND_UP(ucmb->msg_delay_usec % 1000000, 1000));
360 } else if (ucmb->msg_delay_usec >= 1000) {
361 msleep(DIV_ROUND_UP(ucmb->msg_delay_usec, 1000));
362 } else
363 udelay(ucmb->msg_delay_usec);
364 }
365
366 /* Get the status code. */
367 err = spi_read(ucmb->sdev, (u8 *)&status, sizeof(status));
368 if (err)
369 goto out_free;
370 #ifdef DEBUG
371 printk(KERN_DEBUG PFX "Sent message. Status report: 0x%04X 0x%04X\n",
372 le16_to_cpu(status.magic), le16_to_cpu(status.code));
373 #endif
374 err = -EPROTO;
375 if (status.magic != cpu_to_le16(UCMB_MAGIC))
376 goto out_free;
377 err = ucmb_status_code_to_errno(le16_to_cpu(status.code));
378 if (err)
379 goto out_free;
380
381 out_free:
382 free_page((unsigned long)buf);
383 out:
384 mutex_unlock(&ucmb->mutex);
385
386 return err ? err : size;
387 }
388
389 static int __devinit ucmb_probe(struct platform_device *pdev)
390 {
391 struct ucmb_platform_data *pdata;
392 struct ucmb *ucmb;
393 int err;
394 const int bus_num = ucmb_spi_busnum_count++;
395 struct spi_bitbang *bb;
396
397 pdata = pdev->dev.platform_data;
398 if (!pdata)
399 return -ENXIO;
400
401 ucmb = kzalloc(sizeof(struct ucmb), GFP_KERNEL);
402 if (!ucmb)
403 return -ENOMEM;
404 mutex_init(&ucmb->mutex);
405 ucmb->gpio_reset = pdata->gpio_reset;
406 ucmb->reset_activelow = pdata->reset_activelow;
407
408 /* Create the SPI GPIO bus master. */
409
410 #ifdef CONFIG_SPI_GPIO_MODULE
411 err = request_module("spi_gpio");
412 if (err)
413 printk(KERN_WARNING PFX "Failed to request spi_gpio module\n");
414 #endif /* CONFIG_SPI_GPIO_MODULE */
415
416 ucmb->spi_gpio_pdata.sck = pdata->gpio_sck;
417 ucmb->spi_gpio_pdata.mosi = pdata->gpio_mosi;
418 ucmb->spi_gpio_pdata.miso = pdata->gpio_miso;
419 ucmb->spi_gpio_pdata.num_chipselect = 1;
420
421 ucmb->spi_gpio_pdev.name = "spi_gpio";
422 ucmb->spi_gpio_pdev.id = bus_num;
423 ucmb->spi_gpio_pdev.dev.platform_data = &ucmb->spi_gpio_pdata;
424
425 err = platform_device_register(&ucmb->spi_gpio_pdev);
426 if (err) {
427 printk(KERN_ERR PFX "Failed to register SPI-GPIO platform device\n");
428 goto err_free_ucmb;
429 }
430 bb = platform_get_drvdata(&ucmb->spi_gpio_pdev);
431 if (!bb || !bb->master) {
432 printk(KERN_ERR PFX "No bitbanged master device found.\n");
433 goto err_unreg_spi_gpio_pdev;
434 }
435
436 /* Create the SPI device. */
437
438 ucmb->sdev = spi_alloc_device(bb->master);
439 if (!ucmb->sdev) {
440 printk(KERN_ERR PFX "Failed to allocate SPI device\n");
441 goto err_unreg_spi_gpio_pdev;
442 }
443 ucmb->sdev->max_speed_hz = pdata->max_speed_hz;
444 ucmb->sdev->chip_select = 0;
445 ucmb->sdev->mode = pdata->mode;
446 strlcpy(ucmb->sdev->modalias, "ucmb", /* We are the SPI driver. */
447 sizeof(ucmb->sdev->modalias));
448 ucmb->sdev->controller_data = (void *)pdata->gpio_cs;
449 err = spi_add_device(ucmb->sdev);
450 if (err) {
451 printk(KERN_ERR PFX "Failed to add SPI device\n");
452 goto err_free_spi_device;
453 }
454
455 /* Initialize the RESET line. */
456
457 if (pdata->gpio_reset != UCMB_NO_RESET) {
458 err = gpio_request(pdata->gpio_reset, pdata->name);
459 if (err) {
460 printk(KERN_ERR PFX
461 "Failed to request RESET GPIO line\n");
462 goto err_unreg_spi_device;
463 }
464 err = gpio_direction_output(pdata->gpio_reset,
465 pdata->reset_activelow);
466 if (err) {
467 printk(KERN_ERR PFX
468 "Failed to set RESET GPIO direction\n");
469 goto err_free_reset_gpio;
470 }
471 ucmb_reset_microcontroller(ucmb);
472 }
473
474 /* Create the Misc char device. */
475
476 ucmb->mdev.minor = MISC_DYNAMIC_MINOR;
477 ucmb->mdev.name = pdata->name;
478 ucmb->mdev.parent = &pdev->dev;
479 ucmb->mdev_fops.open = ucmb_open;
480 ucmb->mdev_fops.release = ucmb_release;
481 ucmb->mdev_fops.read = ucmb_read;
482 ucmb->mdev_fops.write = ucmb_write;
483 ucmb->mdev_fops.ioctl = ucmb_ioctl;
484 ucmb->mdev.fops = &ucmb->mdev_fops;
485
486 err = misc_register(&ucmb->mdev);
487 if (err) {
488 printk(KERN_ERR PFX "Failed to register miscdev %s\n",
489 ucmb->mdev.name);
490 goto err_free_reset_gpio;
491 }
492
493 platform_set_drvdata(pdev, ucmb);
494
495 printk(KERN_INFO PFX "Registered message bus \"%s\"\n", pdata->name);
496
497 return 0;
498
499 err_free_reset_gpio:
500 if (pdata->gpio_reset != UCMB_NO_RESET)
501 gpio_free(pdata->gpio_reset);
502 err_unreg_spi_device:
503 spi_unregister_device(ucmb->sdev);
504 err_free_spi_device:
505 spi_dev_put(ucmb->sdev);
506 err_unreg_spi_gpio_pdev:
507 platform_device_unregister(&ucmb->spi_gpio_pdev);
508 err_free_ucmb:
509 kfree(ucmb);
510
511 return err;
512 }
513
514 static int __devexit ucmb_remove(struct platform_device *pdev)
515 {
516 struct ucmb *ucmb = platform_get_drvdata(pdev);
517 int err;
518
519 err = misc_deregister(&ucmb->mdev);
520 if (err) {
521 printk(KERN_ERR PFX "Failed to unregister miscdev %s\n",
522 ucmb->mdev.name);
523 }
524 if (ucmb->gpio_reset != UCMB_NO_RESET)
525 gpio_free(ucmb->gpio_reset);
526 spi_unregister_device(ucmb->sdev);
527 spi_dev_put(ucmb->sdev);
528 platform_device_unregister(&ucmb->spi_gpio_pdev);
529
530 kfree(ucmb);
531 platform_set_drvdata(pdev, NULL);
532
533 return 0;
534 }
535
536 static struct platform_driver ucmb_driver = {
537 .driver = {
538 .name = "ucmb",
539 .owner = THIS_MODULE,
540 },
541 .probe = ucmb_probe,
542 .remove = __devexit_p(ucmb_remove),
543 };
544
545 int ucmb_device_register(struct ucmb_platform_data *pdata)
546 {
547 struct platform_device *pdev;
548 int err;
549
550 pdev = platform_device_alloc("ucmb", ucmb_pdev_id_count++);
551 if (!pdev) {
552 printk(KERN_ERR PFX "Failed to allocate platform device.\n");
553 return -ENOMEM;
554 }
555 err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
556 if (err) {
557 printk(KERN_ERR PFX "Failed to add platform data.\n");
558 platform_device_put(pdev);
559 return err;
560 }
561 err = platform_device_add(pdev);
562 if (err) {
563 printk(KERN_ERR PFX "Failed to register platform device.\n");
564 platform_device_put(pdev);
565 return err;
566 }
567 pdata->pdev = pdev;
568
569 return 0;
570 }
571 EXPORT_SYMBOL(ucmb_device_register);
572
573 void ucmb_device_unregister(struct ucmb_platform_data *pdata)
574 {
575 if (!pdata->pdev)
576 return;
577 platform_device_unregister(pdata->pdev);
578 platform_device_put(pdata->pdev);
579 pdata->pdev = NULL;
580 }
581 EXPORT_SYMBOL(ucmb_device_unregister);
582
583 static int ucmb_modinit(void)
584 {
585 int err;
586
587 printk(KERN_INFO "Microcontroller message bus driver\n");
588
589 err = spi_register_driver(&ucmb_spi_driver);
590 if (err) {
591 printk(KERN_ERR PFX "Failed to register SPI driver\n");
592 return err;
593 }
594 err = platform_driver_register(&ucmb_driver);
595 if (err) {
596 printk(KERN_ERR PFX "Failed to register platform driver\n");
597 spi_unregister_driver(&ucmb_spi_driver);
598 return err;
599 }
600
601 return 0;
602 }
603 subsys_initcall(ucmb_modinit);
604
605 static void ucmb_modexit(void)
606 {
607 platform_driver_unregister(&ucmb_driver);
608 spi_unregister_driver(&ucmb_spi_driver);
609 }
610 module_exit(ucmb_modexit);