Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Driver for Rio Karma
0004  *
0005  *   (c) 2006 Bob Copeland <me@bobcopeland.com>
0006  *   (c) 2006 Keith Bennett <keith@mcs.st-and.ac.uk>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/slab.h>
0011 
0012 #include <scsi/scsi.h>
0013 #include <scsi/scsi_cmnd.h>
0014 #include <scsi/scsi_device.h>
0015 
0016 #include "usb.h"
0017 #include "transport.h"
0018 #include "debug.h"
0019 #include "scsiglue.h"
0020 
0021 #define DRV_NAME "ums-karma"
0022 
0023 MODULE_DESCRIPTION("Driver for Rio Karma");
0024 MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>, Keith Bennett <keith@mcs.st-and.ac.uk>");
0025 MODULE_LICENSE("GPL");
0026 MODULE_IMPORT_NS(USB_STORAGE);
0027 
0028 #define RIO_PREFIX "RIOP\x00"
0029 #define RIO_PREFIX_LEN 5
0030 #define RIO_SEND_LEN 40
0031 #define RIO_RECV_LEN 0x200
0032 
0033 #define RIO_ENTER_STORAGE 0x1
0034 #define RIO_LEAVE_STORAGE 0x2
0035 #define RIO_RESET 0xC
0036 
0037 struct karma_data {
0038     int in_storage;
0039     char *recv;
0040 };
0041 
0042 static int rio_karma_init(struct us_data *us);
0043 
0044 
0045 /*
0046  * The table of devices
0047  */
0048 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
0049             vendorName, productName, useProtocol, useTransport, \
0050             initFunction, flags) \
0051 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
0052   .driver_info = (flags) }
0053 
0054 static struct usb_device_id karma_usb_ids[] = {
0055 #   include "unusual_karma.h"
0056     { }     /* Terminating entry */
0057 };
0058 MODULE_DEVICE_TABLE(usb, karma_usb_ids);
0059 
0060 #undef UNUSUAL_DEV
0061 
0062 /*
0063  * The flags table
0064  */
0065 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
0066             vendor_name, product_name, use_protocol, use_transport, \
0067             init_function, Flags) \
0068 { \
0069     .vendorName = vendor_name,  \
0070     .productName = product_name,    \
0071     .useProtocol = use_protocol,    \
0072     .useTransport = use_transport,  \
0073     .initFunction = init_function,  \
0074 }
0075 
0076 static struct us_unusual_dev karma_unusual_dev_list[] = {
0077 #   include "unusual_karma.h"
0078     { }     /* Terminating entry */
0079 };
0080 
0081 #undef UNUSUAL_DEV
0082 
0083 
0084 /*
0085  * Send commands to Rio Karma.
0086  *
0087  * For each command we send 40 bytes starting 'RIOP\0' followed by
0088  * the command number and a sequence number, which the device will ack
0089  * with a 512-byte packet with the high four bits set and everything
0090  * else null.  Then we send 'RIOP\x80' followed by a zero and the
0091  * sequence number, until byte 5 in the response repeats the sequence
0092  * number.
0093  */
0094 static int rio_karma_send_command(char cmd, struct us_data *us)
0095 {
0096     int result;
0097     unsigned long timeout;
0098     static unsigned char seq = 1;
0099     struct karma_data *data = (struct karma_data *) us->extra;
0100 
0101     usb_stor_dbg(us, "sending command %04x\n", cmd);
0102     memset(us->iobuf, 0, RIO_SEND_LEN);
0103     memcpy(us->iobuf, RIO_PREFIX, RIO_PREFIX_LEN);
0104     us->iobuf[5] = cmd;
0105     us->iobuf[6] = seq;
0106 
0107     timeout = jiffies + msecs_to_jiffies(6000);
0108     for (;;) {
0109         result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
0110             us->iobuf, RIO_SEND_LEN, NULL);
0111         if (result != USB_STOR_XFER_GOOD)
0112             goto err;
0113 
0114         result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
0115             data->recv, RIO_RECV_LEN, NULL);
0116         if (result != USB_STOR_XFER_GOOD)
0117             goto err;
0118 
0119         if (data->recv[5] == seq)
0120             break;
0121 
0122         if (time_after(jiffies, timeout))
0123             goto err;
0124 
0125         us->iobuf[4] = 0x80;
0126         us->iobuf[5] = 0;
0127         msleep(50);
0128     }
0129 
0130     seq++;
0131     if (seq == 0)
0132         seq = 1;
0133 
0134     usb_stor_dbg(us, "sent command %04x\n", cmd);
0135     return 0;
0136 err:
0137     usb_stor_dbg(us, "command %04x failed\n", cmd);
0138     return USB_STOR_TRANSPORT_FAILED;
0139 }
0140 
0141 /*
0142  * Trap START_STOP and READ_10 to leave/re-enter storage mode.
0143  * Everything else is propagated to the normal bulk layer.
0144  */
0145 static int rio_karma_transport(struct scsi_cmnd *srb, struct us_data *us)
0146 {
0147     int ret;
0148     struct karma_data *data = (struct karma_data *) us->extra;
0149 
0150     if (srb->cmnd[0] == READ_10 && !data->in_storage) {
0151         ret = rio_karma_send_command(RIO_ENTER_STORAGE, us);
0152         if (ret)
0153             return ret;
0154 
0155         data->in_storage = 1;
0156         return usb_stor_Bulk_transport(srb, us);
0157     } else if (srb->cmnd[0] == START_STOP) {
0158         ret = rio_karma_send_command(RIO_LEAVE_STORAGE, us);
0159         if (ret)
0160             return ret;
0161 
0162         data->in_storage = 0;
0163         return rio_karma_send_command(RIO_RESET, us);
0164     }
0165     return usb_stor_Bulk_transport(srb, us);
0166 }
0167 
0168 static void rio_karma_destructor(void *extra)
0169 {
0170     struct karma_data *data = (struct karma_data *) extra;
0171 
0172     kfree(data->recv);
0173 }
0174 
0175 static int rio_karma_init(struct us_data *us)
0176 {
0177     struct karma_data *data = kzalloc(sizeof(struct karma_data), GFP_NOIO);
0178 
0179     if (!data)
0180         return -ENOMEM;
0181 
0182     data->recv = kmalloc(RIO_RECV_LEN, GFP_NOIO);
0183     if (!data->recv) {
0184         kfree(data);
0185         return -ENOMEM;
0186     }
0187 
0188     us->extra = data;
0189     us->extra_destructor = rio_karma_destructor;
0190     if (rio_karma_send_command(RIO_ENTER_STORAGE, us))
0191         return -EIO;
0192 
0193     data->in_storage = 1;
0194 
0195     return 0;
0196 }
0197 
0198 static struct scsi_host_template karma_host_template;
0199 
0200 static int karma_probe(struct usb_interface *intf,
0201              const struct usb_device_id *id)
0202 {
0203     struct us_data *us;
0204     int result;
0205 
0206     result = usb_stor_probe1(&us, intf, id,
0207             (id - karma_usb_ids) + karma_unusual_dev_list,
0208             &karma_host_template);
0209     if (result)
0210         return result;
0211 
0212     us->transport_name = "Rio Karma/Bulk";
0213     us->transport = rio_karma_transport;
0214     us->transport_reset = usb_stor_Bulk_reset;
0215 
0216     result = usb_stor_probe2(us);
0217     return result;
0218 }
0219 
0220 static struct usb_driver karma_driver = {
0221     .name =     DRV_NAME,
0222     .probe =    karma_probe,
0223     .disconnect =   usb_stor_disconnect,
0224     .suspend =  usb_stor_suspend,
0225     .resume =   usb_stor_resume,
0226     .reset_resume = usb_stor_reset_resume,
0227     .pre_reset =    usb_stor_pre_reset,
0228     .post_reset =   usb_stor_post_reset,
0229     .id_table = karma_usb_ids,
0230     .soft_unbind =  1,
0231     .no_dynamic_id = 1,
0232 };
0233 
0234 module_usb_stor_driver(karma_driver, karma_host_template, DRV_NAME);