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>
27 #include <linux/module.h>
29 #include "smscoreapi.h"
30 #include "sms-cards.h"
31 #include "smsendian.h"
34 module_param_named(debug
, sms_dbg
, int, 0644);
35 MODULE_PARM_DESC(debug
, "set debug level (info=1, adv=2 (or-able))");
37 #define USB1_BUFFER_SIZE 0x1000
38 #define USB2_BUFFER_SIZE 0x4000
40 #define MAX_BUFFERS 50
43 struct smsusb_device_t
;
46 struct smscore_buffer_t
*cb
;
47 struct smsusb_device_t
*dev
;
52 struct smsusb_device_t
{
53 struct usb_device
*udev
;
54 struct smscore_device_t
*coredev
;
56 struct smsusb_urb_t surbs
[MAX_URBS
];
58 int response_alignment
;
62 static int smsusb_submit_urb(struct smsusb_device_t
*dev
,
63 struct smsusb_urb_t
*surb
);
65 static void smsusb_onresponse(struct urb
*urb
)
67 struct smsusb_urb_t
*surb
= (struct smsusb_urb_t
*) urb
->context
;
68 struct smsusb_device_t
*dev
= surb
->dev
;
70 if (urb
->status
== -ESHUTDOWN
) {
71 sms_err("error, urb status %d (-ESHUTDOWN), %d bytes",
72 urb
->status
, urb
->actual_length
);
76 if ((urb
->actual_length
> 0) && (urb
->status
== 0)) {
77 struct SmsMsgHdr_ST
*phdr
= (struct SmsMsgHdr_ST
*)surb
->cb
->p
;
79 smsendian_handle_message_header(phdr
);
80 if (urb
->actual_length
>= phdr
->msgLength
) {
81 surb
->cb
->size
= phdr
->msgLength
;
83 if (dev
->response_alignment
&&
84 (phdr
->msgFlags
& MSG_HDR_FLAG_SPLIT_MSG
)) {
87 dev
->response_alignment
+
88 ((phdr
->msgFlags
>> 8) & 3);
91 if (((int) phdr
->msgLength
+
92 surb
->cb
->offset
) > urb
->actual_length
) {
93 sms_err("invalid response "
94 "msglen %d offset %d "
99 goto exit_and_resubmit
;
102 /* move buffer pointer and
103 * copy header to its new location */
104 memcpy((char *) phdr
+ surb
->cb
->offset
,
105 phdr
, sizeof(struct SmsMsgHdr_ST
));
107 surb
->cb
->offset
= 0;
109 smscore_onresponse(dev
->coredev
, surb
->cb
);
112 sms_err("invalid response "
113 "msglen %d actual %d",
114 phdr
->msgLength
, urb
->actual_length
);
117 sms_err("error, urb status %d, %d bytes",
118 urb
->status
, urb
->actual_length
);
122 smsusb_submit_urb(dev
, surb
);
125 static int smsusb_submit_urb(struct smsusb_device_t
*dev
,
126 struct smsusb_urb_t
*surb
)
129 surb
->cb
= smscore_getbuffer(dev
->coredev
);
131 sms_err("smscore_getbuffer(...) returned NULL");
139 usb_rcvbulkpipe(dev
->udev
, 0x81),
145 surb
->urb
.transfer_dma
= surb
->cb
->phys
;
146 surb
->urb
.transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
148 return usb_submit_urb(&surb
->urb
, GFP_ATOMIC
);
151 static void smsusb_stop_streaming(struct smsusb_device_t
*dev
)
155 for (i
= 0; i
< MAX_URBS
; i
++) {
156 usb_kill_urb(&dev
->surbs
[i
].urb
);
158 if (dev
->surbs
[i
].cb
) {
159 smscore_putbuffer(dev
->coredev
, dev
->surbs
[i
].cb
);
160 dev
->surbs
[i
].cb
= NULL
;
165 static int smsusb_start_streaming(struct smsusb_device_t
*dev
)
169 for (i
= 0; i
< MAX_URBS
; i
++) {
170 rc
= smsusb_submit_urb(dev
, &dev
->surbs
[i
]);
172 sms_err("smsusb_submit_urb(...) failed");
173 smsusb_stop_streaming(dev
);
181 static int smsusb_sendrequest(void *context
, void *buffer
, size_t size
)
183 struct smsusb_device_t
*dev
= (struct smsusb_device_t
*) context
;
186 smsendian_handle_message_header((struct SmsMsgHdr_ST
*)buffer
);
187 return usb_bulk_msg(dev
->udev
, usb_sndbulkpipe(dev
->udev
, 2),
188 buffer
, size
, &dummy
, 1000);
191 static char *smsusb1_fw_lkup
[] = {
192 "dvbt_stellar_usb.inp",
193 "dvbh_stellar_usb.inp",
194 "tdmb_stellar_usb.inp",
196 "dvbt_bda_stellar_usb.inp",
199 static inline char *sms_get_fw_name(int mode
, int board_id
)
201 char **fw
= sms_get_board(board_id
)->fw
;
202 return (fw
&& fw
[mode
]) ? fw
[mode
] : smsusb1_fw_lkup
[mode
];
205 static int smsusb1_load_firmware(struct usb_device
*udev
, int id
, int board_id
)
207 const struct firmware
*fw
;
212 if (id
< DEVICE_MODE_DVBT
|| id
> DEVICE_MODE_DVBT_BDA
) {
213 sms_err("invalid firmware id specified %d", id
);
217 fw_filename
= sms_get_fw_name(id
, board_id
);
219 rc
= request_firmware(&fw
, fw_filename
, &udev
->dev
);
221 sms_warn("failed to open \"%s\" mode %d, "
222 "trying again with default firmware", fw_filename
, id
);
224 fw_filename
= smsusb1_fw_lkup
[id
];
225 rc
= request_firmware(&fw
, fw_filename
, &udev
->dev
);
227 sms_warn("failed to open \"%s\" mode %d",
234 fw_buffer
= kmalloc(fw
->size
, GFP_KERNEL
);
236 memcpy(fw_buffer
, fw
->data
, fw
->size
);
238 rc
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, 2),
239 fw_buffer
, fw
->size
, &dummy
, 1000);
241 sms_info("sent %zd(%d) bytes, rc %d", fw
->size
, dummy
, rc
);
245 sms_err("failed to allocate firmware buffer");
248 sms_info("read FW %s, size=%zd", fw_filename
, fw
->size
);
250 release_firmware(fw
);
255 static void smsusb1_detectmode(void *context
, int *mode
)
257 char *product_string
=
258 ((struct smsusb_device_t
*) context
)->udev
->product
;
260 *mode
= DEVICE_MODE_NONE
;
262 if (!product_string
) {
263 product_string
= "none";
264 sms_err("product string not found");
265 } else if (strstr(product_string
, "DVBH"))
267 else if (strstr(product_string
, "BDA"))
269 else if (strstr(product_string
, "DVBT"))
271 else if (strstr(product_string
, "TDMB"))
274 sms_info("%d \"%s\"", *mode
, product_string
);
277 static int smsusb1_setmode(void *context
, int mode
)
279 struct SmsMsgHdr_ST Msg
= { MSG_SW_RELOAD_REQ
, 0, HIF_TASK
,
280 sizeof(struct SmsMsgHdr_ST
), 0 };
282 if (mode
< DEVICE_MODE_DVBT
|| mode
> DEVICE_MODE_DVBT_BDA
) {
283 sms_err("invalid firmware id specified %d", mode
);
287 return smsusb_sendrequest(context
, &Msg
, sizeof(Msg
));
290 static void smsusb_term_device(struct usb_interface
*intf
)
292 struct smsusb_device_t
*dev
= usb_get_intfdata(intf
);
295 smsusb_stop_streaming(dev
);
297 /* unregister from smscore */
299 smscore_unregister_device(dev
->coredev
);
301 sms_info("device %p destroyed", dev
);
305 usb_set_intfdata(intf
, NULL
);
308 static int smsusb_init_device(struct usb_interface
*intf
, int board_id
)
310 struct smsdevice_params_t params
;
311 struct smsusb_device_t
*dev
;
314 /* create device object */
315 dev
= kzalloc(sizeof(struct smsusb_device_t
), GFP_KERNEL
);
317 sms_err("kzalloc(sizeof(struct smsusb_device_t) failed");
321 memset(¶ms
, 0, sizeof(params
));
322 usb_set_intfdata(intf
, dev
);
323 dev
->udev
= interface_to_usbdev(intf
);
325 params
.device_type
= sms_get_board(board_id
)->type
;
327 switch (params
.device_type
) {
329 dev
->buffer_size
= USB1_BUFFER_SIZE
;
331 params
.setmode_handler
= smsusb1_setmode
;
332 params
.detectmode_handler
= smsusb1_detectmode
;
335 sms_err("Unspecified sms device type!");
340 dev
->buffer_size
= USB2_BUFFER_SIZE
;
341 dev
->response_alignment
=
342 le16_to_cpu(dev
->udev
->ep_in
[1]->desc
.wMaxPacketSize
) -
343 sizeof(struct SmsMsgHdr_ST
);
345 params
.flags
|= SMS_DEVICE_FAMILY2
;
349 params
.device
= &dev
->udev
->dev
;
350 params
.buffer_size
= dev
->buffer_size
;
351 params
.num_buffers
= MAX_BUFFERS
;
352 params
.sendrequest_handler
= smsusb_sendrequest
;
353 params
.context
= dev
;
354 usb_make_path(dev
->udev
, params
.devpath
, sizeof(params
.devpath
));
356 /* register in smscore */
357 rc
= smscore_register_device(¶ms
, &dev
->coredev
);
359 sms_err("smscore_register_device(...) failed, rc %d", rc
);
360 smsusb_term_device(intf
);
364 smscore_set_board_id(dev
->coredev
, board_id
);
366 /* initialize urbs */
367 for (i
= 0; i
< MAX_URBS
; i
++) {
368 dev
->surbs
[i
].dev
= dev
;
369 usb_init_urb(&dev
->surbs
[i
].urb
);
372 sms_info("smsusb_start_streaming(...).");
373 rc
= smsusb_start_streaming(dev
);
375 sms_err("smsusb_start_streaming(...) failed");
376 smsusb_term_device(intf
);
380 rc
= smscore_start_device(dev
->coredev
);
382 sms_err("smscore_start_device(...) failed");
383 smsusb_term_device(intf
);
387 sms_info("device %p created", dev
);
392 static int __devinit
smsusb_probe(struct usb_interface
*intf
,
393 const struct usb_device_id
*id
)
395 struct usb_device
*udev
= interface_to_usbdev(intf
);
399 rc
= usb_clear_halt(udev
, usb_rcvbulkpipe(udev
, 0x81));
400 rc
= usb_clear_halt(udev
, usb_rcvbulkpipe(udev
, 0x02));
402 if (intf
->num_altsetting
> 0) {
403 rc
= usb_set_interface(
404 udev
, intf
->cur_altsetting
->desc
.bInterfaceNumber
, 0);
406 sms_err("usb_set_interface failed, rc %d", rc
);
411 sms_info("smsusb_probe %d",
412 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
413 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++)
414 sms_info("endpoint %d %02x %02x %d", i
,
415 intf
->cur_altsetting
->endpoint
[i
].desc
.bEndpointAddress
,
416 intf
->cur_altsetting
->endpoint
[i
].desc
.bmAttributes
,
417 intf
->cur_altsetting
->endpoint
[i
].desc
.wMaxPacketSize
);
419 if ((udev
->actconfig
->desc
.bNumInterfaces
== 2) &&
420 (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0)) {
421 sms_err("rom interface 0 is not used");
425 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 1) {
426 snprintf(devpath
, sizeof(devpath
), "usb\\%d-%s",
427 udev
->bus
->busnum
, udev
->devpath
);
428 sms_info("stellar device was found.");
429 return smsusb1_load_firmware(
430 udev
, smscore_registry_getmode(devpath
),
434 rc
= smsusb_init_device(intf
, id
->driver_info
);
435 sms_info("rc %d", rc
);
436 sms_board_load_modules(id
->driver_info
);
440 static void smsusb_disconnect(struct usb_interface
*intf
)
442 smsusb_term_device(intf
);
445 static int smsusb_suspend(struct usb_interface
*intf
, pm_message_t msg
)
447 struct smsusb_device_t
*dev
= usb_get_intfdata(intf
);
448 printk(KERN_INFO
"%s: Entering status %d.\n", __func__
, msg
.event
);
449 smsusb_stop_streaming(dev
);
453 static int smsusb_resume(struct usb_interface
*intf
)
456 struct smsusb_device_t
*dev
= usb_get_intfdata(intf
);
457 struct usb_device
*udev
= interface_to_usbdev(intf
);
459 printk(KERN_INFO
"%s: Entering.\n", __func__
);
460 usb_clear_halt(udev
, usb_rcvbulkpipe(udev
, 0x81));
461 usb_clear_halt(udev
, usb_rcvbulkpipe(udev
, 0x02));
463 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++)
464 printk(KERN_INFO
"endpoint %d %02x %02x %d\n", i
,
465 intf
->cur_altsetting
->endpoint
[i
].desc
.bEndpointAddress
,
466 intf
->cur_altsetting
->endpoint
[i
].desc
.bmAttributes
,
467 intf
->cur_altsetting
->endpoint
[i
].desc
.wMaxPacketSize
);
469 if (intf
->num_altsetting
> 0) {
470 rc
= usb_set_interface(udev
,
471 intf
->cur_altsetting
->desc
.
472 bInterfaceNumber
, 0);
474 printk(KERN_INFO
"%s usb_set_interface failed, "
475 "rc %d\n", __func__
, rc
);
480 smsusb_start_streaming(dev
);
484 static const struct usb_device_id smsusb_id_table
[] __devinitconst
= {
485 { USB_DEVICE(0x187f, 0x0010),
486 .driver_info
= SMS1XXX_BOARD_SIANO_STELLAR
},
487 { USB_DEVICE(0x187f, 0x0100),
488 .driver_info
= SMS1XXX_BOARD_SIANO_STELLAR
},
489 { USB_DEVICE(0x187f, 0x0200),
490 .driver_info
= SMS1XXX_BOARD_SIANO_NOVA_A
},
491 { USB_DEVICE(0x187f, 0x0201),
492 .driver_info
= SMS1XXX_BOARD_SIANO_NOVA_B
},
493 { USB_DEVICE(0x187f, 0x0300),
494 .driver_info
= SMS1XXX_BOARD_SIANO_VEGA
},
495 { USB_DEVICE(0x2040, 0x1700),
496 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_CATAMOUNT
},
497 { USB_DEVICE(0x2040, 0x1800),
498 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_OKEMO_A
},
499 { USB_DEVICE(0x2040, 0x1801),
500 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_OKEMO_B
},
501 { USB_DEVICE(0x2040, 0x2000),
502 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD
},
503 { USB_DEVICE(0x2040, 0x2009),
504 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD_R2
},
505 { USB_DEVICE(0x2040, 0x200a),
506 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD
},
507 { USB_DEVICE(0x2040, 0x2010),
508 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD
},
509 { USB_DEVICE(0x2040, 0x2011),
510 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD
},
511 { USB_DEVICE(0x2040, 0x2019),
512 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_TIGER_MINICARD
},
513 { USB_DEVICE(0x2040, 0x5500),
514 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
515 { USB_DEVICE(0x2040, 0x5510),
516 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
517 { USB_DEVICE(0x2040, 0x5520),
518 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
519 { USB_DEVICE(0x2040, 0x5530),
520 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
521 { USB_DEVICE(0x2040, 0x5580),
522 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
523 { USB_DEVICE(0x2040, 0x5590),
524 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
525 { USB_DEVICE(0x187f, 0x0202),
526 .driver_info
= SMS1XXX_BOARD_SIANO_NICE
},
527 { USB_DEVICE(0x187f, 0x0301),
528 .driver_info
= SMS1XXX_BOARD_SIANO_VENICE
},
529 { USB_DEVICE(0x2040, 0xb900),
530 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
531 { USB_DEVICE(0x2040, 0xb910),
532 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
533 { USB_DEVICE(0x2040, 0xb980),
534 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
535 { USB_DEVICE(0x2040, 0xb990),
536 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
537 { USB_DEVICE(0x2040, 0xc000),
538 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
539 { USB_DEVICE(0x2040, 0xc010),
540 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
541 { USB_DEVICE(0x2040, 0xc080),
542 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
543 { USB_DEVICE(0x2040, 0xc090),
544 .driver_info
= SMS1XXX_BOARD_HAUPPAUGE_WINDHAM
},
545 { } /* Terminating entry */
548 MODULE_DEVICE_TABLE(usb
, smsusb_id_table
);
550 static struct usb_driver smsusb_driver
= {
552 .probe
= smsusb_probe
,
553 .disconnect
= smsusb_disconnect
,
554 .id_table
= smsusb_id_table
,
556 .suspend
= smsusb_suspend
,
557 .resume
= smsusb_resume
,
560 module_usb_driver(smsusb_driver
);
562 MODULE_DESCRIPTION("Driver for the Siano SMS1xxx USB dongle");
563 MODULE_AUTHOR("Siano Mobile Silicon, INC. (uris@siano-ms.com)");
564 MODULE_LICENSE("GPL");