Commit | Line | Data |
---|---|---|
8d4f9d0e ST |
1 | /* |
2 | * Driver for the Siano SMS10xx USB dongle | |
3 | * | |
85447060 MK |
4 | * author: Anatoly Greenblat |
5 | * | |
6 | * Copyright (c), 2005-2008 Siano Mobile Silicon, Inc. | |
8d4f9d0e ST |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
85447060 MK |
9 | * it under the terms of the GNU General Public License version 3 as |
10 | * published by the Free Software Foundation; | |
8d4f9d0e | 11 | * |
85447060 MK |
12 | * Software distributed under the License is distributed on an "AS IS" |
13 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. | |
8d4f9d0e | 14 | * |
85447060 | 15 | * See the GNU General Public License for more details. |
8d4f9d0e ST |
16 | * |
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 | ||
2e5c1ec8 MK |
22 | #include <linux/kernel.h> |
23 | #include <linux/init.h> | |
2e5c1ec8 MK |
24 | #include <linux/usb.h> |
25 | #include <linux/firmware.h> | |
26 | ||
2e5c1ec8 | 27 | #include "smscoreapi.h" |
2e5c1ec8 | 28 | |
2e5c1ec8 MK |
29 | #define USB1_BUFFER_SIZE 0x1000 |
30 | #define USB2_BUFFER_SIZE 0x4000 | |
31 | ||
32 | #define MAX_BUFFERS 50 | |
33 | #define MAX_URBS 10 | |
34 | ||
73104fb3 MK |
35 | /* 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 | ||
2e5c1ec8 MK |
43 | typedef struct _smsusb_device smsusb_device_t; |
44 | ||
45 | typedef struct _smsusb_urb | |
46 | { | |
47 | smscore_buffer_t *cb; | |
48 | smsusb_device_t *dev; | |
49 | ||
50 | struct urb urb; | |
51 | } smsusb_urb_t; | |
52 | ||
53 | typedef struct _smsusb_device | |
54 | { | |
a83ccdd6 | 55 | struct usb_device *udev; |
2e5c1ec8 MK |
56 | smscore_device_t *coredev; |
57 | ||
58 | smsusb_urb_t surbs[MAX_URBS]; | |
59 | ||
82237416 MK |
60 | int response_alignment; |
61 | int buffer_size; | |
2e5c1ec8 MK |
62 | } *psmsusb_device_t; |
63 | ||
a83ccdd6 | 64 | int smsusb_submit_urb(smsusb_device_t *dev, smsusb_urb_t *surb); |
2e5c1ec8 MK |
65 | |
66 | void smsusb_onresponse(struct urb *urb) | |
67 | { | |
68 | smsusb_urb_t *surb = (smsusb_urb_t *) urb->context; | |
69 | smsusb_device_t *dev = surb->dev; | |
70 | ||
82237416 MK |
71 | if (urb->status < 0) { |
72 | printk(KERN_INFO "%s error, urb status %d, %d bytes\n", | |
73 | __func__, urb->status, urb->actual_length); | |
2e5c1ec8 MK |
74 | return; |
75 | } | |
76 | ||
82237416 | 77 | if (urb->actual_length > 0) { |
2e5c1ec8 MK |
78 | SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *) surb->cb->p; |
79 | ||
82237416 | 80 | if (urb->actual_length >= phdr->msgLength) { |
2e5c1ec8 MK |
81 | surb->cb->size = phdr->msgLength; |
82 | ||
82237416 MK |
83 | if (dev->response_alignment && |
84 | (phdr->msgFlags & MSG_HDR_FLAG_SPLIT_MSG)) { | |
85 | ||
86 | surb->cb->offset = | |
87 | dev->response_alignment + | |
88 | ((phdr->msgFlags >> 8) & 3); | |
89 | ||
90 | /* sanity check */ | |
91 | if (((int) phdr->msgLength + | |
92 | surb->cb->offset) > urb->actual_length) { | |
93 | printk(KERN_INFO "%s: invalid " | |
94 | "response msglen %d offset %d " | |
95 | "size %d\n", __func__, | |
96 | phdr->msgLength, | |
97 | surb->cb->offset, | |
98 | urb->actual_length); | |
2e5c1ec8 MK |
99 | goto exit_and_resubmit; |
100 | } | |
101 | ||
82237416 MK |
102 | /* move buffer pointer and |
103 | * copy header to its new location */ | |
55ad310c MK |
104 | memcpy((char *) phdr + surb->cb->offset, |
105 | phdr, sizeof(SmsMsgHdr_ST)); | |
82237416 | 106 | } else |
2e5c1ec8 MK |
107 | surb->cb->offset = 0; |
108 | ||
109 | smscore_onresponse(dev->coredev, surb->cb); | |
110 | surb->cb = NULL; | |
82237416 MK |
111 | } else { |
112 | printk(KERN_INFO "%s invalid response " | |
113 | "msglen %d actual %d\n", __func__, | |
114 | phdr->msgLength, urb->actual_length); | |
2e5c1ec8 MK |
115 | } |
116 | } | |
117 | ||
118 | exit_and_resubmit: | |
119 | smsusb_submit_urb(dev, surb); | |
120 | } | |
121 | ||
a83ccdd6 | 122 | int smsusb_submit_urb(smsusb_device_t *dev, smsusb_urb_t *surb) |
2e5c1ec8 | 123 | { |
82237416 | 124 | if (!surb->cb) { |
2e5c1ec8 | 125 | surb->cb = smscore_getbuffer(dev->coredev); |
82237416 MK |
126 | if (!surb->cb) { |
127 | printk(KERN_INFO "%s smscore_getbuffer(...) " | |
128 | "returned NULL\n", __func__); | |
2e5c1ec8 MK |
129 | return -ENOMEM; |
130 | } | |
131 | } | |
132 | ||
133 | usb_fill_bulk_urb( | |
134 | &surb->urb, | |
135 | dev->udev, | |
136 | usb_rcvbulkpipe(dev->udev, 0x81), | |
137 | surb->cb->p, | |
138 | dev->buffer_size, | |
139 | smsusb_onresponse, | |
140 | surb | |
141 | ); | |
142 | surb->urb.transfer_dma = surb->cb->phys; | |
143 | surb->urb.transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
144 | ||
145 | return usb_submit_urb(&surb->urb, GFP_ATOMIC); | |
146 | } | |
147 | ||
a83ccdd6 | 148 | void smsusb_stop_streaming(smsusb_device_t *dev) |
2e5c1ec8 MK |
149 | { |
150 | int i; | |
151 | ||
fa830e8a | 152 | for (i = 0; i < MAX_URBS; i++) { |
2e5c1ec8 MK |
153 | usb_kill_urb(&dev->surbs[i].urb); |
154 | ||
82237416 | 155 | if (dev->surbs[i].cb) { |
2e5c1ec8 MK |
156 | smscore_putbuffer(dev->coredev, dev->surbs[i].cb); |
157 | dev->surbs[i].cb = NULL; | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
a83ccdd6 | 162 | int smsusb_start_streaming(smsusb_device_t *dev) |
2e5c1ec8 MK |
163 | { |
164 | int i, rc; | |
165 | ||
fa830e8a | 166 | for (i = 0; i < MAX_URBS; i++) { |
2e5c1ec8 | 167 | rc = smsusb_submit_urb(dev, &dev->surbs[i]); |
82237416 MK |
168 | if (rc < 0) { |
169 | printk(KERN_INFO "%s smsusb_submit_urb(...) " | |
170 | "failed\n", __func__); | |
2e5c1ec8 MK |
171 | smsusb_stop_streaming(dev); |
172 | break; | |
173 | } | |
174 | } | |
175 | ||
176 | return rc; | |
177 | } | |
178 | ||
179 | int smsusb_sendrequest(void *context, void *buffer, size_t size) | |
180 | { | |
a83ccdd6 | 181 | smsusb_device_t *dev = (smsusb_device_t *) context; |
2e5c1ec8 MK |
182 | int dummy; |
183 | ||
82237416 MK |
184 | return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2), |
185 | buffer, size, &dummy, 1000); | |
2e5c1ec8 MK |
186 | } |
187 | ||
82237416 | 188 | char *smsusb1_fw_lkup[] = { |
2e5c1ec8 MK |
189 | "dvbt_stellar_usb.inp", |
190 | "dvbh_stellar_usb.inp", | |
191 | "tdmb_stellar_usb.inp", | |
192 | "none", | |
193 | "dvbt_bda_stellar_usb.inp", | |
194 | }; | |
195 | ||
196 | int smsusb1_load_firmware(struct usb_device *udev, int id) | |
197 | { | |
198 | const struct firmware *fw; | |
a83ccdd6 | 199 | u8 *fw_buffer; |
2e5c1ec8 MK |
200 | int rc, dummy; |
201 | ||
82237416 MK |
202 | if (id < DEVICE_MODE_DVBT || id > DEVICE_MODE_DVBT_BDA) { |
203 | printk(KERN_INFO "%s invalid firmware id specified %d\n", | |
204 | __func__, id); | |
2e5c1ec8 MK |
205 | return -EINVAL; |
206 | } | |
207 | ||
208 | rc = request_firmware(&fw, smsusb1_fw_lkup[id], &udev->dev); | |
82237416 MK |
209 | if (rc < 0) { |
210 | printk(KERN_INFO "%s failed to open \"%s\" mode %d\n", | |
211 | __func__, smsusb1_fw_lkup[id], id); | |
2e5c1ec8 MK |
212 | return rc; |
213 | } | |
214 | ||
215 | fw_buffer = kmalloc(fw->size, GFP_KERNEL); | |
82237416 | 216 | if (fw_buffer) { |
2e5c1ec8 MK |
217 | memcpy(fw_buffer, fw->data, fw->size); |
218 | ||
82237416 MK |
219 | rc = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 2), |
220 | fw_buffer, fw->size, &dummy, 1000); | |
2e5c1ec8 | 221 | |
82237416 MK |
222 | printk(KERN_INFO "%s: sent %d(%d) bytes, rc %d\n", |
223 | __func__, fw->size, dummy, rc); | |
2e5c1ec8 MK |
224 | |
225 | kfree(fw_buffer); | |
82237416 | 226 | } else { |
2e5c1ec8 MK |
227 | printk(KERN_INFO "failed to allocate firmware buffer\n"); |
228 | rc = -ENOMEM; | |
229 | } | |
230 | ||
231 | release_firmware(fw); | |
232 | ||
233 | return rc; | |
234 | } | |
235 | ||
236 | void smsusb1_detectmode(void *context, int *mode) | |
237 | { | |
238 | char *product_string = ((smsusb_device_t *) context)->udev->product; | |
239 | ||
240 | *mode = DEVICE_MODE_NONE; | |
241 | ||
82237416 | 242 | if (!product_string) { |
2e5c1ec8 | 243 | product_string = "none"; |
9f211397 | 244 | printk(KERN_ERR "%s product string not found\n", __func__); |
82237416 MK |
245 | } else if (strstr(product_string, "DVBH")) |
246 | *mode = 1; | |
247 | else if (strstr(product_string, "BDA")) | |
248 | *mode = 4; | |
249 | else if (strstr(product_string, "DVBT")) | |
250 | *mode = 0; | |
251 | else if (strstr(product_string, "TDMB")) | |
252 | *mode = 2; | |
2e5c1ec8 | 253 | |
9f211397 | 254 | printk(KERN_INFO "%s: %d \"%s\"\n", __func__, *mode, product_string); |
2e5c1ec8 MK |
255 | } |
256 | ||
257 | int smsusb1_setmode(void *context, int mode) | |
258 | { | |
82237416 MK |
259 | SmsMsgHdr_ST Msg = { MSG_SW_RELOAD_REQ, 0, HIF_TASK, |
260 | sizeof(SmsMsgHdr_ST), 0 }; | |
2e5c1ec8 | 261 | |
82237416 MK |
262 | if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) { |
263 | printk(KERN_INFO "%s invalid firmware id specified %d\n", | |
264 | __func__, mode); | |
2e5c1ec8 MK |
265 | return -EINVAL; |
266 | } | |
267 | ||
268 | return smsusb_sendrequest(context, &Msg, sizeof(Msg)); | |
269 | } | |
270 | ||
271 | void smsusb_term_device(struct usb_interface *intf) | |
272 | { | |
55ad310c | 273 | smsusb_device_t *dev = (smsusb_device_t *) usb_get_intfdata(intf); |
2e5c1ec8 | 274 | |
82237416 | 275 | if (dev) { |
2e5c1ec8 MK |
276 | smsusb_stop_streaming(dev); |
277 | ||
fa830e8a | 278 | /* unregister from smscore */ |
2e5c1ec8 MK |
279 | if (dev->coredev) |
280 | smscore_unregister_device(dev->coredev); | |
281 | ||
282 | kfree(dev); | |
283 | ||
18658117 | 284 | printk(KERN_INFO "%s device %p destroyed\n", __func__, dev); |
2e5c1ec8 MK |
285 | } |
286 | ||
287 | usb_set_intfdata(intf, NULL); | |
288 | } | |
289 | ||
290 | int smsusb_init_device(struct usb_interface *intf) | |
291 | { | |
292 | smsdevice_params_t params; | |
a83ccdd6 | 293 | smsusb_device_t *dev; |
2e5c1ec8 MK |
294 | int i, rc; |
295 | ||
fa830e8a | 296 | /* create device object */ |
2e5c1ec8 | 297 | dev = kzalloc(sizeof(smsusb_device_t), GFP_KERNEL); |
82237416 MK |
298 | if (!dev) { |
299 | printk(KERN_INFO "%s kzalloc(sizeof(smsusb_device_t) failed\n", | |
300 | __func__); | |
2e5c1ec8 MK |
301 | return -ENOMEM; |
302 | } | |
303 | ||
304 | memset(¶ms, 0, sizeof(params)); | |
305 | usb_set_intfdata(intf, dev); | |
306 | dev->udev = interface_to_usbdev(intf); | |
307 | ||
f17407a8 | 308 | switch (dev->udev->descriptor.idProduct) { |
73104fb3 MK |
309 | |
310 | case USB_PID_STELLAR: | |
f17407a8 MK |
311 | dev->buffer_size = USB1_BUFFER_SIZE; |
312 | ||
313 | params.setmode_handler = smsusb1_setmode; | |
314 | params.detectmode_handler = smsusb1_detectmode; | |
315 | params.device_type = SMS_STELLAR; | |
fa830e8a | 316 | printk(KERN_INFO "%s stellar device found\n", __func__); |
f17407a8 MK |
317 | break; |
318 | default: | |
73104fb3 MK |
319 | switch (dev->udev->descriptor.idProduct) { |
320 | case USB_PID_NOVA_A: | |
f17407a8 | 321 | params.device_type = SMS_NOVA_A0; |
fa830e8a | 322 | printk(KERN_INFO "%s nova A0 found\n", __func__); |
73104fb3 MK |
323 | break; |
324 | default: | |
325 | case USB_PID_NOVA_B: | |
f17407a8 | 326 | params.device_type = SMS_NOVA_B0; |
494d24c5 | 327 | printk(KERN_INFO "%s nova B0 found\n", __func__); |
73104fb3 MK |
328 | break; |
329 | case USB_PID_VEGA: | |
f17407a8 | 330 | params.device_type = SMS_VEGA; |
494d24c5 | 331 | printk(KERN_INFO "%s Vega found\n", __func__); |
f17407a8 | 332 | } |
2e5c1ec8 | 333 | |
f17407a8 | 334 | dev->buffer_size = USB2_BUFFER_SIZE; |
82237416 MK |
335 | dev->response_alignment = |
336 | dev->udev->ep_in[1]->desc.wMaxPacketSize - | |
337 | sizeof(SmsMsgHdr_ST); | |
2e5c1ec8 | 338 | |
f17407a8 MK |
339 | params.flags |= SMS_DEVICE_FAMILY2; |
340 | break; | |
2e5c1ec8 MK |
341 | } |
342 | ||
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; | |
82237416 MK |
348 | snprintf(params.devpath, sizeof(params.devpath), |
349 | "usb\\%d-%s", dev->udev->bus->busnum, dev->udev->devpath); | |
2e5c1ec8 | 350 | |
82237416 | 351 | /* register in smscore */ |
2e5c1ec8 | 352 | rc = smscore_register_device(¶ms, &dev->coredev); |
82237416 MK |
353 | if (rc < 0) { |
354 | printk(KERN_INFO "%s smscore_register_device(...) failed, " | |
355 | "rc %d\n", __func__, rc); | |
2e5c1ec8 MK |
356 | smsusb_term_device(intf); |
357 | return rc; | |
358 | } | |
359 | ||
fa830e8a | 360 | /* initialize urbs */ |
82237416 | 361 | for (i = 0; i < MAX_URBS; i++) { |
2e5c1ec8 MK |
362 | dev->surbs[i].dev = dev; |
363 | usb_init_urb(&dev->surbs[i].urb); | |
364 | } | |
365 | ||
f17407a8 | 366 | printk(KERN_INFO "%s smsusb_start_streaming(...).\n", __func__); |
2e5c1ec8 | 367 | rc = smsusb_start_streaming(dev); |
82237416 MK |
368 | if (rc < 0) { |
369 | printk(KERN_INFO "%s smsusb_start_streaming(...) failed\n", | |
370 | __func__); | |
2e5c1ec8 MK |
371 | smsusb_term_device(intf); |
372 | return rc; | |
373 | } | |
374 | ||
375 | rc = smscore_start_device(dev->coredev); | |
82237416 MK |
376 | if (rc < 0) { |
377 | printk(KERN_INFO "%s smscore_start_device(...) failed\n", | |
378 | __func__); | |
2e5c1ec8 MK |
379 | smsusb_term_device(intf); |
380 | return rc; | |
381 | } | |
382 | ||
18658117 | 383 | printk(KERN_INFO "%s device %p created\n", __func__, dev); |
2e5c1ec8 MK |
384 | |
385 | return rc; | |
386 | } | |
387 | ||
388 | int smsusb_probe(struct usb_interface *intf, const struct usb_device_id *id) | |
389 | { | |
390 | struct usb_device *udev = interface_to_usbdev(intf); | |
391 | char devpath[32]; | |
392 | int i, rc; | |
393 | ||
f17407a8 MK |
394 | rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x81)); |
395 | rc = usb_clear_halt(udev, usb_rcvbulkpipe(udev, 0x02)); | |
396 | ||
82237416 | 397 | if (intf->num_altsetting > 0) { |
59bf6b8e MK |
398 | rc = usb_set_interface( |
399 | udev, intf->cur_altsetting->desc.bInterfaceNumber, 0); | |
82237416 MK |
400 | if (rc < 0) { |
401 | printk(KERN_INFO "%s usb_set_interface failed, " | |
402 | "rc %d\n", __func__, rc); | |
2e5c1ec8 MK |
403 | return rc; |
404 | } | |
405 | } | |
406 | ||
82237416 MK |
407 | printk(KERN_INFO "smsusb_probe %d\n", |
408 | intf->cur_altsetting->desc.bInterfaceNumber); | |
fa830e8a | 409 | for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) |
82237416 MK |
410 | printk(KERN_INFO "endpoint %d %02x %02x %d\n", i, |
411 | intf->cur_altsetting->endpoint[i].desc.bEndpointAddress, | |
412 | intf->cur_altsetting->endpoint[i].desc.bmAttributes, | |
413 | intf->cur_altsetting->endpoint[i].desc.wMaxPacketSize); | |
2e5c1ec8 | 414 | |
82237416 MK |
415 | if ((udev->actconfig->desc.bNumInterfaces == 2) && |
416 | (intf->cur_altsetting->desc.bInterfaceNumber == 0)) { | |
2e5c1ec8 MK |
417 | printk(KERN_INFO "rom interface 0 is not used\n"); |
418 | return -ENODEV; | |
419 | } | |
420 | ||
82237416 MK |
421 | if (intf->cur_altsetting->desc.bInterfaceNumber == 1) { |
422 | snprintf(devpath, sizeof(devpath), "usb\\%d-%s", | |
423 | udev->bus->busnum, udev->devpath); | |
f17407a8 | 424 | printk(KERN_INFO "stellar device was found.\n"); |
59bf6b8e MK |
425 | return smsusb1_load_firmware( |
426 | udev, smscore_registry_getmode(devpath)); | |
2e5c1ec8 MK |
427 | } |
428 | ||
f17407a8 | 429 | rc = smsusb_init_device(intf); |
494d24c5 | 430 | printk(KERN_INFO "%s rc %d\n", __func__, rc); |
f17407a8 | 431 | return rc; |
2e5c1ec8 MK |
432 | } |
433 | ||
434 | void smsusb_disconnect(struct usb_interface *intf) | |
435 | { | |
436 | smsusb_term_device(intf); | |
437 | } | |
438 | ||
7d18d2e9 | 439 | static struct usb_device_id smsusb_id_table [] = { |
73104fb3 MK |
440 | { USB_DEVICE(USB_VID_SIANO, 0x0010) }, |
441 | { USB_DEVICE(USB_VID_SIANO, USB_PID_STELLAR) }, | |
442 | { USB_DEVICE(USB_VID_SIANO, USB_PID_NOVA_A) }, | |
7d18d2e9 ST |
443 | { } /* Terminating entry */ |
444 | }; | |
fa830e8a | 445 | MODULE_DEVICE_TABLE(usb, smsusb_id_table); |
7d18d2e9 | 446 | |
2e5c1ec8 MK |
447 | static struct usb_driver smsusb_driver = { |
448 | .name = "smsusb", | |
449 | .probe = smsusb_probe, | |
82237416 | 450 | .disconnect = smsusb_disconnect, |
2e5c1ec8 MK |
451 | .id_table = smsusb_id_table, |
452 | }; | |
453 | ||
eae55660 | 454 | int smsusb_register(void) |
2e5c1ec8 MK |
455 | { |
456 | int rc = usb_register(&smsusb_driver); | |
457 | if (rc) | |
458 | printk(KERN_INFO "usb_register failed. Error number %d\n", rc); | |
459 | ||
18658117 | 460 | printk(KERN_INFO "%s\n", __func__); |
2e5c1ec8 MK |
461 | |
462 | return rc; | |
463 | } | |
464 | ||
eae55660 | 465 | void smsusb_unregister(void) |
2e5c1ec8 | 466 | { |
3dd24378 | 467 | /* Regular USB Cleanup */ |
2e5c1ec8 | 468 | usb_deregister(&smsusb_driver); |
18658117 | 469 | printk(KERN_INFO "%s\n", __func__); |
2e5c1ec8 MK |
470 | } |
471 |