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