2 * Driver for the Siano SMS10xx 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 3 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"
29 #define USB1_BUFFER_SIZE 0x1000
30 #define USB2_BUFFER_SIZE 0x4000
32 #define MAX_BUFFERS 50
35 typedef struct _smsusb_device smsusb_device_t
;
37 typedef struct _smsusb_urb
45 typedef struct _smsusb_device
47 struct usb_device
*udev
;
48 smscore_device_t
*coredev
;
50 smsusb_urb_t surbs
[MAX_URBS
];
52 int response_alignment
;
56 int smsusb_submit_urb(smsusb_device_t
*dev
, smsusb_urb_t
*surb
);
58 void smsusb_onresponse(struct urb
*urb
)
60 smsusb_urb_t
*surb
= (smsusb_urb_t
*) urb
->context
;
61 smsusb_device_t
*dev
= surb
->dev
;
65 printk(KERN_INFO
"%s error, urb status %d, %d bytes\n", __func__
, urb
->status
, urb
->actual_length
);
69 if (urb
->actual_length
> 0)
71 SmsMsgHdr_ST
*phdr
= (SmsMsgHdr_ST
*) surb
->cb
->p
;
73 if (urb
->actual_length
>= phdr
->msgLength
)
75 surb
->cb
->size
= phdr
->msgLength
;
77 if (dev
->response_alignment
&& (phdr
->msgFlags
& MSG_HDR_FLAG_SPLIT_MSG
))
79 surb
->cb
->offset
= dev
->response_alignment
+ ((phdr
->msgFlags
>> 8) & 3);
82 if (((int) phdr
->msgLength
+ surb
->cb
->offset
) > urb
->actual_length
)
84 printk("%s: invalid response msglen %d offset %d size %d\n", __func__
, phdr
->msgLength
, surb
->cb
->offset
, urb
->actual_length
);
85 goto exit_and_resubmit
;
88 // move buffer pointer and copy header to its new location
89 memcpy((char*) phdr
+ surb
->cb
->offset
, phdr
, sizeof(SmsMsgHdr_ST
));
94 smscore_onresponse(dev
->coredev
, surb
->cb
);
99 printk("%s invalid response msglen %d actual %d\n", __func__
, phdr
->msgLength
, urb
->actual_length
);
104 smsusb_submit_urb(dev
, surb
);
107 int smsusb_submit_urb(smsusb_device_t
*dev
, smsusb_urb_t
*surb
)
111 surb
->cb
= smscore_getbuffer(dev
->coredev
);
114 printk(KERN_INFO
"%s smscore_getbuffer(...) returned NULL\n", __func__
);
122 usb_rcvbulkpipe(dev
->udev
, 0x81),
128 surb
->urb
.transfer_dma
= surb
->cb
->phys
;
129 surb
->urb
.transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
131 return usb_submit_urb(&surb
->urb
, GFP_ATOMIC
);
134 void smsusb_stop_streaming(smsusb_device_t
*dev
)
138 for (i
= 0; i
< MAX_URBS
; i
++)
140 usb_kill_urb(&dev
->surbs
[i
].urb
);
142 if (dev
->surbs
[i
].cb
)
144 smscore_putbuffer(dev
->coredev
, dev
->surbs
[i
].cb
);
145 dev
->surbs
[i
].cb
= NULL
;
150 int smsusb_start_streaming(smsusb_device_t
*dev
)
154 for (i
= 0; i
< MAX_URBS
; i
++)
156 rc
= smsusb_submit_urb(dev
, &dev
->surbs
[i
]);
159 printk(KERN_INFO
"%s smsusb_submit_urb(...) failed\n", __func__
);
160 smsusb_stop_streaming(dev
);
168 int smsusb_sendrequest(void *context
, void *buffer
, size_t size
)
170 smsusb_device_t
*dev
= (smsusb_device_t
*) context
;
173 return usb_bulk_msg(dev
->udev
, usb_sndbulkpipe(dev
->udev
, 2), buffer
, size
, &dummy
, 1000);
176 char *smsusb1_fw_lkup
[] =
178 "dvbt_stellar_usb.inp",
179 "dvbh_stellar_usb.inp",
180 "tdmb_stellar_usb.inp",
182 "dvbt_bda_stellar_usb.inp",
185 int smsusb1_load_firmware(struct usb_device
*udev
, int id
)
187 const struct firmware
*fw
;
191 if (id
< DEVICE_MODE_DVBT
|| id
> DEVICE_MODE_DVBT_BDA
)
193 printk(KERN_INFO
"%s invalid firmware id specified %d\n", __func__
, id
);
197 rc
= request_firmware(&fw
, smsusb1_fw_lkup
[id
], &udev
->dev
);
200 printk(KERN_INFO
"%s failed to open \"%s\" mode %d\n", __func__
, smsusb1_fw_lkup
[id
], id
);
204 fw_buffer
= kmalloc(fw
->size
, GFP_KERNEL
);
207 memcpy(fw_buffer
, fw
->data
, fw
->size
);
209 rc
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, 2), fw_buffer
, fw
->size
, &dummy
, 1000);
211 printk(KERN_INFO
"%s: sent %d(%d) bytes, rc %d\n", __func__
, fw
->size
, dummy
, rc
);
217 printk(KERN_INFO
"failed to allocate firmware buffer\n");
221 release_firmware(fw
);
226 void smsusb1_detectmode(void *context
, int *mode
)
228 char *product_string
= ((smsusb_device_t
*) context
)->udev
->product
;
230 *mode
= DEVICE_MODE_NONE
;
234 product_string
= "none";
235 printk("%s product string not found\n", __func__
);
239 if (strstr(product_string
, "DVBH"))
241 else if (strstr(product_string
, "BDA"))
243 else if (strstr(product_string
, "DVBT"))
245 else if (strstr(product_string
, "TDMB"))
249 printk("%s: %d \"%s\"\n", __func__
, *mode
, product_string
);
252 int smsusb1_setmode(void *context
, int mode
)
254 SmsMsgHdr_ST Msg
= { MSG_SW_RELOAD_REQ
, 0, HIF_TASK
, sizeof(SmsMsgHdr_ST
), 0 };
256 if (mode
< DEVICE_MODE_DVBT
|| mode
> DEVICE_MODE_DVBT_BDA
)
258 printk(KERN_INFO
"%s invalid firmware id specified %d\n", __func__
, mode
);
262 return smsusb_sendrequest(context
, &Msg
, sizeof(Msg
));
265 void smsusb_term_device(struct usb_interface
*intf
)
267 smsusb_device_t
*dev
= (smsusb_device_t
*) usb_get_intfdata(intf
);
271 smsusb_stop_streaming(dev
);
273 // unregister from smscore
275 smscore_unregister_device(dev
->coredev
);
279 printk(KERN_INFO
"%s device %p destroyed\n", __func__
, dev
);
282 usb_set_intfdata(intf
, NULL
);
285 int smsusb_init_device(struct usb_interface
*intf
)
287 smsdevice_params_t params
;
288 smsusb_device_t
*dev
;
291 // create device object
292 dev
= kzalloc(sizeof(smsusb_device_t
), GFP_KERNEL
);
295 printk(KERN_INFO
"%s kzalloc(sizeof(smsusb_device_t) failed\n", __func__
);
299 memset(¶ms
, 0, sizeof(params
));
300 usb_set_intfdata(intf
, dev
);
301 dev
->udev
= interface_to_usbdev(intf
);
303 switch (dev
->udev
->descriptor
.idProduct
)
306 dev
->buffer_size
= USB1_BUFFER_SIZE
;
308 params
.setmode_handler
= smsusb1_setmode
;
309 params
.detectmode_handler
= smsusb1_detectmode
;
313 dev
->buffer_size
= USB2_BUFFER_SIZE
;
314 dev
->response_alignment
= dev
->udev
->ep_in
[1]->desc
.wMaxPacketSize
- sizeof(SmsMsgHdr_ST
);
316 params
.flags
|= SMS_DEVICE_FAMILY2
;
320 params
.device
= &dev
->udev
->dev
;
321 params
.buffer_size
= dev
->buffer_size
;
322 params
.num_buffers
= MAX_BUFFERS
;
323 params
.sendrequest_handler
= smsusb_sendrequest
;
324 params
.context
= dev
;
325 snprintf(params
.devpath
, sizeof(params
.devpath
), "usb\\%d-%s", dev
->udev
->bus
->busnum
, dev
->udev
->devpath
);
327 // register in smscore
328 rc
= smscore_register_device(¶ms
, &dev
->coredev
);
331 printk(KERN_INFO
"%s smscore_register_device(...) failed, rc %d\n", __func__
, rc
);
332 smsusb_term_device(intf
);
337 for (i
= 0; i
< MAX_URBS
; i
++)
339 dev
->surbs
[i
].dev
= dev
;
340 usb_init_urb(&dev
->surbs
[i
].urb
);
343 rc
= smsusb_start_streaming(dev
);
346 printk(KERN_INFO
"%s smsusb_start_streaming(...) failed\n", __func__
);
347 smsusb_term_device(intf
);
351 rc
= smscore_start_device(dev
->coredev
);
354 printk(KERN_INFO
"%s smscore_start_device(...) failed\n", __func__
);
355 smsusb_term_device(intf
);
359 printk(KERN_INFO
"%s device %p created\n", __func__
, dev
);
364 int smsusb_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
366 struct usb_device
*udev
= interface_to_usbdev(intf
);
370 if (intf
->num_altsetting
> 0)
372 rc
= usb_set_interface(udev
, intf
->cur_altsetting
->desc
.bInterfaceNumber
, 0);
375 printk(KERN_INFO
"%s usb_set_interface failed, rc %d\n", __func__
, rc
);
380 printk(KERN_INFO
"smsusb_probe %d\n", intf
->cur_altsetting
->desc
.bInterfaceNumber
);
381 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++)
382 printk(KERN_INFO
"endpoint %d %02x %02x %d\n", i
, intf
->cur_altsetting
->endpoint
[i
].desc
.bEndpointAddress
, intf
->cur_altsetting
->endpoint
[i
].desc
.bmAttributes
, intf
->cur_altsetting
->endpoint
[i
].desc
.wMaxPacketSize
);
384 if (udev
->actconfig
->desc
.bNumInterfaces
== 2 && intf
->cur_altsetting
->desc
.bInterfaceNumber
== 0)
386 printk(KERN_INFO
"rom interface 0 is not used\n");
390 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
== 1)
392 snprintf(devpath
, 32, "%d:%s", udev
->bus
->busnum
, udev
->devpath
);
393 return smsusb1_load_firmware(udev
, smscore_registry_getmode(devpath
));
396 return smsusb_init_device(intf
);
399 void smsusb_disconnect(struct usb_interface
*intf
)
401 smsusb_term_device(intf
);
404 static struct usb_device_id smsusb_id_table
[] = {
405 { USB_DEVICE(0x187F, 0x0010) },
406 { USB_DEVICE(0x187F, 0x0100) },
407 { USB_DEVICE(0x187F, 0x0200) },
408 { } /* Terminating entry */
410 MODULE_DEVICE_TABLE (usb
, smsusb_id_table
);
412 static struct usb_driver smsusb_driver
= {
414 .probe
= smsusb_probe
,
415 .disconnect
= smsusb_disconnect
,
416 .id_table
= smsusb_id_table
,
419 int smsusb_register(void)
421 int rc
= usb_register(&smsusb_driver
);
423 printk(KERN_INFO
"usb_register failed. Error number %d\n", rc
);
425 printk(KERN_INFO
"%s\n", __func__
);
430 void smsusb_unregister(void)
432 /* Regular USB Cleanup */
433 usb_deregister(&smsusb_driver
);
434 printk(KERN_INFO
"%s\n", __func__
);