ksdazzle-sir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /*****************************************************************************
  2. *
  3. * Filename: ksdazzle.c
  4. * Version: 0.1.2
  5. * Description: Irda KingSun Dazzle USB Dongle
  6. * Status: Experimental
  7. * Author: Alex Villacís Lasso <a_villacis@palosanto.com>
  8. *
  9. * Based on stir4200, mcs7780, kingsun-sir drivers.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. *****************************************************************************/
  25. /*
  26. * Following is my most current (2007-07-26) understanding of how the Kingsun
  27. * 07D0:4100 dongle (sometimes known as the MA-660) is supposed to work. This
  28. * information was deduced by examining the USB traffic captured with USBSnoopy
  29. * from the WinXP driver. Feel free to update here as more of the dongle is
  30. * known.
  31. *
  32. * General: This dongle exposes one interface with two interrupt endpoints, one
  33. * IN and one OUT. In this regard, it is similar to what the Kingsun/Donshine
  34. * dongle (07c0:4200) exposes. Traffic is raw and needs to be wrapped and
  35. * unwrapped manually as in stir4200, kingsun-sir, and ks959-sir.
  36. *
  37. * Transmission: To transmit an IrDA frame, it is necessary to wrap it, then
  38. * split it into multiple segments of up to 7 bytes each, and transmit each in
  39. * sequence. It seems that sending a single big block (like kingsun-sir does)
  40. * won't work with this dongle. Each segment needs to be prefixed with a value
  41. * equal to (unsigned char)0xF8 + <number of bytes in segment>, inside a payload
  42. * of exactly 8 bytes. For example, a segment of 1 byte gets prefixed by 0xF9,
  43. * and one of 7 bytes gets prefixed by 0xFF. The bytes at the end of the
  44. * payload, not considered by the prefix, are ignored (set to 0 by this
  45. * implementation).
  46. *
  47. * Reception: To receive data, the driver must poll the dongle regularly (like
  48. * kingsun-sir.c) with interrupt URBs. If data is available, it will be returned
  49. * in payloads from 0 to 8 bytes long. When concatenated, these payloads form
  50. * a raw IrDA stream that needs to be unwrapped as in stir4200 and kingsun-sir
  51. *
  52. * Speed change: To change the speed of the dongle, the driver prepares a
  53. * control URB with the following as a setup packet:
  54. * bRequestType USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE
  55. * bRequest 0x09
  56. * wValue 0x0200
  57. * wIndex 0x0001
  58. * wLength 0x0008 (length of the payload)
  59. * The payload is a 8-byte record, apparently identical to the one used in
  60. * drivers/usb/serial/cypress_m8.c to change speed:
  61. * __u32 baudSpeed;
  62. * unsigned int dataBits : 2; // 0 - 5 bits 3 - 8 bits
  63. * unsigned int : 1;
  64. * unsigned int stopBits : 1;
  65. * unsigned int parityEnable : 1;
  66. * unsigned int parityType : 1;
  67. * unsigned int : 1;
  68. * unsigned int reset : 1;
  69. * unsigned char reserved[3]; // set to 0
  70. *
  71. * For now only SIR speeds have been observed with this dongle. Therefore,
  72. * nothing is known on what changes (if any) must be done to frame wrapping /
  73. * unwrapping for higher than SIR speeds. This driver assumes no change is
  74. * necessary and announces support for all the way to 115200 bps.
  75. */
  76. #include <linux/module.h>
  77. #include <linux/moduleparam.h>
  78. #include <linux/kernel.h>
  79. #include <linux/types.h>
  80. #include <linux/errno.h>
  81. #include <linux/init.h>
  82. #include <linux/slab.h>
  83. #include <linux/usb.h>
  84. #include <linux/device.h>
  85. #include <linux/crc32.h>
  86. #include <asm/unaligned.h>
  87. #include <asm/byteorder.h>
  88. #include <asm/uaccess.h>
  89. #include <net/irda/irda.h>
  90. #include <net/irda/wrapper.h>
  91. #include <net/irda/crc.h>
  92. #define KSDAZZLE_VENDOR_ID 0x07d0
  93. #define KSDAZZLE_PRODUCT_ID 0x4100
  94. /* These are the currently known USB ids */
  95. static struct usb_device_id dongles[] = {
  96. /* KingSun Co,Ltd IrDA/USB Bridge */
  97. {USB_DEVICE(KSDAZZLE_VENDOR_ID, KSDAZZLE_PRODUCT_ID)},
  98. {}
  99. };
  100. MODULE_DEVICE_TABLE(usb, dongles);
  101. #define KINGSUN_MTT 0x07
  102. #define KINGSUN_REQ_RECV 0x01
  103. #define KINGSUN_REQ_SEND 0x09
  104. #define KINGSUN_SND_FIFO_SIZE 2048 /* Max packet we can send */
  105. #define KINGSUN_RCV_MAX 2048 /* Max transfer we can receive */
  106. struct ksdazzle_speedparams {
  107. __le32 baudrate; /* baud rate, little endian */
  108. __u8 flags;
  109. __u8 reserved[3];
  110. } __packed;
  111. #define KS_DATA_5_BITS 0x00
  112. #define KS_DATA_6_BITS 0x01
  113. #define KS_DATA_7_BITS 0x02
  114. #define KS_DATA_8_BITS 0x03
  115. #define KS_STOP_BITS_1 0x00
  116. #define KS_STOP_BITS_2 0x08
  117. #define KS_PAR_DISABLE 0x00
  118. #define KS_PAR_EVEN 0x10
  119. #define KS_PAR_ODD 0x30
  120. #define KS_RESET 0x80
  121. #define KINGSUN_EP_IN 0
  122. #define KINGSUN_EP_OUT 1
  123. struct ksdazzle_cb {
  124. struct usb_device *usbdev; /* init: probe_irda */
  125. struct net_device *netdev; /* network layer */
  126. struct irlap_cb *irlap; /* The link layer we are binded to */
  127. struct qos_info qos;
  128. struct urb *tx_urb;
  129. __u8 *tx_buf_clear;
  130. unsigned int tx_buf_clear_used;
  131. unsigned int tx_buf_clear_sent;
  132. __u8 tx_payload[8];
  133. struct urb *rx_urb;
  134. __u8 *rx_buf;
  135. iobuff_t rx_unwrap_buff;
  136. struct usb_ctrlrequest *speed_setuprequest;
  137. struct urb *speed_urb;
  138. struct ksdazzle_speedparams speedparams;
  139. unsigned int new_speed;
  140. __u8 ep_in;
  141. __u8 ep_out;
  142. spinlock_t lock;
  143. int receiving;
  144. };
  145. /* Callback transmission routine */
  146. static void ksdazzle_speed_irq(struct urb *urb)
  147. {
  148. /* unlink, shutdown, unplug, other nasties */
  149. if (urb->status != 0) {
  150. err("ksdazzle_speed_irq: urb asynchronously failed - %d",
  151. urb->status);
  152. }
  153. }
  154. /* Send a control request to change speed of the dongle */
  155. static int ksdazzle_change_speed(struct ksdazzle_cb *kingsun, unsigned speed)
  156. {
  157. static unsigned int supported_speeds[] = { 2400, 9600, 19200, 38400,
  158. 57600, 115200, 576000, 1152000, 4000000, 0
  159. };
  160. int err;
  161. unsigned int i;
  162. if (kingsun->speed_setuprequest == NULL || kingsun->speed_urb == NULL)
  163. return -ENOMEM;
  164. /* Check that requested speed is among the supported ones */
  165. for (i = 0; supported_speeds[i] && supported_speeds[i] != speed; i++) ;
  166. if (supported_speeds[i] == 0)
  167. return -EOPNOTSUPP;
  168. memset(&(kingsun->speedparams), 0, sizeof(struct ksdazzle_speedparams));
  169. kingsun->speedparams.baudrate = cpu_to_le32(speed);
  170. kingsun->speedparams.flags = KS_DATA_8_BITS;
  171. /* speed_setuprequest pre-filled in ksdazzle_probe */
  172. usb_fill_control_urb(kingsun->speed_urb, kingsun->usbdev,
  173. usb_sndctrlpipe(kingsun->usbdev, 0),
  174. (unsigned char *)kingsun->speed_setuprequest,
  175. &(kingsun->speedparams),
  176. sizeof(struct ksdazzle_speedparams),
  177. ksdazzle_speed_irq, kingsun);
  178. kingsun->speed_urb->status = 0;
  179. err = usb_submit_urb(kingsun->speed_urb, GFP_ATOMIC);
  180. return err;
  181. }
  182. /* Submit one fragment of an IrDA frame to the dongle */
  183. static void ksdazzle_send_irq(struct urb *urb);
  184. static int ksdazzle_submit_tx_fragment(struct ksdazzle_cb *kingsun)
  185. {
  186. unsigned int wraplen;
  187. int ret;
  188. /* We can send at most 7 bytes of payload at a time */
  189. wraplen = 7;
  190. if (wraplen > kingsun->tx_buf_clear_used)
  191. wraplen = kingsun->tx_buf_clear_used;
  192. /* Prepare payload prefix with used length */
  193. memset(kingsun->tx_payload, 0, 8);
  194. kingsun->tx_payload[0] = (unsigned char)0xf8 + wraplen;
  195. memcpy(kingsun->tx_payload + 1, kingsun->tx_buf_clear, wraplen);
  196. usb_fill_int_urb(kingsun->tx_urb, kingsun->usbdev,
  197. usb_sndintpipe(kingsun->usbdev, kingsun->ep_out),
  198. kingsun->tx_payload, 8, ksdazzle_send_irq, kingsun, 1);
  199. kingsun->tx_urb->status = 0;
  200. ret = usb_submit_urb(kingsun->tx_urb, GFP_ATOMIC);
  201. /* Remember how much data was sent, in order to update at callback */
  202. kingsun->tx_buf_clear_sent = (ret == 0) ? wraplen : 0;
  203. return ret;
  204. }
  205. /* Callback transmission routine */
  206. static void ksdazzle_send_irq(struct urb *urb)
  207. {
  208. struct ksdazzle_cb *kingsun = urb->context;
  209. struct net_device *netdev = kingsun->netdev;
  210. int ret = 0;
  211. /* in process of stopping, just drop data */
  212. if (!netif_running(kingsun->netdev)) {
  213. err("ksdazzle_send_irq: Network not running!");
  214. return;
  215. }
  216. /* unlink, shutdown, unplug, other nasties */
  217. if (urb->status != 0) {
  218. err("ksdazzle_send_irq: urb asynchronously failed - %d",
  219. urb->status);
  220. return;
  221. }
  222. if (kingsun->tx_buf_clear_used > 0) {
  223. /* Update data remaining to be sent */
  224. if (kingsun->tx_buf_clear_sent < kingsun->tx_buf_clear_used) {
  225. memmove(kingsun->tx_buf_clear,
  226. kingsun->tx_buf_clear +
  227. kingsun->tx_buf_clear_sent,
  228. kingsun->tx_buf_clear_used -
  229. kingsun->tx_buf_clear_sent);
  230. }
  231. kingsun->tx_buf_clear_used -= kingsun->tx_buf_clear_sent;
  232. kingsun->tx_buf_clear_sent = 0;
  233. if (kingsun->tx_buf_clear_used > 0) {
  234. /* There is more data to be sent */
  235. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  236. err("ksdazzle_send_irq: failed tx_urb submit: %d", ret);
  237. switch (ret) {
  238. case -ENODEV:
  239. case -EPIPE:
  240. break;
  241. default:
  242. netdev->stats.tx_errors++;
  243. netif_start_queue(netdev);
  244. }
  245. }
  246. } else {
  247. /* All data sent, send next speed && wake network queue */
  248. if (kingsun->new_speed != -1 &&
  249. cpu_to_le32(kingsun->new_speed) !=
  250. kingsun->speedparams.baudrate)
  251. ksdazzle_change_speed(kingsun,
  252. kingsun->new_speed);
  253. netif_wake_queue(netdev);
  254. }
  255. }
  256. }
  257. /*
  258. * Called from net/core when new frame is available.
  259. */
  260. static netdev_tx_t ksdazzle_hard_xmit(struct sk_buff *skb,
  261. struct net_device *netdev)
  262. {
  263. struct ksdazzle_cb *kingsun;
  264. unsigned int wraplen;
  265. int ret = 0;
  266. netif_stop_queue(netdev);
  267. /* the IRDA wrapping routines don't deal with non linear skb */
  268. SKB_LINEAR_ASSERT(skb);
  269. kingsun = netdev_priv(netdev);
  270. spin_lock(&kingsun->lock);
  271. kingsun->new_speed = irda_get_next_speed(skb);
  272. /* Append data to the end of whatever data remains to be transmitted */
  273. wraplen =
  274. async_wrap_skb(skb, kingsun->tx_buf_clear, KINGSUN_SND_FIFO_SIZE);
  275. kingsun->tx_buf_clear_used = wraplen;
  276. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  277. err("ksdazzle_hard_xmit: failed tx_urb submit: %d", ret);
  278. switch (ret) {
  279. case -ENODEV:
  280. case -EPIPE:
  281. break;
  282. default:
  283. netdev->stats.tx_errors++;
  284. netif_start_queue(netdev);
  285. }
  286. } else {
  287. netdev->stats.tx_packets++;
  288. netdev->stats.tx_bytes += skb->len;
  289. }
  290. dev_kfree_skb(skb);
  291. spin_unlock(&kingsun->lock);
  292. return NETDEV_TX_OK;
  293. }
  294. /* Receive callback function */
  295. static void ksdazzle_rcv_irq(struct urb *urb)
  296. {
  297. struct ksdazzle_cb *kingsun = urb->context;
  298. struct net_device *netdev = kingsun->netdev;
  299. /* in process of stopping, just drop data */
  300. if (!netif_running(netdev)) {
  301. kingsun->receiving = 0;
  302. return;
  303. }
  304. /* unlink, shutdown, unplug, other nasties */
  305. if (urb->status != 0) {
  306. err("ksdazzle_rcv_irq: urb asynchronously failed - %d",
  307. urb->status);
  308. kingsun->receiving = 0;
  309. return;
  310. }
  311. if (urb->actual_length > 0) {
  312. __u8 *bytes = urb->transfer_buffer;
  313. unsigned int i;
  314. for (i = 0; i < urb->actual_length; i++) {
  315. async_unwrap_char(netdev, &netdev->stats,
  316. &kingsun->rx_unwrap_buff, bytes[i]);
  317. }
  318. kingsun->receiving =
  319. (kingsun->rx_unwrap_buff.state != OUTSIDE_FRAME) ? 1 : 0;
  320. }
  321. /* This urb has already been filled in ksdazzle_net_open. It is assumed that
  322. urb keeps the pointer to the payload buffer.
  323. */
  324. urb->status = 0;
  325. usb_submit_urb(urb, GFP_ATOMIC);
  326. }
  327. /*
  328. * Function ksdazzle_net_open (dev)
  329. *
  330. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  331. */
  332. static int ksdazzle_net_open(struct net_device *netdev)
  333. {
  334. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  335. int err = -ENOMEM;
  336. char hwname[16];
  337. /* At this point, urbs are NULL, and skb is NULL (see ksdazzle_probe) */
  338. kingsun->receiving = 0;
  339. /* Initialize for SIR to copy data directly into skb. */
  340. kingsun->rx_unwrap_buff.in_frame = FALSE;
  341. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  342. kingsun->rx_unwrap_buff.truesize = IRDA_SKB_MAX_MTU;
  343. kingsun->rx_unwrap_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
  344. if (!kingsun->rx_unwrap_buff.skb)
  345. goto free_mem;
  346. skb_reserve(kingsun->rx_unwrap_buff.skb, 1);
  347. kingsun->rx_unwrap_buff.head = kingsun->rx_unwrap_buff.skb->data;
  348. kingsun->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  349. if (!kingsun->rx_urb)
  350. goto free_mem;
  351. kingsun->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  352. if (!kingsun->tx_urb)
  353. goto free_mem;
  354. kingsun->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
  355. if (!kingsun->speed_urb)
  356. goto free_mem;
  357. /* Initialize speed for dongle */
  358. kingsun->new_speed = 9600;
  359. err = ksdazzle_change_speed(kingsun, 9600);
  360. if (err < 0)
  361. goto free_mem;
  362. /*
  363. * Now that everything should be initialized properly,
  364. * Open new IrLAP layer instance to take care of us...
  365. */
  366. sprintf(hwname, "usb#%d", kingsun->usbdev->devnum);
  367. kingsun->irlap = irlap_open(netdev, &kingsun->qos, hwname);
  368. if (!kingsun->irlap) {
  369. err("ksdazzle-sir: irlap_open failed");
  370. goto free_mem;
  371. }
  372. /* Start reception. */
  373. usb_fill_int_urb(kingsun->rx_urb, kingsun->usbdev,
  374. usb_rcvintpipe(kingsun->usbdev, kingsun->ep_in),
  375. kingsun->rx_buf, KINGSUN_RCV_MAX, ksdazzle_rcv_irq,
  376. kingsun, 1);
  377. kingsun->rx_urb->status = 0;
  378. err = usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
  379. if (err) {
  380. err("ksdazzle-sir: first urb-submit failed: %d", err);
  381. goto close_irlap;
  382. }
  383. netif_start_queue(netdev);
  384. /* Situation at this point:
  385. - all work buffers allocated
  386. - urbs allocated and ready to fill
  387. - max rx packet known (in max_rx)
  388. - unwrap state machine initialized, in state outside of any frame
  389. - receive request in progress
  390. - IrLAP layer started, about to hand over packets to send
  391. */
  392. return 0;
  393. close_irlap:
  394. irlap_close(kingsun->irlap);
  395. free_mem:
  396. usb_free_urb(kingsun->speed_urb);
  397. kingsun->speed_urb = NULL;
  398. usb_free_urb(kingsun->tx_urb);
  399. kingsun->tx_urb = NULL;
  400. usb_free_urb(kingsun->rx_urb);
  401. kingsun->rx_urb = NULL;
  402. if (kingsun->rx_unwrap_buff.skb) {
  403. kfree_skb(kingsun->rx_unwrap_buff.skb);
  404. kingsun->rx_unwrap_buff.skb = NULL;
  405. kingsun->rx_unwrap_buff.head = NULL;
  406. }
  407. return err;
  408. }
  409. /*
  410. * Function ksdazzle_net_close (dev)
  411. *
  412. * Network device is taken down. Usually this is done by
  413. * "ifconfig irda0 down"
  414. */
  415. static int ksdazzle_net_close(struct net_device *netdev)
  416. {
  417. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  418. /* Stop transmit processing */
  419. netif_stop_queue(netdev);
  420. /* Mop up receive && transmit urb's */
  421. usb_kill_urb(kingsun->tx_urb);
  422. usb_free_urb(kingsun->tx_urb);
  423. kingsun->tx_urb = NULL;
  424. usb_kill_urb(kingsun->speed_urb);
  425. usb_free_urb(kingsun->speed_urb);
  426. kingsun->speed_urb = NULL;
  427. usb_kill_urb(kingsun->rx_urb);
  428. usb_free_urb(kingsun->rx_urb);
  429. kingsun->rx_urb = NULL;
  430. kfree_skb(kingsun->rx_unwrap_buff.skb);
  431. kingsun->rx_unwrap_buff.skb = NULL;
  432. kingsun->rx_unwrap_buff.head = NULL;
  433. kingsun->rx_unwrap_buff.in_frame = FALSE;
  434. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  435. kingsun->receiving = 0;
  436. /* Stop and remove instance of IrLAP */
  437. irlap_close(kingsun->irlap);
  438. kingsun->irlap = NULL;
  439. return 0;
  440. }
  441. /*
  442. * IOCTLs : Extra out-of-band network commands...
  443. */
  444. static int ksdazzle_net_ioctl(struct net_device *netdev, struct ifreq *rq,
  445. int cmd)
  446. {
  447. struct if_irda_req *irq = (struct if_irda_req *)rq;
  448. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  449. int ret = 0;
  450. switch (cmd) {
  451. case SIOCSBANDWIDTH: /* Set bandwidth */
  452. if (!capable(CAP_NET_ADMIN))
  453. return -EPERM;
  454. /* Check if the device is still there */
  455. if (netif_device_present(kingsun->netdev))
  456. return ksdazzle_change_speed(kingsun,
  457. irq->ifr_baudrate);
  458. break;
  459. case SIOCSMEDIABUSY: /* Set media busy */
  460. if (!capable(CAP_NET_ADMIN))
  461. return -EPERM;
  462. /* Check if the IrDA stack is still there */
  463. if (netif_running(kingsun->netdev))
  464. irda_device_set_media_busy(kingsun->netdev, TRUE);
  465. break;
  466. case SIOCGRECEIVING:
  467. /* Only approximately true */
  468. irq->ifr_receiving = kingsun->receiving;
  469. break;
  470. default:
  471. ret = -EOPNOTSUPP;
  472. }
  473. return ret;
  474. }
  475. static const struct net_device_ops ksdazzle_ops = {
  476. .ndo_start_xmit = ksdazzle_hard_xmit,
  477. .ndo_open = ksdazzle_net_open,
  478. .ndo_stop = ksdazzle_net_close,
  479. .ndo_do_ioctl = ksdazzle_net_ioctl,
  480. };
  481. /*
  482. * This routine is called by the USB subsystem for each new device
  483. * in the system. We need to check if the device is ours, and in
  484. * this case start handling it.
  485. */
  486. static int ksdazzle_probe(struct usb_interface *intf,
  487. const struct usb_device_id *id)
  488. {
  489. struct usb_host_interface *interface;
  490. struct usb_endpoint_descriptor *endpoint;
  491. struct usb_device *dev = interface_to_usbdev(intf);
  492. struct ksdazzle_cb *kingsun = NULL;
  493. struct net_device *net = NULL;
  494. int ret = -ENOMEM;
  495. int pipe, maxp_in, maxp_out;
  496. __u8 ep_in;
  497. __u8 ep_out;
  498. /* Check that there really are two interrupt endpoints. Check based on the
  499. one in drivers/usb/input/usbmouse.c
  500. */
  501. interface = intf->cur_altsetting;
  502. if (interface->desc.bNumEndpoints != 2) {
  503. err("ksdazzle: expected 2 endpoints, found %d",
  504. interface->desc.bNumEndpoints);
  505. return -ENODEV;
  506. }
  507. endpoint = &interface->endpoint[KINGSUN_EP_IN].desc;
  508. if (!usb_endpoint_is_int_in(endpoint)) {
  509. err("ksdazzle: endpoint 0 is not interrupt IN");
  510. return -ENODEV;
  511. }
  512. ep_in = endpoint->bEndpointAddress;
  513. pipe = usb_rcvintpipe(dev, ep_in);
  514. maxp_in = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  515. if (maxp_in > 255 || maxp_in <= 1) {
  516. err("ksdazzle: endpoint 0 has max packet size %d not in range [2..255]", maxp_in);
  517. return -ENODEV;
  518. }
  519. endpoint = &interface->endpoint[KINGSUN_EP_OUT].desc;
  520. if (!usb_endpoint_is_int_out(endpoint)) {
  521. err("ksdazzle: endpoint 1 is not interrupt OUT");
  522. return -ENODEV;
  523. }
  524. ep_out = endpoint->bEndpointAddress;
  525. pipe = usb_sndintpipe(dev, ep_out);
  526. maxp_out = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  527. /* Allocate network device container. */
  528. net = alloc_irdadev(sizeof(*kingsun));
  529. if (!net)
  530. goto err_out1;
  531. SET_NETDEV_DEV(net, &intf->dev);
  532. kingsun = netdev_priv(net);
  533. kingsun->netdev = net;
  534. kingsun->usbdev = dev;
  535. kingsun->ep_in = ep_in;
  536. kingsun->ep_out = ep_out;
  537. kingsun->irlap = NULL;
  538. kingsun->tx_urb = NULL;
  539. kingsun->tx_buf_clear = NULL;
  540. kingsun->tx_buf_clear_used = 0;
  541. kingsun->tx_buf_clear_sent = 0;
  542. kingsun->rx_urb = NULL;
  543. kingsun->rx_buf = NULL;
  544. kingsun->rx_unwrap_buff.in_frame = FALSE;
  545. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  546. kingsun->rx_unwrap_buff.skb = NULL;
  547. kingsun->receiving = 0;
  548. spin_lock_init(&kingsun->lock);
  549. kingsun->speed_setuprequest = NULL;
  550. kingsun->speed_urb = NULL;
  551. kingsun->speedparams.baudrate = 0;
  552. /* Allocate input buffer */
  553. kingsun->rx_buf = kmalloc(KINGSUN_RCV_MAX, GFP_KERNEL);
  554. if (!kingsun->rx_buf)
  555. goto free_mem;
  556. /* Allocate output buffer */
  557. kingsun->tx_buf_clear = kmalloc(KINGSUN_SND_FIFO_SIZE, GFP_KERNEL);
  558. if (!kingsun->tx_buf_clear)
  559. goto free_mem;
  560. /* Allocate and initialize speed setup packet */
  561. kingsun->speed_setuprequest =
  562. kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  563. if (!kingsun->speed_setuprequest)
  564. goto free_mem;
  565. kingsun->speed_setuprequest->bRequestType =
  566. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  567. kingsun->speed_setuprequest->bRequest = KINGSUN_REQ_SEND;
  568. kingsun->speed_setuprequest->wValue = cpu_to_le16(0x0200);
  569. kingsun->speed_setuprequest->wIndex = cpu_to_le16(0x0001);
  570. kingsun->speed_setuprequest->wLength =
  571. cpu_to_le16(sizeof(struct ksdazzle_speedparams));
  572. printk(KERN_INFO "KingSun/Dazzle IRDA/USB found at address %d, "
  573. "Vendor: %x, Product: %x\n",
  574. dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
  575. le16_to_cpu(dev->descriptor.idProduct));
  576. /* Initialize QoS for this device */
  577. irda_init_max_qos_capabilies(&kingsun->qos);
  578. /* Baud rates known to be supported. Please uncomment if devices (other
  579. than a SonyEriccson K300 phone) can be shown to support higher speeds
  580. with this dongle.
  581. */
  582. kingsun->qos.baud_rate.bits =
  583. IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200;
  584. kingsun->qos.min_turn_time.bits &= KINGSUN_MTT;
  585. irda_qos_bits_to_value(&kingsun->qos);
  586. /* Override the network functions we need to use */
  587. net->netdev_ops = &ksdazzle_ops;
  588. ret = register_netdev(net);
  589. if (ret != 0)
  590. goto free_mem;
  591. dev_info(&net->dev, "IrDA: Registered KingSun/Dazzle device %s\n",
  592. net->name);
  593. usb_set_intfdata(intf, kingsun);
  594. /* Situation at this point:
  595. - all work buffers allocated
  596. - setup requests pre-filled
  597. - urbs not allocated, set to NULL
  598. - max rx packet known (is KINGSUN_FIFO_SIZE)
  599. - unwrap state machine (partially) initialized, but skb == NULL
  600. */
  601. return 0;
  602. free_mem:
  603. kfree(kingsun->speed_setuprequest);
  604. kfree(kingsun->tx_buf_clear);
  605. kfree(kingsun->rx_buf);
  606. free_netdev(net);
  607. err_out1:
  608. return ret;
  609. }
  610. /*
  611. * The current device is removed, the USB layer tell us to shut it down...
  612. */
  613. static void ksdazzle_disconnect(struct usb_interface *intf)
  614. {
  615. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  616. if (!kingsun)
  617. return;
  618. unregister_netdev(kingsun->netdev);
  619. /* Mop up receive && transmit urb's */
  620. usb_kill_urb(kingsun->speed_urb);
  621. usb_free_urb(kingsun->speed_urb);
  622. kingsun->speed_urb = NULL;
  623. usb_kill_urb(kingsun->tx_urb);
  624. usb_free_urb(kingsun->tx_urb);
  625. kingsun->tx_urb = NULL;
  626. usb_kill_urb(kingsun->rx_urb);
  627. usb_free_urb(kingsun->rx_urb);
  628. kingsun->rx_urb = NULL;
  629. kfree(kingsun->speed_setuprequest);
  630. kfree(kingsun->tx_buf_clear);
  631. kfree(kingsun->rx_buf);
  632. free_netdev(kingsun->netdev);
  633. usb_set_intfdata(intf, NULL);
  634. }
  635. #ifdef CONFIG_PM
  636. /* USB suspend, so power off the transmitter/receiver */
  637. static int ksdazzle_suspend(struct usb_interface *intf, pm_message_t message)
  638. {
  639. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  640. netif_device_detach(kingsun->netdev);
  641. if (kingsun->speed_urb != NULL)
  642. usb_kill_urb(kingsun->speed_urb);
  643. if (kingsun->tx_urb != NULL)
  644. usb_kill_urb(kingsun->tx_urb);
  645. if (kingsun->rx_urb != NULL)
  646. usb_kill_urb(kingsun->rx_urb);
  647. return 0;
  648. }
  649. /* Coming out of suspend, so reset hardware */
  650. static int ksdazzle_resume(struct usb_interface *intf)
  651. {
  652. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  653. if (kingsun->rx_urb != NULL) {
  654. /* Setup request already filled in ksdazzle_probe */
  655. usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
  656. }
  657. netif_device_attach(kingsun->netdev);
  658. return 0;
  659. }
  660. #endif
  661. /*
  662. * USB device callbacks
  663. */
  664. static struct usb_driver irda_driver = {
  665. .name = "ksdazzle-sir",
  666. .probe = ksdazzle_probe,
  667. .disconnect = ksdazzle_disconnect,
  668. .id_table = dongles,
  669. #ifdef CONFIG_PM
  670. .suspend = ksdazzle_suspend,
  671. .resume = ksdazzle_resume,
  672. #endif
  673. };
  674. module_usb_driver(irda_driver);
  675. MODULE_AUTHOR("Alex Villacís Lasso <a_villacis@palosanto.com>");
  676. MODULE_DESCRIPTION("IrDA-USB Dongle Driver for KingSun Dazzle");
  677. MODULE_LICENSE("GPL");