blob: ca9bb36544c0dd4e176c1d40bb87ae6725d09f69 [file] [log] [blame]
Steven Toth8d4f9d02008-05-22 18:05:26 -03001/*
2 * Driver for the Siano SMS10xx USB dongle
3 *
Michael Krufky85447062008-05-22 18:29:20 -03004 * author: Anatoly Greenblat
5 *
6 * Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
Steven Toth8d4f9d02008-05-22 18:05:26 -03007 *
8 * This program is free software; you can redistribute it and/or modify
Michael Krufky85447062008-05-22 18:29:20 -03009 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation;
Steven Toth8d4f9d02008-05-22 18:05:26 -030011 *
Michael Krufky85447062008-05-22 18:29:20 -030012 * Software distributed under the License is distributed on an "AS IS"
13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
Steven Toth8d4f9d02008-05-22 18:05:26 -030014 *
Michael Krufky85447062008-05-22 18:29:20 -030015 * See the GNU General Public License for more details.
Steven Toth8d4f9d02008-05-22 18:05:26 -030016 *
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.
20 */
21
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030022#include <linux/kernel.h>
23#include <linux/init.h>
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030024#include <linux/usb.h>
25#include <linux/firmware.h>
26
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030027#include "smscoreapi.h"
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030028
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030029#define USB1_BUFFER_SIZE 0x1000
30#define USB2_BUFFER_SIZE 0x4000
31
32#define MAX_BUFFERS 50
33#define MAX_URBS 10
34
Michael Krufky73104fb2008-06-14 18:27:18 -030035/* TO DO: move these to a header file */
36#define USB_VID_SIANO 0x187f
37
38#define USB_PID_STELLAR 0x0100
39#define USB_PID_NOVA_A 0x0200
40#define USB_PID_NOVA_B 0x0201
41#define USB_PID_VEGA 0x0300
42
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030043typedef struct _smsusb_device smsusb_device_t;
44
45typedef struct _smsusb_urb
46{
47 smscore_buffer_t *cb;
48 smsusb_device_t *dev;
49
50 struct urb urb;
51} smsusb_urb_t;
52
53typedef struct _smsusb_device
54{
Michael Krufkya83ccdd2008-05-06 03:11:51 -030055 struct usb_device *udev;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030056 smscore_device_t *coredev;
57
58 smsusb_urb_t surbs[MAX_URBS];
59
60 int response_alignment;
61 int buffer_size;
62} *psmsusb_device_t;
63
Michael Krufkya83ccdd2008-05-06 03:11:51 -030064int smsusb_submit_urb(smsusb_device_t *dev, smsusb_urb_t *surb);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030065
66void smsusb_onresponse(struct urb *urb)
67{
68 smsusb_urb_t *surb = (smsusb_urb_t *) urb->context;
69 smsusb_device_t *dev = surb->dev;
70
71 if (urb->status < 0)
72 {
Michael Krufky18658112008-05-22 18:30:17 -030073 printk(KERN_INFO "%s error, urb status %d, %d bytes\n", __func__, urb->status, urb->actual_length);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030074 return;
75 }
76
77 if (urb->actual_length > 0)
78 {
79 SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *) surb->cb->p;
80
81 if (urb->actual_length >= phdr->msgLength)
82 {
83 surb->cb->size = phdr->msgLength;
84
85 if (dev->response_alignment && (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG))
86 {
87 surb->cb->offset = dev->response_alignment + ((phdr->msgFlags >> 8) & 3);
88
89 // sanity check
90 if (((int) phdr->msgLength + surb->cb->offset) > urb->actual_length)
91 {
Michael Krufky18658112008-05-22 18:30:17 -030092 printk("%s: invalid response msglen %d offset %d size %d\n", __func__, phdr->msgLength, surb->cb->offset, urb->actual_length);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030093 goto exit_and_resubmit;
94 }
95
96 // move buffer pointer and copy header to its new location
Michael Krufky55ad3102008-05-06 03:52:44 -030097 memcpy((char *) phdr + surb->cb->offset,
98 phdr, sizeof(SmsMsgHdr_ST));
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030099 }
100 else
101 surb->cb->offset = 0;
102
103 smscore_onresponse(dev->coredev, surb->cb);
104 surb->cb = NULL;
105 }
106 else
107 {
Michael Krufky18658112008-05-22 18:30:17 -0300108 printk("%s invalid response msglen %d actual %d\n", __func__, phdr->msgLength, urb->actual_length);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300109 }
110 }
111
112exit_and_resubmit:
113 smsusb_submit_urb(dev, surb);
114}
115
Michael Krufkya83ccdd2008-05-06 03:11:51 -0300116int smsusb_submit_urb(smsusb_device_t *dev, smsusb_urb_t *surb)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300117{
118 if (!surb->cb)
119 {
120 surb->cb = smscore_getbuffer(dev->coredev);
121 if (!surb->cb)
122 {
Michael Krufky18658112008-05-22 18:30:17 -0300123 printk(KERN_INFO "%s smscore_getbuffer(...) returned NULL\n", __func__);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300124 return -ENOMEM;
125 }
126 }
127
128 usb_fill_bulk_urb(
129 &surb->urb,
130 dev->udev,
131 usb_rcvbulkpipe(dev->udev, 0x81),
132 surb->cb->p,
133 dev->buffer_size,
134 smsusb_onresponse,
135 surb
136 );
137 surb->urb.transfer_dma = surb->cb->phys;
138 surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
139
140 return usb_submit_urb(&surb->urb, GFP_ATOMIC);
141}
142
Michael Krufkya83ccdd2008-05-06 03:11:51 -0300143void smsusb_stop_streaming(smsusb_device_t *dev)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300144{
145 int i;
146
147 for (i = 0; i < MAX_URBS; i ++)
148 {
149 usb_kill_urb(&dev->surbs[i].urb);
150
151 if (dev->surbs[i].cb)
152 {
153 smscore_putbuffer(dev->coredev, dev->surbs[i].cb);
154 dev->surbs[i].cb = NULL;
155 }
156 }
157}
158
Michael Krufkya83ccdd2008-05-06 03:11:51 -0300159int smsusb_start_streaming(smsusb_device_t *dev)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300160{
161 int i, rc;
162
163 for (i = 0; i < MAX_URBS; i ++)
164 {
165 rc = smsusb_submit_urb(dev, &dev->surbs[i]);
166 if (rc < 0)
167 {
Michael Krufky18658112008-05-22 18:30:17 -0300168 printk(KERN_INFO "%s smsusb_submit_urb(...) failed\n", __func__);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300169 smsusb_stop_streaming(dev);
170 break;
171 }
172 }
173
174 return rc;
175}
176
177int smsusb_sendrequest(void *context, void *buffer, size_t size)
178{
Michael Krufkya83ccdd2008-05-06 03:11:51 -0300179 smsusb_device_t *dev = (smsusb_device_t *) context;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300180 int dummy;
181
182 return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2), buffer, size, &dummy, 1000);
183}
184
185char *smsusb1_fw_lkup[] =
186{
187 "dvbt_stellar_usb.inp",
188 "dvbh_stellar_usb.inp",
189 "tdmb_stellar_usb.inp",
190 "none",
191 "dvbt_bda_stellar_usb.inp",
192};
193
194int smsusb1_load_firmware(struct usb_device *udev, int id)
195{
196 const struct firmware *fw;
Michael Krufkya83ccdd2008-05-06 03:11:51 -0300197 u8 *fw_buffer;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300198 int rc, dummy;
199
200 if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA)
201 {
Michael Krufky18658112008-05-22 18:30:17 -0300202 printk(KERN_INFO "%s invalid firmware id specified %d\n", __func__, id);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300203 return -EINVAL;
204 }
205
206 rc = request_firmware(&fw, smsusb1_fw_lkup[id], &udev->dev);
207 if (rc < 0)
208 {
Michael Krufky18658112008-05-22 18:30:17 -0300209 printk(KERN_INFO "%s failed to open \"%s\" mode %d\n", __func__, smsusb1_fw_lkup[id], id);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300210 return rc;
211 }
212
213 fw_buffer = kmalloc(fw->size, GFP_KERNEL);
214 if (fw_buffer)
215 {
216 memcpy(fw_buffer, fw->data, fw->size);
217
218 rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2), fw_buffer, fw->size, &dummy, 1000);
219
Michael Krufky18658112008-05-22 18:30:17 -0300220 printk(KERN_INFO "%s: sent %d(%d) bytes, rc %d\n", __func__, fw->size, dummy, rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300221
222 kfree(fw_buffer);
223 }
224 else
225 {
226 printk(KERN_INFO "failed to allocate firmware buffer\n");
227 rc = -ENOMEM;
228 }
229
230 release_firmware(fw);
231
232 return rc;
233}
234
235void smsusb1_detectmode(void *context, int *mode)
236{
237 char *product_string = ((smsusb_device_t *) context)->udev->product;
238
239 *mode = DEVICE_MODE_NONE;
240
241 if (!product_string)
242 {
243 product_string = "none";
Michael Krufky18658112008-05-22 18:30:17 -0300244 printk("%s product string not found\n", __func__);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300245 }
246 else
247 {
248 if (strstr(product_string, "DVBH"))
249 *mode = 1;
250 else if (strstr(product_string, "BDA"))
251 *mode = 4;
252 else if (strstr(product_string, "DVBT"))
253 *mode = 0;
254 else if (strstr(product_string, "TDMB"))
255 *mode = 2;
256 }
257
Michael Krufky18658112008-05-22 18:30:17 -0300258 printk("%s: %d \"%s\"\n", __func__, *mode, product_string);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300259}
260
261int smsusb1_setmode(void *context, int mode)
262{
263 SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK, sizeof(SmsMsgHdr_ST), 0 };
264
265 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA)
266 {
Michael Krufky18658112008-05-22 18:30:17 -0300267 printk(KERN_INFO "%s invalid firmware id specified %d\n", __func__, mode);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300268 return -EINVAL;
269 }
270
271 return smsusb_sendrequest(context, &Msg, sizeof(Msg));
272}
273
274void smsusb_term_device(struct usb_interface *intf)
275{
Michael Krufky55ad3102008-05-06 03:52:44 -0300276 smsusb_device_t *dev = (smsusb_device_t *) usb_get_intfdata(intf);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300277
278 if (dev)
279 {
280 smsusb_stop_streaming(dev);
281
282 // unregister from smscore
283 if (dev->coredev)
284 smscore_unregister_device(dev->coredev);
285
286 kfree(dev);
287
Michael Krufky18658112008-05-22 18:30:17 -0300288 printk(KERN_INFO "%s device %p destroyed\n", __func__, dev);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300289 }
290
291 usb_set_intfdata(intf, NULL);
292}
293
294int smsusb_init_device(struct usb_interface *intf)
295{
296 smsdevice_params_t params;
Michael Krufkya83ccdd2008-05-06 03:11:51 -0300297 smsusb_device_t *dev;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300298 int i, rc;
299
300 // create device object
301 dev = kzalloc(sizeof(smsusb_device_t), GFP_KERNEL);
302 if (!dev)
303 {
Michael Krufky18658112008-05-22 18:30:17 -0300304 printk(KERN_INFO "%s kzalloc(sizeof(smsusb_device_t) failed\n", __func__);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300305 return -ENOMEM;
306 }
307
308 memset(&params, 0, sizeof(params));
309 usb_set_intfdata(intf, dev);
310 dev->udev = interface_to_usbdev(intf);
311
Michael Krufkyf17407a2008-06-14 00:43:26 -0300312 switch (dev->udev->descriptor.idProduct) {
Michael Krufky73104fb2008-06-14 18:27:18 -0300313
314 case USB_PID_STELLAR:
Michael Krufkyf17407a2008-06-14 00:43:26 -0300315 dev->buffer_size = USB1_BUFFER_SIZE;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300316
Michael Krufkyf17407a2008-06-14 00:43:26 -0300317 params.setmode_handler = smsusb1_setmode;
318 params.detectmode_handler = smsusb1_detectmode;
319 params.device_type = SMS_STELLAR;
320 printk(KERN_INFO "%s stellar device found\n", __func__ );
321 break;
322 default:
Michael Krufky73104fb2008-06-14 18:27:18 -0300323 switch (dev->udev->descriptor.idProduct) {
324 case USB_PID_NOVA_A:
Michael Krufkyf17407a2008-06-14 00:43:26 -0300325 params.device_type = SMS_NOVA_A0;
Michael Krufky494d24c2008-06-14 07:40:41 -0300326 printk(KERN_INFO "%s nova A0 found\n", __func__ );
Michael Krufky73104fb2008-06-14 18:27:18 -0300327 break;
328 default:
329 case USB_PID_NOVA_B:
Michael Krufkyf17407a2008-06-14 00:43:26 -0300330 params.device_type = SMS_NOVA_B0;
Michael Krufky494d24c2008-06-14 07:40:41 -0300331 printk(KERN_INFO "%s nova B0 found\n", __func__);
Michael Krufky73104fb2008-06-14 18:27:18 -0300332 break;
333 case USB_PID_VEGA:
Michael Krufkyf17407a2008-06-14 00:43:26 -0300334 params.device_type = SMS_VEGA;
Michael Krufky494d24c2008-06-14 07:40:41 -0300335 printk(KERN_INFO "%s Vega found\n", __func__);
Michael Krufkyf17407a2008-06-14 00:43:26 -0300336 }
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300337
Michael Krufkyf17407a2008-06-14 00:43:26 -0300338 dev->buffer_size = USB2_BUFFER_SIZE;
339 dev->response_alignment = dev->udev->ep_in[1]->desc.wMaxPacketSize - sizeof(SmsMsgHdr_ST);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300340
Michael Krufkyf17407a2008-06-14 00:43:26 -0300341 params.flags |= SMS_DEVICE_FAMILY2;
342 break;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300343 }
344
345 params.device = &dev->udev->dev;
346 params.buffer_size = dev->buffer_size;
347 params.num_buffers = MAX_BUFFERS;
348 params.sendrequest_handler = smsusb_sendrequest;
349 params.context = dev;
350 snprintf(params.devpath, sizeof(params.devpath), "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath);
351
352 // register in smscore
353 rc = smscore_register_device(&params, &dev->coredev);
354 if (rc < 0)
355 {
Michael Krufky18658112008-05-22 18:30:17 -0300356 printk(KERN_INFO "%s smscore_register_device(...) failed, rc %d\n", __func__, rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300357 smsusb_term_device(intf);
358 return rc;
359 }
360
361 // initialize urbs
362 for (i = 0; i < MAX_URBS; i ++)
363 {
364 dev->surbs[i].dev = dev;
365 usb_init_urb(&dev->surbs[i].urb);
366 }
367
Michael Krufkyf17407a2008-06-14 00:43:26 -0300368 printk(KERN_INFO "%s smsusb_start_streaming(...).\n", __func__);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300369 rc = smsusb_start_streaming(dev);
370 if (rc < 0)
371 {
Michael Krufky18658112008-05-22 18:30:17 -0300372 printk(KERN_INFO "%s smsusb_start_streaming(...) failed\n", __func__);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300373 smsusb_term_device(intf);
374 return rc;
375 }
376
377 rc = smscore_start_device(dev->coredev);
378 if (rc < 0)
379 {
Michael Krufky18658112008-05-22 18:30:17 -0300380 printk(KERN_INFO "%s smscore_start_device(...) failed\n", __func__);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300381 smsusb_term_device(intf);
382 return rc;
383 }
384
Michael Krufky18658112008-05-22 18:30:17 -0300385 printk(KERN_INFO "%s device %p created\n", __func__, dev);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300386
387 return rc;
388}
389
390int smsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
391{
392 struct usb_device *udev = interface_to_usbdev(intf);
393 char devpath[32];
394 int i, rc;
395
Michael Krufkyf17407a2008-06-14 00:43:26 -0300396 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81));
397 rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02));
398
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300399 if (intf->num_altsetting > 0)
400 {
401 rc = usb_set_interface(udev, intf->cur_altsetting->desc.bInterfaceNumber, 0);
402 if (rc < 0)
403 {
Michael Krufky18658112008-05-22 18:30:17 -0300404 printk(KERN_INFO "%s usb_set_interface failed, rc %d\n", __func__, rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300405 return rc;
406 }
407 }
408
409 printk(KERN_INFO "smsusb_probe %d\n", intf->cur_altsetting->desc.bInterfaceNumber);
410 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i ++)
411 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);
412
413 if (udev->actconfig->desc.bNumInterfaces == 2 && intf->cur_altsetting->desc.bInterfaceNumber == 0)
414 {
415 printk(KERN_INFO "rom interface 0 is not used\n");
416 return -ENODEV;
417 }
418
419 if (intf->cur_altsetting->desc.bInterfaceNumber == 1)
420 {
Michael Krufkyf17407a2008-06-14 00:43:26 -0300421 snprintf(devpath, sizeof(devpath), "usb\\%d-%s", udev->bus->busnum, udev->devpath);
422 printk(KERN_INFO "stellar device was found.\n");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300423 return smsusb1_load_firmware(udev, smscore_registry_getmode(devpath));
424 }
425
Michael Krufkyf17407a2008-06-14 00:43:26 -0300426 rc = smsusb_init_device(intf);
Michael Krufky494d24c2008-06-14 07:40:41 -0300427 printk(KERN_INFO "%s rc %d\n", __func__, rc);
Michael Krufkyf17407a2008-06-14 00:43:26 -0300428 return rc;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300429}
430
431void smsusb_disconnect(struct usb_interface *intf)
432{
433 smsusb_term_device(intf);
434}
435
Steven Toth7d18d2e2008-05-22 18:07:39 -0300436static struct usb_device_id smsusb_id_table [] = {
Michael Krufky73104fb2008-06-14 18:27:18 -0300437 { USB_DEVICE(USB_VID_SIANO, 0x0010) },
438 { USB_DEVICE(USB_VID_SIANO, USB_PID_STELLAR) },
439 { USB_DEVICE(USB_VID_SIANO, USB_PID_NOVA_A) },
Steven Toth7d18d2e2008-05-22 18:07:39 -0300440 { } /* Terminating entry */
441};
442MODULE_DEVICE_TABLE (usb, smsusb_id_table);
443
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300444static struct usb_driver smsusb_driver = {
445 .name = "smsusb",
446 .probe = smsusb_probe,
447 .disconnect = smsusb_disconnect,
448 .id_table = smsusb_id_table,
449};
450
Steven Totheae55662008-05-22 18:04:36 -0300451int smsusb_register(void)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300452{
453 int rc = usb_register(&smsusb_driver);
454 if (rc)
455 printk(KERN_INFO "usb_register failed. Error number %d\n", rc);
456
Michael Krufky18658112008-05-22 18:30:17 -0300457 printk(KERN_INFO "%s\n", __func__);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300458
459 return rc;
460}
461
Steven Totheae55662008-05-22 18:04:36 -0300462void smsusb_unregister(void)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300463{
Steven Toth3dd243782008-05-22 18:01:02 -0300464 /* Regular USB Cleanup */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300465 usb_deregister(&smsusb_driver);
Michael Krufky18658112008-05-22 18:30:17 -0300466 printk(KERN_INFO "%s\n", __func__);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300467}
468