Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / dvb / siano / smssdio.c
blob195244a3e69b35400df3dd83a9856c76417cef9b
1 /*
2 * smssdio.c - Siano 1xxx SDIO interface driver
4 * Copyright 2008 Pierre Ossman
6 * Based on code by Siano Mobile Silicon, Inc.,
7 * Copyright (C) 2006-2008, Uri Shkolnik
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
15 * This hardware is a bit odd in that all transfers should be done
16 * to/from the SMSSDIO_DATA register, yet the "increase address" bit
17 * always needs to be set.
19 * Also, buffers from the card are always aligned to 128 byte
20 * boundaries.
24 * General cleanup notes:
26 * - only typedefs should be name *_t
28 * - use ERR_PTR and friends for smscore_register_device()
30 * - smscore_getbuffer should zero fields
32 * Fix stop command
35 #include <linux/moduleparam.h>
36 #include <linux/firmware.h>
37 #include <linux/delay.h>
38 #include <linux/mmc/card.h>
39 #include <linux/mmc/sdio_func.h>
40 #include <linux/mmc/sdio_ids.h>
42 #include "smscoreapi.h"
43 #include "sms-cards.h"
45 /* Registers */
47 #define SMSSDIO_DATA 0x00
48 #define SMSSDIO_INT 0x04
49 #define SMSSDIO_BLOCK_SIZE 128
51 static const struct sdio_device_id smssdio_ids[] __devinitconst = {
52 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_STELLAR),
53 .driver_data = SMS1XXX_BOARD_SIANO_STELLAR},
54 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_A0),
55 .driver_data = SMS1XXX_BOARD_SIANO_NOVA_A},
56 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_B0),
57 .driver_data = SMS1XXX_BOARD_SIANO_NOVA_B},
58 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_VEGA_A0),
59 .driver_data = SMS1XXX_BOARD_SIANO_VEGA},
60 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_VENICE),
61 .driver_data = SMS1XXX_BOARD_SIANO_VEGA},
62 { /* end: all zeroes */ },
65 MODULE_DEVICE_TABLE(sdio, smssdio_ids);
67 struct smssdio_device {
68 struct sdio_func *func;
70 struct smscore_device_t *coredev;
72 struct smscore_buffer_t *split_cb;
75 /*******************************************************************/
76 /* Siano core callbacks */
77 /*******************************************************************/
79 static int smssdio_sendrequest(void *context, void *buffer, size_t size)
81 int ret = 0;
82 struct smssdio_device *smsdev;
84 smsdev = context;
86 sdio_claim_host(smsdev->func);
88 while (size >= smsdev->func->cur_blksize) {
89 ret = sdio_memcpy_toio(smsdev->func, SMSSDIO_DATA,
90 buffer, smsdev->func->cur_blksize);
91 if (ret)
92 goto out;
94 buffer += smsdev->func->cur_blksize;
95 size -= smsdev->func->cur_blksize;
98 if (size) {
99 ret = sdio_memcpy_toio(smsdev->func, SMSSDIO_DATA,
100 buffer, size);
103 out:
104 sdio_release_host(smsdev->func);
106 return ret;
109 /*******************************************************************/
110 /* SDIO callbacks */
111 /*******************************************************************/
113 static void smssdio_interrupt(struct sdio_func *func)
115 int ret, isr;
117 struct smssdio_device *smsdev;
118 struct smscore_buffer_t *cb;
119 struct SmsMsgHdr_ST *hdr;
120 size_t size;
122 smsdev = sdio_get_drvdata(func);
125 * The interrupt register has no defined meaning. It is just
126 * a way of turning of the level triggered interrupt.
128 isr = sdio_readb(func, SMSSDIO_INT, &ret);
129 if (ret) {
130 sms_err("Unable to read interrupt register!\n");
131 return;
134 if (smsdev->split_cb == NULL) {
135 cb = smscore_getbuffer(smsdev->coredev);
136 if (!cb) {
137 sms_err("Unable to allocate data buffer!\n");
138 return;
141 ret = sdio_memcpy_fromio(smsdev->func,
142 cb->p,
143 SMSSDIO_DATA,
144 SMSSDIO_BLOCK_SIZE);
145 if (ret) {
146 sms_err("Error %d reading initial block!\n", ret);
147 return;
150 hdr = cb->p;
152 if (hdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG) {
153 smsdev->split_cb = cb;
154 return;
157 if (hdr->msgLength > smsdev->func->cur_blksize)
158 size = hdr->msgLength - smsdev->func->cur_blksize;
159 else
160 size = 0;
161 } else {
162 cb = smsdev->split_cb;
163 hdr = cb->p;
165 size = hdr->msgLength - sizeof(struct SmsMsgHdr_ST);
167 smsdev->split_cb = NULL;
170 if (size) {
171 void *buffer;
173 buffer = cb->p + (hdr->msgLength - size);
174 size = ALIGN(size, SMSSDIO_BLOCK_SIZE);
176 BUG_ON(smsdev->func->cur_blksize != SMSSDIO_BLOCK_SIZE);
179 * First attempt to transfer all of it in one go...
181 ret = sdio_memcpy_fromio(smsdev->func,
182 buffer,
183 SMSSDIO_DATA,
184 size);
185 if (ret && ret != -EINVAL) {
186 smscore_putbuffer(smsdev->coredev, cb);
187 sms_err("Error %d reading data from card!\n", ret);
188 return;
192 * ..then fall back to one block at a time if that is
193 * not possible...
195 * (we have to do this manually because of the
196 * problem with the "increase address" bit)
198 if (ret == -EINVAL) {
199 while (size) {
200 ret = sdio_memcpy_fromio(smsdev->func,
201 buffer, SMSSDIO_DATA,
202 smsdev->func->cur_blksize);
203 if (ret) {
204 smscore_putbuffer(smsdev->coredev, cb);
205 sms_err("Error %d reading "
206 "data from card!\n", ret);
207 return;
210 buffer += smsdev->func->cur_blksize;
211 if (size > smsdev->func->cur_blksize)
212 size -= smsdev->func->cur_blksize;
213 else
214 size = 0;
219 cb->size = hdr->msgLength;
220 cb->offset = 0;
222 smscore_onresponse(smsdev->coredev, cb);
225 static int __devinit smssdio_probe(struct sdio_func *func,
226 const struct sdio_device_id *id)
228 int ret;
230 int board_id;
231 struct smssdio_device *smsdev;
232 struct smsdevice_params_t params;
234 board_id = id->driver_data;
236 smsdev = kzalloc(sizeof(struct smssdio_device), GFP_KERNEL);
237 if (!smsdev)
238 return -ENOMEM;
240 smsdev->func = func;
242 memset(&params, 0, sizeof(struct smsdevice_params_t));
244 params.device = &func->dev;
245 params.buffer_size = 0x5000; /* ?? */
246 params.num_buffers = 22; /* ?? */
247 params.context = smsdev;
249 snprintf(params.devpath, sizeof(params.devpath),
250 "sdio\\%s", sdio_func_id(func));
252 params.sendrequest_handler = smssdio_sendrequest;
254 params.device_type = sms_get_board(board_id)->type;
256 if (params.device_type != SMS_STELLAR)
257 params.flags |= SMS_DEVICE_FAMILY2;
258 else {
260 * FIXME: Stellar needs special handling...
262 ret = -ENODEV;
263 goto free;
266 ret = smscore_register_device(&params, &smsdev->coredev);
267 if (ret < 0)
268 goto free;
270 smscore_set_board_id(smsdev->coredev, board_id);
272 sdio_claim_host(func);
274 ret = sdio_enable_func(func);
275 if (ret)
276 goto release;
278 ret = sdio_set_block_size(func, SMSSDIO_BLOCK_SIZE);
279 if (ret)
280 goto disable;
282 ret = sdio_claim_irq(func, smssdio_interrupt);
283 if (ret)
284 goto disable;
286 sdio_set_drvdata(func, smsdev);
288 sdio_release_host(func);
290 ret = smscore_start_device(smsdev->coredev);
291 if (ret < 0)
292 goto reclaim;
294 return 0;
296 reclaim:
297 sdio_claim_host(func);
298 sdio_release_irq(func);
299 disable:
300 sdio_disable_func(func);
301 release:
302 sdio_release_host(func);
303 smscore_unregister_device(smsdev->coredev);
304 free:
305 kfree(smsdev);
307 return ret;
310 static void smssdio_remove(struct sdio_func *func)
312 struct smssdio_device *smsdev;
314 smsdev = sdio_get_drvdata(func);
316 /* FIXME: racy! */
317 if (smsdev->split_cb)
318 smscore_putbuffer(smsdev->coredev, smsdev->split_cb);
320 smscore_unregister_device(smsdev->coredev);
322 sdio_claim_host(func);
323 sdio_release_irq(func);
324 sdio_disable_func(func);
325 sdio_release_host(func);
327 kfree(smsdev);
330 static struct sdio_driver smssdio_driver = {
331 .name = "smssdio",
332 .id_table = smssdio_ids,
333 .probe = smssdio_probe,
334 .remove = smssdio_remove,
337 /*******************************************************************/
338 /* Module functions */
339 /*******************************************************************/
341 static int __init smssdio_module_init(void)
343 int ret = 0;
345 printk(KERN_INFO "smssdio: Siano SMS1xxx SDIO driver\n");
346 printk(KERN_INFO "smssdio: Copyright Pierre Ossman\n");
348 ret = sdio_register_driver(&smssdio_driver);
350 return ret;
353 static void __exit smssdio_module_exit(void)
355 sdio_unregister_driver(&smssdio_driver);
358 module_init(smssdio_module_init);
359 module_exit(smssdio_module_exit);
361 MODULE_DESCRIPTION("Siano SMS1xxx SDIO driver");
362 MODULE_AUTHOR("Pierre Ossman");
363 MODULE_LICENSE("GPL");