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"
30 #define USB1_BUFFER_SIZE 0x1000
31 #define USB2_BUFFER_SIZE 0x4000
33 #define MAX_BUFFERS 50
36 struct smsusb_device_t;
39 struct smscore_buffer_t *cb;
40 struct smsusb_device_t *dev;
45 struct smsusb_device_t {
46 struct usb_device *udev;
47 struct smscore_device_t *coredev;
49 struct smsusb_urb_t surbs[MAX_URBS];
51 int response_alignment;
55 static int smsusb_submit_urb(struct smsusb_device_t *dev,
56 struct smsusb_urb_t *surb);
58 static void smsusb_onresponse(struct urb *urb)
60 struct smsusb_urb_t *surb = (struct smsusb_urb_t *) urb->context;
61 struct smsusb_device_t *dev = surb->dev;
63 if (urb->status < 0) {
64 sms_err("error, urb status %d, %d bytes",
65 urb->status, urb->actual_length);
69 if (urb->actual_length > 0) {
70 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) surb->cb->p;
72 if (urb->actual_length >= phdr->msgLength) {
73 surb->cb->size = phdr->msgLength;
75 if (dev->response_alignment &&
76 (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG)) {
79 dev->response_alignment +
80 ((phdr->msgFlags >> 8) & 3);
83 if (((int) phdr->msgLength +
84 surb->cb->offset) > urb->actual_length) {
85 sms_err("invalid response "
86 "msglen %d offset %d "
91 goto exit_and_resubmit;
94 /* move buffer pointer and
95 * copy header to its new location */
96 memcpy((char *) phdr + surb->cb->offset,
97 phdr, sizeof(struct SmsMsgHdr_ST));
101 smscore_onresponse(dev->coredev, surb->cb);
104 sms_err("invalid response "
105 "msglen %d actual %d",
106 phdr->msgLength, urb->actual_length);
111 smsusb_submit_urb(dev, surb);
114 static int smsusb_submit_urb(struct smsusb_device_t *dev,
115 struct smsusb_urb_t *surb)
118 surb->cb = smscore_getbuffer(dev->coredev);
120 sms_err("smscore_getbuffer(...) returned NULL");
128 usb_rcvbulkpipe(dev->udev, 0x81),
134 surb->urb.transfer_dma = surb->cb->phys;
135 surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
137 return usb_submit_urb(&surb->urb, GFP_ATOMIC);
140 static void smsusb_stop_streaming(struct smsusb_device_t *dev)
144 for (i = 0; i < MAX_URBS; i++) {
145 usb_kill_urb(&dev->surbs[i].urb);
147 if (dev->surbs[i].cb) {
148 smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
149 dev->surbs[i].cb = NULL;
154 static int smsusb_start_streaming(struct smsusb_device_t *dev)
158 for (i = 0; i < MAX_URBS; i++) {
159 rc = smsusb_submit_urb(dev, &dev->surbs[i]);
161 sms_err("smsusb_submit_urb(...) failed");
162 smsusb_stop_streaming(dev);
170 static int smsusb_sendrequest(void *context, void *buffer, size_t size)
172 struct smsusb_device_t *dev = (struct smsusb_device_t *) context;
175 return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
176 buffer, size, &dummy, 1000);
179 static char *smsusb1_fw_lkup[] = {
180 "dvbt_stellar_usb.inp",
181 "dvbh_stellar_usb.inp",
182 "tdmb_stellar_usb.inp",
184 "dvbt_bda_stellar_usb.inp",
187 static inline char *sms_get_fw_name(int mode, int board_id)
189 char **fw = sms_get_board(board_id)->fw;
190 return (fw && fw[mode]) ? fw[mode] : smsusb1_fw_lkup[mode];
193 static int smsusb1_load_firmware(struct usb_device *udev, int id, int board_id)
195 const struct firmware *fw;
200 if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) {
201 sms_err("invalid firmware id specified %d", id);
205 fw_filename = sms_get_fw_name(id, board_id);
207 rc = request_firmware(&fw, fw_filename, &udev->dev);
209 sms_warn("failed to open \"%s\" mode %d, "
210 "trying again with default firmware", fw_filename, id);
212 fw_filename = smsusb1_fw_lkup[id];
213 rc = request_firmware(&fw, fw_filename, &udev->dev);
215 sms_warn("failed to open \"%s\" mode %d",
222 fw_buffer = kmalloc(fw->size, GFP_KERNEL);
224 memcpy(fw_buffer, fw->data, fw->size);
226 rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2),
227 fw_buffer, fw->size, &dummy, 1000);
229 sms_info("sent %zd(%d) bytes, rc %d", fw->size, dummy, rc);
233 sms_err("failed to allocate firmware buffer");
236 sms_info("read FW %s, size=%zd", fw_filename, fw->size);
238 release_firmware(fw);
243 static void smsusb1_detectmode(void *context, int *mode)
245 char *product_string =
246 ((struct smsusb_device_t *) context)->udev->product;
248 *mode = DEVICE_MODE_NONE;
250 if (!product_string) {
251 product_string = "none";
252 sms_err("product string not found");
253 } else if (strstr(product_string, "DVBH"))
255 else if (strstr(product_string, "BDA"))
257 else if (strstr(product_string, "DVBT"))
259 else if (strstr(product_string, "TDMB"))
262 sms_info("%d \"%s\"", *mode, product_string);
265 static int smsusb1_setmode(void *context, int mode)
267 struct SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK,
268 sizeof(struct SmsMsgHdr_ST), 0 };
270 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
271 sms_err("invalid firmware id specified %d", mode);
275 return smsusb_sendrequest(context, &Msg, sizeof(Msg));
278 static void smsusb_term_device(struct usb_interface *intf)
280 struct smsusb_device_t *dev =
281 (struct smsusb_device_t *) usb_get_intfdata(intf);
284 smsusb_stop_streaming(dev);
286 /* unregister from smscore */
288 smscore_unregister_device(dev->coredev);
292 sms_info("device %p destroyed", dev);
295 usb_set_intfdata(intf, NULL);
298 static int smsusb_init_device(struct usb_interface *intf, int board_id)
300 struct smsdevice_params_t params;
301 struct smsusb_device_t *dev;
304 /* create device object */
305 dev = kzalloc(sizeof(struct smsusb_device_t), GFP_KERNEL);
307 sms_err("kzalloc(sizeof(struct smsusb_device_t) failed");
311 memset(¶ms, 0, sizeof(params));
312 usb_set_intfdata(intf, dev);
313 dev->udev = interface_to_usbdev(intf);
315 params.device_type = sms_get_board(board_id)->type;
317 switch (params.device_type) {
319 dev->buffer_size = USB1_BUFFER_SIZE;
321 params.setmode_handler = smsusb1_setmode;
322 params.detectmode_handler = smsusb1_detectmode;
325 sms_err("Unspecified sms device type!");
330 dev->buffer_size = USB2_BUFFER_SIZE;
331 dev->response_alignment =
332 dev->udev->ep_in[1]->desc.wMaxPacketSize -
333 sizeof(struct SmsMsgHdr_ST);
335 params.flags |= SMS_DEVICE_FAMILY2;
339 params.device = &dev->udev->dev;
340 params.buffer_size = dev->buffer_size;
341 params.num_buffers = MAX_BUFFERS;
342 params.sendrequest_handler = smsusb_sendrequest;
343 params.context = dev;
344 snprintf(params.devpath, sizeof(params.devpath),
345 "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath);
347 /* register in smscore */
348 rc = smscore_register_device(¶ms, &dev->coredev);
350 sms_err("smscore_register_device(...) failed, rc %d", rc);
351 smsusb_term_device(intf);
355 smscore_set_board_id(dev->coredev, board_id);
357 /* initialize urbs */
358 for (i = 0; i < MAX_URBS; i++) {
359 dev->surbs[i].dev = dev;
360 usb_init_urb(&dev->surbs[i].urb);
363 sms_info("smsusb_start_streaming(...).");
364 rc = smsusb_start_streaming(dev);
366 sms_err("smsusb_start_streaming(...) failed");
367 smsusb_term_device(intf);
371 rc = smscore_start_device(dev->coredev);
373 sms_err("smscore_start_device(...) failed");
374 smsusb_term_device(intf);
378 sms_info("device %p created", dev);
383 static int smsusb_probe(struct usb_interface *intf,
384 const struct usb_device_id *id)
386 struct usb_device *udev = interface_to_usbdev(intf);
390 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
391 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
393 if (intf->num_altsetting > 0) {
394 rc = usb_set_interface(
395 udev, intf->cur_altsetting->desc.bInterfaceNumber, 0);
397 sms_err("usb_set_interface failed, rc %d", rc);
402 sms_info("smsusb_probe %d",
403 intf->cur_altsetting->desc.bInterfaceNumber);
404 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
405 sms_info("endpoint %d %02x %02x %d", i,
406 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
407 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
408 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
410 if ((udev->actconfig->desc.bNumInterfaces == 2) &&
411 (intf->cur_altsetting->desc.bInterfaceNumber == 0)) {
412 sms_err("rom interface 0 is not used");
416 if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
417 snprintf(devpath, sizeof(devpath), "usb\\%d-%s",
418 udev->bus->busnum, udev->devpath);
419 sms_info("stellar device was found.");
420 return smsusb1_load_firmware(
421 udev, smscore_registry_getmode(devpath),
425 rc = smsusb_init_device(intf, id->driver_info);
426 sms_info("rc %d", rc);
430 static void smsusb_disconnect(struct usb_interface *intf)
432 smsusb_term_device(intf);
435 static int smsusb_suspend(struct usb_interface *intf, pm_message_t msg)
437 struct smsusb_device_t *dev =
438 (struct smsusb_device_t *)usb_get_intfdata(intf);
439 printk(KERN_INFO "%s Entering status %d.\n", __func__, msg.event);
440 smsusb_stop_streaming(dev);
444 static int smsusb_resume(struct usb_interface *intf)
447 struct smsusb_device_t *dev =
448 (struct smsusb_device_t *)usb_get_intfdata(intf);
449 struct usb_device *udev = interface_to_usbdev(intf);
451 printk(KERN_INFO "%s Entering.\n", __func__);
452 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
453 usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
455 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++)
456 printk(KERN_INFO "endpoint %d %02x %02x %d\n", i,
457 intf->cur_altsetting->endpoint[i].desc.bEndpointAddress,
458 intf->cur_altsetting->endpoint[i].desc.bmAttributes,
459 intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
461 if (intf->num_altsetting > 0) {
462 rc = usb_set_interface(udev,
463 intf->cur_altsetting->desc.
464 bInterfaceNumber, 0);
467 "%s usb_set_interface failed, rc %d\n",
473 smsusb_start_streaming(dev);
477 struct usb_device_id smsusb_id_table[] = {
478 #ifdef CONFIG_DVB_SIANO_SMS1XXX_SMS_IDS
479 { USB_DEVICE(0x187f, 0x0010),
480 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
481 { USB_DEVICE(0x187f, 0x0100),
482 .driver_info = SMS1XXX_BOARD_SIANO_STELLAR },
483 { USB_DEVICE(0x187f, 0x0200),
484 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_A },
485 { USB_DEVICE(0x187f, 0x0201),
486 .driver_info = SMS1XXX_BOARD_SIANO_NOVA_B },
487 { USB_DEVICE(0x187f, 0x0300),
488 .driver_info = SMS1XXX_BOARD_SIANO_VEGA },
490 { USB_DEVICE(0x2040, 0x1700),
491 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_CATAMOUNT },
492 { USB_DEVICE(0x2040, 0x1800),
493 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_A },
494 { USB_DEVICE(0x2040, 0x1801),
495 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_OKEMO_B },
496 { USB_DEVICE(0x2040, 0x2000),
497 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
498 { USB_DEVICE(0x2040, 0x2009),
499 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD_R2 },
500 { USB_DEVICE(0x2040, 0x200a),
501 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
502 { USB_DEVICE(0x2040, 0x2010),
503 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
504 { USB_DEVICE(0x2040, 0x2011),
505 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
506 { USB_DEVICE(0x2040, 0x2019),
507 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD },
508 { USB_DEVICE(0x2040, 0x5500),
509 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
510 { USB_DEVICE(0x2040, 0x5510),
511 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
512 { USB_DEVICE(0x2040, 0x5520),
513 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
514 { USB_DEVICE(0x2040, 0x5530),
515 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
516 { USB_DEVICE(0x2040, 0x5580),
517 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
518 { USB_DEVICE(0x2040, 0x5590),
519 .driver_info = SMS1XXX_BOARD_HAUPPAUGE_WINDHAM },
520 { } /* Terminating entry */
522 MODULE_DEVICE_TABLE(usb, smsusb_id_table);
524 static struct usb_driver smsusb_driver = {
526 .probe = smsusb_probe,
527 .disconnect = smsusb_disconnect,
528 .id_table = smsusb_id_table,
530 .suspend = smsusb_suspend,
531 .resume = smsusb_resume,
534 int smsusb_register(void)
536 int rc = usb_register(&smsusb_driver);
538 sms_err("usb_register failed. Error number %d", rc);
545 void smsusb_unregister(void)
548 /* Regular USB Cleanup */
549 usb_deregister(&smsusb_driver);