2 * Driver for the Siano SMS1xxx USB dongle
4 * author: Anatoly Greenblat
6 * Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation;
12 * Software distributed under the License is distributed on an "AS IS"
13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
15 * See the 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, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/usb.h>
25 #include <linux/firmware.h>
27 #include "smscoreapi.h"
28 #include "sms-cards.h"
31 module_param_named(debug, sms_dbg, int, 0644);
32 MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
34 #define USB1_BUFFER_SIZE 0x1000
35 #define USB2_BUFFER_SIZE 0x4000
37 #define MAX_BUFFERS 50
40 struct smsusb_device_t;
43 struct smscore_buffer_t *cb;
44 struct smsusb_device_t *dev;
49 struct smsusb_device_t {
50 struct usb_device *udev;
51 struct smscore_device_t *coredev;
53 struct smsusb_urb_t surbs[MAX_URBS];
55 int response_alignment;
59 static int smsusb_submit_urb(struct smsusb_device_t *dev,
60 struct smsusb_urb_t *surb);
62 static void smsusb_onresponse(struct urb *urb)
64 struct smsusb_urb_t *surb = (struct smsusb_urb_t *) urb->context;
65 struct smsusb_device_t *dev = surb->dev;
67 if (urb->status < 0) {
68 sms_err("error, urb status %d, %d bytes",
69 urb->status, urb->actual_length);
73 if (urb->actual_length > 0) {
74 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) surb->cb->p;
76 if (urb->actual_length >= phdr->msgLength) {
77 surb->cb->size = phdr->msgLength;
79 if (dev->response_alignment &&
80 (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG)) {
83 dev->response_alignment +
84 ((phdr->msgFlags >> 8) & 3);
87 if (((int) phdr->msgLength +
88 surb->cb->offset) > urb->actual_length) {
89 sms_err("invalid response "
90 "msglen %d offset %d "
95 goto exit_and_resubmit;
98 /* move buffer pointer and
99 * copy header to its new location */
100 memcpy((char *) phdr + surb->cb->offset,
101 phdr, sizeof(struct SmsMsgHdr_ST));
103 surb->cb->offset = 0;
105 smscore_onresponse(dev->coredev, surb->cb);
108 sms_err("invalid response "
109 "msglen %d actual %d",
110 phdr->msgLength, urb->actual_length);
115 smsusb_submit_urb(dev, surb);
118 static int smsusb_submit_urb(struct smsusb_device_t *dev,
119 struct smsusb_urb_t *surb)
122 surb->cb = smscore_getbuffer(dev->coredev);
124 sms_err("smscore_getbuffer(...) returned NULL");
132 usb_rcvbulkpipe(dev->udev, 0x81),
138 surb->urb.transfer_dma = surb->cb->phys;
139 surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
141 return usb_submit_urb(&surb->urb, GFP_ATOMIC);
144 static void smsusb_stop_streaming(struct smsusb_device_t *dev)
148 for (i = 0; i < MAX_URBS; i++) {
149 usb_kill_urb(&dev->surbs[i].urb);
151 if (dev->surbs[i].cb) {
152 smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
153 dev->surbs[i].cb = NULL;
158 static int smsusb_start_streaming(struct smsusb_device_t *dev)
162 for (i = 0; i < MAX_URBS; i++) {
163 rc = smsusb_submit_urb(dev, &dev->surbs[i]);
165 sms_err("smsusb_submit_urb(...) failed");
166 smsusb_stop_streaming(dev);
174 static int smsusb_sendrequest(void *context, void *buffer, size_t size)
176 struct smsusb_device_t *dev = (struct smsusb_device_t *) context;
179 return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
180 buffer, size, &dummy, 1000);
183 static char *smsusb1_fw_lkup[] = {
184 "dvbt_stellar_usb.inp",
185 "dvbh_stellar_usb.inp",
186 "tdmb_stellar_usb.inp",
188 "dvbt_bda_stellar_usb.inp",
191 static inline char *sms_get_fw_name(int mode, int board_id)
193 char **fw = sms_get_board(board_id)->fw;
194 return (fw && fw[mode]) ? fw[mode] : smsusb1_fw_lkup[mode];
197 static int smsusb1_load_firmware(struct usb_device *udev, int id, int board_id)
199 const struct firmware *fw;
204 if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) {
205 sms_err("invalid firmware id specified %d", id);
209 fw_filename = sms_get_fw_name(id, board_id);
211 rc = request_firmware(&fw, fw_filename, &udev->dev);
213 sms_warn("failed to open \"%s\" mode %d, "
214 "trying again with default firmware", fw_filename, id);
216 fw_filename = smsusb1_fw_lkup[id];
217 rc = request_firmware(&fw, fw_filename, &udev->dev);
219 sms_warn("failed to open \"%s\" mode %d",
226 fw_buffer = kmalloc(fw->size, GFP_KERNEL);
228 memcpy(fw_buffer, fw->data, fw->size);
230 rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2),
231 fw_buffer, fw->size, &dummy, 1000);
233 sms_info("sent %zd(%d) bytes, rc %d", fw->size, dummy, rc);
237 sms_err("failed to allocate firmware buffer");
240 sms_info("read FW %s, size=%zd", fw_filename, fw->size);
242 release_firmware(fw);
247 static void smsusb1_detectmode(void *context, int *mode)
249 char *product_string =
250 ((struct smsusb_device_t *) context)->udev->product;
252 *mode = DEVICE_MODE_NONE;
254 if (!product_string) {
255 product_string = "none";
256 sms_err("product string not found");
257 } else if (strstr(product_string, "DVBH"))
259 else if (strstr(product_string, "BDA"))
261 else if (strstr(product_string, "DVBT"))
263 else if (strstr(product_string, "TDMB"))
266 sms_info("%d \"%s\"", *mode, product_string);
269 static int smsusb1_setmode(void *context, int mode)
271 struct SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK,
272 sizeof(struct SmsMsgHdr_ST), 0 };
274 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
275 sms_err("invalid firmware id specified %d", mode);
279 return smsusb_sendrequest(context, &Msg, sizeof(Msg));
282 static void smsusb_term_device(struct usb_interface *intf)
284 struct smsusb_device_t *dev =
285 (struct smsusb_device_t *) usb_get_intfdata(intf);
288 smsusb_stop_streaming(dev);
290 /* unregister from smscore */
292 smscore_unregister_device(dev->coredev);
296 sms_info("device %p destroyed", dev);
299 usb_set_intfdata(intf, NULL);
302 static int smsusb_init_device(struct usb_interface *intf, int board_id)
304 struct smsdevice_params_t params;
305 struct smsusb_device_t *dev;
308 /* create device object */
309 dev = kzalloc(sizeof(struct smsusb_device_t), GFP_KERNEL);
311 sms_err("kzalloc(sizeof(struct smsusb_device_t) failed");
315 memset(¶ms, 0, sizeof(params));
316 usb_set_intfdata(intf, dev);
317 dev->udev = interface_to_usbdev(intf);
319 params.device_type = sms_get_board(board_id)->type;
321 switch (params.device_type) {
323 dev->buffer_size = USB1_BUFFER_SIZE;
325 params.setmode_handler = smsusb1_setmode;
326 params.detectmode_handler = smsusb1_detectmode;
329 sms_err("Unspecified sms device type!");
334 dev->buffer_size = USB2_BUFFER_SIZE;
335 dev->response_alignment =
336 dev->udev->ep_in[1]->desc.wMaxPacketSize -
337 sizeof(struct SmsMsgHdr_ST);
339 params.flags |= SMS_DEVICE_FAMILY2;
343 params.device = &dev->udev->dev;
344 params.buffer_size = dev->buffer_size;
345 params.num_buffers = MAX_BUFFERS;
346 params.sendrequest_handler = smsusb_sendrequest;
347 params.context = dev;
348 snprintf(params.devpath, sizeof(params.devpath),
349 "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath);
351 /* register in smscore */
352 rc = smscore_register_device(¶ms, &dev->coredev);
354 sms_err("smscore_register_device(...) failed, rc %d", rc);
355 smsusb_term_device(intf);
359 smscore_set_board_id(dev->coredev, board_id);
361 /* initialize urbs */
362 for (i = 0; i < MAX_URBS; i++) {
363 dev->surbs[i].dev = dev;
364 usb_init_urb(&dev->surbs[i].urb);
367 sms_info("smsusb_start_streaming(...).");
368 rc = smsusb_start_streaming(dev);
370 sms_err("smsusb_start_streaming(...) failed");
371 smsusb_term_device(intf);
375 rc = smscore_start_device(dev->coredev);
377 sms_err("smscore_start_device(...) failed");
378 smsusb_term_device(intf);
382 sms_info("device %p created", dev);
387 static int smsusb_probe(struct usb_interface *intf,
388 const struct usb_device_id *id)
390 struct usb_device *udev = interface_to_usbdev(intf);
394 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
395 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
397 if (intf->num_altsetting > 0) {
398 rc = usb_set_interface(
399 udev, intf->cur_altsetting->desc.bInterfaceNumber, 0);
401 sms_err("usb_set_interface failed, rc %d", rc);
406 sms_info("smsusb_probe %d",
407 intf->cur_altsetting->desc.bInterfaceNumber);
408 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
409 sms_info("endpoint %d %02x %02x %d", i,
410 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
411 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
412 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
414 if ((udev->actconfig->desc.bNumInterfaces == 2) &&
415 (intf->cur_altsetting->desc.bInterfaceNumber == 0)) {
416 sms_err("rom interface 0 is not used");
420 if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
421 snprintf(devpath, sizeof(devpath), "usb\\%d-%s",
422 udev->bus->busnum, udev->devpath);
423 sms_info("stellar device was found.");
424 return smsusb1_load_firmware(
425 udev, smscore_registry_getmode(devpath),
429 rc = smsusb_init_device(intf, id->driver_info);
430 sms_info("rc %d", rc);
431 sms_board_load_modules(id->driver_info);
435 static void smsusb_disconnect(struct usb_interface *intf)
437 smsusb_term_device(intf);
440 static int smsusb_suspend(struct usb_interface *intf, pm_message_t msg)
442 struct smsusb_device_t *dev =
443 (struct smsusb_device_t *)usb_get_intfdata(intf);
444 printk(KERN_INFO "%s: Entering status %d.\n", __func__, msg.event);
445 smsusb_stop_streaming(dev);
449 static int smsusb_resume(struct usb_interface *intf)
452 struct smsusb_device_t *dev =
453 (struct smsusb_device_t *)usb_get_intfdata(intf);
454 struct usb_device *udev = interface_to_usbdev(intf);
456 printk(KERN_INFO "%s: Entering.\n", __func__);
457 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
458 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
460 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
461 printk(KERN_INFO "endpoint %d %02x %02x %d\n", i,
462 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
463 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
464 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
466 if (intf->num_altsetting > 0) {
467 rc = usb_set_interface(udev,
468 intf->cur_altsetting->desc.
469 bInterfaceNumber, 0);
471 printk(KERN_INFO "%s usb_set_interface failed, "
472 "rc %d\n", __func__, rc);
477 smsusb_start_streaming(dev);
481 struct usb_device_id smsusb_id_table[] = {
482 #ifdef CONFIG_DVB_SIANO_SMS1XXX_SMS_IDS
483 { USB_DEVICE(0x187f, 0x0010),
484 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
485 { USB_DEVICE(0x187f, 0x0100),
486 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
487 { USB_DEVICE(0x187f, 0x0200),
488 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_A },
489 { USB_DEVICE(0x187f, 0x0201),
490 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_B },
491 { USB_DEVICE(0x187f, 0x0300),
492 .driver_info = SMS1XXX_BOARD_SIANO_VEGA },
494 { USB_DEVICE(0x2040, 0x1700),
495 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_CATAMOUNT },
496 { USB_DEVICE(0x2040, 0x1800),
497 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_A },
498 { USB_DEVICE(0x2040, 0x1801),
499 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_B },
500 { USB_DEVICE(0x2040, 0x2000),
501 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
502 { USB_DEVICE(0x2040, 0x2009),
503 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD_R2 },
504 { USB_DEVICE(0x2040, 0x200a),
505 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
506 { USB_DEVICE(0x2040, 0x2010),
507 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
508 { USB_DEVICE(0x2040, 0x2011),
509 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
510 { USB_DEVICE(0x2040, 0x2019),
511 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
512 { USB_DEVICE(0x2040, 0x5500),
513 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
514 { USB_DEVICE(0x2040, 0x5510),
515 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
516 { USB_DEVICE(0x2040, 0x5520),
517 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
518 { USB_DEVICE(0x2040, 0x5530),
519 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
520 { USB_DEVICE(0x2040, 0x5580),
521 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
522 { USB_DEVICE(0x2040, 0x5590),
523 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
524 { } /* Terminating entry */
526 MODULE_DEVICE_TABLE(usb, smsusb_id_table);
528 static struct usb_driver smsusb_driver = {
530 .probe = smsusb_probe,
531 .disconnect = smsusb_disconnect,
532 .id_table = smsusb_id_table,
534 .suspend = smsusb_suspend,
535 .resume = smsusb_resume,
538 int smsusb_module_init(void)
540 int rc = usb_register(&smsusb_driver);
542 sms_err("usb_register failed. Error number %d", rc);
549 void smsusb_module_exit(void)
552 /* Regular USB Cleanup */
553 usb_deregister(&smsusb_driver);
556 module_init(smsusb_module_init);
557 module_exit(smsusb_module_exit);
559 MODULE_DESCRIPTION("Driver for the Siano SMS1XXX USB dongle");
560 MODULE_AUTHOR("Siano Mobile Silicon, INC. (uris@siano-ms.com)");
561 MODULE_LICENSE("GPL");