Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / media / dvb / siano / smsusb.c
bloba9c27fb69ba747272393bb033c65d4f48a7e194d
1 /****************************************************************
3 Siano Mobile Silicon, Inc.
4 MDTV receiver kernel modules.
5 Copyright (C) 2005-2009, Uri Shkolnik, Anatoly Greenblat
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ****************************************************************/
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/usb.h>
25 #include <linux/firmware.h>
26 #include <linux/slab.h>
28 #include "smscoreapi.h"
29 #include "sms-cards.h"
30 #include "smsendian.h"
32 static int sms_dbg;
33 module_param_named(debug, sms_dbg, int, 0644);
34 MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
36 #define USB1_BUFFER_SIZE 0x1000
37 #define USB2_BUFFER_SIZE 0x4000
39 #define MAX_BUFFERS 50
40 #define MAX_URBS 10
42 struct smsusb_device_t;
44 struct smsusb_urb_t {
45 struct smscore_buffer_t *cb;
46 struct smsusb_device_t *dev;
48 struct urb urb;
51 struct smsusb_device_t {
52 struct usb_device *udev;
53 struct smscore_device_t *coredev;
55 struct smsusb_urb_t surbs[MAX_URBS];
57 int response_alignment;
58 int buffer_size;
61 static int smsusb_submit_urb(struct smsusb_device_t *dev,
62 struct smsusb_urb_t *surb);
64 static void smsusb_onresponse(struct urb *urb)
66 struct smsusb_urb_t *surb = (struct smsusb_urb_t *) urb->context;
67 struct smsusb_device_t *dev = surb->dev;
69 if (urb->status == -ESHUTDOWN) {
70 sms_err("error, urb status %d (-ESHUTDOWN), %d bytes",
71 urb->status, urb->actual_length);
72 return;
75 if ((urb->actual_length > 0) && (urb->status == 0)) {
76 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *)surb->cb->p;
78 smsendian_handle_message_header(phdr);
79 if (urb->actual_length >= phdr->msgLength) {
80 surb->cb->size = phdr->msgLength;
82 if (dev->response_alignment &&
83 (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG)) {
85 surb->cb->offset =
86 dev->response_alignment +
87 ((phdr->msgFlags >> 8) & 3);
89 /* sanity check */
90 if (((int) phdr->msgLength +
91 surb->cb->offset) > urb->actual_length) {
92 sms_err("invalid response "
93 "msglen %d offset %d "
94 "size %d",
95 phdr->msgLength,
96 surb->cb->offset,
97 urb->actual_length);
98 goto exit_and_resubmit;
101 /* move buffer pointer and
102 * copy header to its new location */
103 memcpy((char *) phdr + surb->cb->offset,
104 phdr, sizeof(struct SmsMsgHdr_ST));
105 } else
106 surb->cb->offset = 0;
108 smscore_onresponse(dev->coredev, surb->cb);
109 surb->cb = NULL;
110 } else {
111 sms_err("invalid response "
112 "msglen %d actual %d",
113 phdr->msgLength, urb->actual_length);
115 } else
116 sms_err("error, urb status %d, %d bytes",
117 urb->status, urb->actual_length);
120 exit_and_resubmit:
121 smsusb_submit_urb(dev, surb);
124 static int smsusb_submit_urb(struct smsusb_device_t *dev,
125 struct smsusb_urb_t *surb)
127 if (!surb->cb) {
128 surb->cb = smscore_getbuffer(dev->coredev);
129 if (!surb->cb) {
130 sms_err("smscore_getbuffer(...) returned NULL");
131 return -ENOMEM;
135 usb_fill_bulk_urb(
136 &surb->urb,
137 dev->udev,
138 usb_rcvbulkpipe(dev->udev, 0x81),
139 surb->cb->p,
140 dev->buffer_size,
141 smsusb_onresponse,
142 surb
144 surb->urb.transfer_dma = surb->cb->phys;
145 surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
147 return usb_submit_urb(&surb->urb, GFP_ATOMIC);
150 static void smsusb_stop_streaming(struct smsusb_device_t *dev)
152 int i;
154 for (i = 0; i < MAX_URBS; i++) {
155 usb_kill_urb(&dev->surbs[i].urb);
157 if (dev->surbs[i].cb) {
158 smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
159 dev->surbs[i].cb = NULL;
164 static int smsusb_start_streaming(struct smsusb_device_t *dev)
166 int i, rc;
168 for (i = 0; i < MAX_URBS; i++) {
169 rc = smsusb_submit_urb(dev, &dev->surbs[i]);
170 if (rc < 0) {
171 sms_err("smsusb_submit_urb(...) failed");
172 smsusb_stop_streaming(dev);
173 break;
177 return rc;
180 static int smsusb_sendrequest(void *context, void *buffer, size_t size)
182 struct smsusb_device_t *dev = (struct smsusb_device_t *) context;
183 int dummy;
185 smsendian_handle_message_header((struct SmsMsgHdr_ST *)buffer);
186 return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
187 buffer, size, &dummy, 1000);
190 static char *smsusb1_fw_lkup[] = {
191 "dvbt_stellar_usb.inp",
192 "dvbh_stellar_usb.inp",
193 "tdmb_stellar_usb.inp",
194 "none",
195 "dvbt_bda_stellar_usb.inp",
198 static inline char *sms_get_fw_name(int mode, int board_id)
200 char **fw = sms_get_board(board_id)->fw;
201 return (fw && fw[mode]) ? fw[mode] : smsusb1_fw_lkup[mode];
204 static int smsusb1_load_firmware(struct usb_device *udev, int id, int board_id)
206 const struct firmware *fw;
207 u8 *fw_buffer;
208 int rc, dummy;
209 char *fw_filename;
211 if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) {
212 sms_err("invalid firmware id specified %d", id);
213 return -EINVAL;
216 fw_filename = sms_get_fw_name(id, board_id);
218 rc = request_firmware(&fw, fw_filename, &udev->dev);
219 if (rc < 0) {
220 sms_warn("failed to open \"%s\" mode %d, "
221 "trying again with default firmware", fw_filename, id);
223 fw_filename = smsusb1_fw_lkup[id];
224 rc = request_firmware(&fw, fw_filename, &udev->dev);
225 if (rc < 0) {
226 sms_warn("failed to open \"%s\" mode %d",
227 fw_filename, id);
229 return rc;
233 fw_buffer = kmalloc(fw->size, GFP_KERNEL);
234 if (fw_buffer) {
235 memcpy(fw_buffer, fw->data, fw->size);
237 rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2),
238 fw_buffer, fw->size, &dummy, 1000);
240 sms_info("sent %zd(%d) bytes, rc %d", fw->size, dummy, rc);
242 kfree(fw_buffer);
243 } else {
244 sms_err("failed to allocate firmware buffer");
245 rc = -ENOMEM;
247 sms_info("read FW %s, size=%zd", fw_filename, fw->size);
249 release_firmware(fw);
251 return rc;
254 static void smsusb1_detectmode(void *context, int *mode)
256 char *product_string =
257 ((struct smsusb_device_t *) context)->udev->product;
259 *mode = DEVICE_MODE_NONE;
261 if (!product_string) {
262 product_string = "none";
263 sms_err("product string not found");
264 } else if (strstr(product_string, "DVBH"))
265 *mode = 1;
266 else if (strstr(product_string, "BDA"))
267 *mode = 4;
268 else if (strstr(product_string, "DVBT"))
269 *mode = 0;
270 else if (strstr(product_string, "TDMB"))
271 *mode = 2;
273 sms_info("%d \"%s\"", *mode, product_string);
276 static int smsusb1_setmode(void *context, int mode)
278 struct SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK,
279 sizeof(struct SmsMsgHdr_ST), 0 };
281 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
282 sms_err("invalid firmware id specified %d", mode);
283 return -EINVAL;
286 return smsusb_sendrequest(context, &Msg, sizeof(Msg));
289 static void smsusb_term_device(struct usb_interface *intf)
291 struct smsusb_device_t *dev =
292 (struct smsusb_device_t *) usb_get_intfdata(intf);
294 if (dev) {
295 smsusb_stop_streaming(dev);
297 /* unregister from smscore */
298 if (dev->coredev)
299 smscore_unregister_device(dev->coredev);
301 kfree(dev);
303 sms_info("device %p destroyed", dev);
306 usb_set_intfdata(intf, NULL);
309 static int smsusb_init_device(struct usb_interface *intf, int board_id)
311 struct smsdevice_params_t params;
312 struct smsusb_device_t *dev;
313 int i, rc;
315 /* create device object */
316 dev = kzalloc(sizeof(struct smsusb_device_t), GFP_KERNEL);
317 if (!dev) {
318 sms_err("kzalloc(sizeof(struct smsusb_device_t) failed");
319 return -ENOMEM;
322 memset(&params, 0, sizeof(params));
323 usb_set_intfdata(intf, dev);
324 dev->udev = interface_to_usbdev(intf);
326 params.device_type = sms_get_board(board_id)->type;
328 switch (params.device_type) {
329 case SMS_STELLAR:
330 dev->buffer_size = USB1_BUFFER_SIZE;
332 params.setmode_handler = smsusb1_setmode;
333 params.detectmode_handler = smsusb1_detectmode;
334 break;
335 default:
336 sms_err("Unspecified sms device type!");
337 /* fall-thru */
338 case SMS_NOVA_A0:
339 case SMS_NOVA_B0:
340 case SMS_VEGA:
341 dev->buffer_size = USB2_BUFFER_SIZE;
342 dev->response_alignment =
343 le16_to_cpu(dev->udev->ep_in[1]->desc.wMaxPacketSize) -
344 sizeof(struct SmsMsgHdr_ST);
346 params.flags |= SMS_DEVICE_FAMILY2;
347 break;
350 params.device = &dev->udev->dev;
351 params.buffer_size = dev->buffer_size;
352 params.num_buffers = MAX_BUFFERS;
353 params.sendrequest_handler = smsusb_sendrequest;
354 params.context = dev;
355 snprintf(params.devpath, sizeof(params.devpath),
356 "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath);
358 /* register in smscore */
359 rc = smscore_register_device(&params, &dev->coredev);
360 if (rc < 0) {
361 sms_err("smscore_register_device(...) failed, rc %d", rc);
362 smsusb_term_device(intf);
363 return rc;
366 smscore_set_board_id(dev->coredev, board_id);
368 /* initialize urbs */
369 for (i = 0; i < MAX_URBS; i++) {
370 dev->surbs[i].dev = dev;
371 usb_init_urb(&dev->surbs[i].urb);
374 sms_info("smsusb_start_streaming(...).");
375 rc = smsusb_start_streaming(dev);
376 if (rc < 0) {
377 sms_err("smsusb_start_streaming(...) failed");
378 smsusb_term_device(intf);
379 return rc;
382 rc = smscore_start_device(dev->coredev);
383 if (rc < 0) {
384 sms_err("smscore_start_device(...) failed");
385 smsusb_term_device(intf);
386 return rc;
389 sms_info("device %p created", dev);
391 return rc;
394 static int __devinit smsusb_probe(struct usb_interface *intf,
395 const struct usb_device_id *id)
397 struct usb_device *udev = interface_to_usbdev(intf);
398 char devpath[32];
399 int i, rc;
401 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
402 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
404 if (intf->num_altsetting > 0) {
405 rc = usb_set_interface(
406 udev, intf->cur_altsetting->desc.bInterfaceNumber, 0);
407 if (rc < 0) {
408 sms_err("usb_set_interface failed, rc %d", rc);
409 return rc;
413 sms_info("smsusb_probe %d",
414 intf->cur_altsetting->desc.bInterfaceNumber);
415 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
416 sms_info("endpoint %d %02x %02x %d", i,
417 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
418 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
419 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
421 if ((udev->actconfig->desc.bNumInterfaces == 2) &&
422 (intf->cur_altsetting->desc.bInterfaceNumber == 0)) {
423 sms_err("rom interface 0 is not used");
424 return -ENODEV;
427 if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
428 snprintf(devpath, sizeof(devpath), "usb\\%d-%s",
429 udev->bus->busnum, udev->devpath);
430 sms_info("stellar device was found.");
431 return smsusb1_load_firmware(
432 udev, smscore_registry_getmode(devpath),
433 id->driver_info);
436 rc = smsusb_init_device(intf, id->driver_info);
437 sms_info("rc %d", rc);
438 sms_board_load_modules(id->driver_info);
439 return rc;
442 static void smsusb_disconnect(struct usb_interface *intf)
444 smsusb_term_device(intf);
447 static int smsusb_suspend(struct usb_interface *intf, pm_message_t msg)
449 struct smsusb_device_t *dev =
450 (struct smsusb_device_t *)usb_get_intfdata(intf);
451 printk(KERN_INFO "%s: Entering status %d.\n", __func__, msg.event);
452 smsusb_stop_streaming(dev);
453 return 0;
456 static int smsusb_resume(struct usb_interface *intf)
458 int rc, i;
459 struct smsusb_device_t *dev =
460 (struct smsusb_device_t *)usb_get_intfdata(intf);
461 struct usb_device *udev = interface_to_usbdev(intf);
463 printk(KERN_INFO "%s: Entering.\n", __func__);
464 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
465 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
467 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
468 printk(KERN_INFO "endpoint %d %02x %02x %d\n", i,
469 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
470 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
471 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
473 if (intf->num_altsetting > 0) {
474 rc = usb_set_interface(udev,
475 intf->cur_altsetting->desc.
476 bInterfaceNumber, 0);
477 if (rc < 0) {
478 printk(KERN_INFO "%s usb_set_interface failed, "
479 "rc %d\n", __func__, rc);
480 return rc;
484 smsusb_start_streaming(dev);
485 return 0;
488 static const struct usb_device_id smsusb_id_table[] __devinitconst = {
489 { USB_DEVICE(0x187f, 0x0010),
490 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
491 { USB_DEVICE(0x187f, 0x0100),
492 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
493 { USB_DEVICE(0x187f, 0x0200),
494 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_A },
495 { USB_DEVICE(0x187f, 0x0201),
496 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_B },
497 { USB_DEVICE(0x187f, 0x0300),
498 .driver_info = SMS1XXX_BOARD_SIANO_VEGA },
499 { USB_DEVICE(0x2040, 0x1700),
500 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_CATAMOUNT },
501 { USB_DEVICE(0x2040, 0x1800),
502 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_A },
503 { USB_DEVICE(0x2040, 0x1801),
504 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_B },
505 { USB_DEVICE(0x2040, 0x2000),
506 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
507 { USB_DEVICE(0x2040, 0x2009),
508 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD_R2 },
509 { USB_DEVICE(0x2040, 0x200a),
510 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
511 { USB_DEVICE(0x2040, 0x2010),
512 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
513 { USB_DEVICE(0x2040, 0x2011),
514 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
515 { USB_DEVICE(0x2040, 0x2019),
516 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
517 { USB_DEVICE(0x2040, 0x5500),
518 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
519 { USB_DEVICE(0x2040, 0x5510),
520 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
521 { USB_DEVICE(0x2040, 0x5520),
522 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
523 { USB_DEVICE(0x2040, 0x5530),
524 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
525 { USB_DEVICE(0x2040, 0x5580),
526 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
527 { USB_DEVICE(0x2040, 0x5590),
528 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
529 { USB_DEVICE(0x187f, 0x0202),
530 .driver_info = SMS1XXX_BOARD_SIANO_NICE },
531 { USB_DEVICE(0x187f, 0x0301),
532 .driver_info = SMS1XXX_BOARD_SIANO_VENICE },
533 { USB_DEVICE(0x2040, 0xb900),
534 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
535 { USB_DEVICE(0x2040, 0xb910),
536 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
537 { USB_DEVICE(0x2040, 0xb980),
538 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
539 { USB_DEVICE(0x2040, 0xb990),
540 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
541 { USB_DEVICE(0x2040, 0xc000),
542 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
543 { USB_DEVICE(0x2040, 0xc010),
544 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
545 { USB_DEVICE(0x2040, 0xc080),
546 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
547 { USB_DEVICE(0x2040, 0xc090),
548 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
549 { } /* Terminating entry */
552 MODULE_DEVICE_TABLE(usb, smsusb_id_table);
554 static struct usb_driver smsusb_driver = {
555 .name = "smsusb",
556 .probe = smsusb_probe,
557 .disconnect = smsusb_disconnect,
558 .id_table = smsusb_id_table,
560 .suspend = smsusb_suspend,
561 .resume = smsusb_resume,
564 static int __init smsusb_module_init(void)
566 int rc = usb_register(&smsusb_driver);
567 if (rc)
568 sms_err("usb_register failed. Error number %d", rc);
570 sms_debug("");
572 return rc;
575 static void __exit smsusb_module_exit(void)
577 /* Regular USB Cleanup */
578 usb_deregister(&smsusb_driver);
579 sms_info("end");
582 module_init(smsusb_module_init);
583 module_exit(smsusb_module_exit);
585 MODULE_DESCRIPTION("Driver for the Siano SMS1xxx USB dongle");
586 MODULE_AUTHOR("Siano Mobile Silicon, INC. (uris@siano-ms.com)");
587 MODULE_LICENSE("GPL");