blob: 85306d35fe07d9932c506c2e179091cdda537ed9 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
Duy Truong790f06d2013-02-13 16:38:12 -08002 * Copyright (c) 2010, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/mutex.h>
19#include <linux/serial_reg.h>
20#include <linux/circ_buf.h>
21#include <linux/gfp.h>
22#include <linux/uaccess.h>
23#include <linux/slab.h>
24#include <linux/platform_device.h>
25
26/* Char device */
27#include <linux/cdev.h>
28#include <linux/fs.h>
29
30/* Sdio device */
31#include <linux/mmc/core.h>
32#include <linux/mmc/host.h>
33#include <linux/mmc/card.h>
34#include <linux/mmc/sdio.h>
35#include <linux/mmc/sdio_func.h>
36#include <linux/mmc/sdio_ids.h>
37
38#include <linux/csdio.h>
39
40#define FALSE 0
41#define TRUE 1
42
43#define VERSION "0.5"
44#define CSDIO_NUM_OF_SDIO_FUNCTIONS 7
45#define CSDIO_DEV_NAME "csdio"
46#define TP_DEV_NAME CSDIO_DEV_NAME"f"
47#define CSDIO_DEV_PERMISSIONS 0666
48
49#define CSDIO_SDIO_BUFFER_SIZE (64*512)
50
51int csdio_major;
52int csdio_minor;
53int csdio_transport_nr_devs = CSDIO_NUM_OF_SDIO_FUNCTIONS;
54static uint csdio_vendor_id;
55static uint csdio_device_id;
56static char *host_name;
57
58static struct csdio_func_t {
59 struct sdio_func *m_func;
60 int m_enabled;
61 struct cdev m_cdev; /* char device structure */
62 struct device *m_device;
63 u32 m_block_size;
64} *g_csdio_func_table[CSDIO_NUM_OF_SDIO_FUNCTIONS] = {0};
65
66struct csdio_t {
67 struct cdev m_cdev;
68 struct device *m_device;
69 struct class *m_driver_class;
70 struct fasync_struct *m_async_queue;
71 unsigned char m_current_irq_mask; /* currently enabled irqs */
72 struct mmc_host *m_host;
73 unsigned int m_num_of_func;
74} g_csdio;
75
76struct csdio_file_descriptor {
77 struct csdio_func_t *m_port;
78 u32 m_block_mode;/* data tran. byte(0)/block(1) */
79 u32 m_op_code; /* address auto increment flag */
80 u32 m_address;
81};
82
83static void *g_sdio_buffer;
84
85/*
86 * Open and release
87 */
88static int csdio_transport_open(struct inode *inode, struct file *filp)
89{
90 int ret = 0;
91 struct csdio_func_t *port = NULL; /* device information */
92 struct sdio_func *func = NULL;
93 struct csdio_file_descriptor *descriptor = NULL;
94
95 port = container_of(inode->i_cdev, struct csdio_func_t, m_cdev);
96 func = port->m_func;
97 descriptor = kzalloc(sizeof(struct csdio_file_descriptor), GFP_KERNEL);
98 if (!descriptor) {
99 ret = -ENOMEM;
100 goto exit;
101 }
102
103 pr_info(TP_DEV_NAME"%d: open: func=%p, port=%p\n",
104 func->num, func, port);
105 sdio_claim_host(func);
106 ret = sdio_enable_func(func);
107 if (ret) {
108 pr_err(TP_DEV_NAME"%d:Enable func failed (%d)\n",
109 func->num, ret);
110 ret = -EIO;
111 goto free_descriptor;
112 }
113 descriptor->m_port = port;
114 filp->private_data = descriptor;
115 goto release_host;
116
117free_descriptor:
118 kfree(descriptor);
119release_host:
120 sdio_release_host(func);
121exit:
122 return ret;
123}
124
125static int csdio_transport_release(struct inode *inode, struct file *filp)
126{
127 int ret = 0;
128 struct csdio_file_descriptor *descriptor = filp->private_data;
129 struct csdio_func_t *port = descriptor->m_port;
130 struct sdio_func *func = port->m_func;
131
132 pr_info(TP_DEV_NAME"%d: release\n", func->num);
133 sdio_claim_host(func);
134 ret = sdio_disable_func(func);
135 if (ret) {
136 pr_err(TP_DEV_NAME"%d:Disable func failed(%d)\n",
137 func->num, ret);
138 ret = -EIO;
139 }
140 sdio_release_host(func);
141 kfree(descriptor);
142 return ret;
143}
144
145/*
146 * Data management: read and write
147 */
148static ssize_t csdio_transport_read(struct file *filp,
149 char __user *buf,
150 size_t count,
151 loff_t *f_pos)
152{
153 ssize_t ret = 0;
154 struct csdio_file_descriptor *descriptor = filp->private_data;
155 struct csdio_func_t *port = descriptor->m_port;
156 struct sdio_func *func = port->m_func;
157 size_t t_count = count;
158
159 if (descriptor->m_block_mode) {
160 pr_info(TP_DEV_NAME "%d: CMD53 read, Md:%d, Addr:0x%04X,"
161 " Un:%d (Bl:%d, BlSz:%d)\n", func->num,
162 descriptor->m_block_mode,
163 descriptor->m_address,
164 count*port->m_block_size,
165 count, port->m_block_size);
166 /* recalculate size */
167 count *= port->m_block_size;
168 }
169 sdio_claim_host(func);
170 if (descriptor->m_op_code) {
171 /* auto increment */
172 ret = sdio_memcpy_fromio(func, g_sdio_buffer,
173 descriptor->m_address, count);
174 } else { /* FIFO */
175 ret = sdio_readsb(func, g_sdio_buffer,
176 descriptor->m_address, count);
177 }
178 sdio_release_host(func);
179 if (!ret) {
180 if (copy_to_user(buf, g_sdio_buffer, count))
181 ret = -EFAULT;
182 else
183 ret = t_count;
184 }
185 if (ret < 0) {
186 pr_err(TP_DEV_NAME "%d: CMD53 read failed (%d)"
187 "(Md:%d, Addr:0x%04X, Sz:%d)\n",
188 func->num, ret,
189 descriptor->m_block_mode,
190 descriptor->m_address, count);
191 }
192 return ret;
193}
194
195static ssize_t csdio_transport_write(struct file *filp,
196 const char __user *buf,
197 size_t count,
198 loff_t *f_pos)
199{
200 ssize_t ret = 0;
201 struct csdio_file_descriptor *descriptor = filp->private_data;
202 struct csdio_func_t *port = descriptor->m_port;
203 struct sdio_func *func = port->m_func;
204 size_t t_count = count;
205
206 if (descriptor->m_block_mode)
207 count *= port->m_block_size;
208
209 if (copy_from_user(g_sdio_buffer, buf, count)) {
210 pr_err(TP_DEV_NAME"%d:copy_from_user failed\n", func->num);
211 ret = -EFAULT;
212 } else {
213 sdio_claim_host(func);
214 if (descriptor->m_op_code) {
215 /* auto increment */
216 ret = sdio_memcpy_toio(func, descriptor->m_address,
217 g_sdio_buffer, count);
218 } else {
219 /* FIFO */
220 ret = sdio_writesb(func, descriptor->m_address,
221 g_sdio_buffer, count);
222 }
223 sdio_release_host(func);
224 if (!ret) {
225 ret = t_count;
226 } else {
227 pr_err(TP_DEV_NAME "%d: CMD53 write failed (%d)"
228 "(Md:%d, Addr:0x%04X, Sz:%d)\n",
229 func->num, ret, descriptor->m_block_mode,
230 descriptor->m_address, count);
231 }
232 }
233 return ret;
234}
235
236/* disable interrupt for sdio client */
237static int disable_sdio_client_isr(struct sdio_func *func)
238{
239 int ret;
240
241 /* disable for all functions, to restore interrupts
242 * use g_csdio.m_current_irq_mask */
243 sdio_f0_writeb(func, 0, SDIO_CCCR_IENx, &ret);
244 if (ret)
245 pr_err(CSDIO_DEV_NAME" Can't sdio_f0_writeb (%d)\n", ret);
246
247 return ret;
248}
249
250/*
251 * This handles the interrupt from SDIO.
252 */
253static void csdio_sdio_irq(struct sdio_func *func)
254{
255 int ret;
256
257 pr_info(CSDIO_DEV_NAME" csdio_sdio_irq: func=%d\n", func->num);
258 ret = disable_sdio_client_isr(func);
259 if (ret) {
260 pr_err(CSDIO_DEV_NAME" Can't disable client isr(%d)\n", ret);
261 return;
262 }
263 /* signal asynchronous readers */
264 if (g_csdio.m_async_queue)
265 kill_fasync(&g_csdio.m_async_queue, SIGIO, POLL_IN);
266}
267
268/*
269 * The ioctl() implementation
270 */
271static int csdio_transport_ioctl(struct inode *inode,
272 struct file *filp,
273 unsigned int cmd,
274 unsigned long arg)
275{
276 int err = 0;
277 int ret = 0;
278 struct csdio_file_descriptor *descriptor = filp->private_data;
279 struct csdio_func_t *port = descriptor->m_port;
280 struct sdio_func *func = port->m_func;
281
282 /* extract the type and number bitfields
283 sanity check: return ENOTTY (inappropriate ioctl) before
284 access_ok()
285 */
286 if ((_IOC_TYPE(cmd) != CSDIO_IOC_MAGIC) ||
287 (_IOC_NR(cmd) > CSDIO_IOC_MAXNR)) {
288 pr_err(TP_DEV_NAME "Wrong ioctl command parameters\n");
289 ret = -ENOTTY;
290 goto exit;
291 }
292
293 /* the direction is a bitmask, and VERIFY_WRITE catches R/W
294 * transfers. `Type' is user-oriented, while access_ok is
295 kernel-oriented, so the concept of "read" and "write" is reversed
296 */
297 if (_IOC_DIR(cmd) & _IOC_READ) {
298 err = !access_ok(VERIFY_WRITE, (void __user *)arg,
299 _IOC_SIZE(cmd));
300 } else {
301 if (_IOC_DIR(cmd) & _IOC_WRITE) {
302 err = !access_ok(VERIFY_READ, (void __user *)arg,
303 _IOC_SIZE(cmd));
304 }
305 }
306 if (err) {
307 pr_err(TP_DEV_NAME "Wrong ioctl access direction\n");
308 ret = -EFAULT;
309 goto exit;
310 }
311
312 switch (cmd) {
313 case CSDIO_IOC_SET_OP_CODE:
314 {
315 pr_info(TP_DEV_NAME"%d:SET_OP_CODE=%d\n",
316 func->num, descriptor->m_op_code);
317 ret = get_user(descriptor->m_op_code,
318 (unsigned char __user *)arg);
319 if (ret) {
320 pr_err(TP_DEV_NAME"%d:SET_OP_CODE get data"
321 " from user space failed(%d)\n",
322 func->num, ret);
323 ret = -ENOTTY;
324 break;
325 }
326 }
327 break;
328 case CSDIO_IOC_FUNCTION_SET_BLOCK_SIZE:
329 {
330 unsigned block_size;
331
332 ret = get_user(block_size, (unsigned __user *)arg);
333 if (ret) {
334 pr_err(TP_DEV_NAME"%d:SET_BLOCK_SIZE get data"
335 " from user space failed(%d)\n",
336 func->num, ret);
337 ret = -ENOTTY;
338 break;
339 }
340 pr_info(TP_DEV_NAME"%d:SET_BLOCK_SIZE=%d\n",
341 func->num, block_size);
342 sdio_claim_host(func);
343 ret = sdio_set_block_size(func, block_size);
344 if (!ret) {
345 port->m_block_size = block_size;
346 } else {
347 pr_err(TP_DEV_NAME"%d:SET_BLOCK_SIZE set block"
348 " size to %d failed (%d)\n",
349 func->num, block_size, ret);
350 ret = -ENOTTY;
351 break;
352 }
353 sdio_release_host(func);
354 }
355 break;
356 case CSDIO_IOC_SET_BLOCK_MODE:
357 {
358 pr_info(TP_DEV_NAME"%d:SET_BLOCK_MODE=%d\n",
359 func->num, descriptor->m_block_mode);
360 ret = get_user(descriptor->m_block_mode,
361 (unsigned char __user *)arg);
362 if (ret) {
363 pr_err(TP_DEV_NAME"%d:SET_BLOCK_MODE get data"
364 " from user space failed\n",
365 func->num);
366 ret = -ENOTTY;
367 break;
368 }
369 }
370 break;
371 case CSDIO_IOC_CMD52:
372 {
373 struct csdio_cmd52_ctrl_t cmd52ctrl;
374 int cmd52ret;
375
376 if (copy_from_user(&cmd52ctrl,
377 (const unsigned char __user *)arg,
378 sizeof(cmd52ctrl))) {
379 pr_err(TP_DEV_NAME"%d:IOC_CMD52 get data"
380 " from user space failed\n",
381 func->num);
382 ret = -ENOTTY;
383 break;
384 }
385 sdio_claim_host(func);
386 if (cmd52ctrl.m_write)
387 sdio_writeb(func, cmd52ctrl.m_data,
388 cmd52ctrl.m_address, &cmd52ret);
389 else
390 cmd52ctrl.m_data = sdio_readb(func,
391 cmd52ctrl.m_address, &cmd52ret);
392
393 cmd52ctrl.m_ret = cmd52ret;
394 sdio_release_host(func);
395 if (cmd52ctrl.m_ret)
396 pr_err(TP_DEV_NAME"%d:IOC_CMD52 failed (%d)\n",
397 func->num, cmd52ctrl.m_ret);
398
399 if (copy_to_user((unsigned char __user *)arg,
400 &cmd52ctrl,
401 sizeof(cmd52ctrl))) {
402 pr_err(TP_DEV_NAME"%d:IOC_CMD52 put data"
403 " to user space failed\n",
404 func->num);
405 ret = -ENOTTY;
406 break;
407 }
408 }
409 break;
410 case CSDIO_IOC_CMD53:
411 {
412 struct csdio_cmd53_ctrl_t csdio_cmd53_ctrl;
413
414 if (copy_from_user(&csdio_cmd53_ctrl,
415 (const char __user *)arg,
416 sizeof(csdio_cmd53_ctrl))) {
417 ret = -EPERM;
418 pr_err(TP_DEV_NAME"%d:"
419 "Get data from user space failed\n",
420 func->num);
421 break;
422 }
423 descriptor->m_block_mode =
424 csdio_cmd53_ctrl.m_block_mode;
425 descriptor->m_op_code = csdio_cmd53_ctrl.m_op_code;
426 descriptor->m_address = csdio_cmd53_ctrl.m_address;
427 }
428 break;
429 case CSDIO_IOC_CONNECT_ISR:
430 {
431 pr_info(CSDIO_DEV_NAME" SDIO_CONNECT_ISR"
432 " func=%d, csdio_sdio_irq=%x\n",
433 func->num, (unsigned int)csdio_sdio_irq);
434 sdio_claim_host(func);
435 ret = sdio_claim_irq(func, csdio_sdio_irq);
436 sdio_release_host(func);
437 if (ret) {
438 pr_err(CSDIO_DEV_NAME" SDIO_CONNECT_ISR"
439 " claim irq failed(%d)\n", ret);
440 } else {
441 /* update current irq mask for disable/enable */
442 g_csdio.m_current_irq_mask |= (1 << func->num);
443 }
444 }
445 break;
446 case CSDIO_IOC_DISCONNECT_ISR:
447 {
448 pr_info(CSDIO_DEV_NAME " SDIO_DISCONNECT_ISR func=%d\n",
449 func->num);
450 sdio_claim_host(func);
451 sdio_release_irq(func);
452 sdio_release_host(func);
453 /* update current irq mask for disable/enable */
454 g_csdio.m_current_irq_mask &= ~(1 << func->num);
455 }
456 break;
457 default: /* redundant, as cmd was checked against MAXNR */
458 pr_warning(TP_DEV_NAME"%d: Redundant IOCTL\n",
459 func->num);
460 ret = -ENOTTY;
461 }
462exit:
463 return ret;
464}
465
466static const struct file_operations csdio_transport_fops = {
467 .owner = THIS_MODULE,
468 .read = csdio_transport_read,
469 .write = csdio_transport_write,
470 .ioctl = csdio_transport_ioctl,
471 .open = csdio_transport_open,
472 .release = csdio_transport_release,
473};
474
475static void csdio_transport_cleanup(struct csdio_func_t *port)
476{
477 int devno = MKDEV(csdio_major, csdio_minor + port->m_func->num);
478 device_destroy(g_csdio.m_driver_class, devno);
479 port->m_device = NULL;
480 cdev_del(&port->m_cdev);
481}
482
483#if defined(CONFIG_DEVTMPFS)
484static inline int csdio_cdev_update_permissions(
485 const char *devname, int dev_minor)
486{
487 return 0;
488}
489#else
490static int csdio_cdev_update_permissions(
491 const char *devname, int dev_minor)
492{
493 int ret = 0;
494 mm_segment_t fs;
495 struct file *file;
496 struct inode *inode;
497 struct iattr newattrs;
498 int mode = CSDIO_DEV_PERMISSIONS;
499 char dev_file[64];
500
501 fs = get_fs();
502 set_fs(get_ds());
503
504 snprintf(dev_file, sizeof(dev_file), "/dev/%s%d",
505 devname, dev_minor);
506 file = filp_open(dev_file, O_RDWR, 0);
507 if (IS_ERR(file)) {
508 ret = -EFAULT;
509 goto exit;
510 }
511
512 inode = file->f_path.dentry->d_inode;
513
514 mutex_lock(&inode->i_mutex);
515 newattrs.ia_mode =
516 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
517 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
518 ret = notify_change(file->f_path.dentry, &newattrs);
519 mutex_unlock(&inode->i_mutex);
520
521 filp_close(file, NULL);
522
523exit:
524 set_fs(fs);
525 return ret;
526}
527#endif
528
529static struct device *csdio_cdev_init(struct cdev *char_dev,
530 const struct file_operations *file_op, int dev_minor,
531 const char *devname, struct device *parent)
532{
533 int ret = 0;
534 struct device *new_device = NULL;
535 dev_t devno = MKDEV(csdio_major, dev_minor);
536
537 /* Initialize transport device */
538 cdev_init(char_dev, file_op);
539 char_dev->owner = THIS_MODULE;
540 char_dev->ops = file_op;
541 ret = cdev_add(char_dev, devno, 1);
542
543 /* Fail gracefully if need be */
544 if (ret) {
545 pr_warning("Error %d adding CSDIO char device '%s%d'",
546 ret, devname, dev_minor);
547 goto exit;
548 }
549 pr_info("'%s%d' char driver registered\n", devname, dev_minor);
550
551 /* create a /dev entry for transport drivers */
552 new_device = device_create(g_csdio.m_driver_class, parent, devno, NULL,
553 "%s%d", devname, dev_minor);
554 if (!new_device) {
555 pr_err("Can't create device node '/dev/%s%d'\n",
556 devname, dev_minor);
557 goto cleanup;
558 }
559 /* no irq attached */
560 g_csdio.m_current_irq_mask = 0;
561
562 if (csdio_cdev_update_permissions(devname, dev_minor)) {
563 pr_warning("%s%d: Unable to update access permissions of the"
564 " '/dev/%s%d'\n",
565 devname, dev_minor, devname, dev_minor);
566 }
567
568 pr_info("%s%d: Device node '/dev/%s%d' created successfully\n",
569 devname, dev_minor, devname, dev_minor);
570 goto exit;
571cleanup:
572 cdev_del(char_dev);
573exit:
574 return new_device;
575}
576
577/* Looks for first non empty function, returns NULL otherwise */
578static struct sdio_func *get_active_func(void)
579{
580 int i;
581
582 for (i = 0; i < CSDIO_NUM_OF_SDIO_FUNCTIONS; i++) {
583 if (g_csdio_func_table[i])
584 return g_csdio_func_table[i]->m_func;
585 }
586 return NULL;
587}
588
589static ssize_t
590show_vdd(struct device *dev, struct device_attribute *attr, char *buf)
591{
592 if (NULL == g_csdio.m_host)
593 return snprintf(buf, PAGE_SIZE, "N/A\n");
594 return snprintf(buf, PAGE_SIZE, "%d\n",
595 g_csdio.m_host->ios.vdd);
596}
597
598static int
599set_vdd_helper(int value)
600{
601 struct mmc_ios *ios = NULL;
602
603 if (NULL == g_csdio.m_host) {
604 pr_err("%s0: Set VDD, no MMC host assigned\n", CSDIO_DEV_NAME);
605 return -ENXIO;
606 }
607
608 mmc_claim_host(g_csdio.m_host);
609 ios = &g_csdio.m_host->ios;
610 ios->vdd = value;
611 g_csdio.m_host->ops->set_ios(g_csdio.m_host, ios);
612 mmc_release_host(g_csdio.m_host);
613 return 0;
614}
615
616static ssize_t
617set_vdd(struct device *dev, struct device_attribute *att,
618 const char *buf, size_t count)
619{
620 int value = 0;
621
622 sscanf(buf, "%d", &value);
623 if (set_vdd_helper(value))
624 return -ENXIO;
625 return count;
626}
627
628static DEVICE_ATTR(vdd, S_IRUGO | S_IWUSR,
629 show_vdd, set_vdd);
630
631static struct attribute *dev_attrs[] = {
632 &dev_attr_vdd.attr,
633 NULL,
634};
635
636static struct attribute_group dev_attr_grp = {
637 .attrs = dev_attrs,
638};
639
640/*
641 * The ioctl() implementation for control device
642 */
643static int csdio_ctrl_ioctl(struct inode *inode, struct file *filp,
644 unsigned int cmd, unsigned long arg)
645{
646 int err = 0;
647 int ret = 0;
648
649 pr_info("CSDIO ctrl ioctl.\n");
650
651 /* extract the type and number bitfields
652 sanity check: return ENOTTY (inappropriate ioctl) before
653 access_ok()
654 */
655 if ((_IOC_TYPE(cmd) != CSDIO_IOC_MAGIC) ||
656 (_IOC_NR(cmd) > CSDIO_IOC_MAXNR)) {
657 pr_err(CSDIO_DEV_NAME "Wrong ioctl command parameters\n");
658 ret = -ENOTTY;
659 goto exit;
660 }
661
662 /* the direction is a bitmask, and VERIFY_WRITE catches R/W
663 transfers. `Type' is user-oriented, while access_ok is
664 kernel-oriented, so the concept of "read" and "write" is reversed
665 */
666 if (_IOC_DIR(cmd) & _IOC_READ) {
667 err = !access_ok(VERIFY_WRITE, (void __user *)arg,
668 _IOC_SIZE(cmd));
669 } else {
670 if (_IOC_DIR(cmd) & _IOC_WRITE)
671 err = !access_ok(VERIFY_READ, (void __user *)arg,
672 _IOC_SIZE(cmd));
673 }
674 if (err) {
675 pr_err(CSDIO_DEV_NAME "Wrong ioctl access direction\n");
676 ret = -EFAULT;
677 goto exit;
678 }
679
680 switch (cmd) {
681 case CSDIO_IOC_ENABLE_HIGHSPEED_MODE:
682 pr_info(CSDIO_DEV_NAME" ENABLE_HIGHSPEED_MODE\n");
683 break;
684 case CSDIO_IOC_SET_DATA_TRANSFER_CLOCKS:
685 {
686 struct mmc_host *host = g_csdio.m_host;
687 struct mmc_ios *ios = NULL;
688
689 if (NULL == host) {
690 pr_err("%s0: "
691 "CSDIO_IOC_SET_DATA_TRANSFER_CLOCKS,"
692 " no MMC host assigned\n",
693 CSDIO_DEV_NAME);
694 ret = -EFAULT;
695 goto exit;
696 }
697 ios = &host->ios;
698
699 mmc_claim_host(host);
700 ret = get_user(host->ios.clock,
701 (unsigned int __user *)arg);
702 if (ret) {
703 pr_err(CSDIO_DEV_NAME
704 " get data from user space failed\n");
705 } else {
706 pr_err(CSDIO_DEV_NAME
707 "SET_DATA_TRANSFER_CLOCKS(%d-%d)(%d)\n",
708 host->f_min, host->f_max,
709 host->ios.clock);
710 host->ops->set_ios(host, ios);
711 }
712 mmc_release_host(host);
713 }
714 break;
715 case CSDIO_IOC_ENABLE_ISR:
716 {
717 int ret;
718 unsigned char reg;
719 struct sdio_func *func = get_active_func();
720
721 if (!func) {
722 pr_err(CSDIO_DEV_NAME " CSDIO_IOC_ENABLE_ISR"
723 " no active sdio function\n");
724 ret = -EFAULT;
725 goto exit;
726 }
727 pr_info(CSDIO_DEV_NAME
728 " CSDIO_IOC_ENABLE_ISR func=%d\n",
729 func->num);
730 reg = g_csdio.m_current_irq_mask | 1;
731
732 sdio_claim_host(func);
733 sdio_f0_writeb(func, reg, SDIO_CCCR_IENx, &ret);
734 sdio_release_host(func);
735 if (ret) {
736 pr_err(CSDIO_DEV_NAME
737 " Can't sdio_f0_writeb (%d)\n",
738 ret);
739 goto exit;
740 }
741 }
742 break;
743 case CSDIO_IOC_DISABLE_ISR:
744 {
745 int ret;
746 struct sdio_func *func = get_active_func();
747 if (!func) {
748 pr_err(CSDIO_DEV_NAME " CSDIO_IOC_ENABLE_ISR"
749 " no active sdio function\n");
750 ret = -EFAULT;
751 goto exit;
752 }
753 pr_info(CSDIO_DEV_NAME
754 " CSDIO_IOC_DISABLE_ISR func=%p\n",
755 func);
756
757 sdio_claim_host(func);
758 ret = disable_sdio_client_isr(func);
759 sdio_release_host(func);
760 if (ret) {
761 pr_err("%s0: Can't disable client isr (%d)\n",
762 CSDIO_DEV_NAME, ret);
763 goto exit;
764 }
765 }
766 break;
767 case CSDIO_IOC_SET_VDD:
768 {
769 unsigned int vdd = 0;
770
771 ret = get_user(vdd, (unsigned int __user *)arg);
772 if (ret) {
773 pr_err("%s0: CSDIO_IOC_SET_VDD,"
774 " get data from user space failed\n",
775 CSDIO_DEV_NAME);
776 goto exit;
777 }
778 pr_info(CSDIO_DEV_NAME" CSDIO_IOC_SET_VDD - %d\n", vdd);
779
780 ret = set_vdd_helper(vdd);
781 if (ret)
782 goto exit;
783 }
784 break;
785 case CSDIO_IOC_GET_VDD:
786 {
787 if (NULL == g_csdio.m_host) {
788 pr_err("%s0: CSDIO_IOC_GET_VDD,"
789 " no MMC host assigned\n",
790 CSDIO_DEV_NAME);
791 ret = -EFAULT;
792 goto exit;
793 }
794 ret = put_user(g_csdio.m_host->ios.vdd,
795 (unsigned short __user *)arg);
796 if (ret) {
797 pr_err("%s0: CSDIO_IOC_GET_VDD, put data"
798 " to user space failed\n",
799 CSDIO_DEV_NAME);
800 goto exit;
801 }
802 }
803 break;
804 default: /* redundant, as cmd was checked against MAXNR */
805 pr_warning(CSDIO_DEV_NAME" Redundant IOCTL\n");
806 ret = -ENOTTY;
807 }
808exit:
809 return ret;
810}
811
812static int csdio_ctrl_fasync(int fd, struct file *filp, int mode)
813{
814 pr_info(CSDIO_DEV_NAME
815 " csdio_ctrl_fasync: fd=%d, filp=%p, mode=%d\n",
816 fd, filp, mode);
817 return fasync_helper(fd, filp, mode, &g_csdio.m_async_queue);
818}
819
820/*
821 * Open and close
822 */
823static int csdio_ctrl_open(struct inode *inode, struct file *filp)
824{
825 int ret = 0;
826 struct csdio_t *csdio_ctrl_drv = NULL; /* device information */
827
828 pr_info("CSDIO ctrl open.\n");
829 csdio_ctrl_drv = container_of(inode->i_cdev, struct csdio_t, m_cdev);
830 filp->private_data = csdio_ctrl_drv; /* for other methods */
831 return ret;
832}
833
834static int csdio_ctrl_release(struct inode *inode, struct file *filp)
835{
836 pr_info("CSDIO ctrl release.\n");
837 /* remove this filp from the asynchronously notified filp's */
838 csdio_ctrl_fasync(-1, filp, 0);
839 return 0;
840}
841
842static const struct file_operations csdio_ctrl_fops = {
843 .owner = THIS_MODULE,
844 .ioctl = csdio_ctrl_ioctl,
845 .open = csdio_ctrl_open,
846 .release = csdio_ctrl_release,
847 .fasync = csdio_ctrl_fasync,
848};
849
850static int csdio_probe(struct sdio_func *func,
851 const struct sdio_device_id *id)
852{
853 struct csdio_func_t *port;
854 int ret = 0;
855 struct mmc_host *host = func->card->host;
856
857 if (NULL != g_csdio.m_host && g_csdio.m_host != host) {
858 pr_info("%s: Device is on unexpected host\n",
859 CSDIO_DEV_NAME);
860 ret = -ENODEV;
861 goto exit;
862 }
863
864 /* enforce single instance policy */
865 if (g_csdio_func_table[func->num-1]) {
866 pr_err("%s - only single SDIO device supported",
867 sdio_func_id(func));
868 ret = -EEXIST;
869 goto exit;
870 }
871
872 port = kzalloc(sizeof(struct csdio_func_t), GFP_KERNEL);
873 if (!port) {
874 pr_err("Can't allocate memory\n");
875 ret = -ENOMEM;
876 goto exit;
877 }
878
879 /* initialize SDIO side */
880 port->m_func = func;
881 sdio_set_drvdata(func, port);
882
883 pr_info("%s - SDIO device found. Function %d\n",
884 sdio_func_id(func), func->num);
885
886 port->m_device = csdio_cdev_init(&port->m_cdev, &csdio_transport_fops,
887 csdio_minor + port->m_func->num,
888 TP_DEV_NAME, &port->m_func->dev);
889
890 /* create appropriate char device */
891 if (!port->m_device)
892 goto free;
893
894 if (0 == g_csdio.m_num_of_func && NULL == host_name)
895 g_csdio.m_host = host;
896 g_csdio.m_num_of_func++;
897 g_csdio_func_table[func->num-1] = port;
898 port->m_enabled = TRUE;
899 goto exit;
900free:
901 kfree(port);
902exit:
903 return ret;
904}
905
906static void csdio_remove(struct sdio_func *func)
907{
908 struct csdio_func_t *port = sdio_get_drvdata(func);
909
910 csdio_transport_cleanup(port);
911 sdio_claim_host(func);
912 sdio_release_irq(func);
913 sdio_disable_func(func);
914 sdio_release_host(func);
915 kfree(port);
916 g_csdio_func_table[func->num-1] = NULL;
917 g_csdio.m_num_of_func--;
918 if (0 == g_csdio.m_num_of_func && NULL == host_name)
919 g_csdio.m_host = NULL;
920 pr_info("%s%d: Device removed (%s). Function %d\n",
921 CSDIO_DEV_NAME, func->num, sdio_func_id(func), func->num);
922}
923
924/* CONFIG_CSDIO_VENDOR_ID and CONFIG_CSDIO_DEVICE_ID are defined in Kconfig.
925 * Use kernel configuration to change the values or overwrite them through
926 * module parameters */
927static struct sdio_device_id csdio_ids[] = {
928 { SDIO_DEVICE(CONFIG_CSDIO_VENDOR_ID, CONFIG_CSDIO_DEVICE_ID) },
929 { /* end: all zeroes */},
930};
931
932MODULE_DEVICE_TABLE(sdio, csdio_ids);
933
934static struct sdio_driver csdio_driver = {
935 .probe = csdio_probe,
936 .remove = csdio_remove,
937 .name = "csdio",
938 .id_table = csdio_ids,
939};
940
941static void __exit csdio_exit(void)
942{
943 dev_t devno = MKDEV(csdio_major, csdio_minor);
944
945 sdio_unregister_driver(&csdio_driver);
946 sysfs_remove_group(&g_csdio.m_device->kobj, &dev_attr_grp);
947 kfree(g_sdio_buffer);
948 device_destroy(g_csdio.m_driver_class, devno);
949 cdev_del(&g_csdio.m_cdev);
950 class_destroy(g_csdio.m_driver_class);
951 unregister_chrdev_region(devno, csdio_transport_nr_devs);
952 pr_info("%s: Exit driver module\n", CSDIO_DEV_NAME);
953}
954
955static char *csdio_devnode(struct device *dev, mode_t *mode)
956{
957 *mode = CSDIO_DEV_PERMISSIONS;
958 return NULL;
959}
960
961static int __init csdio_init(void)
962{
963 int ret = 0;
964 dev_t devno = 0;
965
966 pr_info("Init CSDIO driver module.\n");
967
968 /* Get a range of minor numbers to work with, asking for a dynamic */
969 /* major unless directed otherwise at load time. */
970 if (csdio_major) {
971 devno = MKDEV(csdio_major, csdio_minor);
972 ret = register_chrdev_region(devno, csdio_transport_nr_devs,
973 CSDIO_DEV_NAME);
974 } else {
975 ret = alloc_chrdev_region(&devno, csdio_minor,
976 csdio_transport_nr_devs, CSDIO_DEV_NAME);
977 csdio_major = MAJOR(devno);
978 }
979 if (ret < 0) {
980 pr_err("CSDIO: can't get major %d\n", csdio_major);
981 goto exit;
982 }
983 pr_info("CSDIO char driver major number is %d\n", csdio_major);
984
985 /* kernel module got parameters: overwrite vendor and device id's */
986 if ((csdio_vendor_id != 0) && (csdio_device_id != 0)) {
987 csdio_ids[0].vendor = (u16)csdio_vendor_id;
988 csdio_ids[0].device = (u16)csdio_device_id;
989 }
990
991 /* prepare create /dev/... instance */
992 g_csdio.m_driver_class = class_create(THIS_MODULE, CSDIO_DEV_NAME);
993 if (IS_ERR(g_csdio.m_driver_class)) {
994 ret = -ENOMEM;
995 pr_err(CSDIO_DEV_NAME " class_create failed\n");
996 goto unregister_region;
997 }
998 g_csdio.m_driver_class->devnode = csdio_devnode;
999
1000 /* create CSDIO ctrl driver */
1001 g_csdio.m_device = csdio_cdev_init(&g_csdio.m_cdev,
1002 &csdio_ctrl_fops, csdio_minor, CSDIO_DEV_NAME, NULL);
1003 if (!g_csdio.m_device) {
1004 pr_err("%s: Unable to create ctrl driver\n",
1005 CSDIO_DEV_NAME);
1006 goto destroy_class;
1007 }
1008
1009 g_sdio_buffer = kmalloc(CSDIO_SDIO_BUFFER_SIZE, GFP_KERNEL);
1010 if (!g_sdio_buffer) {
1011 pr_err("Unable to allocate %d bytes\n", CSDIO_SDIO_BUFFER_SIZE);
1012 ret = -ENOMEM;
1013 goto destroy_cdev;
1014 }
1015
1016 ret = sysfs_create_group(&g_csdio.m_device->kobj, &dev_attr_grp);
1017 if (ret) {
1018 pr_err("%s: Unable to create device attribute\n",
1019 CSDIO_DEV_NAME);
1020 goto free_sdio_buff;
1021 }
1022
1023 g_csdio.m_num_of_func = 0;
1024 g_csdio.m_host = NULL;
1025
1026 if (NULL != host_name) {
1027 struct device *dev = bus_find_device_by_name(&platform_bus_type,
1028 NULL, host_name);
1029 if (NULL != dev) {
1030 g_csdio.m_host = dev_get_drvdata(dev);
1031 } else {
1032 pr_err("%s: Host '%s' doesn't exist!\n", CSDIO_DEV_NAME,
1033 host_name);
1034 }
1035 }
1036
1037 pr_info("%s: Match with VendorId=0x%X, DeviceId=0x%X, Host = %s\n",
1038 CSDIO_DEV_NAME, csdio_device_id, csdio_vendor_id,
1039 (NULL == host_name) ? "Any" : host_name);
1040
1041 /* register sdio driver */
1042 ret = sdio_register_driver(&csdio_driver);
1043 if (ret) {
1044 pr_err("%s: Unable to register as SDIO driver\n",
1045 CSDIO_DEV_NAME);
1046 goto remove_group;
1047 }
1048
1049 goto exit;
1050
1051remove_group:
1052 sysfs_remove_group(&g_csdio.m_device->kobj, &dev_attr_grp);
1053free_sdio_buff:
1054 kfree(g_sdio_buffer);
1055destroy_cdev:
1056 cdev_del(&g_csdio.m_cdev);
1057destroy_class:
1058 class_destroy(g_csdio.m_driver_class);
1059unregister_region:
1060 unregister_chrdev_region(devno, csdio_transport_nr_devs);
1061exit:
1062 return ret;
1063}
1064module_param(csdio_vendor_id, uint, S_IRUGO);
1065module_param(csdio_device_id, uint, S_IRUGO);
1066module_param(host_name, charp, S_IRUGO);
1067
1068module_init(csdio_init);
1069module_exit(csdio_exit);
1070
Duy Truong790f06d2013-02-13 16:38:12 -08001071MODULE_AUTHOR("The Linux Foundation");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072MODULE_DESCRIPTION("CSDIO device driver version " VERSION);
1073MODULE_VERSION(VERSION);
1074MODULE_LICENSE("GPL v2");