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