Lines Matching refs:s

170 static int airspy_ctrl_msg(struct airspy *s, u8 request, u16 value, u16 index,  in airspy_ctrl_msg()  argument
180 pipe = usb_sndctrlpipe(s->udev, 0); in airspy_ctrl_msg()
191 pipe = usb_rcvctrlpipe(s->udev, 0); in airspy_ctrl_msg()
195 dev_err(s->dev, "Unknown command %02x\n", request); in airspy_ctrl_msg()
202 memcpy(s->buf, data, size); in airspy_ctrl_msg()
204 ret = usb_control_msg(s->udev, pipe, request, requesttype, value, in airspy_ctrl_msg()
205 index, s->buf, size, 1000); in airspy_ctrl_msg()
206 airspy_dbg_usb_control_msg(s->dev, request, requesttype, value, in airspy_ctrl_msg()
207 index, s->buf, size); in airspy_ctrl_msg()
209 dev_err(s->dev, "usb_control_msg() failed %d request %02x\n", in airspy_ctrl_msg()
216 memcpy(data, s->buf, size); in airspy_ctrl_msg()
224 static struct airspy_frame_buf *airspy_get_next_fill_buf(struct airspy *s) in airspy_get_next_fill_buf() argument
229 spin_lock_irqsave(&s->queued_bufs_lock, flags); in airspy_get_next_fill_buf()
230 if (list_empty(&s->queued_bufs)) in airspy_get_next_fill_buf()
233 buf = list_entry(s->queued_bufs.next, in airspy_get_next_fill_buf()
237 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); in airspy_get_next_fill_buf()
241 static unsigned int airspy_convert_stream(struct airspy *s, in airspy_convert_stream() argument
246 if (s->pixelformat == V4L2_SDR_FMT_RU12LE) { in airspy_convert_stream()
254 if (unlikely(time_is_before_jiffies(s->jiffies_next))) { in airspy_convert_stream()
257 s->jiffies_next + msecs_to_jiffies(MSECS)); in airspy_convert_stream()
258 unsigned int samples = s->sample - s->sample_measured; in airspy_convert_stream()
260 s->jiffies_next = jiffies + msecs_to_jiffies(MSECS); in airspy_convert_stream()
261 s->sample_measured = s->sample; in airspy_convert_stream()
262 dev_dbg(s->dev, "slen=%u samples=%u msecs=%u sample rate=%lu\n", in airspy_convert_stream()
268 s->sample += src_len / 2; in airspy_convert_stream()
279 struct airspy *s = urb->context; in airspy_urb_complete() local
282 dev_dbg_ratelimited(s->dev, "status=%d length=%d/%d errors=%d\n", in airspy_urb_complete()
295 dev_err_ratelimited(s->dev, "URB failed %d\n", urb->status); in airspy_urb_complete()
303 fbuf = airspy_get_next_fill_buf(s); in airspy_urb_complete()
305 s->vb_full++; in airspy_urb_complete()
306 dev_notice_ratelimited(s->dev, in airspy_urb_complete()
308 s->vb_full); in airspy_urb_complete()
314 len = airspy_convert_stream(s, ptr, urb->transfer_buffer, in airspy_urb_complete()
318 fbuf->vb.v4l2_buf.sequence = s->sequence++; in airspy_urb_complete()
325 static int airspy_kill_urbs(struct airspy *s) in airspy_kill_urbs() argument
329 for (i = s->urbs_submitted - 1; i >= 0; i--) { in airspy_kill_urbs()
330 dev_dbg(s->dev, "kill urb=%d\n", i); in airspy_kill_urbs()
332 usb_kill_urb(s->urb_list[i]); in airspy_kill_urbs()
334 s->urbs_submitted = 0; in airspy_kill_urbs()
339 static int airspy_submit_urbs(struct airspy *s) in airspy_submit_urbs() argument
343 for (i = 0; i < s->urbs_initialized; i++) { in airspy_submit_urbs()
344 dev_dbg(s->dev, "submit urb=%d\n", i); in airspy_submit_urbs()
345 ret = usb_submit_urb(s->urb_list[i], GFP_ATOMIC); in airspy_submit_urbs()
347 dev_err(s->dev, "Could not submit URB no. %d - get them all back\n", in airspy_submit_urbs()
349 airspy_kill_urbs(s); in airspy_submit_urbs()
352 s->urbs_submitted++; in airspy_submit_urbs()
358 static int airspy_free_stream_bufs(struct airspy *s) in airspy_free_stream_bufs() argument
360 if (s->flags & USB_STATE_URB_BUF) { in airspy_free_stream_bufs()
361 while (s->buf_num) { in airspy_free_stream_bufs()
362 s->buf_num--; in airspy_free_stream_bufs()
363 dev_dbg(s->dev, "free buf=%d\n", s->buf_num); in airspy_free_stream_bufs()
364 usb_free_coherent(s->udev, s->buf_size, in airspy_free_stream_bufs()
365 s->buf_list[s->buf_num], in airspy_free_stream_bufs()
366 s->dma_addr[s->buf_num]); in airspy_free_stream_bufs()
369 s->flags &= ~USB_STATE_URB_BUF; in airspy_free_stream_bufs()
374 static int airspy_alloc_stream_bufs(struct airspy *s) in airspy_alloc_stream_bufs() argument
376 s->buf_num = 0; in airspy_alloc_stream_bufs()
377 s->buf_size = BULK_BUFFER_SIZE; in airspy_alloc_stream_bufs()
379 dev_dbg(s->dev, "all in all I will use %u bytes for streaming\n", in airspy_alloc_stream_bufs()
382 for (s->buf_num = 0; s->buf_num < MAX_BULK_BUFS; s->buf_num++) { in airspy_alloc_stream_bufs()
383 s->buf_list[s->buf_num] = usb_alloc_coherent(s->udev, in airspy_alloc_stream_bufs()
385 &s->dma_addr[s->buf_num]); in airspy_alloc_stream_bufs()
386 if (!s->buf_list[s->buf_num]) { in airspy_alloc_stream_bufs()
387 dev_dbg(s->dev, "alloc buf=%d failed\n", s->buf_num); in airspy_alloc_stream_bufs()
388 airspy_free_stream_bufs(s); in airspy_alloc_stream_bufs()
392 dev_dbg(s->dev, "alloc buf=%d %p (dma %llu)\n", s->buf_num, in airspy_alloc_stream_bufs()
393 s->buf_list[s->buf_num], in airspy_alloc_stream_bufs()
394 (long long)s->dma_addr[s->buf_num]); in airspy_alloc_stream_bufs()
395 s->flags |= USB_STATE_URB_BUF; in airspy_alloc_stream_bufs()
401 static int airspy_free_urbs(struct airspy *s) in airspy_free_urbs() argument
405 airspy_kill_urbs(s); in airspy_free_urbs()
407 for (i = s->urbs_initialized - 1; i >= 0; i--) { in airspy_free_urbs()
408 if (s->urb_list[i]) { in airspy_free_urbs()
409 dev_dbg(s->dev, "free urb=%d\n", i); in airspy_free_urbs()
411 usb_free_urb(s->urb_list[i]); in airspy_free_urbs()
414 s->urbs_initialized = 0; in airspy_free_urbs()
419 static int airspy_alloc_urbs(struct airspy *s) in airspy_alloc_urbs() argument
425 dev_dbg(s->dev, "alloc urb=%d\n", i); in airspy_alloc_urbs()
426 s->urb_list[i] = usb_alloc_urb(0, GFP_ATOMIC); in airspy_alloc_urbs()
427 if (!s->urb_list[i]) { in airspy_alloc_urbs()
428 dev_dbg(s->dev, "failed\n"); in airspy_alloc_urbs()
430 usb_free_urb(s->urb_list[j]); in airspy_alloc_urbs()
433 usb_fill_bulk_urb(s->urb_list[i], in airspy_alloc_urbs()
434 s->udev, in airspy_alloc_urbs()
435 usb_rcvbulkpipe(s->udev, 0x81), in airspy_alloc_urbs()
436 s->buf_list[i], in airspy_alloc_urbs()
438 airspy_urb_complete, s); in airspy_alloc_urbs()
440 s->urb_list[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; in airspy_alloc_urbs()
441 s->urb_list[i]->transfer_dma = s->dma_addr[i]; in airspy_alloc_urbs()
442 s->urbs_initialized++; in airspy_alloc_urbs()
449 static void airspy_cleanup_queued_bufs(struct airspy *s) in airspy_cleanup_queued_bufs() argument
453 dev_dbg(s->dev, "\n"); in airspy_cleanup_queued_bufs()
455 spin_lock_irqsave(&s->queued_bufs_lock, flags); in airspy_cleanup_queued_bufs()
456 while (!list_empty(&s->queued_bufs)) { in airspy_cleanup_queued_bufs()
459 buf = list_entry(s->queued_bufs.next, in airspy_cleanup_queued_bufs()
464 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); in airspy_cleanup_queued_bufs()
471 struct airspy *s = container_of(v, struct airspy, v4l2_dev); in airspy_disconnect() local
473 dev_dbg(s->dev, "\n"); in airspy_disconnect()
475 mutex_lock(&s->vb_queue_lock); in airspy_disconnect()
476 mutex_lock(&s->v4l2_lock); in airspy_disconnect()
478 s->udev = NULL; in airspy_disconnect()
479 v4l2_device_disconnect(&s->v4l2_dev); in airspy_disconnect()
480 video_unregister_device(&s->vdev); in airspy_disconnect()
481 mutex_unlock(&s->v4l2_lock); in airspy_disconnect()
482 mutex_unlock(&s->vb_queue_lock); in airspy_disconnect()
484 v4l2_device_put(&s->v4l2_dev); in airspy_disconnect()
492 struct airspy *s = vb2_get_drv_priv(vq); in airspy_queue_setup() local
494 dev_dbg(s->dev, "nbuffers=%d\n", *nbuffers); in airspy_queue_setup()
500 sizes[0] = PAGE_ALIGN(s->buffersize); in airspy_queue_setup()
502 dev_dbg(s->dev, "nbuffers=%d sizes[0]=%d\n", *nbuffers, sizes[0]); in airspy_queue_setup()
508 struct airspy *s = vb2_get_drv_priv(vb->vb2_queue); in airspy_buf_queue() local
514 if (unlikely(!s->udev)) { in airspy_buf_queue()
519 spin_lock_irqsave(&s->queued_bufs_lock, flags); in airspy_buf_queue()
520 list_add_tail(&buf->list, &s->queued_bufs); in airspy_buf_queue()
521 spin_unlock_irqrestore(&s->queued_bufs_lock, flags); in airspy_buf_queue()
526 struct airspy *s = vb2_get_drv_priv(vq); in airspy_start_streaming() local
529 dev_dbg(s->dev, "\n"); in airspy_start_streaming()
531 if (!s->udev) in airspy_start_streaming()
534 mutex_lock(&s->v4l2_lock); in airspy_start_streaming()
536 s->sequence = 0; in airspy_start_streaming()
538 set_bit(POWER_ON, &s->flags); in airspy_start_streaming()
540 ret = airspy_alloc_stream_bufs(s); in airspy_start_streaming()
544 ret = airspy_alloc_urbs(s); in airspy_start_streaming()
548 ret = airspy_submit_urbs(s); in airspy_start_streaming()
553 ret = airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 1, 0, NULL, 0); in airspy_start_streaming()
560 airspy_kill_urbs(s); in airspy_start_streaming()
562 airspy_free_urbs(s); in airspy_start_streaming()
564 airspy_free_stream_bufs(s); in airspy_start_streaming()
566 clear_bit(POWER_ON, &s->flags); in airspy_start_streaming()
572 list_for_each_entry_safe(buf, tmp, &s->queued_bufs, list) { in airspy_start_streaming()
579 mutex_unlock(&s->v4l2_lock); in airspy_start_streaming()
586 struct airspy *s = vb2_get_drv_priv(vq); in airspy_stop_streaming() local
588 dev_dbg(s->dev, "\n"); in airspy_stop_streaming()
590 mutex_lock(&s->v4l2_lock); in airspy_stop_streaming()
593 airspy_ctrl_msg(s, CMD_RECEIVER_MODE, 0, 0, NULL, 0); in airspy_stop_streaming()
595 airspy_kill_urbs(s); in airspy_stop_streaming()
596 airspy_free_urbs(s); in airspy_stop_streaming()
597 airspy_free_stream_bufs(s); in airspy_stop_streaming()
599 airspy_cleanup_queued_bufs(s); in airspy_stop_streaming()
601 clear_bit(POWER_ON, &s->flags); in airspy_stop_streaming()
603 mutex_unlock(&s->v4l2_lock); in airspy_stop_streaming()
618 struct airspy *s = video_drvdata(file); in airspy_querycap() local
621 strlcpy(cap->card, s->vdev.name, sizeof(cap->card)); in airspy_querycap()
622 usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info)); in airspy_querycap()
645 struct airspy *s = video_drvdata(file); in airspy_g_fmt_sdr_cap() local
647 f->fmt.sdr.pixelformat = s->pixelformat; in airspy_g_fmt_sdr_cap()
648 f->fmt.sdr.buffersize = s->buffersize; in airspy_g_fmt_sdr_cap()
657 struct airspy *s = video_drvdata(file); in airspy_s_fmt_sdr_cap() local
658 struct vb2_queue *q = &s->vb_queue; in airspy_s_fmt_sdr_cap()
667 s->pixelformat = formats[i].pixelformat; in airspy_s_fmt_sdr_cap()
668 s->buffersize = formats[i].buffersize; in airspy_s_fmt_sdr_cap()
674 s->pixelformat = formats[0].pixelformat; in airspy_s_fmt_sdr_cap()
675 s->buffersize = formats[0].buffersize; in airspy_s_fmt_sdr_cap()
744 struct airspy *s = video_drvdata(file); in airspy_g_frequency() local
749 f->frequency = s->f_adc; in airspy_g_frequency()
750 dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc); in airspy_g_frequency()
754 f->frequency = s->f_rf; in airspy_g_frequency()
755 dev_dbg(s->dev, "RF frequency=%u Hz\n", s->f_rf); in airspy_g_frequency()
767 struct airspy *s = video_drvdata(file); in airspy_s_frequency() local
772 s->f_adc = clamp_t(unsigned int, f->frequency, in airspy_s_frequency()
775 dev_dbg(s->dev, "ADC frequency=%u Hz\n", s->f_adc); in airspy_s_frequency()
778 s->f_rf = clamp_t(unsigned int, f->frequency, in airspy_s_frequency()
781 dev_dbg(s->dev, "RF frequency=%u Hz\n", s->f_rf); in airspy_s_frequency()
782 buf[0] = (s->f_rf >> 0) & 0xff; in airspy_s_frequency()
783 buf[1] = (s->f_rf >> 8) & 0xff; in airspy_s_frequency()
784 buf[2] = (s->f_rf >> 16) & 0xff; in airspy_s_frequency()
785 buf[3] = (s->f_rf >> 24) & 0xff; in airspy_s_frequency()
786 ret = airspy_ctrl_msg(s, CMD_SET_FREQ, 0, 0, buf, 4); in airspy_s_frequency()
869 struct airspy *s = container_of(v, struct airspy, v4l2_dev); in airspy_video_release() local
871 v4l2_ctrl_handler_free(&s->hdl); in airspy_video_release()
872 v4l2_device_unregister(&s->v4l2_dev); in airspy_video_release()
873 kfree(s); in airspy_video_release()
876 static int airspy_set_lna_gain(struct airspy *s) in airspy_set_lna_gain() argument
881 dev_dbg(s->dev, "lna auto=%d->%d val=%d->%d\n", in airspy_set_lna_gain()
882 s->lna_gain_auto->cur.val, s->lna_gain_auto->val, in airspy_set_lna_gain()
883 s->lna_gain->cur.val, s->lna_gain->val); in airspy_set_lna_gain()
885 ret = airspy_ctrl_msg(s, CMD_SET_LNA_AGC, 0, s->lna_gain_auto->val, in airspy_set_lna_gain()
890 if (s->lna_gain_auto->val == false) { in airspy_set_lna_gain()
891 ret = airspy_ctrl_msg(s, CMD_SET_LNA_GAIN, 0, s->lna_gain->val, in airspy_set_lna_gain()
898 dev_dbg(s->dev, "failed=%d\n", ret); in airspy_set_lna_gain()
903 static int airspy_set_mixer_gain(struct airspy *s) in airspy_set_mixer_gain() argument
908 dev_dbg(s->dev, "mixer auto=%d->%d val=%d->%d\n", in airspy_set_mixer_gain()
909 s->mixer_gain_auto->cur.val, s->mixer_gain_auto->val, in airspy_set_mixer_gain()
910 s->mixer_gain->cur.val, s->mixer_gain->val); in airspy_set_mixer_gain()
912 ret = airspy_ctrl_msg(s, CMD_SET_MIXER_AGC, 0, s->mixer_gain_auto->val, in airspy_set_mixer_gain()
917 if (s->mixer_gain_auto->val == false) { in airspy_set_mixer_gain()
918 ret = airspy_ctrl_msg(s, CMD_SET_MIXER_GAIN, 0, in airspy_set_mixer_gain()
919 s->mixer_gain->val, &u8tmp, 1); in airspy_set_mixer_gain()
925 dev_dbg(s->dev, "failed=%d\n", ret); in airspy_set_mixer_gain()
930 static int airspy_set_if_gain(struct airspy *s) in airspy_set_if_gain() argument
935 dev_dbg(s->dev, "val=%d->%d\n", s->if_gain->cur.val, s->if_gain->val); in airspy_set_if_gain()
937 ret = airspy_ctrl_msg(s, CMD_SET_VGA_GAIN, 0, s->if_gain->val, in airspy_set_if_gain()
943 dev_dbg(s->dev, "failed=%d\n", ret); in airspy_set_if_gain()
950 struct airspy *s = container_of(ctrl->handler, struct airspy, hdl); in airspy_s_ctrl() local
956 ret = airspy_set_lna_gain(s); in airspy_s_ctrl()
960 ret = airspy_set_mixer_gain(s); in airspy_s_ctrl()
963 ret = airspy_set_if_gain(s); in airspy_s_ctrl()
966 dev_dbg(s->dev, "unknown ctrl: id=%d name=%s\n", in airspy_s_ctrl()
981 struct airspy *s; in airspy_probe() local
985 s = kzalloc(sizeof(struct airspy), GFP_KERNEL); in airspy_probe()
986 if (s == NULL) { in airspy_probe()
991 mutex_init(&s->v4l2_lock); in airspy_probe()
992 mutex_init(&s->vb_queue_lock); in airspy_probe()
993 spin_lock_init(&s->queued_bufs_lock); in airspy_probe()
994 INIT_LIST_HEAD(&s->queued_bufs); in airspy_probe()
995 s->dev = &intf->dev; in airspy_probe()
996 s->udev = interface_to_usbdev(intf); in airspy_probe()
997 s->f_adc = bands[0].rangelow; in airspy_probe()
998 s->f_rf = bands_rf[0].rangelow; in airspy_probe()
999 s->pixelformat = formats[0].pixelformat; in airspy_probe()
1000 s->buffersize = formats[0].buffersize; in airspy_probe()
1003 ret = airspy_ctrl_msg(s, CMD_BOARD_ID_READ, 0, 0, &u8tmp, 1); in airspy_probe()
1005 ret = airspy_ctrl_msg(s, CMD_VERSION_STRING_READ, 0, 0, in airspy_probe()
1008 dev_err(s->dev, "Could not detect board\n"); in airspy_probe()
1014 dev_info(s->dev, "Board ID: %02x\n", u8tmp); in airspy_probe()
1015 dev_info(s->dev, "Firmware version: %s\n", buf); in airspy_probe()
1018 s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; in airspy_probe()
1019 s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; in airspy_probe()
1020 s->vb_queue.drv_priv = s; in airspy_probe()
1021 s->vb_queue.buf_struct_size = sizeof(struct airspy_frame_buf); in airspy_probe()
1022 s->vb_queue.ops = &airspy_vb2_ops; in airspy_probe()
1023 s->vb_queue.mem_ops = &vb2_vmalloc_memops; in airspy_probe()
1024 s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; in airspy_probe()
1025 ret = vb2_queue_init(&s->vb_queue); in airspy_probe()
1027 dev_err(s->dev, "Could not initialize vb2 queue\n"); in airspy_probe()
1032 s->vdev = airspy_template; in airspy_probe()
1033 s->vdev.queue = &s->vb_queue; in airspy_probe()
1034 s->vdev.queue->lock = &s->vb_queue_lock; in airspy_probe()
1035 video_set_drvdata(&s->vdev, s); in airspy_probe()
1038 s->v4l2_dev.release = airspy_video_release; in airspy_probe()
1039 ret = v4l2_device_register(&intf->dev, &s->v4l2_dev); in airspy_probe()
1041 dev_err(s->dev, "Failed to register v4l2-device (%d)\n", ret); in airspy_probe()
1046 v4l2_ctrl_handler_init(&s->hdl, 5); in airspy_probe()
1047 s->lna_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1049 s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1051 v4l2_ctrl_auto_cluster(2, &s->lna_gain_auto, 0, false); in airspy_probe()
1052 s->mixer_gain_auto = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1054 s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1056 v4l2_ctrl_auto_cluster(2, &s->mixer_gain_auto, 0, false); in airspy_probe()
1057 s->if_gain = v4l2_ctrl_new_std(&s->hdl, &airspy_ctrl_ops, in airspy_probe()
1059 if (s->hdl.error) { in airspy_probe()
1060 ret = s->hdl.error; in airspy_probe()
1061 dev_err(s->dev, "Could not initialize controls\n"); in airspy_probe()
1065 v4l2_ctrl_handler_setup(&s->hdl); in airspy_probe()
1067 s->v4l2_dev.ctrl_handler = &s->hdl; in airspy_probe()
1068 s->vdev.v4l2_dev = &s->v4l2_dev; in airspy_probe()
1069 s->vdev.lock = &s->v4l2_lock; in airspy_probe()
1071 ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1); in airspy_probe()
1073 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()
1085 v4l2_device_unregister(&s->v4l2_dev); in airspy_probe()
1087 kfree(s); in airspy_probe()