Lines Matching refs:s

160 static int airspy_ctrl_msg(struct airspy *s, u8 request, u16 value, u16 index,  in airspy_ctrl_msg()  argument
170 pipe = usb_sndctrlpipe(s->udev, 0); in airspy_ctrl_msg()
181 pipe = usb_rcvctrlpipe(s->udev, 0); in airspy_ctrl_msg()
185 dev_err(s->dev, "Unknown command %02x\n", request); in airspy_ctrl_msg()
192 memcpy(s->buf, data, size); in airspy_ctrl_msg()
194 ret = usb_control_msg(s->udev, pipe, request, requesttype, value, in airspy_ctrl_msg()
195 index, s->buf, size, 1000); in airspy_ctrl_msg()
196 airspy_dbg_usb_control_msg(s->dev, request, requesttype, value, in airspy_ctrl_msg()
197 index, s->buf, size); in airspy_ctrl_msg()
199 dev_err(s->dev, "usb_control_msg() failed %d request %02x\n", in airspy_ctrl_msg()
206 memcpy(data, s->buf, size); in airspy_ctrl_msg()
214 static struct airspy_frame_buf *airspy_get_next_fill_buf(struct airspy *s) in airspy_get_next_fill_buf() argument
219 spin_lock_irqsave(&s->queued_bufs_lock, flags); in airspy_get_next_fill_buf()
220 if (list_empty(&s->queued_bufs)) in airspy_get_next_fill_buf()
223 buf = list_entry(s->queued_bufs.next, in airspy_get_next_fill_buf()
227 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); in airspy_get_next_fill_buf()
231 static unsigned int airspy_convert_stream(struct airspy *s, in airspy_convert_stream() argument
236 if (s->pixelformat == V4L2_SDR_FMT_RU12LE) { in airspy_convert_stream()
244 if (unlikely(time_is_before_jiffies(s->jiffies_next))) { in airspy_convert_stream()
247 s->jiffies_next + msecs_to_jiffies(MSECS)); in airspy_convert_stream()
248 unsigned int samples = s->sample - s->sample_measured; in airspy_convert_stream()
250 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS); in airspy_convert_stream()
251 s->sample_measured = s->sample; in airspy_convert_stream()
252 dev_dbg(s->dev, "slen=%u samples=%u msecs=%u sample rate=%lu\n", in airspy_convert_stream()
258 s->sample += src_len / 2; in airspy_convert_stream()
269 struct airspy *s = urb->context; in airspy_urb_complete() local
272 dev_dbg_ratelimited(s->dev, "status=%d length=%d/%d errors=%d\n", in airspy_urb_complete()
285 dev_err_ratelimited(s->dev, "URB failed %d\n", urb->status); in airspy_urb_complete()
293 fbuf = airspy_get_next_fill_buf(s); in airspy_urb_complete()
295 s->vb_full++; in airspy_urb_complete()
296 dev_notice_ratelimited(s->dev, in airspy_urb_complete()
298 s->vb_full); in airspy_urb_complete()
304 len = airspy_convert_stream(s, ptr, urb->transfer_buffer, in airspy_urb_complete()
308 fbuf->vb.sequence = s->sequence++; in airspy_urb_complete()
315 static int airspy_kill_urbs(struct airspy *s) in airspy_kill_urbs() argument
319 for (i = s->urbs_submitted - 1; i >= 0; i--) { in airspy_kill_urbs()
320 dev_dbg(s->dev, "kill urb=%d\n", i); in airspy_kill_urbs()
322 usb_kill_urb(s->urb_list[i]); in airspy_kill_urbs()
324 s->urbs_submitted = 0; in airspy_kill_urbs()
329 static int airspy_submit_urbs(struct airspy *s) in airspy_submit_urbs() argument
333 for (i = 0; i < s->urbs_initialized; i++) { in airspy_submit_urbs()
334 dev_dbg(s->dev, "submit urb=%d\n", i); in airspy_submit_urbs()
335 ret = usb_submit_urb(s->urb_list[i], GFP_ATOMIC); in airspy_submit_urbs()
337 dev_err(s->dev, "Could not submit URB no. %d - get them all back\n", in airspy_submit_urbs()
339 airspy_kill_urbs(s); in airspy_submit_urbs()
342 s->urbs_submitted++; in airspy_submit_urbs()
348 static int airspy_free_stream_bufs(struct airspy *s) in airspy_free_stream_bufs() argument
350 if (test_bit(USB_STATE_URB_BUF, &s->flags)) { in airspy_free_stream_bufs()
351 while (s->buf_num) { in airspy_free_stream_bufs()
352 s->buf_num--; in airspy_free_stream_bufs()
353 dev_dbg(s->dev, "free buf=%d\n", s->buf_num); in airspy_free_stream_bufs()
354 usb_free_coherent(s->udev, s->buf_size, in airspy_free_stream_bufs()
355 s->buf_list[s->buf_num], in airspy_free_stream_bufs()
356 s->dma_addr[s->buf_num]); in airspy_free_stream_bufs()
359 clear_bit(USB_STATE_URB_BUF, &s->flags); in airspy_free_stream_bufs()
364 static int airspy_alloc_stream_bufs(struct airspy *s) in airspy_alloc_stream_bufs() argument
366 s->buf_num = 0; in airspy_alloc_stream_bufs()
367 s->buf_size = BULK_BUFFER_SIZE; in airspy_alloc_stream_bufs()
369 dev_dbg(s->dev, "all in all I will use %u bytes for streaming\n", in airspy_alloc_stream_bufs()
372 for (s->buf_num = 0; s->buf_num < MAX_BULK_BUFS; s->buf_num++) { in airspy_alloc_stream_bufs()
373 s->buf_list[s->buf_num] = usb_alloc_coherent(s->udev, in airspy_alloc_stream_bufs()
375 &s->dma_addr[s->buf_num]); in airspy_alloc_stream_bufs()
376 if (!s->buf_list[s->buf_num]) { in airspy_alloc_stream_bufs()
377 dev_dbg(s->dev, "alloc buf=%d failed\n", s->buf_num); in airspy_alloc_stream_bufs()
378 airspy_free_stream_bufs(s); in airspy_alloc_stream_bufs()
382 dev_dbg(s->dev, "alloc buf=%d %p (dma %llu)\n", s->buf_num, in airspy_alloc_stream_bufs()
383 s->buf_list[s->buf_num], in airspy_alloc_stream_bufs()
384 (long long)s->dma_addr[s->buf_num]); in airspy_alloc_stream_bufs()
385 set_bit(USB_STATE_URB_BUF, &s->flags); in airspy_alloc_stream_bufs()
391 static int airspy_free_urbs(struct airspy *s) in airspy_free_urbs() argument
395 airspy_kill_urbs(s); in airspy_free_urbs()
397 for (i = s->urbs_initialized - 1; i >= 0; i--) { in airspy_free_urbs()
398 if (s->urb_list[i]) { in airspy_free_urbs()
399 dev_dbg(s->dev, "free urb=%d\n", i); in airspy_free_urbs()
401 usb_free_urb(s->urb_list[i]); in airspy_free_urbs()
404 s->urbs_initialized = 0; in airspy_free_urbs()
409 static int airspy_alloc_urbs(struct airspy *s) in airspy_alloc_urbs() argument
415 dev_dbg(s->dev, "alloc urb=%d\n", i); in airspy_alloc_urbs()
416 s->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC); in airspy_alloc_urbs()
417 if (!s->urb_list[i]) { in airspy_alloc_urbs()
419 usb_free_urb(s->urb_list[j]); in airspy_alloc_urbs()
420 s->urb_list[j] = NULL; in airspy_alloc_urbs()
422 s->urbs_initialized = 0; in airspy_alloc_urbs()
425 usb_fill_bulk_urb(s->urb_list[i], in airspy_alloc_urbs()
426 s->udev, in airspy_alloc_urbs()
427 usb_rcvbulkpipe(s->udev, 0x81), in airspy_alloc_urbs()
428 s->buf_list[i], in airspy_alloc_urbs()
430 airspy_urb_complete, s); in airspy_alloc_urbs()
432 s->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; in airspy_alloc_urbs()
433 s->urb_list[i]->transfer_dma = s->dma_addr[i]; in airspy_alloc_urbs()
434 s->urbs_initialized++; in airspy_alloc_urbs()
441 static void airspy_cleanup_queued_bufs(struct airspy *s) in airspy_cleanup_queued_bufs() argument
445 dev_dbg(s->dev, "\n"); in airspy_cleanup_queued_bufs()
447 spin_lock_irqsave(&s->queued_bufs_lock, flags); in airspy_cleanup_queued_bufs()
448 while (!list_empty(&s->queued_bufs)) { in airspy_cleanup_queued_bufs()
451 buf = list_entry(s->queued_bufs.next, in airspy_cleanup_queued_bufs()
456 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); in airspy_cleanup_queued_bufs()
463 struct airspy *s = container_of(v, struct airspy, v4l2_dev); in airspy_disconnect() local
465 dev_dbg(s->dev, "\n"); in airspy_disconnect()
467 mutex_lock(&s->vb_queue_lock); in airspy_disconnect()
468 mutex_lock(&s->v4l2_lock); in airspy_disconnect()
470 s->udev = NULL; in airspy_disconnect()
471 v4l2_device_disconnect(&s->v4l2_dev); in airspy_disconnect()
472 video_unregister_device(&s->vdev); in airspy_disconnect()
473 mutex_unlock(&s->v4l2_lock); in airspy_disconnect()
474 mutex_unlock(&s->vb_queue_lock); in airspy_disconnect()
476 v4l2_device_put(&s->v4l2_dev); in airspy_disconnect()
484 struct airspy *s = vb2_get_drv_priv(vq); in airspy_queue_setup() local
486 dev_dbg(s->dev, "nbuffers=%d\n", *nbuffers); in airspy_queue_setup()
492 sizes[0] = PAGE_ALIGN(s->buffersize); in airspy_queue_setup()
494 dev_dbg(s->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]); in airspy_queue_setup()
501 struct airspy *s = vb2_get_drv_priv(vb->vb2_queue); in airspy_buf_queue() local
507 if (unlikely(!s->udev)) { in airspy_buf_queue()
512 spin_lock_irqsave(&s->queued_bufs_lock, flags); in airspy_buf_queue()
513 list_add_tail(&buf->list, &s->queued_bufs); in airspy_buf_queue()
514 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); in airspy_buf_queue()
519 struct airspy *s = vb2_get_drv_priv(vq); in airspy_start_streaming() local
522 dev_dbg(s->dev, "\n"); in airspy_start_streaming()
524 if (!s->udev) in airspy_start_streaming()
527 mutex_lock(&s->v4l2_lock); in airspy_start_streaming()
529 s->sequence = 0; in airspy_start_streaming()
531 set_bit(POWER_ON, &s->flags); in airspy_start_streaming()
533 ret = airspy_alloc_stream_bufs(s); in airspy_start_streaming()
537 ret = airspy_alloc_urbs(s); in airspy_start_streaming()
541 ret = airspy_submit_urbs(s); in airspy_start_streaming()
546 ret = airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 1, 0, NULL, 0); in airspy_start_streaming()
553 airspy_kill_urbs(s); in airspy_start_streaming()
555 airspy_free_urbs(s); in airspy_start_streaming()
557 airspy_free_stream_bufs(s); in airspy_start_streaming()
559 clear_bit(POWER_ON, &s->flags); in airspy_start_streaming()
565 list_for_each_entry_safe(buf, tmp, &s->queued_bufs, list) { in airspy_start_streaming()
573 mutex_unlock(&s->v4l2_lock); in airspy_start_streaming()
580 struct airspy *s = vb2_get_drv_priv(vq); in airspy_stop_streaming() local
582 dev_dbg(s->dev, "\n"); in airspy_stop_streaming()
584 mutex_lock(&s->v4l2_lock); in airspy_stop_streaming()
587 airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 0, 0, NULL, 0); in airspy_stop_streaming()
589 airspy_kill_urbs(s); in airspy_stop_streaming()
590 airspy_free_urbs(s); in airspy_stop_streaming()
591 airspy_free_stream_bufs(s); in airspy_stop_streaming()
593 airspy_cleanup_queued_bufs(s); in airspy_stop_streaming()
595 clear_bit(POWER_ON, &s->flags); in airspy_stop_streaming()
597 mutex_unlock(&s->v4l2_lock); in airspy_stop_streaming()
612 struct airspy *s = video_drvdata(file); in airspy_querycap() local
615 strscpy(cap->card, s->vdev.name, sizeof(cap->card)); in airspy_querycap()
616 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info)); in airspy_querycap()
634 struct airspy *s = video_drvdata(file); in airspy_g_fmt_sdr_cap() local
636 f->fmt.sdr.pixelformat = s->pixelformat; in airspy_g_fmt_sdr_cap()
637 f->fmt.sdr.buffersize = s->buffersize; in airspy_g_fmt_sdr_cap()
645 struct airspy *s = video_drvdata(file); in airspy_s_fmt_sdr_cap() local
646 struct vb2_queue *q = &s->vb_queue; in airspy_s_fmt_sdr_cap()
654 s->pixelformat = formats[i].pixelformat; in airspy_s_fmt_sdr_cap()
655 s->buffersize = formats[i].buffersize; in airspy_s_fmt_sdr_cap()
661 s->pixelformat = formats[0].pixelformat; in airspy_s_fmt_sdr_cap()
662 s->buffersize = formats[0].buffersize; in airspy_s_fmt_sdr_cap()
730 struct airspy *s = video_drvdata(file); in airspy_g_frequency() local
735 f->frequency = s->f_adc; in airspy_g_frequency()
736 dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc); in airspy_g_frequency()
740 f->frequency = s->f_rf; in airspy_g_frequency()
741 dev_dbg(s->dev, "RF frequency=%u Hz\n", s->f_rf); in airspy_g_frequency()
753 struct airspy *s = video_drvdata(file); in airspy_s_frequency() local
758 s->f_adc = clamp_t(unsigned int, f->frequency, in airspy_s_frequency()
761 dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc); in airspy_s_frequency()
764 s->f_rf = clamp_t(unsigned int, f->frequency, in airspy_s_frequency()
767 dev_dbg(s->dev, "RF frequency=%u Hz\n", s->f_rf); in airspy_s_frequency()
768 buf[0] = (s->f_rf >> 0) & 0xff; in airspy_s_frequency()
769 buf[1] = (s->f_rf >> 8) & 0xff; in airspy_s_frequency()
770 buf[2] = (s->f_rf >> 16) & 0xff; in airspy_s_frequency()
771 buf[3] = (s->f_rf >> 24) & 0xff; in airspy_s_frequency()
772 ret = airspy_ctrl_msg(s, CMD_SET_FREQ, 0, 0, buf, 4); in airspy_s_frequency()
855 struct airspy *s = container_of(v, struct airspy, v4l2_dev); in airspy_video_release() local
857 v4l2_ctrl_handler_free(&s->hdl); in airspy_video_release()
858 v4l2_device_unregister(&s->v4l2_dev); in airspy_video_release()
859 kfree(s->buf); in airspy_video_release()
860 kfree(s); in airspy_video_release()
863 static int airspy_set_lna_gain(struct airspy *s) in airspy_set_lna_gain() argument
868 dev_dbg(s->dev, "lna auto=%d->%d val=%d->%d\n", in airspy_set_lna_gain()
869 s->lna_gain_auto->cur.val, s->lna_gain_auto->val, in airspy_set_lna_gain()
870 s->lna_gain->cur.val, s->lna_gain->val); in airspy_set_lna_gain()
872 ret = airspy_ctrl_msg(s, CMD_SET_LNA_AGC, 0, s->lna_gain_auto->val, in airspy_set_lna_gain()
877 if (s->lna_gain_auto->val == false) { in airspy_set_lna_gain()
878 ret = airspy_ctrl_msg(s, CMD_SET_LNA_GAIN, 0, s->lna_gain->val, in airspy_set_lna_gain()
885 dev_dbg(s->dev, "failed=%d\n", ret); in airspy_set_lna_gain()
890 static int airspy_set_mixer_gain(struct airspy *s) in airspy_set_mixer_gain() argument
895 dev_dbg(s->dev, "mixer auto=%d->%d val=%d->%d\n", in airspy_set_mixer_gain()
896 s->mixer_gain_auto->cur.val, s->mixer_gain_auto->val, in airspy_set_mixer_gain()
897 s->mixer_gain->cur.val, s->mixer_gain->val); in airspy_set_mixer_gain()
899 ret = airspy_ctrl_msg(s, CMD_SET_MIXER_AGC, 0, s->mixer_gain_auto->val, in airspy_set_mixer_gain()
904 if (s->mixer_gain_auto->val == false) { in airspy_set_mixer_gain()
905 ret = airspy_ctrl_msg(s, CMD_SET_MIXER_GAIN, 0, in airspy_set_mixer_gain()
906 s->mixer_gain->val, &u8tmp, 1); in airspy_set_mixer_gain()
912 dev_dbg(s->dev, "failed=%d\n", ret); in airspy_set_mixer_gain()
917 static int airspy_set_if_gain(struct airspy *s) in airspy_set_if_gain() argument
922 dev_dbg(s->dev, "val=%d->%d\n", s->if_gain->cur.val, s->if_gain->val); in airspy_set_if_gain()
924 ret = airspy_ctrl_msg(s, CMD_SET_VGA_GAIN, 0, s->if_gain->val, in airspy_set_if_gain()
927 dev_dbg(s->dev, "failed=%d\n", ret); in airspy_set_if_gain()
934 struct airspy *s = container_of(ctrl->handler, struct airspy, hdl); in airspy_s_ctrl() local
940 ret = airspy_set_lna_gain(s); in airspy_s_ctrl()
944 ret = airspy_set_mixer_gain(s); in airspy_s_ctrl()
947 ret = airspy_set_if_gain(s); in airspy_s_ctrl()
950 dev_dbg(s->dev, "unknown ctrl: id=%d name=%s\n", in airspy_s_ctrl()
965 struct airspy *s; in airspy_probe() local
972 s = kzalloc(sizeof(struct airspy), GFP_KERNEL); in airspy_probe()
973 if (s == NULL) { in airspy_probe()
978 s->buf = kzalloc(BUF_SIZE, GFP_KERNEL); in airspy_probe()
979 if (!s->buf) in airspy_probe()
985 mutex_init(&s->v4l2_lock); in airspy_probe()
986 mutex_init(&s->vb_queue_lock); in airspy_probe()
987 spin_lock_init(&s->queued_bufs_lock); in airspy_probe()
988 INIT_LIST_HEAD(&s->queued_bufs); in airspy_probe()
989 s->dev = &intf->dev; in airspy_probe()
990 s->udev = interface_to_usbdev(intf); in airspy_probe()
991 s->f_adc = bands[0].rangelow; in airspy_probe()
992 s->f_rf = bands_rf[0].rangelow; in airspy_probe()
993 s->pixelformat = formats[0].pixelformat; in airspy_probe()
994 s->buffersize = formats[0].buffersize; in airspy_probe()
997 ret = airspy_ctrl_msg(s, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1); in airspy_probe()
999 ret = airspy_ctrl_msg(s, CMD_VERSION_STRING_READ, 0, 0, in airspy_probe()
1002 dev_err(s->dev, "Could not detect board\n"); in airspy_probe()
1008 dev_info(s->dev, "Board ID: %02x\n", u8tmp); in airspy_probe()
1009 dev_info(s->dev, "Firmware version: %s\n", buf); in airspy_probe()
1012 s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; in airspy_probe()
1013 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; in airspy_probe()
1014 s->vb_queue.drv_priv = s; in airspy_probe()
1015 s->vb_queue.buf_struct_size = sizeof(struct airspy_frame_buf); in airspy_probe()
1016 s->vb_queue.ops = &airspy_vb2_ops; in airspy_probe()
1017 s->vb_queue.mem_ops = &vb2_vmalloc_memops; in airspy_probe()
1018 s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; in airspy_probe()
1019 ret = vb2_queue_init(&s->vb_queue); in airspy_probe()
1021 dev_err(s->dev, "Could not initialize vb2 queue\n"); in airspy_probe()
1026 s->vdev = airspy_template; in airspy_probe()
1027 s->vdev.queue = &s->vb_queue; in airspy_probe()
1028 s->vdev.queue->lock = &s->vb_queue_lock; in airspy_probe()
1029 video_set_drvdata(&s->vdev, s); in airspy_probe()
1032 s->v4l2_dev.release = airspy_video_release; in airspy_probe()
1033 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev); in airspy_probe()
1035 dev_err(s->dev, "Failed to register v4l2-device (%d)\n", ret); in airspy_probe()
1040 v4l2_ctrl_handler_init(&s->hdl, 5); in airspy_probe()
1041 s->lna_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1043 s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1045 v4l2_ctrl_auto_cluster(2, &s->lna_gain_auto, 0, false); in airspy_probe()
1046 s->mixer_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1048 s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1050 v4l2_ctrl_auto_cluster(2, &s->mixer_gain_auto, 0, false); in airspy_probe()
1051 s->if_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1053 if (s->hdl.error) { in airspy_probe()
1054 ret = s->hdl.error; in airspy_probe()
1055 dev_err(s->dev, "Could not initialize controls\n"); in airspy_probe()
1059 v4l2_ctrl_handler_setup(&s->hdl); in airspy_probe()
1061 s->v4l2_dev.ctrl_handler = &s->hdl; in airspy_probe()
1062 s->vdev.v4l2_dev = &s->v4l2_dev; in airspy_probe()
1063 s->vdev.lock = &s->v4l2_lock; in airspy_probe()
1064 s->vdev.device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING | in airspy_probe()
1067 ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1); in airspy_probe()
1069 dev_err(s->dev, "Failed to register as video device (%d)\n", in airspy_probe()
1077 dev_info(s->dev, "Registered as %s\n", in airspy_probe()
1078 video_device_node_name(&s->vdev)); in airspy_probe()
1079 …dev_notice(s->dev, "SDR API is still slightly experimental and functionality changes may follow\n"… in airspy_probe()
1083 v4l2_ctrl_handler_free(&s->hdl); in airspy_probe()
1084 v4l2_device_unregister(&s->v4l2_dev); in airspy_probe()
1087 kfree(s->buf); in airspy_probe()
1088 kfree(s); in airspy_probe()