V4L/DVB (8295): sms1xxx: add debug module option, to enable debug messages
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / dvb / siano / smsusb.c
CommitLineData
8d4f9d0e
ST
1/*
2 * Driver for the Siano SMS10xx USB dongle
3 *
85447060
MK
4 * author: Anatoly Greenblat
5 *
6 * Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
8d4f9d0e
ST
7 *
8 * This program is free software; you can redistribute it and/or modify
85447060
MK
9 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation;
8d4f9d0e 11 *
85447060
MK
12 * Software distributed under the License is distributed on an "AS IS"
13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
8d4f9d0e 14 *
85447060 15 * See the GNU General Public License for more details.
8d4f9d0e
ST
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
2e5c1ec8
MK
22#include <linux/kernel.h>
23#include <linux/init.h>
2e5c1ec8
MK
24#include <linux/usb.h>
25#include <linux/firmware.h>
26
2e5c1ec8 27#include "smscoreapi.h"
1c11d546 28#include "sms-cards.h"
2e5c1ec8 29
2e5c1ec8
MK
30#define USB1_BUFFER_SIZE 0x1000
31#define USB2_BUFFER_SIZE 0x4000
32
33#define MAX_BUFFERS 50
34#define MAX_URBS 10
35
18245e18 36struct smsusb_device_t;
2e5c1ec8 37
18245e18
MK
38struct smsusb_urb_t {
39 struct smscore_buffer_t *cb;
40 struct smsusb_device_t *dev;
2e5c1ec8 41
18245e18
MK
42 struct urb urb;
43};
2e5c1ec8 44
18245e18 45struct smsusb_device_t {
a83ccdd6 46 struct usb_device *udev;
18245e18 47 struct smscore_device_t *coredev;
2e5c1ec8 48
18245e18 49 struct smsusb_urb_t surbs[MAX_URBS];
2e5c1ec8 50
82237416
MK
51 int response_alignment;
52 int buffer_size;
18245e18 53};
2e5c1ec8 54
18245e18 55int smsusb_submit_urb(struct smsusb_device_t *dev, struct smsusb_urb_t *surb);
2e5c1ec8
MK
56
57void smsusb_onresponse(struct urb *urb)
58{
18245e18
MK
59 struct smsusb_urb_t *surb = (struct smsusb_urb_t *) urb->context;
60 struct smsusb_device_t *dev = surb->dev;
2e5c1ec8 61
82237416 62 if (urb->status < 0) {
a0c0abcb
MK
63 sms_info("error, urb status %d, %d bytes",
64 urb->status, urb->actual_length);
2e5c1ec8
MK
65 return;
66 }
67
82237416 68 if (urb->actual_length > 0) {
18245e18 69 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) surb->cb->p;
2e5c1ec8 70
82237416 71 if (urb->actual_length >= phdr->msgLength) {
2e5c1ec8
MK
72 surb->cb->size = phdr->msgLength;
73
82237416
MK
74 if (dev->response_alignment &&
75 (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG)) {
76
77 surb->cb->offset =
78 dev->response_alignment +
79 ((phdr->msgFlags >> 8) & 3);
80
81 /* sanity check */
82 if (((int) phdr->msgLength +
83 surb->cb->offset) > urb->actual_length) {
a0c0abcb 84 sms_info("invalid response "
068d6c0f 85 "msglen %d offset %d "
a0c0abcb 86 "size %d",
068d6c0f
MK
87 phdr->msgLength,
88 surb->cb->offset,
89 urb->actual_length);
2e5c1ec8
MK
90 goto exit_and_resubmit;
91 }
92
82237416
MK
93 /* move buffer pointer and
94 * copy header to its new location */
55ad310c 95 memcpy((char *) phdr + surb->cb->offset,
18245e18 96 phdr, sizeof(struct SmsMsgHdr_ST));
82237416 97 } else
2e5c1ec8
MK
98 surb->cb->offset = 0;
99
100 smscore_onresponse(dev->coredev, surb->cb);
101 surb->cb = NULL;
82237416 102 } else {
a0c0abcb
MK
103 sms_info("invalid response "
104 "msglen %d actual %d",
068d6c0f 105 phdr->msgLength, urb->actual_length);
2e5c1ec8
MK
106 }
107 }
108
109exit_and_resubmit:
110 smsusb_submit_urb(dev, surb);
111}
112
18245e18 113int smsusb_submit_urb(struct smsusb_device_t *dev, struct smsusb_urb_t *surb)
2e5c1ec8 114{
82237416 115 if (!surb->cb) {
2e5c1ec8 116 surb->cb = smscore_getbuffer(dev->coredev);
82237416 117 if (!surb->cb) {
a0c0abcb
MK
118 sms_info("smscore_getbuffer(...) "
119 "returned NULL");
2e5c1ec8
MK
120 return -ENOMEM;
121 }
122 }
123
124 usb_fill_bulk_urb(
125 &surb->urb,
126 dev->udev,
127 usb_rcvbulkpipe(dev->udev, 0x81),
128 surb->cb->p,
129 dev->buffer_size,
130 smsusb_onresponse,
131 surb
132 );
133 surb->urb.transfer_dma = surb->cb->phys;
134 surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
135
136 return usb_submit_urb(&surb->urb, GFP_ATOMIC);
137}
138
18245e18 139void smsusb_stop_streaming(struct smsusb_device_t *dev)
2e5c1ec8
MK
140{
141 int i;
142
fa830e8a 143 for (i = 0; i < MAX_URBS; i++) {
2e5c1ec8
MK
144 usb_kill_urb(&dev->surbs[i].urb);
145
82237416 146 if (dev->surbs[i].cb) {
2e5c1ec8
MK
147 smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
148 dev->surbs[i].cb = NULL;
149 }
150 }
151}
152
18245e18 153int smsusb_start_streaming(struct smsusb_device_t *dev)
2e5c1ec8
MK
154{
155 int i, rc;
156
fa830e8a 157 for (i = 0; i < MAX_URBS; i++) {
2e5c1ec8 158 rc = smsusb_submit_urb(dev, &dev->surbs[i]);
82237416 159 if (rc < 0) {
a0c0abcb
MK
160 sms_info("smsusb_submit_urb(...) "
161 "failed");
2e5c1ec8
MK
162 smsusb_stop_streaming(dev);
163 break;
164 }
165 }
166
167 return rc;
168}
169
170int smsusb_sendrequest(void *context, void *buffer, size_t size)
171{
18245e18 172 struct smsusb_device_t *dev = (struct smsusb_device_t *) context;
2e5c1ec8
MK
173 int dummy;
174
82237416
MK
175 return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
176 buffer, size, &dummy, 1000);
2e5c1ec8
MK
177}
178
82237416 179char *smsusb1_fw_lkup[] = {
2e5c1ec8
MK
180 "dvbt_stellar_usb.inp",
181 "dvbh_stellar_usb.inp",
182 "tdmb_stellar_usb.inp",
183 "none",
184 "dvbt_bda_stellar_usb.inp",
185};
186
187int smsusb1_load_firmware(struct usb_device *udev, int id)
188{
189 const struct firmware *fw;
a83ccdd6 190 u8 *fw_buffer;
2e5c1ec8
MK
191 int rc, dummy;
192
82237416 193 if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) {
a0c0abcb 194 sms_info("invalid firmware id specified %d", id);
2e5c1ec8
MK
195 return -EINVAL;
196 }
197
198 rc = request_firmware(&fw, smsusb1_fw_lkup[id], &udev->dev);
82237416 199 if (rc < 0) {
a0c0abcb
MK
200 sms_info("failed to open \"%s\" mode %d",
201 smsusb1_fw_lkup[id], id);
2e5c1ec8
MK
202 return rc;
203 }
204
205 fw_buffer = kmalloc(fw->size, GFP_KERNEL);
82237416 206 if (fw_buffer) {
2e5c1ec8
MK
207 memcpy(fw_buffer, fw->data, fw->size);
208
82237416
MK
209 rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2),
210 fw_buffer, fw->size, &dummy, 1000);
2e5c1ec8 211
a0c0abcb 212 sms_info("sent %d(%d) bytes, rc %d", fw->size, dummy, rc);
2e5c1ec8
MK
213
214 kfree(fw_buffer);
82237416 215 } else {
a0c0abcb 216 sms_info("failed to allocate firmware buffer");
2e5c1ec8
MK
217 rc = -ENOMEM;
218 }
219
220 release_firmware(fw);
221
222 return rc;
223}
224
225void smsusb1_detectmode(void *context, int *mode)
226{
18245e18
MK
227 char *product_string =
228 ((struct smsusb_device_t *) context)->udev->product;
2e5c1ec8
MK
229
230 *mode = DEVICE_MODE_NONE;
231
82237416 232 if (!product_string) {
2e5c1ec8 233 product_string = "none";
a0c0abcb 234 sms_err("product string not found");
82237416
MK
235 } else if (strstr(product_string, "DVBH"))
236 *mode = 1;
237 else if (strstr(product_string, "BDA"))
238 *mode = 4;
239 else if (strstr(product_string, "DVBT"))
240 *mode = 0;
241 else if (strstr(product_string, "TDMB"))
242 *mode = 2;
2e5c1ec8 243
a0c0abcb 244 sms_info("%d \"%s\"", *mode, product_string);
2e5c1ec8
MK
245}
246
247int smsusb1_setmode(void *context, int mode)
248{
18245e18
MK
249 struct SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK,
250 sizeof(struct SmsMsgHdr_ST), 0 };
2e5c1ec8 251
82237416 252 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
a0c0abcb 253 sms_info("invalid firmware id specified %d", mode);
2e5c1ec8
MK
254 return -EINVAL;
255 }
256
257 return smsusb_sendrequest(context, &Msg, sizeof(Msg));
258}
259
260void smsusb_term_device(struct usb_interface *intf)
261{
18245e18
MK
262 struct smsusb_device_t *dev =
263 (struct smsusb_device_t *) usb_get_intfdata(intf);
2e5c1ec8 264
82237416 265 if (dev) {
2e5c1ec8
MK
266 smsusb_stop_streaming(dev);
267
fa830e8a 268 /* unregister from smscore */
2e5c1ec8
MK
269 if (dev->coredev)
270 smscore_unregister_device(dev->coredev);
271
272 kfree(dev);
273
a0c0abcb 274 sms_info("device %p destroyed", dev);
2e5c1ec8
MK
275 }
276
277 usb_set_intfdata(intf, NULL);
278}
279
1c11d546 280int smsusb_init_device(struct usb_interface *intf, int board_id)
2e5c1ec8 281{
18245e18
MK
282 struct smsdevice_params_t params;
283 struct smsusb_device_t *dev;
1c11d546 284 struct sms_board *board;
2e5c1ec8
MK
285 int i, rc;
286
fa830e8a 287 /* create device object */
18245e18 288 dev = kzalloc(sizeof(struct smsusb_device_t), GFP_KERNEL);
82237416 289 if (!dev) {
a0c0abcb
MK
290 sms_info("kzalloc(sizeof(struct smsusb_device_t) "
291 "failed");
2e5c1ec8
MK
292 return -ENOMEM;
293 }
294
295 memset(&params, 0, sizeof(params));
296 usb_set_intfdata(intf, dev);
297 dev->udev = interface_to_usbdev(intf);
298
1c11d546
MK
299 board = sms_get_board(board_id);
300
301 switch (board->type) {
73104fb3 302
1c11d546 303 case SMS_STELLAR:
f17407a8
MK
304 dev->buffer_size = USB1_BUFFER_SIZE;
305
306 params.setmode_handler = smsusb1_setmode;
307 params.detectmode_handler = smsusb1_detectmode;
308 params.device_type = SMS_STELLAR;
a0c0abcb 309 sms_info("stellar device found");
f17407a8
MK
310 break;
311 default:
1c11d546
MK
312 switch (board->type) {
313 case SMS_NOVA_A0:
f17407a8 314 params.device_type = SMS_NOVA_A0;
a0c0abcb 315 sms_info("nova A0 found");
73104fb3 316 break;
1c11d546 317 case SMS_NOVA_B0:
f17407a8 318 params.device_type = SMS_NOVA_B0;
a0c0abcb 319 sms_info("nova B0 found");
73104fb3 320 break;
1c11d546 321 case SMS_VEGA:
f17407a8 322 params.device_type = SMS_VEGA;
a0c0abcb 323 sms_info("Vega found");
1c11d546
MK
324 break;
325 default:
a0c0abcb 326 sms_err("Unspecified sms device type!");
f17407a8 327 }
2e5c1ec8 328
f17407a8 329 dev->buffer_size = USB2_BUFFER_SIZE;
82237416
MK
330 dev->response_alignment =
331 dev->udev->ep_in[1]->desc.wMaxPacketSize -
18245e18 332 sizeof(struct SmsMsgHdr_ST);
2e5c1ec8 333
f17407a8
MK
334 params.flags |= SMS_DEVICE_FAMILY2;
335 break;
2e5c1ec8
MK
336 }
337
338 params.device = &dev->udev->dev;
339 params.buffer_size = dev->buffer_size;
340 params.num_buffers = MAX_BUFFERS;
341 params.sendrequest_handler = smsusb_sendrequest;
342 params.context = dev;
82237416
MK
343 snprintf(params.devpath, sizeof(params.devpath),
344 "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath);
2e5c1ec8 345
82237416 346 /* register in smscore */
2e5c1ec8 347 rc = smscore_register_device(&params, &dev->coredev);
82237416 348 if (rc < 0) {
a0c0abcb
MK
349 sms_info("smscore_register_device(...) failed, "
350 "rc %d", rc);
2e5c1ec8
MK
351 smsusb_term_device(intf);
352 return rc;
353 }
354
1c11d546
MK
355 smscore_set_board_id(dev->coredev, board_id);
356
fa830e8a 357 /* initialize urbs */
82237416 358 for (i = 0; i < MAX_URBS; i++) {
2e5c1ec8
MK
359 dev->surbs[i].dev = dev;
360 usb_init_urb(&dev->surbs[i].urb);
361 }
362
a0c0abcb 363 sms_info("smsusb_start_streaming(...).");
2e5c1ec8 364 rc = smsusb_start_streaming(dev);
82237416 365 if (rc < 0) {
a0c0abcb 366 sms_info("smsusb_start_streaming(...) failed");
2e5c1ec8
MK
367 smsusb_term_device(intf);
368 return rc;
369 }
370
371 rc = smscore_start_device(dev->coredev);
82237416 372 if (rc < 0) {
a0c0abcb 373 sms_info("smscore_start_device(...) failed");
2e5c1ec8
MK
374 smsusb_term_device(intf);
375 return rc;
376 }
377
a0c0abcb 378 sms_info("device %p created", dev);
2e5c1ec8
MK
379
380 return rc;
381}
382
383int smsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
384{
385 struct usb_device *udev = interface_to_usbdev(intf);
386 char devpath[32];
387 int i, rc;
388
f17407a8
MK
389 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
390 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
391
82237416 392 if (intf->num_altsetting > 0) {
59bf6b8e
MK
393 rc = usb_set_interface(
394 udev, intf->cur_altsetting->desc.bInterfaceNumber, 0);
82237416 395 if (rc < 0) {
a0c0abcb
MK
396 sms_info("usb_set_interface failed, "
397 "rc %d", rc);
2e5c1ec8
MK
398 return rc;
399 }
400 }
401
a0c0abcb 402 sms_info("smsusb_probe %d",
82237416 403 intf->cur_altsetting->desc.bInterfaceNumber);
fa830e8a 404 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
a0c0abcb 405 sms_info("endpoint %d %02x %02x %d", i,
82237416
MK
406 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
407 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
408 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
2e5c1ec8 409
82237416
MK
410 if ((udev->actconfig->desc.bNumInterfaces == 2) &&
411 (intf->cur_altsetting->desc.bInterfaceNumber == 0)) {
a0c0abcb 412 sms_info("rom interface 0 is not used");
2e5c1ec8
MK
413 return -ENODEV;
414 }
415
82237416
MK
416 if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
417 snprintf(devpath, sizeof(devpath), "usb\\%d-%s",
418 udev->bus->busnum, udev->devpath);
a0c0abcb 419 sms_info("stellar device was found.");
59bf6b8e
MK
420 return smsusb1_load_firmware(
421 udev, smscore_registry_getmode(devpath));
2e5c1ec8
MK
422 }
423
1c11d546 424 rc = smsusb_init_device(intf, id->driver_info);
a0c0abcb 425 sms_info("rc %d", rc);
f17407a8 426 return rc;
2e5c1ec8
MK
427}
428
429void smsusb_disconnect(struct usb_interface *intf)
430{
431 smsusb_term_device(intf);
432}
433
434static struct usb_driver smsusb_driver = {
435 .name = "smsusb",
436 .probe = smsusb_probe,
82237416 437 .disconnect = smsusb_disconnect,
2e5c1ec8
MK
438 .id_table = smsusb_id_table,
439};
440
eae55660 441int smsusb_register(void)
2e5c1ec8
MK
442{
443 int rc = usb_register(&smsusb_driver);
444 if (rc)
a0c0abcb 445 sms_info("usb_register failed. Error number %d", rc);
2e5c1ec8 446
a0c0abcb 447 sms_debug("");
2e5c1ec8
MK
448
449 return rc;
450}
451
eae55660 452void smsusb_unregister(void)
2e5c1ec8 453{
a0c0abcb 454 sms_debug("");
3dd24378 455 /* Regular USB Cleanup */
2e5c1ec8 456 usb_deregister(&smsusb_driver);
2e5c1ec8
MK
457}
458