Path: blob/master/drivers/input/misc/ati_remote2.c
15109 views
/*1* ati_remote2 - ATI/Philips USB RF remote driver2*3* Copyright (C) 2005-2008 Ville Syrjala <[email protected]>4* Copyright (C) 2007-2008 Peter Stokes <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License version 28* as published by the Free Software Foundation.9*/1011#include <linux/usb/input.h>12#include <linux/slab.h>1314#define DRIVER_DESC "ATI/Philips USB RF remote driver"15#define DRIVER_VERSION "0.3"1617MODULE_DESCRIPTION(DRIVER_DESC);18MODULE_VERSION(DRIVER_VERSION);19MODULE_AUTHOR("Ville Syrjala <[email protected]>");20MODULE_LICENSE("GPL");2122/*23* ATI Remote Wonder II Channel Configuration24*25* The remote control can by assigned one of sixteen "channels" in order to facilitate26* the use of multiple remote controls within range of each other.27* A remote's "channel" may be altered by pressing and holding the "PC" button for28* approximately 3 seconds, after which the button will slowly flash the count of the29* currently configured "channel", using the numeric keypad enter a number between 1 and30* 16 and then press the "PC" button again, the button will slowly flash the count of the31* newly configured "channel".32*/3334enum {35ATI_REMOTE2_MAX_CHANNEL_MASK = 0xFFFF,36ATI_REMOTE2_MAX_MODE_MASK = 0x1F,37};3839static int ati_remote2_set_mask(const char *val,40const struct kernel_param *kp,41unsigned int max)42{43unsigned long mask;44int ret;4546if (!val)47return -EINVAL;4849ret = strict_strtoul(val, 0, &mask);50if (ret)51return ret;5253if (mask & ~max)54return -EINVAL;5556*(unsigned int *)kp->arg = mask;5758return 0;59}6061static int ati_remote2_set_channel_mask(const char *val,62const struct kernel_param *kp)63{64pr_debug("%s()\n", __func__);6566return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_CHANNEL_MASK);67}6869static int ati_remote2_get_channel_mask(char *buffer,70const struct kernel_param *kp)71{72pr_debug("%s()\n", __func__);7374return sprintf(buffer, "0x%04x", *(unsigned int *)kp->arg);75}7677static int ati_remote2_set_mode_mask(const char *val,78const struct kernel_param *kp)79{80pr_debug("%s()\n", __func__);8182return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_MODE_MASK);83}8485static int ati_remote2_get_mode_mask(char *buffer,86const struct kernel_param *kp)87{88pr_debug("%s()\n", __func__);8990return sprintf(buffer, "0x%02x", *(unsigned int *)kp->arg);91}9293static unsigned int channel_mask = ATI_REMOTE2_MAX_CHANNEL_MASK;94#define param_check_channel_mask(name, p) __param_check(name, p, unsigned int)95static struct kernel_param_ops param_ops_channel_mask = {96.set = ati_remote2_set_channel_mask,97.get = ati_remote2_get_channel_mask,98};99module_param(channel_mask, channel_mask, 0644);100MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");101102static unsigned int mode_mask = ATI_REMOTE2_MAX_MODE_MASK;103#define param_check_mode_mask(name, p) __param_check(name, p, unsigned int)104static struct kernel_param_ops param_ops_mode_mask = {105.set = ati_remote2_set_mode_mask,106.get = ati_remote2_get_mode_mask,107};108module_param(mode_mask, mode_mask, 0644);109MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");110111static struct usb_device_id ati_remote2_id_table[] = {112{ USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */113{ }114};115MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);116117static DEFINE_MUTEX(ati_remote2_mutex);118119enum {120ATI_REMOTE2_OPENED = 0x1,121ATI_REMOTE2_SUSPENDED = 0x2,122};123124enum {125ATI_REMOTE2_AUX1,126ATI_REMOTE2_AUX2,127ATI_REMOTE2_AUX3,128ATI_REMOTE2_AUX4,129ATI_REMOTE2_PC,130ATI_REMOTE2_MODES,131};132133static const struct {134u8 hw_code;135u16 keycode;136} ati_remote2_key_table[] = {137{ 0x00, KEY_0 },138{ 0x01, KEY_1 },139{ 0x02, KEY_2 },140{ 0x03, KEY_3 },141{ 0x04, KEY_4 },142{ 0x05, KEY_5 },143{ 0x06, KEY_6 },144{ 0x07, KEY_7 },145{ 0x08, KEY_8 },146{ 0x09, KEY_9 },147{ 0x0c, KEY_POWER },148{ 0x0d, KEY_MUTE },149{ 0x10, KEY_VOLUMEUP },150{ 0x11, KEY_VOLUMEDOWN },151{ 0x20, KEY_CHANNELUP },152{ 0x21, KEY_CHANNELDOWN },153{ 0x28, KEY_FORWARD },154{ 0x29, KEY_REWIND },155{ 0x2c, KEY_PLAY },156{ 0x30, KEY_PAUSE },157{ 0x31, KEY_STOP },158{ 0x37, KEY_RECORD },159{ 0x38, KEY_DVD },160{ 0x39, KEY_TV },161{ 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */162{ 0x54, KEY_MENU },163{ 0x58, KEY_UP },164{ 0x59, KEY_DOWN },165{ 0x5a, KEY_LEFT },166{ 0x5b, KEY_RIGHT },167{ 0x5c, KEY_OK },168{ 0x78, KEY_A },169{ 0x79, KEY_B },170{ 0x7a, KEY_C },171{ 0x7b, KEY_D },172{ 0x7c, KEY_E },173{ 0x7d, KEY_F },174{ 0x82, KEY_ENTER },175{ 0x8e, KEY_VENDOR },176{ 0x96, KEY_COFFEE },177{ 0xa9, BTN_LEFT },178{ 0xaa, BTN_RIGHT },179{ 0xbe, KEY_QUESTION },180{ 0xd0, KEY_EDIT },181{ 0xd5, KEY_FRONT },182{ 0xf9, KEY_INFO },183};184185struct ati_remote2 {186struct input_dev *idev;187struct usb_device *udev;188189struct usb_interface *intf[2];190struct usb_endpoint_descriptor *ep[2];191struct urb *urb[2];192void *buf[2];193dma_addr_t buf_dma[2];194195unsigned long jiffies;196int mode;197198char name[64];199char phys[64];200201/* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */202u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)];203204unsigned int flags;205206unsigned int channel_mask;207unsigned int mode_mask;208};209210static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id);211static void ati_remote2_disconnect(struct usb_interface *interface);212static int ati_remote2_suspend(struct usb_interface *interface, pm_message_t message);213static int ati_remote2_resume(struct usb_interface *interface);214static int ati_remote2_reset_resume(struct usb_interface *interface);215static int ati_remote2_pre_reset(struct usb_interface *interface);216static int ati_remote2_post_reset(struct usb_interface *interface);217218static struct usb_driver ati_remote2_driver = {219.name = "ati_remote2",220.probe = ati_remote2_probe,221.disconnect = ati_remote2_disconnect,222.id_table = ati_remote2_id_table,223.suspend = ati_remote2_suspend,224.resume = ati_remote2_resume,225.reset_resume = ati_remote2_reset_resume,226.pre_reset = ati_remote2_pre_reset,227.post_reset = ati_remote2_post_reset,228.supports_autosuspend = 1,229};230231static int ati_remote2_submit_urbs(struct ati_remote2 *ar2)232{233int r;234235r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);236if (r) {237dev_err(&ar2->intf[0]->dev,238"%s(): usb_submit_urb() = %d\n", __func__, r);239return r;240}241r = usb_submit_urb(ar2->urb[1], GFP_KERNEL);242if (r) {243usb_kill_urb(ar2->urb[0]);244dev_err(&ar2->intf[1]->dev,245"%s(): usb_submit_urb() = %d\n", __func__, r);246return r;247}248249return 0;250}251252static void ati_remote2_kill_urbs(struct ati_remote2 *ar2)253{254usb_kill_urb(ar2->urb[1]);255usb_kill_urb(ar2->urb[0]);256}257258static int ati_remote2_open(struct input_dev *idev)259{260struct ati_remote2 *ar2 = input_get_drvdata(idev);261int r;262263dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);264265r = usb_autopm_get_interface(ar2->intf[0]);266if (r) {267dev_err(&ar2->intf[0]->dev,268"%s(): usb_autopm_get_interface() = %d\n", __func__, r);269goto fail1;270}271272mutex_lock(&ati_remote2_mutex);273274if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {275r = ati_remote2_submit_urbs(ar2);276if (r)277goto fail2;278}279280ar2->flags |= ATI_REMOTE2_OPENED;281282mutex_unlock(&ati_remote2_mutex);283284usb_autopm_put_interface(ar2->intf[0]);285286return 0;287288fail2:289mutex_unlock(&ati_remote2_mutex);290usb_autopm_put_interface(ar2->intf[0]);291fail1:292return r;293}294295static void ati_remote2_close(struct input_dev *idev)296{297struct ati_remote2 *ar2 = input_get_drvdata(idev);298299dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);300301mutex_lock(&ati_remote2_mutex);302303if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))304ati_remote2_kill_urbs(ar2);305306ar2->flags &= ~ATI_REMOTE2_OPENED;307308mutex_unlock(&ati_remote2_mutex);309}310311static void ati_remote2_input_mouse(struct ati_remote2 *ar2)312{313struct input_dev *idev = ar2->idev;314u8 *data = ar2->buf[0];315int channel, mode;316317channel = data[0] >> 4;318319if (!((1 << channel) & ar2->channel_mask))320return;321322mode = data[0] & 0x0F;323324if (mode > ATI_REMOTE2_PC) {325dev_err(&ar2->intf[0]->dev,326"Unknown mode byte (%02x %02x %02x %02x)\n",327data[3], data[2], data[1], data[0]);328return;329}330331if (!((1 << mode) & ar2->mode_mask))332return;333334input_event(idev, EV_REL, REL_X, (s8) data[1]);335input_event(idev, EV_REL, REL_Y, (s8) data[2]);336input_sync(idev);337}338339static int ati_remote2_lookup(unsigned int hw_code)340{341int i;342343for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++)344if (ati_remote2_key_table[i].hw_code == hw_code)345return i;346347return -1;348}349350static void ati_remote2_input_key(struct ati_remote2 *ar2)351{352struct input_dev *idev = ar2->idev;353u8 *data = ar2->buf[1];354int channel, mode, hw_code, index;355356channel = data[0] >> 4;357358if (!((1 << channel) & ar2->channel_mask))359return;360361mode = data[0] & 0x0F;362363if (mode > ATI_REMOTE2_PC) {364dev_err(&ar2->intf[1]->dev,365"Unknown mode byte (%02x %02x %02x %02x)\n",366data[3], data[2], data[1], data[0]);367return;368}369370hw_code = data[2];371if (hw_code == 0x3f) {372/*373* For some incomprehensible reason the mouse pad generates374* events which look identical to the events from the last375* pressed mode key. Naturally we don't want to generate key376* events for the mouse pad so we filter out any subsequent377* events from the same mode key.378*/379if (ar2->mode == mode)380return;381382if (data[1] == 0)383ar2->mode = mode;384}385386if (!((1 << mode) & ar2->mode_mask))387return;388389index = ati_remote2_lookup(hw_code);390if (index < 0) {391dev_err(&ar2->intf[1]->dev,392"Unknown code byte (%02x %02x %02x %02x)\n",393data[3], data[2], data[1], data[0]);394return;395}396397switch (data[1]) {398case 0: /* release */399break;400case 1: /* press */401ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);402break;403case 2: /* repeat */404405/* No repeat for mouse buttons. */406if (ar2->keycode[mode][index] == BTN_LEFT ||407ar2->keycode[mode][index] == BTN_RIGHT)408return;409410if (!time_after_eq(jiffies, ar2->jiffies))411return;412413ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);414break;415default:416dev_err(&ar2->intf[1]->dev,417"Unknown state byte (%02x %02x %02x %02x)\n",418data[3], data[2], data[1], data[0]);419return;420}421422input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]);423input_sync(idev);424}425426static void ati_remote2_complete_mouse(struct urb *urb)427{428struct ati_remote2 *ar2 = urb->context;429int r;430431switch (urb->status) {432case 0:433usb_mark_last_busy(ar2->udev);434ati_remote2_input_mouse(ar2);435break;436case -ENOENT:437case -EILSEQ:438case -ECONNRESET:439case -ESHUTDOWN:440dev_dbg(&ar2->intf[0]->dev,441"%s(): urb status = %d\n", __func__, urb->status);442return;443default:444usb_mark_last_busy(ar2->udev);445dev_err(&ar2->intf[0]->dev,446"%s(): urb status = %d\n", __func__, urb->status);447}448449r = usb_submit_urb(urb, GFP_ATOMIC);450if (r)451dev_err(&ar2->intf[0]->dev,452"%s(): usb_submit_urb() = %d\n", __func__, r);453}454455static void ati_remote2_complete_key(struct urb *urb)456{457struct ati_remote2 *ar2 = urb->context;458int r;459460switch (urb->status) {461case 0:462usb_mark_last_busy(ar2->udev);463ati_remote2_input_key(ar2);464break;465case -ENOENT:466case -EILSEQ:467case -ECONNRESET:468case -ESHUTDOWN:469dev_dbg(&ar2->intf[1]->dev,470"%s(): urb status = %d\n", __func__, urb->status);471return;472default:473usb_mark_last_busy(ar2->udev);474dev_err(&ar2->intf[1]->dev,475"%s(): urb status = %d\n", __func__, urb->status);476}477478r = usb_submit_urb(urb, GFP_ATOMIC);479if (r)480dev_err(&ar2->intf[1]->dev,481"%s(): usb_submit_urb() = %d\n", __func__, r);482}483484static int ati_remote2_getkeycode(struct input_dev *idev,485struct input_keymap_entry *ke)486{487struct ati_remote2 *ar2 = input_get_drvdata(idev);488unsigned int mode;489int offset;490unsigned int index;491unsigned int scancode;492493if (ke->flags & INPUT_KEYMAP_BY_INDEX) {494index = ke->index;495if (index >= ATI_REMOTE2_MODES *496ARRAY_SIZE(ati_remote2_key_table))497return -EINVAL;498499mode = ke->index / ARRAY_SIZE(ati_remote2_key_table);500offset = ke->index % ARRAY_SIZE(ati_remote2_key_table);501scancode = (mode << 8) + ati_remote2_key_table[offset].hw_code;502} else {503if (input_scancode_to_scalar(ke, &scancode))504return -EINVAL;505506mode = scancode >> 8;507if (mode > ATI_REMOTE2_PC)508return -EINVAL;509510offset = ati_remote2_lookup(scancode & 0xff);511if (offset < 0)512return -EINVAL;513514index = mode * ARRAY_SIZE(ati_remote2_key_table) + offset;515}516517ke->keycode = ar2->keycode[mode][offset];518ke->len = sizeof(scancode);519memcpy(&ke->scancode, &scancode, sizeof(scancode));520ke->index = index;521522return 0;523}524525static int ati_remote2_setkeycode(struct input_dev *idev,526const struct input_keymap_entry *ke,527unsigned int *old_keycode)528{529struct ati_remote2 *ar2 = input_get_drvdata(idev);530unsigned int mode;531int offset;532unsigned int index;533unsigned int scancode;534535if (ke->flags & INPUT_KEYMAP_BY_INDEX) {536if (ke->index >= ATI_REMOTE2_MODES *537ARRAY_SIZE(ati_remote2_key_table))538return -EINVAL;539540mode = ke->index / ARRAY_SIZE(ati_remote2_key_table);541offset = ke->index % ARRAY_SIZE(ati_remote2_key_table);542} else {543if (input_scancode_to_scalar(ke, &scancode))544return -EINVAL;545546mode = scancode >> 8;547if (mode > ATI_REMOTE2_PC)548return -EINVAL;549550offset = ati_remote2_lookup(scancode & 0xff);551if (offset < 0)552return -EINVAL;553}554555*old_keycode = ar2->keycode[mode][offset];556ar2->keycode[mode][offset] = ke->keycode;557__set_bit(ke->keycode, idev->keybit);558559for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {560for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {561if (ar2->keycode[mode][index] == *old_keycode)562return 0;563}564}565566__clear_bit(*old_keycode, idev->keybit);567568return 0;569}570571static int ati_remote2_input_init(struct ati_remote2 *ar2)572{573struct input_dev *idev;574int index, mode, retval;575576idev = input_allocate_device();577if (!idev)578return -ENOMEM;579580ar2->idev = idev;581input_set_drvdata(idev, ar2);582583idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);584idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |585BIT_MASK(BTN_RIGHT);586idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);587588for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {589for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {590ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode;591__set_bit(ar2->keycode[mode][index], idev->keybit);592}593}594595/* AUX1-AUX4 and PC generate the same scancode. */596index = ati_remote2_lookup(0x3f);597ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1;598ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2;599ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3;600ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4;601ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC;602__set_bit(KEY_PROG1, idev->keybit);603__set_bit(KEY_PROG2, idev->keybit);604__set_bit(KEY_PROG3, idev->keybit);605__set_bit(KEY_PROG4, idev->keybit);606__set_bit(KEY_PC, idev->keybit);607608idev->rep[REP_DELAY] = 250;609idev->rep[REP_PERIOD] = 33;610611idev->open = ati_remote2_open;612idev->close = ati_remote2_close;613614idev->getkeycode = ati_remote2_getkeycode;615idev->setkeycode = ati_remote2_setkeycode;616617idev->name = ar2->name;618idev->phys = ar2->phys;619620usb_to_input_id(ar2->udev, &idev->id);621idev->dev.parent = &ar2->udev->dev;622623retval = input_register_device(idev);624if (retval)625input_free_device(idev);626627return retval;628}629630static int ati_remote2_urb_init(struct ati_remote2 *ar2)631{632struct usb_device *udev = ar2->udev;633int i, pipe, maxp;634635for (i = 0; i < 2; i++) {636ar2->buf[i] = usb_alloc_coherent(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);637if (!ar2->buf[i])638return -ENOMEM;639640ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);641if (!ar2->urb[i])642return -ENOMEM;643644pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);645maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));646maxp = maxp > 4 ? 4 : maxp;647648usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,649i ? ati_remote2_complete_key : ati_remote2_complete_mouse,650ar2, ar2->ep[i]->bInterval);651ar2->urb[i]->transfer_dma = ar2->buf_dma[i];652ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;653}654655return 0;656}657658static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)659{660int i;661662for (i = 0; i < 2; i++) {663usb_free_urb(ar2->urb[i]);664usb_free_coherent(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);665}666}667668static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask)669{670int r, i, channel;671672/*673* Configure receiver to only accept input from remote "channel"674* channel == 0 -> Accept input from any remote channel675* channel == 1 -> Only accept input from remote channel 1676* channel == 2 -> Only accept input from remote channel 2677* ...678* channel == 16 -> Only accept input from remote channel 16679*/680681channel = 0;682for (i = 0; i < 16; i++) {683if ((1 << i) & ch_mask) {684if (!(~(1 << i) & ch_mask))685channel = i + 1;686break;687}688}689690r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),6910x20,692USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,693channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);694if (r) {695dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",696__func__, r);697return r;698}699700return 0;701}702703static ssize_t ati_remote2_show_channel_mask(struct device *dev,704struct device_attribute *attr,705char *buf)706{707struct usb_device *udev = to_usb_device(dev);708struct usb_interface *intf = usb_ifnum_to_if(udev, 0);709struct ati_remote2 *ar2 = usb_get_intfdata(intf);710711return sprintf(buf, "0x%04x\n", ar2->channel_mask);712}713714static ssize_t ati_remote2_store_channel_mask(struct device *dev,715struct device_attribute *attr,716const char *buf, size_t count)717{718struct usb_device *udev = to_usb_device(dev);719struct usb_interface *intf = usb_ifnum_to_if(udev, 0);720struct ati_remote2 *ar2 = usb_get_intfdata(intf);721unsigned long mask;722int r;723724if (strict_strtoul(buf, 0, &mask))725return -EINVAL;726727if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK)728return -EINVAL;729730r = usb_autopm_get_interface(ar2->intf[0]);731if (r) {732dev_err(&ar2->intf[0]->dev,733"%s(): usb_autopm_get_interface() = %d\n", __func__, r);734return r;735}736737mutex_lock(&ati_remote2_mutex);738739if (mask != ar2->channel_mask) {740r = ati_remote2_setup(ar2, mask);741if (!r)742ar2->channel_mask = mask;743}744745mutex_unlock(&ati_remote2_mutex);746747usb_autopm_put_interface(ar2->intf[0]);748749return r ? r : count;750}751752static ssize_t ati_remote2_show_mode_mask(struct device *dev,753struct device_attribute *attr,754char *buf)755{756struct usb_device *udev = to_usb_device(dev);757struct usb_interface *intf = usb_ifnum_to_if(udev, 0);758struct ati_remote2 *ar2 = usb_get_intfdata(intf);759760return sprintf(buf, "0x%02x\n", ar2->mode_mask);761}762763static ssize_t ati_remote2_store_mode_mask(struct device *dev,764struct device_attribute *attr,765const char *buf, size_t count)766{767struct usb_device *udev = to_usb_device(dev);768struct usb_interface *intf = usb_ifnum_to_if(udev, 0);769struct ati_remote2 *ar2 = usb_get_intfdata(intf);770unsigned long mask;771772if (strict_strtoul(buf, 0, &mask))773return -EINVAL;774775if (mask & ~ATI_REMOTE2_MAX_MODE_MASK)776return -EINVAL;777778ar2->mode_mask = mask;779780return count;781}782783static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask,784ati_remote2_store_channel_mask);785786static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask,787ati_remote2_store_mode_mask);788789static struct attribute *ati_remote2_attrs[] = {790&dev_attr_channel_mask.attr,791&dev_attr_mode_mask.attr,792NULL,793};794795static struct attribute_group ati_remote2_attr_group = {796.attrs = ati_remote2_attrs,797};798799static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)800{801struct usb_device *udev = interface_to_usbdev(interface);802struct usb_host_interface *alt = interface->cur_altsetting;803struct ati_remote2 *ar2;804int r;805806if (alt->desc.bInterfaceNumber)807return -ENODEV;808809ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);810if (!ar2)811return -ENOMEM;812813ar2->udev = udev;814815ar2->intf[0] = interface;816ar2->ep[0] = &alt->endpoint[0].desc;817818ar2->intf[1] = usb_ifnum_to_if(udev, 1);819r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);820if (r)821goto fail1;822alt = ar2->intf[1]->cur_altsetting;823ar2->ep[1] = &alt->endpoint[0].desc;824825r = ati_remote2_urb_init(ar2);826if (r)827goto fail2;828829ar2->channel_mask = channel_mask;830ar2->mode_mask = mode_mask;831832r = ati_remote2_setup(ar2, ar2->channel_mask);833if (r)834goto fail2;835836usb_make_path(udev, ar2->phys, sizeof(ar2->phys));837strlcat(ar2->phys, "/input0", sizeof(ar2->phys));838839strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));840841r = sysfs_create_group(&udev->dev.kobj, &ati_remote2_attr_group);842if (r)843goto fail2;844845r = ati_remote2_input_init(ar2);846if (r)847goto fail3;848849usb_set_intfdata(interface, ar2);850851interface->needs_remote_wakeup = 1;852853return 0;854855fail3:856sysfs_remove_group(&udev->dev.kobj, &ati_remote2_attr_group);857fail2:858ati_remote2_urb_cleanup(ar2);859usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);860fail1:861kfree(ar2);862863return r;864}865866static void ati_remote2_disconnect(struct usb_interface *interface)867{868struct ati_remote2 *ar2;869struct usb_host_interface *alt = interface->cur_altsetting;870871if (alt->desc.bInterfaceNumber)872return;873874ar2 = usb_get_intfdata(interface);875usb_set_intfdata(interface, NULL);876877input_unregister_device(ar2->idev);878879sysfs_remove_group(&ar2->udev->dev.kobj, &ati_remote2_attr_group);880881ati_remote2_urb_cleanup(ar2);882883usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);884885kfree(ar2);886}887888static int ati_remote2_suspend(struct usb_interface *interface,889pm_message_t message)890{891struct ati_remote2 *ar2;892struct usb_host_interface *alt = interface->cur_altsetting;893894if (alt->desc.bInterfaceNumber)895return 0;896897ar2 = usb_get_intfdata(interface);898899dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);900901mutex_lock(&ati_remote2_mutex);902903if (ar2->flags & ATI_REMOTE2_OPENED)904ati_remote2_kill_urbs(ar2);905906ar2->flags |= ATI_REMOTE2_SUSPENDED;907908mutex_unlock(&ati_remote2_mutex);909910return 0;911}912913static int ati_remote2_resume(struct usb_interface *interface)914{915struct ati_remote2 *ar2;916struct usb_host_interface *alt = interface->cur_altsetting;917int r = 0;918919if (alt->desc.bInterfaceNumber)920return 0;921922ar2 = usb_get_intfdata(interface);923924dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);925926mutex_lock(&ati_remote2_mutex);927928if (ar2->flags & ATI_REMOTE2_OPENED)929r = ati_remote2_submit_urbs(ar2);930931if (!r)932ar2->flags &= ~ATI_REMOTE2_SUSPENDED;933934mutex_unlock(&ati_remote2_mutex);935936return r;937}938939static int ati_remote2_reset_resume(struct usb_interface *interface)940{941struct ati_remote2 *ar2;942struct usb_host_interface *alt = interface->cur_altsetting;943int r = 0;944945if (alt->desc.bInterfaceNumber)946return 0;947948ar2 = usb_get_intfdata(interface);949950dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);951952mutex_lock(&ati_remote2_mutex);953954r = ati_remote2_setup(ar2, ar2->channel_mask);955if (r)956goto out;957958if (ar2->flags & ATI_REMOTE2_OPENED)959r = ati_remote2_submit_urbs(ar2);960961if (!r)962ar2->flags &= ~ATI_REMOTE2_SUSPENDED;963964out:965mutex_unlock(&ati_remote2_mutex);966967return r;968}969970static int ati_remote2_pre_reset(struct usb_interface *interface)971{972struct ati_remote2 *ar2;973struct usb_host_interface *alt = interface->cur_altsetting;974975if (alt->desc.bInterfaceNumber)976return 0;977978ar2 = usb_get_intfdata(interface);979980dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);981982mutex_lock(&ati_remote2_mutex);983984if (ar2->flags == ATI_REMOTE2_OPENED)985ati_remote2_kill_urbs(ar2);986987return 0;988}989990static int ati_remote2_post_reset(struct usb_interface *interface)991{992struct ati_remote2 *ar2;993struct usb_host_interface *alt = interface->cur_altsetting;994int r = 0;995996if (alt->desc.bInterfaceNumber)997return 0;998999ar2 = usb_get_intfdata(interface);10001001dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);10021003if (ar2->flags == ATI_REMOTE2_OPENED)1004r = ati_remote2_submit_urbs(ar2);10051006mutex_unlock(&ati_remote2_mutex);10071008return r;1009}10101011static int __init ati_remote2_init(void)1012{1013int r;10141015r = usb_register(&ati_remote2_driver);1016if (r)1017printk(KERN_ERR "ati_remote2: usb_register() = %d\n", r);1018else1019printk(KERN_INFO "ati_remote2: " DRIVER_DESC " " DRIVER_VERSION "\n");10201021return r;1022}10231024static void __exit ati_remote2_exit(void)1025{1026usb_deregister(&ati_remote2_driver);1027}10281029module_init(ati_remote2_init);1030module_exit(ati_remote2_exit);103110321033