Path: blob/master/drivers/gpib/lpvo_usb_gpib/lpvo_usb_gpib.c
122915 views
// SPDX-License-Identifier: GPL-2.012/***************************************************************************3* This code has been developed at the Department of Physics (University *4* of Florence, Italy) to support in linux-gpib the open usb-gpib adapter *5* implemented at the University of Ljubljana (lpvo.fe.uni-lj.si/gpib) *6* *7* copyright : (C) 2011 Marcello Carla' *8***************************************************************************/910#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt11#define dev_fmt pr_fmt12#define NAME KBUILD_MODNAME1314/* base module includes */1516#include <linux/module.h>17#include <linux/sched.h>18#include <linux/init.h>19#include <linux/kernel.h>20#include <linux/tty.h>21#include <linux/types.h>22#include <linux/slab.h>23#include <linux/mm.h>24#include <linux/vmalloc.h>25#include <linux/spinlock.h>26#include <linux/file.h>27#include <linux/timer.h>28#include <linux/delay.h>29#include <linux/sched/signal.h>30#include <linux/usb.h>3132#include "gpibP.h"3334MODULE_LICENSE("GPL");35MODULE_DESCRIPTION("GPIB driver for LPVO usb devices");3637/*38* Table of devices that work with this driver.39*40* Currently, only one device is known to be used in the lpvo_usb_gpib41* adapter (FTDI 0403:6001) but as this device id is already handled by the42* ftdi_sio USB serial driver the LPVO driver must not bind to it by default.43*44* If your adapter uses a different chip, insert a line45* in the following table with proper <Vendor-id>, <Product-id>.46*47* To have your chip automatically handled by the driver,48* update files "/usr/local/etc/modprobe.d/lpvo_usb_gpib.conf"49* and /usr/local/etc/udev/rules.d/99-lpvo_usb_gpib.rules.50*51*/5253static const struct usb_device_id skel_table[] = {54{ } /* Terminating entry */55};56MODULE_DEVICE_TABLE(usb, skel_table);5758/*59* *** Diagnostics and Debug ***60* To enable the diagnostic and debug messages either compile with DEBUG set61* or control via the dynamic debug mechanisms.62* The module parameter "debug" controls the sending of debug messages to63* syslog. By default it is set to 064* debug = 0: only attach/detach messages are sent65* 1: every action is logged66* 2: extended logging; each single exchanged byte is documented67* (about twice the log volume of [1])68* To switch debug level:69* At module loading: modprobe lpvo_usb_gpib debug={0,1,2}70* On the fly: echo {0,1,2} > /sys/modules/lpvo_usb_gpib/parameters/debug71*/7273static int debug;74module_param(debug, int, 0644);7576#define DIA_LOG(level, format, ...) \77do { if (debug >= (level)) \78dev_dbg(board->gpib_dev, format, ## __VA_ARGS__); } \79while (0)8081#define WQT wait_queue_entry_t82#define WQH head83#define WQE entry8485/* standard and extended command sets of the usb-gpib adapter */8687#define USB_GPIB_ON "\nIB\n"88#define USB_GPIB_OFF "\nIBO\n"89#define USB_GPIB_IBm0 "\nIBm0\n" /* do not assert REN with IFC */90#define USB_GPIB_IBm1 "\nIBm1\n" /* assert REN with IFC */91#define USB_GPIB_IBCL "\nIBZ\n"92#define USB_GPIB_STATUS "\nIBS\n"93#define USB_GPIB_READ "\nIB?\n"94#define USB_GPIB_READ_1 "\nIBB\n"95#define USB_GPIB_EOI "\nIBe0\n"96#define USB_GPIB_FTMO "\nIBf0\n" /* disable first byte timeout */97#define USB_GPIB_TTMOZ "\nIBt0\n" /* disable byte timeout */9899/* incomplete commands */100101#define USB_GPIB_BTMO "\nIBt" /* set byte timeout */102#define USB_GPIB_TTMO "\nIBT" /* set total timeout */103104#define USB_GPIB_DEBUG_ON "\nIBDE\xAA\n"105#define USB_GPIB_SET_LISTEN "\nIBDT0\n"106#define USB_GPIB_SET_TALK "\nIBDT1\n"107#define USB_GPIB_SET_LINES "\nIBDC.\n"108#define USB_GPIB_SET_DATA "\nIBDM.\n"109#define USB_GPIB_READ_LINES "\nIBD?C\n"110#define USB_GPIB_READ_DATA "\nIBD?M\n"111#define USB_GPIB_READ_BUS "\nIBD??\n"112113/* command sequences */114115#define USB_GPIB_UNTALK "\nIBC_\n"116#define USB_GPIB_UNLISTEN "\nIBC?\n"117118/* special characters used by the adapter */119120#define DLE ('\020')121#define STX ('\02')122#define ETX ('\03')123#define ACK ('\06')124#define NODATA ('\03')125#define NODAV ('\011')126127#define IB_BUS_REN 0x01128#define IB_BUS_IFC 0x02129#define IB_BUS_NDAC 0x04130#define IB_BUS_NRFD 0x08131#define IB_BUS_DAV 0x10132#define IB_BUS_EOI 0x20133#define IB_BUS_ATN 0x40134#define IB_BUS_SRQ 0x80135136#define INBUF_SIZE 128137138struct char_buf { /* used by one_char() routine */139char *inbuf;140int last;141int nchar;142};143144struct usb_gpib_priv { /* private data to the device */145u8 eos; /* eos character */146short eos_flags; /* eos mode */147int timeout; /* current value for timeout */148void *dev; /* the usb device private data structure */149};150151#define GPIB_DEV (((struct usb_gpib_priv *)board->private_data)->dev)152153static void show_status(struct gpib_board *board)154{155DIA_LOG(2, "# - buffer_length %d\n", board->buffer_length);156DIA_LOG(2, "# - status %lx\n", board->status);157DIA_LOG(2, "# - use_count %d\n", board->use_count);158DIA_LOG(2, "# - pad %x\n", board->pad);159DIA_LOG(2, "# - sad %x\n", board->sad);160DIA_LOG(2, "# - timeout %d\n", board->usec_timeout);161DIA_LOG(2, "# - ppc %d\n", board->parallel_poll_configuration);162DIA_LOG(2, "# - t1delay %d\n", board->t1_nano_sec);163DIA_LOG(2, "# - online %d\n", board->online);164DIA_LOG(2, "# - autopoll %d\n", board->autospollers);165DIA_LOG(2, "# - autopoll task %p\n", board->autospoll_task);166DIA_LOG(2, "# - minor %d\n", board->minor);167DIA_LOG(2, "# - master %d\n", board->master);168DIA_LOG(2, "# - list %d\n", board->ist);169}170171/*172* GLOBAL VARIABLES: required for173* pairing among gpib minor and usb minor.174* MAX_DEV is the max number of usb-gpib adapters; free175* to change as you like, but no more than 32176*/177178#define MAX_DEV 8179static struct usb_interface *lpvo_usb_interfaces[MAX_DEV]; /* registered interfaces */180static int usb_minors[MAX_DEV]; /* usb minors */181static int assigned_usb_minors; /* mask of filled slots */182static struct mutex minors_lock; /* operations on usb_minors are to be protected */183184/*185* usb-skeleton prototypes186*/187188struct usb_skel;189static ssize_t skel_do_write(struct usb_skel *, const char *, size_t);190static ssize_t skel_do_read(struct usb_skel *, char *, size_t);191static int skel_do_open(struct gpib_board *, int);192static int skel_do_release(struct gpib_board *);193194/*195* usec_diff : take difference in MICROsec between two 'timespec'196* (unix time in sec and NANOsec)197*/198199static inline int usec_diff(struct timespec64 *a, struct timespec64 *b)200{201return ((a->tv_sec - b->tv_sec) * 1000000 +202(a->tv_nsec - b->tv_nsec) / 1000);203}204205/*206* *** these routines are specific to the usb-gpib adapter ***207*/208209/**210* write_loop() - Send a byte sequence to the adapter211*212* @dev: the private device structure213* @msg: the byte sequence.214* @leng: the byte sequence length.215*216*/217218static int write_loop(void *dev, char *msg, int leng)219{220return skel_do_write(dev, msg, leng);221}222223/**224* send_command() - Send a byte sequence and return a single byte reply.225*226* @board: the gpib_board_struct data area for this gpib interface227* @msg: the byte sequence.228* @leng: the byte sequence length; can be given as zero and is229* computed automatically, but if 'msg' contains a zero byte,230* it has to be given explicitly.231*/232233static int send_command(struct gpib_board *board, char *msg, int leng)234{235char buffer[64];236int nchar;237int retval;238struct timespec64 before, after;239240ktime_get_real_ts64 (&before);241242if (!leng)243leng = strlen(msg);244retval = write_loop(GPIB_DEV, msg, leng);245if (retval < 0)246return retval;247248nchar = skel_do_read(GPIB_DEV, buffer, 64);249250if (nchar < 0) {251dev_err(board->gpib_dev, " return from read: %d\n", nchar);252return nchar;253} else if (nchar != 1) {254dev_err(board->gpib_dev, " Irregular reply to command: %s\n", msg);255return -EIO;256}257ktime_get_real_ts64 (&after);258259DIA_LOG(1, "Sent %d - done %d us.\n", leng, usec_diff(&after, &before));260261return buffer[0] & 0xff;262}263264/*265* set_control_line() - Set the value of a single gpib control line266*267* @board: the gpib_board_struct data area for this gpib interface268* @line: line mask269* @value: line new value (0/1)270*/271272static int set_control_line(struct gpib_board *board, int line, int value)273{274char msg[] = USB_GPIB_SET_LINES;275int retval;276int leng = strlen(msg);277278DIA_LOG(1, "setting line %x to %x\n", line, value);279280retval = send_command(board, USB_GPIB_READ_LINES, 0);281282DIA_LOG(1, "old line values: %x\n", retval);283284if (retval == -EIO)285return retval;286287msg[leng - 2] = value ? (retval & ~line) : retval | line;288289retval = send_command(board, msg, 0);290291DIA_LOG(1, "operation result: %x\n", retval);292293return retval;294}295296/*297* one_char() - read one single byte from input buffer298*299* @board: the gpib_board_struct data area for this gpib interface300* @char_buf: the routine private data structure301*/302303static int one_char(struct gpib_board *board, struct char_buf *b)304{305struct timespec64 before, after;306307if (b->nchar) {308DIA_LOG(2, "-> %x\n", b->inbuf[b->last - b->nchar]);309return b->inbuf[b->last - b->nchar--];310}311ktime_get_real_ts64 (&before);312b->nchar = skel_do_read(GPIB_DEV, b->inbuf, INBUF_SIZE);313b->last = b->nchar;314ktime_get_real_ts64 (&after);315316DIA_LOG(2, "read %d bytes in %d usec\n",317b->nchar, usec_diff(&after, &before));318319if (b->nchar > 0) {320DIA_LOG(2, "--> %x\n", b->inbuf[b->last - b->nchar]);321return b->inbuf[b->last - b->nchar--];322}323return -EIO;324}325326/**327* set_timeout() - set single byte / total timeouts on the adapter328*329* @board: the gpib_board_struct data area for this gpib interface330*331* For sake of speed, the operation is performed only if it332* modifies the current (saved) value. Minimum allowed timeout333* is 30 ms (T30ms -> 8); timeout disable (TNONE -> 0) currently334* not supported.335*/336337static void set_timeout(struct gpib_board *board)338{339int n, val;340char command[sizeof(USB_GPIB_TTMO) + 6];341struct usb_gpib_priv *data = board->private_data;342343if (data->timeout == board->usec_timeout)344return;345346n = (board->usec_timeout + 32767) / 32768;347if (n < 2)348n = 2;349350DIA_LOG(1, "Set timeout to %d us -> %d\n", board->usec_timeout, n);351352sprintf(command, "%s%d\n", USB_GPIB_BTMO, n > 255 ? 255 : n);353val = send_command(board, command, 0);354355if (val == ACK) {356if (n > 65535)357n = 65535;358sprintf(command, "%s%d\n", USB_GPIB_TTMO, n);359val = send_command(board, command, 0);360}361362if (val != ACK)363dev_err(board->gpib_dev, "error in timeout set: <%s>\n", command);364else365data->timeout = board->usec_timeout;366}367368/*369* now the standard interface functions - attach and detach370*/371372/**373* usb_gpib_attach() - activate the usb-gpib converter board374*375* @board: the gpib_board_struct data area for this gpib interface376* @config: firmware data, if any (from gpib_config -I <file>)377*378* The channel name is ttyUSBn, with n=0 by default. Other values for n379* passed with gpib_config -b <n>.380*381* In this routine I trust that when an error code is returned382* detach() will be called. Always.383*/384385static int usb_gpib_attach(struct gpib_board *board, const struct gpib_board_config *config)386{387int retval, j;388u32 base = config->ibbase;389char *device_path;390int match;391struct usb_device *udev;392393DIA_LOG(0, "Board %p -t %s -m %d -a %p -u %d -l %d -b %d\n",394board, board->interface->name, board->minor, config->device_path,395config->pci_bus, config->pci_slot, base);396397board->private_data = NULL; /* to be sure - we can detach before setting */398399/* identify device to be attached */400401mutex_lock(&minors_lock);402403if (config->device_path) {404/* if config->device_path given, try that first */405for (j = 0 ; j < MAX_DEV ; j++) {406if ((assigned_usb_minors & 1 << j) == 0)407continue;408udev = usb_get_dev(interface_to_usbdev(lpvo_usb_interfaces[j]));409device_path = kobject_get_path(&udev->dev.kobj, GFP_KERNEL);410match = gpib_match_device_path(&lpvo_usb_interfaces[j]->dev,411config->device_path);412DIA_LOG(1, "dev. %d: minor %d path: %s --> %d\n", j,413lpvo_usb_interfaces[j]->minor, device_path, match);414kfree(device_path);415if (match)416break;417}418} else if (config->pci_bus != -1 && config->pci_slot != -1) {419/* second: look for bus and slot */420for (j = 0 ; j < MAX_DEV ; j++) {421if ((assigned_usb_minors & 1 << j) == 0)422continue;423udev = usb_get_dev(interface_to_usbdev(lpvo_usb_interfaces[j]));424DIA_LOG(1, "dev. %d: bus %d -> %d dev: %d -> %d\n", j,425udev->bus->busnum, config->pci_bus, udev->devnum, config->pci_slot);426if (config->pci_bus == udev->bus->busnum &&427config->pci_slot == udev->devnum)428break;429}430} else { /* last chance: usb_minor, given as ibbase */431for (j = 0 ; j < MAX_DEV ; j++) {432if (usb_minors[j] == base && assigned_usb_minors & 1 << j)433break;434}435}436mutex_unlock(&minors_lock);437438if (j == MAX_DEV) {439dev_err(board->gpib_dev, "Requested device is not registered.\n");440return -EIO;441}442443board->private_data = kzalloc_obj(struct usb_gpib_priv);444if (!board->private_data)445return -ENOMEM;446447retval = skel_do_open(board, usb_minors[j]);448449DIA_LOG(1, "Skel open: %d\n", retval);450451if (retval) {452dev_err(board->gpib_dev, "skel open failed.\n");453kfree(board->private_data);454board->private_data = NULL;455return -ENODEV;456}457458show_status(board);459460retval = send_command(board, USB_GPIB_ON, 0);461DIA_LOG(1, "USB_GPIB_ON returns %x\n", retval);462if (retval != ACK)463return -EIO;464465/*466* We must setup debug mode because we need the extended instruction467* set to cope with the Core (gpib_common) point of view468*/469470retval = send_command(board, USB_GPIB_DEBUG_ON, 0);471DIA_LOG(1, "USB_GPIB_DEBUG_ON returns %x\n", retval);472if (retval != ACK)473return -EIO;474475/*476* We must keep REN off after an IFC because so it is477* assumed by the Core478*/479480retval = send_command(board, USB_GPIB_IBm0, 0);481DIA_LOG(1, "USB_GPIB_IBm0 returns %x\n", retval);482if (retval != ACK)483return -EIO;484485retval = set_control_line(board, IB_BUS_REN, 0);486if (retval != ACK)487return -EIO;488489retval = send_command(board, USB_GPIB_FTMO, 0);490DIA_LOG(1, "USB_GPIB_FTMO returns %x\n", retval);491if (retval != ACK)492return -EIO;493494show_status(board);495DIA_LOG(0, "attached\n");496return 0;497}498499/**500* usb_gpib_detach() - deactivate the usb-gpib converter board501*502* @board: the gpib_board data area for this gpib interface503*504*/505506static void usb_gpib_detach(struct gpib_board *board)507{508int retval;509510show_status(board);511512DIA_LOG(0, "detaching\n");513514if (board->private_data) {515if (GPIB_DEV) {516write_loop(GPIB_DEV, USB_GPIB_OFF, strlen(USB_GPIB_OFF));517msleep(100);518DIA_LOG(1, "%s", "GPIB off\n");519retval = skel_do_release(board);520DIA_LOG(1, "skel release -> %d\n", retval);521}522kfree(board->private_data);523board->private_data = NULL;524}525526DIA_LOG(0, "detached\n");527}528529/*530* Other functions follow in alphabetical order531*/532/* command */533static int usb_gpib_command(struct gpib_board *board,534u8 *buffer,535size_t length,536size_t *bytes_written)537{538int i, retval;539char command[6] = "IBc.\n";540541DIA_LOG(1, "enter %p\n", board);542543set_timeout(board);544545*bytes_written = 0;546for (i = 0 ; i < length ; i++) {547command[3] = buffer[i];548retval = send_command(board, command, 5);549DIA_LOG(2, "%d ==> %x %x\n", i, buffer[i], retval);550if (retval != 0x06)551return retval;552++(*bytes_written);553}554return 0;555}556557/**558* usb_gpib_disable_eos() - Disable END on eos byte (END on EOI only)559*560* @board: the gpib_board data area for this gpib interface561*562* With the lpvo adapter eos can only be handled via software.563* Cannot do nothing here, but remember for future use.564*/565566static void usb_gpib_disable_eos(struct gpib_board *board)567{568((struct usb_gpib_priv *)board->private_data)->eos_flags &= ~REOS;569DIA_LOG(1, "done: %x\n",570((struct usb_gpib_priv *)board->private_data)->eos_flags);571}572573/**574* usb_gpib_enable_eos() - Enable END for reads when eos byte is received.575*576* @board: the gpib_board data area for this gpib interface577* @eos_byte: the 'eos' byte578* @compare_8_bits: if zero ignore eigthth bit when comparing579*580*/581582static int usb_gpib_enable_eos(struct gpib_board *board,583u8 eos_byte,584int compare_8_bits)585{586struct usb_gpib_priv *pd = (struct usb_gpib_priv *)board->private_data;587588DIA_LOG(1, "enter with %x\n", eos_byte);589pd->eos = eos_byte;590pd->eos_flags = REOS;591if (compare_8_bits)592pd->eos_flags |= BIN;593return 0;594}595596/**597* usb_gpib_go_to_standby() - De-assert ATN598*599* @board: the gpib_board data area for this gpib interface600*/601602static int usb_gpib_go_to_standby(struct gpib_board *board)603{604int retval = set_control_line(board, IB_BUS_ATN, 0);605606DIA_LOG(1, "done with %x\n", retval);607608if (retval == ACK)609return 0;610return -EIO;611}612613/**614* usb_gpib_interface_clear() - Assert or de-assert IFC615*616* @board: the gpib_board data area for this gpib interface617* @assert: 1: assert IFC; 0: de-assert IFC618*619* Currently on the assert request we issue the lpvo IBZ620* command that cycles IFC low for 100 usec, then we ignore621* the de-assert request.622*/623624static void usb_gpib_interface_clear(struct gpib_board *board, int assert)625{626int retval = 0;627628DIA_LOG(1, "enter with %d\n", assert);629630if (assert) {631retval = send_command(board, USB_GPIB_IBCL, 0);632633set_bit(CIC_NUM, &board->status);634}635636DIA_LOG(1, "done with %d %d\n", assert, retval);637}638639/**640* usb_gpib_line_status() - Read the status of the bus lines.641*642* @board: the gpib_board data area for this gpib interface643*644* We can read all lines.645*/646static int usb_gpib_line_status(const struct gpib_board *board)647{648int buffer;649int line_status = VALID_ALL; /* all lines will be read */650struct list_head *p, *q;651WQT *item;652unsigned long flags;653int sleep = 0;654655DIA_LOG(1, "%s\n", "request");656657/*658* if we are on the wait queue (board->wait), do not hurry659* reading status line; instead, pause a little660*/661662spin_lock_irqsave((spinlock_t *)&board->wait.lock, flags);663q = (struct list_head *)&board->wait.WQH;664list_for_each(p, q) {665item = container_of(p, WQT, WQE);666if (item->private == current) {667sleep = 20;668break;669}670/* pid is: ((struct task_struct *) item->private)->pid); */671}672spin_unlock_irqrestore((spinlock_t *)&board->wait.lock, flags);673if (sleep) {674DIA_LOG(1, "we are on the wait queue - sleep %d ms\n", sleep);675msleep(sleep);676}677678buffer = send_command((struct gpib_board *)board, USB_GPIB_STATUS, 0);679680if (buffer < 0) {681dev_err(board->gpib_dev, "line status read failed with %d\n", buffer);682return -1;683}684685if ((buffer & 0x01) == 0)686line_status |= BUS_REN;687if ((buffer & 0x02) == 0)688line_status |= BUS_IFC;689if ((buffer & 0x04) == 0)690line_status |= BUS_NDAC;691if ((buffer & 0x08) == 0)692line_status |= BUS_NRFD;693if ((buffer & 0x10) == 0)694line_status |= BUS_DAV;695if ((buffer & 0x20) == 0)696line_status |= BUS_EOI;697if ((buffer & 0x40) == 0)698line_status |= BUS_ATN;699if ((buffer & 0x80) == 0)700line_status |= BUS_SRQ;701702DIA_LOG(1, "done with %x %x\n", buffer, line_status);703704return line_status;705}706707/* parallel_poll */708709static int usb_gpib_parallel_poll(struct gpib_board *board, u8 *result)710{711/*712* request parallel poll asserting ATN | EOI;713* we suppose ATN already asserted714*/715716int retval;717718DIA_LOG(1, "enter %p\n", board);719720retval = set_control_line(board, IB_BUS_EOI, 1);721if (retval != ACK)722return -EIO;723724*result = send_command(board, USB_GPIB_READ_DATA, 0);725726DIA_LOG(1, "done with %x\n", *result);727728retval = set_control_line(board, IB_BUS_EOI, 0);729if (retval != 0x06)730return -EIO;731732return 0;733}734735/* read */736737static int usb_gpib_read(struct gpib_board *board,738u8 *buffer,739size_t length,740int *end,741size_t *bytes_read)742{743#define MAX_READ_EXCESS 16384744745struct char_buf b = {NULL, 0};746747int retval;748char c, nc;749int ic;750struct timespec64 before, after;751int read_count = MAX_READ_EXCESS;752struct usb_gpib_priv *pd = (struct usb_gpib_priv *)board->private_data;753754DIA_LOG(1, "enter %p -> %zu\n", board, length);755756*bytes_read = 0; /* by default, things go wrong */757*end = 0;758759set_timeout(board);760761/* single byte read has a special handling */762763if (length == 1) {764char inbuf[2] = {0, 0};765766/* read a single character */767768ktime_get_real_ts64 (&before);769770retval = write_loop(GPIB_DEV, USB_GPIB_READ_1, strlen(USB_GPIB_READ_1));771if (retval < 0)772return retval;773774retval = skel_do_read(GPIB_DEV, inbuf, 1);775retval += skel_do_read(GPIB_DEV, inbuf + 1, 1);776777ktime_get_real_ts64 (&after);778779DIA_LOG(1, "single read: %x %x %x in %d\n", retval,780inbuf[0], inbuf[1],781usec_diff(&after, &before));782783/* good char / last char? */784785if (retval == 2 && inbuf[1] == ACK) {786buffer[0] = inbuf[0];787*bytes_read = 1;788return 0;789}790if (retval < 2)791return -EIO;792else793return -ETIME;794}795796/* allocate buffer for multibyte read */797798b.inbuf = kmalloc(INBUF_SIZE, GFP_KERNEL);799if (!b.inbuf)800return -ENOMEM;801802/* send read command and check <DLE><STX> sequence */803804retval = write_loop(GPIB_DEV, USB_GPIB_READ, strlen(USB_GPIB_READ));805if (retval < 0)806goto read_return;807808if (one_char(board, &b) != DLE || one_char(board, &b) != STX) {809dev_err(board->gpib_dev, "wrong <DLE><STX> sequence\n");810retval = -EIO;811goto read_return;812}813814/* get data flow */815816while (1) {817ic = one_char(board, &b);818if (ic == -EIO) {819retval = -EIO;820goto read_return;821}822c = ic;823824if (c == DLE)825nc = one_char(board, &b);826if (c != DLE || nc == DLE) {827/* data byte - store into buffer */828829if (*bytes_read == length)830break; /* data overflow */831if (c == DLE)832c = nc;833buffer[(*bytes_read)++] = c;834if (c == pd->eos) {835*end = 1;836break;837}838839} else {840/* we are in the closing <DLE><ETX> sequence */841c = nc;842if (c == ETX) {843c = one_char(board, &b);844if (c == ACK) {845*end = 1;846retval = 0;847goto read_return;848} else {849dev_err(board->gpib_dev, "wrong end of message %x", c);850retval = -ETIME;851goto read_return;852}853} else {854dev_err(board->gpib_dev, "lone <DLE> in stream");855retval = -EIO;856goto read_return;857}858}859}860861/* we had a data overflow - flush excess data */862863while (read_count--) {864if (one_char(board, &b) != DLE)865continue;866c = one_char(board, &b);867if (c == DLE)868continue;869if (c == ETX) {870c = one_char(board, &b);871if (c == ACK) {872if (MAX_READ_EXCESS - read_count > 1)873dev_dbg(board->gpib_dev, "small buffer - maybe some data lost");874retval = 0;875goto read_return;876}877break;878}879}880881dev_err(board->gpib_dev, "no input end - board in odd state\n");882retval = -EIO;883884read_return:885kfree(b.inbuf);886887DIA_LOG(1, "done with byte/status: %d %x %d\n", (int)*bytes_read, retval, *end);888889if (retval == 0 || retval == -ETIME) {890if (send_command(board, USB_GPIB_UNTALK, sizeof(USB_GPIB_UNTALK)) == 0x06)891return retval;892return -EIO;893}894895return retval;896}897898/* remote_enable */899900static void usb_gpib_remote_enable(struct gpib_board *board, int enable)901{902int retval;903904retval = set_control_line(board, IB_BUS_REN, enable ? 1 : 0);905if (retval != ACK)906dev_err(board->gpib_dev, "could not set REN line: %x\n", retval);907908DIA_LOG(1, "done with %x\n", retval);909}910911/* request_system_control */912913static int usb_gpib_request_system_control(struct gpib_board *board, int request_control)914{915if (!request_control)916return -EINVAL;917918DIA_LOG(1, "done with %d -> %lx\n", request_control, board->status);919return 0;920}921922/* take_control */923/* beware: the sync flag is ignored; what is its real meaning? */924925static int usb_gpib_take_control(struct gpib_board *board, int sync)926{927int retval;928929retval = set_control_line(board, IB_BUS_ATN, 1);930931DIA_LOG(1, "done with %d %x\n", sync, retval);932933if (retval == ACK)934return 0;935return -EIO;936}937938/* update_status */939940static unsigned int usb_gpib_update_status(struct gpib_board *board,941unsigned int clear_mask)942{943/* There is nothing we can do here, I guess */944945board->status &= ~clear_mask;946947DIA_LOG(1, "done with %x %lx\n", clear_mask, board->status);948949return board->status;950}951952/* write */953/* beware: DLE characters are not escaped - can only send ASCII data */954955static int usb_gpib_write(struct gpib_board *board,956u8 *buffer,957size_t length,958int send_eoi,959size_t *bytes_written)960{961int retval;962char *msg;963964DIA_LOG(1, "enter %p -> %zu\n", board, length);965966set_timeout(board);967968msg = kmalloc(length + 8, GFP_KERNEL);969if (!msg)970return -ENOMEM;971972memcpy(msg, "\nIB\020\002", 5);973memcpy(msg + 5, buffer, length);974memcpy(msg + 5 + length, "\020\003\n", 3);975976retval = send_command(board, msg, length + 8);977kfree(msg);978979DIA_LOG(1, "<%.*s> -> %x\n", (int)length, buffer, retval);980981if (retval != ACK)982return -EPIPE;983984*bytes_written = length;985986if (send_command(board, USB_GPIB_UNLISTEN, sizeof(USB_GPIB_UNLISTEN)) != 0x06)987return -EPIPE;988989return length;990}991992/*993* *** following functions not implemented yet ***994*/995996/* parallel_poll configure */997998static void usb_gpib_parallel_poll_configure(struct gpib_board *board,999u8 configuration)1000{1001}10021003/* parallel_poll_response */10041005static void usb_gpib_parallel_poll_response(struct gpib_board *board, int ist)1006{1007}10081009/* primary_address */10101011static int usb_gpib_primary_address(struct gpib_board *board, unsigned int address)1012{1013return 0;1014}10151016/* return_to_local */10171018static void usb_gpib_return_to_local(struct gpib_board *board)1019{1020}10211022/* secondary_address */10231024static int usb_gpib_secondary_address(struct gpib_board *board,1025unsigned int address,1026int enable)1027{1028return 0;1029}10301031/* serial_poll_response */10321033static void usb_gpib_serial_poll_response(struct gpib_board *board, u8 status)1034{1035}10361037/* serial_poll_status */10381039static u8 usb_gpib_serial_poll_status(struct gpib_board *board)1040{1041return 0;1042}10431044/* t1_delay */10451046static int usb_gpib_t1_delay(struct gpib_board *board, unsigned int nano_sec)1047{1048return 0;1049}10501051/*1052* *** module dispatch table and init/exit functions ***1053*/10541055static struct gpib_interface usb_gpib_interface = {1056.name = NAME,1057.attach = usb_gpib_attach,1058.detach = usb_gpib_detach,1059.read = usb_gpib_read,1060.write = usb_gpib_write,1061.command = usb_gpib_command,1062.take_control = usb_gpib_take_control,1063.go_to_standby = usb_gpib_go_to_standby,1064.request_system_control = usb_gpib_request_system_control,1065.interface_clear = usb_gpib_interface_clear,1066.remote_enable = usb_gpib_remote_enable,1067.enable_eos = usb_gpib_enable_eos,1068.disable_eos = usb_gpib_disable_eos,1069.parallel_poll = usb_gpib_parallel_poll,1070.parallel_poll_configure = usb_gpib_parallel_poll_configure,1071.parallel_poll_response = usb_gpib_parallel_poll_response,1072.local_parallel_poll_mode = NULL, // XXX1073.line_status = usb_gpib_line_status,1074.update_status = usb_gpib_update_status,1075.primary_address = usb_gpib_primary_address,1076.secondary_address = usb_gpib_secondary_address,1077.serial_poll_response = usb_gpib_serial_poll_response,1078.serial_poll_status = usb_gpib_serial_poll_status,1079.t1_delay = usb_gpib_t1_delay,1080.return_to_local = usb_gpib_return_to_local,1081.skip_check_for_command_acceptors = 11082};10831084/*1085* usb_gpib_init_module(), usb_gpib_exit_module()1086*1087* This functions are called every time a new device is detected1088* and registered or is removed and unregistered.1089* We must take note of created and destroyed usb minors to be used1090* when usb_gpib_attach() and usb_gpib_detach() will be called on1091* request by gpib_config.1092*/10931094static int usb_gpib_init_module(struct usb_interface *interface)1095{1096int j, mask, rv;10971098rv = mutex_lock_interruptible(&minors_lock);1099if (rv < 0)1100return rv;11011102if (!assigned_usb_minors) {1103rv = gpib_register_driver(&usb_gpib_interface, THIS_MODULE);1104if (rv) {1105pr_err("gpib_register_driver failed: error = %d\n", rv);1106goto exit;1107}1108} else {1109/*1110* check if minor is already registered - maybe useless, but if1111* it happens the code is inconsistent somewhere1112*/11131114for (j = 0 ; j < MAX_DEV ; j++) {1115if (usb_minors[j] == interface->minor && assigned_usb_minors & 1 << j) {1116pr_err("CODE BUG: USB minor %d registered at %d.\n",1117interface->minor, j);1118rv = -1;1119goto exit;1120}1121}1122}11231124/* find a free slot */11251126for (j = 0 ; j < MAX_DEV ; j++) {1127mask = 1 << j;1128if ((assigned_usb_minors & mask) == 0) {1129usb_minors[j] = interface->minor;1130lpvo_usb_interfaces[j] = interface;1131assigned_usb_minors |= mask;1132rv = 0;1133goto exit;1134}1135}1136pr_err("No slot available for interface %p minor %d\n", interface, interface->minor);1137rv = -1;11381139exit:1140mutex_unlock(&minors_lock);1141return rv;1142}11431144static void usb_gpib_exit_module(int minor)1145{1146int j;11471148mutex_lock(&minors_lock);1149for (j = 0 ; j < MAX_DEV ; j++) {1150if (usb_minors[j] == minor && assigned_usb_minors & 1 << j) {1151assigned_usb_minors &= ~(1 << j);1152usb_minors[j] = -1;1153if (assigned_usb_minors == 0)1154gpib_unregister_driver(&usb_gpib_interface);1155goto exit;1156}1157}1158pr_err("CODE BUG: USB minor %d not found.\n", minor);11591160exit:1161mutex_unlock(&minors_lock);1162}11631164/*1165* Default latency time (16 msec) is too long.1166* We must use 1 msec (best); anyhow, no more than 5 msec.1167*1168* Defines and function taken and modified from the kernel tree1169* (see ftdi_sio.h and ftdi_sio.c).1170*/11711172#define FTDI_SIO_SET_LATENCY_TIMER 9 /* Set the latency timer */1173#define FTDI_SIO_SET_LATENCY_TIMER_REQUEST FTDI_SIO_SET_LATENCY_TIMER1174#define FTDI_SIO_SET_LATENCY_TIMER_REQUEST_TYPE 0x401175#define WDR_TIMEOUT 5000 /* default urb timeout */1176#define WDR_SHORT_TIMEOUT 1000 /* shorter urb timeout */11771178#define LATENCY_TIMER 1 /* use a small latency timer: 1 ... 5 msec */1179#define LATENCY_CHANNEL 0 /* channel selection in multichannel devices */1180static int write_latency_timer(struct usb_device *udev)1181{1182int rv = usb_control_msg(udev,1183usb_sndctrlpipe(udev, 0),1184FTDI_SIO_SET_LATENCY_TIMER_REQUEST,1185FTDI_SIO_SET_LATENCY_TIMER_REQUEST_TYPE,1186LATENCY_TIMER, LATENCY_CHANNEL,1187NULL, 0, WDR_TIMEOUT);1188if (rv < 0)1189dev_err(&udev->dev, "Unable to write latency timer: %i\n", rv);1190return rv;1191}11921193/*****************************************************************************1194* *1195* The following code is a modified version of the USB Skeleton driver *1196* written by Greg Kroah-Hartman and available in the kernel tree. *1197* *1198* Functions skel_open() and skel_release() have been rewritten and named *1199* skel_do_open() and skel_do_release() to process the attach and detach *1200* requests coming from gpib_config. *1201* *1202* Functions skel_read() and skel_write() have been split into a *1203* skel_do_read() and skel_do_write(), that cover the kernel stuff of read *1204* and write operations, and the original skel_read() and skel_write(), *1205* that handle communication with user space and call their _do_ companion. *1206* *1207* Only the _do_ versions are used by the lpvo_usb_gpib driver; other ones *1208* can be (optionally) maintained in the compilation to have direct access *1209* to a gpib controller for debug and diagnostics. *1210* *1211* To avoid collisions in names, devices in user space have been renamed *1212* lpvo_raw1, lpvo_raw2 .... and the usb driver has been renamed with the *1213* gpib module name. *1214* *1215*****************************************************************************/12161217/*1218* USB Skeleton driver - 2.21219*1220* Copyright (C) 2001-2004 Greg Kroah-Hartman ([email protected])1221*1222* This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c1223* but has been rewritten to be easier to read and use.1224*/12251226#include <linux/errno.h>1227#include <linux/kref.h>1228#include <linux/uaccess.h>1229#include <linux/mutex.h>12301231/* Get a minor range for your devices from the usb maintainer */1232#define USB_SKEL_MINOR_BASE 19212331234/* private defines */12351236#define MAX_TRANSFER (PAGE_SIZE - 512)1237/*1238* MAX_TRANSFER is chosen so that the VM is not stressed by1239* allocations > PAGE_SIZE and the number of packets in a page1240* is an integer 512 is the largest possible packet on EHCI1241*/12421243#define WRITES_IN_FLIGHT 1 /* we do not want more than one pending write */1244#define USER_DEVICE 1 /* compile for device(s) in user space */12451246/* Structure to hold all of our device specific stuff */1247struct usb_skel {1248struct usb_device *udev; /* the usb device for this device */1249struct usb_interface *interface; /* the interface for this device */1250struct semaphore limit_sem; /* limiting the number of writes in progress */1251struct usb_anchor submitted; /* in case need to retract our submissions */1252struct urb *bulk_in_urb; /* the urb to read data with */1253unsigned char *bulk_in_buffer; /* the buffer to receive data */1254size_t bulk_in_size; /* the size of the receive buffer */1255size_t bulk_in_filled; /* number of bytes in the buffer */1256size_t bulk_in_copied; /* already copied to user space */1257__u8 bulk_in_endpoint_addr; /* the address of the bulk in endpoint */1258__u8 bulk_out_endpoint_addr; /* the address of the bulk out endpoint */1259int errors; /* the last request tanked */1260bool ongoing_read; /* a read is going on */1261spinlock_t err_lock; /* lock for errors */1262struct kref kref;1263struct mutex io_mutex; /* synchronize I/O with disconnect */1264wait_queue_head_t bulk_in_wait; /* to wait for an ongoing read */1265};12661267#define to_skel_dev(d) container_of(d, struct usb_skel, kref)12681269static struct usb_driver skel_driver;1270static void skel_draw_down(struct usb_skel *dev);12711272static void skel_delete(struct kref *kref)1273{1274struct usb_skel *dev = to_skel_dev(kref);12751276usb_free_urb(dev->bulk_in_urb);1277usb_put_dev(dev->udev);1278kfree(dev->bulk_in_buffer);1279kfree(dev);1280}12811282/*1283* skel_do_open() - to be called by usb_gpib_attach1284*/12851286static int skel_do_open(struct gpib_board *board, int subminor)1287{1288struct usb_skel *dev;1289struct usb_interface *interface;1290int retval = 0;12911292interface = usb_find_interface(&skel_driver, subminor);1293if (!interface) {1294dev_err(board->gpib_dev, "can't find device for minor %d\n", subminor);1295retval = -ENODEV;1296goto exit;1297}12981299dev = usb_get_intfdata(interface);1300if (!dev) {1301retval = -ENODEV;1302goto exit;1303}13041305retval = usb_autopm_get_interface(interface);1306if (retval)1307goto exit;13081309/* increment our usage count for the device */1310kref_get(&dev->kref);13111312/* save our object in the file's private structure */1313GPIB_DEV = dev;13141315exit:1316return retval;1317}13181319/*1320* skel_do_release() - to be called by usb_gpib_detach1321*/13221323static int skel_do_release(struct gpib_board *board)1324{1325struct usb_skel *dev;13261327dev = GPIB_DEV;1328if (!dev)1329return -ENODEV;13301331/* allow the device to be autosuspended */1332mutex_lock(&dev->io_mutex);1333if (dev->interface)1334usb_autopm_put_interface(dev->interface);1335mutex_unlock(&dev->io_mutex);13361337/* decrement the count on our device */1338kref_put(&dev->kref, skel_delete);1339return 0;1340}13411342/*1343* read functions1344*/13451346static void skel_read_bulk_callback(struct urb *urb)1347{1348struct usb_skel *dev;1349unsigned long flags;13501351dev = urb->context;13521353spin_lock_irqsave(&dev->err_lock, flags);1354/* sync/async unlink faults aren't errors */1355if (urb->status) {1356if (!(urb->status == -ENOENT ||1357urb->status == -ECONNRESET ||1358urb->status == -ESHUTDOWN))1359dev_err(&dev->interface->dev, "nonzero read bulk status received: %d\n",1360urb->status);13611362dev->errors = urb->status;1363} else {1364dev->bulk_in_filled = urb->actual_length;1365}1366dev->ongoing_read = 0;1367spin_unlock_irqrestore(&dev->err_lock, flags);13681369wake_up_interruptible(&dev->bulk_in_wait);1370}13711372static int skel_do_read_io(struct usb_skel *dev, size_t count)1373{1374int rv;13751376/* prepare a read */1377usb_fill_bulk_urb(dev->bulk_in_urb,1378dev->udev,1379usb_rcvbulkpipe(dev->udev,1380dev->bulk_in_endpoint_addr),1381dev->bulk_in_buffer,1382min(dev->bulk_in_size, count),1383skel_read_bulk_callback,1384dev);1385/* tell everybody to leave the URB alone */1386spin_lock_irq(&dev->err_lock);1387dev->ongoing_read = 1;1388spin_unlock_irq(&dev->err_lock);13891390/* submit bulk in urb, which means no data to deliver */1391dev->bulk_in_filled = 0;1392dev->bulk_in_copied = 0;13931394/* do it */1395rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL);1396if (rv < 0) {1397dev_err(&dev->interface->dev, "failed submitting read urb, error %d\n", rv);1398rv = (rv == -ENOMEM) ? rv : -EIO;1399spin_lock_irq(&dev->err_lock);1400dev->ongoing_read = 0;1401spin_unlock_irq(&dev->err_lock);1402}14031404return rv;1405}14061407/*1408* skel_do_read() - read operations from lpvo_usb_gpib1409*/14101411static ssize_t skel_do_read(struct usb_skel *dev, char *buffer, size_t count)1412{1413int rv;1414bool ongoing_io;14151416/* if we cannot read at all, return EOF */14171418if (!dev->bulk_in_urb || !count)1419return 0;14201421restart: /* added to comply with ftdi timeout technique */14221423/* no concurrent readers */14241425rv = mutex_lock_interruptible(&dev->io_mutex);1426if (rv < 0)1427return rv;14281429if (!dev->interface) { /* disconnect() was called */1430rv = -ENODEV;1431goto exit;1432}14331434retry:1435/* if IO is under way, we must not touch things */1436spin_lock_irq(&dev->err_lock);1437ongoing_io = dev->ongoing_read;1438spin_unlock_irq(&dev->err_lock);14391440if (ongoing_io) {1441// /* nonblocking IO shall not wait */1442// /* no file, no O_NONBLOCK; maybe provide when from user space */1443// if (file->f_flags & O_NONBLOCK) {1444// rv = -EAGAIN;1445// goto exit;1446// }14471448/*1449* IO may take forever1450* hence wait in an interruptible state1451*/1452rv = wait_event_interruptible(dev->bulk_in_wait, (!dev->ongoing_read));1453if (rv < 0)1454goto exit;1455}14561457/* errors must be reported */1458rv = dev->errors;1459if (rv < 0) {1460/* any error is reported once */1461dev->errors = 0;1462/* to preserve notifications about reset */1463rv = (rv == -EPIPE) ? rv : -EIO;1464/* report it */1465goto exit;1466}14671468/*1469* if the buffer is filled we may satisfy the read1470* else we need to start IO1471*/14721473if (dev->bulk_in_filled) {1474/* we had read data */14751476size_t available = dev->bulk_in_filled - dev->bulk_in_copied;1477// size_t chunk = min(available, count); /* compute chunk later */1478size_t chunk;14791480if (!available) {1481/*1482* all data has been used1483* actual IO needs to be done1484*/1485/*1486* it seems that requests for less than dev->bulk_in_size1487* are not accepted1488*/1489rv = skel_do_read_io(dev, dev->bulk_in_size);1490if (rv < 0)1491goto exit;1492else1493goto retry;1494}14951496/*1497* data is available - chunk tells us how much shall be copied1498*/14991500/*1501* Condition dev->bulk_in_copied > 0 maybe will never happen. In case,1502* signal the event and copy using the original procedure, i.e., copy1503* first two bytes also1504*/15051506if (dev->bulk_in_copied) {1507chunk = min(available, count);1508memcpy(buffer, dev->bulk_in_buffer + dev->bulk_in_copied, chunk);1509rv = chunk;1510dev->bulk_in_copied += chunk;15111512/* copy discarding first two bytes that contain ftdi chip status */15131514} else {1515/* account for two bytes to be discarded */1516chunk = min(available, count + 2);1517if (chunk < 2) {1518dev_err(&dev->udev->dev, "BAD READ - chunk: %zu\n", chunk);1519rv = -EIO;1520goto exit;1521}15221523memcpy(buffer, dev->bulk_in_buffer + 2, chunk - 2);1524rv = chunk;1525dev->bulk_in_copied += chunk;1526}15271528/*1529* if we are asked for more than we have,1530* we start IO but don't wait1531*1532* No, no read ahead allowed; if the case, more data will be1533* asked for by the lpvo_usb_gpib layer.1534*/1535// if (available < count)1536// skel_do_read_io(dev, dev->bulk_in_size);1537} else {1538/* no data in the buffer */1539rv = skel_do_read_io(dev, dev->bulk_in_size);1540if (rv < 0)1541goto exit;1542else1543goto retry;1544}1545exit:1546mutex_unlock(&dev->io_mutex);1547if (rv == 2)1548goto restart; /* ftdi chip returns two status bytes after a latency anyhow */15491550if (rv > 0)1551return rv - 2; /* account for 2 discarded bytes in a valid buffer */1552return rv;1553}15541555/*1556* write functions1557*/15581559static void skel_write_bulk_callback(struct urb *urb)1560{1561struct usb_skel *dev;1562unsigned long flags;15631564dev = urb->context;15651566/* sync/async unlink faults aren't errors */1567if (urb->status) {1568if (!(urb->status == -ENOENT ||1569urb->status == -ECONNRESET ||1570urb->status == -ESHUTDOWN))1571dev_err(&dev->interface->dev,1572"nonzero write bulk status received: %d\n", urb->status);15731574spin_lock_irqsave(&dev->err_lock, flags);1575dev->errors = urb->status;1576spin_unlock_irqrestore(&dev->err_lock, flags);1577}15781579/* free up our allocated buffer */1580usb_free_coherent(urb->dev, urb->transfer_buffer_length,1581urb->transfer_buffer, urb->transfer_dma);1582up(&dev->limit_sem);1583}15841585/*1586* skel_do_write() - write operations from lpvo_usb_gpib1587*/15881589static ssize_t skel_do_write(struct usb_skel *dev, const char *buffer, size_t count)1590{1591int retval = 0;1592struct urb *urb = NULL;1593char *buf = NULL;1594size_t writesize = min_t(size_t, count, (size_t)MAX_TRANSFER);15951596/* verify that we actually have some data to write */1597if (count == 0)1598goto exit;15991600/*1601* limit the number of URBs in flight to stop a user from using up all1602* RAM1603*/1604/* Only one URB is used, because we can't have a pending write() and go on */16051606// if (!(file->f_flags & O_NONBLOCK)) { /* no NONBLOCK provided */1607if (down_interruptible(&dev->limit_sem)) {1608retval = -ERESTARTSYS;1609goto exit;1610}1611// } else {1612// if (down_trylock(&dev->limit_sem)) {1613// retval = -EAGAIN;1614// goto exit;1615// }1616// }16171618spin_lock_irq(&dev->err_lock);1619retval = dev->errors;1620if (retval < 0) {1621/* any error is reported once */1622dev->errors = 0;1623/* to preserve notifications about reset */1624retval = (retval == -EPIPE) ? retval : -EIO;1625}1626spin_unlock_irq(&dev->err_lock);1627if (retval < 0)1628goto error;16291630/* create a urb, and a buffer for it, and copy the data to the urb */1631urb = usb_alloc_urb(0, GFP_KERNEL);1632if (!urb) {1633retval = -ENOMEM;1634goto error;1635}16361637buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL,1638&urb->transfer_dma);1639if (!buf) {1640retval = -ENOMEM;1641goto error;1642}16431644memcpy(buf, buffer, count);16451646/* this lock makes sure we don't submit URBs to gone devices */1647mutex_lock(&dev->io_mutex);1648if (!dev->interface) { /* disconnect() was called */1649mutex_unlock(&dev->io_mutex);1650retval = -ENODEV;1651goto error;1652}16531654/* initialize the urb properly */1655usb_fill_bulk_urb(urb, dev->udev,1656usb_sndbulkpipe(dev->udev, dev->bulk_out_endpoint_addr),1657buf, writesize, skel_write_bulk_callback, dev);1658urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;1659usb_anchor_urb(urb, &dev->submitted);16601661/* send the data out the bulk port */1662retval = usb_submit_urb(urb, GFP_KERNEL);1663mutex_unlock(&dev->io_mutex);1664if (retval) {1665dev_err(&dev->interface->dev, "failed submitting write urb, error %d\n", retval);1666goto error_unanchor;1667}16681669/*1670* release our reference to this urb, the USB core will eventually free1671* it entirely1672*/1673usb_free_urb(urb);16741675return writesize;16761677error_unanchor:1678usb_unanchor_urb(urb);1679error:1680if (urb) {1681usb_free_coherent(dev->udev, writesize, buf, urb->transfer_dma);1682usb_free_urb(urb);1683}1684up(&dev->limit_sem);16851686exit:1687return retval;1688}16891690/*1691* services for the user space devices1692*/16931694#if USER_DEVICE /* conditional compilation of user space device */16951696static int skel_flush(struct file *file, fl_owner_t id)1697{1698struct usb_skel *dev;1699int res;17001701dev = file->private_data;1702if (!dev)1703return -ENODEV;17041705/* wait for io to stop */1706mutex_lock(&dev->io_mutex);1707skel_draw_down(dev);17081709/* read out errors, leave subsequent opens a clean slate */1710spin_lock_irq(&dev->err_lock);1711res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0;1712dev->errors = 0;1713spin_unlock_irq(&dev->err_lock);17141715mutex_unlock(&dev->io_mutex);17161717return res;1718}17191720static int skel_open(struct inode *inode, struct file *file)1721{1722struct usb_skel *dev;1723struct usb_interface *interface;1724int subminor;1725int retval = 0;17261727subminor = iminor(inode);17281729interface = usb_find_interface(&skel_driver, subminor);1730if (!interface) {1731pr_err("can't find device for minor %d\n", subminor);1732retval = -ENODEV;1733goto exit;1734}17351736dev = usb_get_intfdata(interface);1737if (!dev) {1738retval = -ENODEV;1739goto exit;1740}17411742retval = usb_autopm_get_interface(interface);1743if (retval)1744goto exit;17451746/* increment our usage count for the device */1747kref_get(&dev->kref);17481749/* save our object in the file's private structure */1750file->private_data = dev;17511752exit:1753return retval;1754}17551756static int skel_release(struct inode *inode, struct file *file)1757{1758struct usb_skel *dev;17591760dev = file->private_data;1761if (!dev)1762return -ENODEV;17631764/* allow the device to be autosuspended */1765mutex_lock(&dev->io_mutex);1766if (dev->interface)1767usb_autopm_put_interface(dev->interface);1768mutex_unlock(&dev->io_mutex);17691770/* decrement the count on our device */1771kref_put(&dev->kref, skel_delete);1772return 0;1773}17741775/*1776* user space access to read function1777*/17781779static ssize_t skel_read(struct file *file, char __user *buffer, size_t count,1780loff_t *ppos)1781{1782struct usb_skel *dev;1783char *buf;1784ssize_t rv;17851786dev = file->private_data;17871788buf = kmalloc(count, GFP_KERNEL);1789if (!buf)1790return -ENOMEM;17911792rv = skel_do_read(dev, buf, count);17931794if (rv > 0) {1795if (copy_to_user(buffer, buf, rv)) {1796kfree(buf);1797return -EFAULT;1798}1799}1800kfree(buf);1801return rv;1802}18031804/*1805* user space access to write function1806*/18071808static ssize_t skel_write(struct file *file, const char __user *user_buffer,1809size_t count, loff_t *ppos)1810{1811struct usb_skel *dev;1812char *buf;1813ssize_t rv;18141815dev = file->private_data;18161817buf = kmalloc(count, GFP_KERNEL);1818if (!buf)1819return -ENOMEM;18201821if (copy_from_user(buf, user_buffer, count)) {1822kfree(buf);1823return -EFAULT;1824}18251826rv = skel_do_write(dev, buf, count);1827kfree(buf);1828return rv;1829}1830#endif18311832static const struct file_operations skel_fops = {1833.owner = THIS_MODULE,1834#if USER_DEVICE1835.read = skel_read,1836.write = skel_write,1837.open = skel_open,1838.release = skel_release,1839.flush = skel_flush,1840.llseek = noop_llseek,1841#endif1842};18431844/*1845* usb class driver info in order to get a minor number from the usb core,1846* and to have the device registered with the driver core1847*/1848#if USER_DEVICE1849static struct usb_class_driver skel_class = {1850.name = "lpvo_raw%d",1851.fops = &skel_fops,1852.minor_base = USB_SKEL_MINOR_BASE,1853};1854#endif18551856static int skel_probe(struct usb_interface *interface,1857const struct usb_device_id *id)1858{1859struct usb_skel *dev;1860struct usb_endpoint_descriptor *bulk_in, *bulk_out;1861int retval;1862char *device_path;18631864mutex_init(&minors_lock); /* required for handling minor numbers table */18651866/* allocate memory for our device state and initialize it */1867dev = kzalloc_obj(*dev);1868if (!dev)1869return -ENOMEM;18701871kref_init(&dev->kref);1872sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);1873mutex_init(&dev->io_mutex);1874spin_lock_init(&dev->err_lock);1875init_usb_anchor(&dev->submitted);1876init_waitqueue_head(&dev->bulk_in_wait);18771878dev->udev = usb_get_dev(interface_to_usbdev(interface));1879dev->interface = interface;18801881/* set up the endpoint information */1882/* use only the first bulk-in and bulk-out endpoints */1883retval = usb_find_common_endpoints(interface->cur_altsetting,1884&bulk_in, &bulk_out, NULL, NULL);1885if (retval) {1886dev_err(&interface->dev,1887"Could not find both bulk-in and bulk-out endpoints\n");1888goto error;1889}18901891dev->bulk_in_size = usb_endpoint_maxp(bulk_in);1892dev->bulk_in_endpoint_addr = bulk_in->bEndpointAddress;1893dev->bulk_in_buffer = kmalloc(dev->bulk_in_size, GFP_KERNEL);1894if (!dev->bulk_in_buffer) {1895retval = -ENOMEM;1896goto error;1897}1898dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL);1899if (!dev->bulk_in_urb) {1900retval = -ENOMEM;1901goto error;1902}19031904dev->bulk_out_endpoint_addr = bulk_out->bEndpointAddress;19051906/* save our data pointer in this interface device */1907usb_set_intfdata(interface, dev);19081909/* let the world know */19101911device_path = kobject_get_path(&dev->udev->dev.kobj, GFP_KERNEL);1912dev_dbg(&interface->dev, "New lpvo_usb_device -> bus: %d dev: %d path: %s\n",1913dev->udev->bus->busnum, dev->udev->devnum, device_path);1914kfree(device_path);19151916#if USER_DEVICE1917/* we can register the device now, as it is ready */1918retval = usb_register_dev(interface, &skel_class);1919if (retval) {1920/* something prevented us from registering this driver */1921dev_err(&interface->dev,1922"Not able to get a minor for this device.\n");1923usb_set_intfdata(interface, NULL);1924goto error;1925}1926#endif19271928write_latency_timer(dev->udev); /* adjust the latency timer */19291930usb_gpib_init_module(interface); /* last, init the lpvo for this minor */19311932return 0;19331934error:1935/* this frees allocated memory */1936kref_put(&dev->kref, skel_delete);19371938return retval;1939}19401941static void skel_disconnect(struct usb_interface *interface)1942{1943struct usb_skel *dev;1944int minor = interface->minor;19451946usb_gpib_exit_module(minor); /* first, disactivate the lpvo */19471948dev = usb_get_intfdata(interface);1949usb_set_intfdata(interface, NULL);19501951#if USER_DEVICE1952/* give back our minor */1953usb_deregister_dev(interface, &skel_class);1954#endif19551956/* prevent more I/O from starting */1957mutex_lock(&dev->io_mutex);1958dev->interface = NULL;1959mutex_unlock(&dev->io_mutex);19601961usb_kill_anchored_urbs(&dev->submitted);19621963/* decrement our usage count */1964kref_put(&dev->kref, skel_delete);1965}19661967static void skel_draw_down(struct usb_skel *dev)1968{1969int time;19701971time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);1972if (!time)1973usb_kill_anchored_urbs(&dev->submitted);1974usb_kill_urb(dev->bulk_in_urb);1975}19761977static int skel_suspend(struct usb_interface *intf, pm_message_t message)1978{1979struct usb_skel *dev = usb_get_intfdata(intf);19801981if (!dev)1982return 0;1983skel_draw_down(dev);1984return 0;1985}19861987static int skel_resume(struct usb_interface *intf)1988{1989return 0;1990}19911992static int skel_pre_reset(struct usb_interface *intf)1993{1994struct usb_skel *dev = usb_get_intfdata(intf);19951996mutex_lock(&dev->io_mutex);1997skel_draw_down(dev);19981999return 0;2000}20012002static int skel_post_reset(struct usb_interface *intf)2003{2004struct usb_skel *dev = usb_get_intfdata(intf);20052006/* we are sure no URBs are active - no locking needed */2007dev->errors = -EPIPE;2008mutex_unlock(&dev->io_mutex);20092010return 0;2011}20122013static struct usb_driver skel_driver = {2014.name = NAME,2015.probe = skel_probe,2016.disconnect = skel_disconnect,2017.suspend = skel_suspend,2018.resume = skel_resume,2019.pre_reset = skel_pre_reset,2020.post_reset = skel_post_reset,2021.id_table = skel_table,2022.supports_autosuspend = 1,2023};20242025module_usb_driver(skel_driver);202620272028