Merge branch 'fix/pcm-hwptr' into for-linus
[linux/fpc-iii.git] / drivers / media / dvb / siano / smssdio.c
blobdfaa49a53f325ea917bd72d21fe91cc0a8c80cae
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
50 static const struct sdio_device_id smssdio_ids[] = {
51 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_STELLAR),
52 .driver_data = SMS1XXX_BOARD_SIANO_STELLAR},
53 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_A0),
54 .driver_data = SMS1XXX_BOARD_SIANO_NOVA_A},
55 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_NOVA_B0),
56 .driver_data = SMS1XXX_BOARD_SIANO_NOVA_B},
57 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_VEGA_A0),
58 .driver_data = SMS1XXX_BOARD_SIANO_VEGA},
59 {SDIO_DEVICE(SDIO_VENDOR_ID_SIANO, SDIO_DEVICE_ID_SIANO_VENICE),
60 .driver_data = SMS1XXX_BOARD_SIANO_VEGA},
61 { /* end: all zeroes */ },
64 MODULE_DEVICE_TABLE(sdio, smssdio_ids);
66 struct smssdio_device {
67 struct sdio_func *func;
69 struct smscore_device_t *coredev;
71 struct smscore_buffer_t *split_cb;
74 /*******************************************************************/
75 /* Siano core callbacks */
76 /*******************************************************************/
78 static int smssdio_sendrequest(void *context, void *buffer, size_t size)
80 int ret;
81 struct smssdio_device *smsdev;
83 smsdev = context;
85 sdio_claim_host(smsdev->func);
87 while (size >= smsdev->func->cur_blksize) {
88 ret = sdio_write_blocks(smsdev->func, SMSSDIO_DATA, buffer, 1);
89 if (ret)
90 goto out;
92 buffer += smsdev->func->cur_blksize;
93 size -= smsdev->func->cur_blksize;
96 if (size) {
97 ret = sdio_write_bytes(smsdev->func, SMSSDIO_DATA,
98 buffer, size);
101 out:
102 sdio_release_host(smsdev->func);
104 return ret;
107 /*******************************************************************/
108 /* SDIO callbacks */
109 /*******************************************************************/
111 static void smssdio_interrupt(struct sdio_func *func)
113 int ret, isr;
115 struct smssdio_device *smsdev;
116 struct smscore_buffer_t *cb;
117 struct SmsMsgHdr_ST *hdr;
118 size_t size;
120 smsdev = sdio_get_drvdata(func);
123 * The interrupt register has no defined meaning. It is just
124 * a way of turning of the level triggered interrupt.
126 isr = sdio_readb(func, SMSSDIO_INT, &ret);
127 if (ret) {
128 dev_err(&smsdev->func->dev,
129 "Unable to read interrupt register!\n");
130 return;
133 if (smsdev->split_cb == NULL) {
134 cb = smscore_getbuffer(smsdev->coredev);
135 if (!cb) {
136 dev_err(&smsdev->func->dev,
137 "Unable to allocate data buffer!\n");
138 return;
141 ret = sdio_read_blocks(smsdev->func, cb->p, SMSSDIO_DATA, 1);
142 if (ret) {
143 dev_err(&smsdev->func->dev,
144 "Error %d reading initial block!\n", ret);
145 return;
148 hdr = cb->p;
150 if (hdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG) {
151 smsdev->split_cb = cb;
152 return;
155 size = hdr->msgLength - smsdev->func->cur_blksize;
156 } else {
157 cb = smsdev->split_cb;
158 hdr = cb->p;
160 size = hdr->msgLength - sizeof(struct SmsMsgHdr_ST);
162 smsdev->split_cb = NULL;
165 if (hdr->msgLength > smsdev->func->cur_blksize) {
166 void *buffer;
168 size = ALIGN(size, 128);
169 buffer = cb->p + hdr->msgLength;
171 BUG_ON(smsdev->func->cur_blksize != 128);
174 * First attempt to transfer all of it in one go...
176 ret = sdio_read_blocks(smsdev->func, buffer,
177 SMSSDIO_DATA, size / 128);
178 if (ret && ret != -EINVAL) {
179 smscore_putbuffer(smsdev->coredev, cb);
180 dev_err(&smsdev->func->dev,
181 "Error %d reading data from card!\n", ret);
182 return;
186 * ..then fall back to one block at a time if that is
187 * not possible...
189 * (we have to do this manually because of the
190 * problem with the "increase address" bit)
192 if (ret == -EINVAL) {
193 while (size) {
194 ret = sdio_read_blocks(smsdev->func,
195 buffer, SMSSDIO_DATA, 1);
196 if (ret) {
197 smscore_putbuffer(smsdev->coredev, cb);
198 dev_err(&smsdev->func->dev,
199 "Error %d reading "
200 "data from card!\n", ret);
201 return;
204 buffer += smsdev->func->cur_blksize;
205 if (size > smsdev->func->cur_blksize)
206 size -= smsdev->func->cur_blksize;
207 else
208 size = 0;
213 cb->size = hdr->msgLength;
214 cb->offset = 0;
216 smscore_onresponse(smsdev->coredev, cb);
219 static int smssdio_probe(struct sdio_func *func,
220 const struct sdio_device_id *id)
222 int ret;
224 int board_id;
225 struct smssdio_device *smsdev;
226 struct smsdevice_params_t params;
228 board_id = id->driver_data;
230 smsdev = kzalloc(sizeof(struct smssdio_device), GFP_KERNEL);
231 if (!smsdev)
232 return -ENOMEM;
234 smsdev->func = func;
236 memset(&params, 0, sizeof(struct smsdevice_params_t));
238 params.device = &func->dev;
239 params.buffer_size = 0x5000; /* ?? */
240 params.num_buffers = 22; /* ?? */
241 params.context = smsdev;
243 snprintf(params.devpath, sizeof(params.devpath),
244 "sdio\\%s", sdio_func_id(func));
246 params.sendrequest_handler = smssdio_sendrequest;
248 params.device_type = sms_get_board(board_id)->type;
250 if (params.device_type != SMS_STELLAR)
251 params.flags |= SMS_DEVICE_FAMILY2;
252 else {
254 * FIXME: Stellar needs special handling...
256 ret = -ENODEV;
257 goto free;
260 ret = smscore_register_device(&params, &smsdev->coredev);
261 if (ret < 0)
262 goto free;
264 smscore_set_board_id(smsdev->coredev, board_id);
266 sdio_claim_host(func);
268 ret = sdio_enable_func(func);
269 if (ret)
270 goto release;
272 ret = sdio_set_block_size(func, 128);
273 if (ret)
274 goto disable;
276 ret = sdio_claim_irq(func, smssdio_interrupt);
277 if (ret)
278 goto disable;
280 sdio_set_drvdata(func, smsdev);
282 sdio_release_host(func);
284 ret = smscore_start_device(smsdev->coredev);
285 if (ret < 0)
286 goto reclaim;
288 return 0;
290 reclaim:
291 sdio_claim_host(func);
292 sdio_release_irq(func);
293 disable:
294 sdio_disable_func(func);
295 release:
296 sdio_release_host(func);
297 smscore_unregister_device(smsdev->coredev);
298 free:
299 kfree(smsdev);
301 return ret;
304 static void smssdio_remove(struct sdio_func *func)
306 struct smssdio_device *smsdev;
308 smsdev = sdio_get_drvdata(func);
310 /* FIXME: racy! */
311 if (smsdev->split_cb)
312 smscore_putbuffer(smsdev->coredev, smsdev->split_cb);
314 smscore_unregister_device(smsdev->coredev);
316 sdio_claim_host(func);
317 sdio_release_irq(func);
318 sdio_disable_func(func);
319 sdio_release_host(func);
321 kfree(smsdev);
324 static struct sdio_driver smssdio_driver = {
325 .name = "smssdio",
326 .id_table = smssdio_ids,
327 .probe = smssdio_probe,
328 .remove = smssdio_remove,
331 /*******************************************************************/
332 /* Module functions */
333 /*******************************************************************/
335 int smssdio_module_init(void)
337 int ret = 0;
339 printk(KERN_INFO "smssdio: Siano SMS1xxx SDIO driver\n");
340 printk(KERN_INFO "smssdio: Copyright Pierre Ossman\n");
342 ret = sdio_register_driver(&smssdio_driver);
344 return ret;
347 void smssdio_module_exit(void)
349 sdio_unregister_driver(&smssdio_driver);
352 module_init(smssdio_module_init);
353 module_exit(smssdio_module_exit);
355 MODULE_DESCRIPTION("Siano SMS1xxx SDIO driver");
356 MODULE_AUTHOR("Pierre Ossman");
357 MODULE_LICENSE("GPL");