ksdazzle-sir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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/kref.h>
  84. #include <linux/usb.h>
  85. #include <linux/device.h>
  86. #include <linux/crc32.h>
  87. #include <asm/unaligned.h>
  88. #include <asm/byteorder.h>
  89. #include <asm/uaccess.h>
  90. #include <net/irda/irda.h>
  91. #include <net/irda/wrapper.h>
  92. #include <net/irda/crc.h>
  93. #define KSDAZZLE_VENDOR_ID 0x07d0
  94. #define KSDAZZLE_PRODUCT_ID 0x4100
  95. /* These are the currently known USB ids */
  96. static struct usb_device_id dongles[] = {
  97. /* KingSun Co,Ltd IrDA/USB Bridge */
  98. {USB_DEVICE(KSDAZZLE_VENDOR_ID, KSDAZZLE_PRODUCT_ID)},
  99. {}
  100. };
  101. MODULE_DEVICE_TABLE(usb, dongles);
  102. #define KINGSUN_MTT 0x07
  103. #define KINGSUN_REQ_RECV 0x01
  104. #define KINGSUN_REQ_SEND 0x09
  105. #define KINGSUN_SND_FIFO_SIZE 2048 /* Max packet we can send */
  106. #define KINGSUN_RCV_MAX 2048 /* Max transfer we can receive */
  107. struct ksdazzle_speedparams {
  108. __le32 baudrate; /* baud rate, little endian */
  109. __u8 flags;
  110. __u8 reserved[3];
  111. } __attribute__ ((packed));
  112. #define KS_DATA_5_BITS 0x00
  113. #define KS_DATA_6_BITS 0x01
  114. #define KS_DATA_7_BITS 0x02
  115. #define KS_DATA_8_BITS 0x03
  116. #define KS_STOP_BITS_1 0x00
  117. #define KS_STOP_BITS_2 0x08
  118. #define KS_PAR_DISABLE 0x00
  119. #define KS_PAR_EVEN 0x10
  120. #define KS_PAR_ODD 0x30
  121. #define KS_RESET 0x80
  122. #define KINGSUN_EP_IN 0
  123. #define KINGSUN_EP_OUT 1
  124. struct ksdazzle_cb {
  125. struct usb_device *usbdev; /* init: probe_irda */
  126. struct net_device *netdev; /* network layer */
  127. struct irlap_cb *irlap; /* The link layer we are binded to */
  128. struct net_device_stats stats; /* network statistics */
  129. struct qos_info qos;
  130. struct urb *tx_urb;
  131. __u8 *tx_buf_clear;
  132. unsigned int tx_buf_clear_used;
  133. unsigned int tx_buf_clear_sent;
  134. __u8 tx_payload[8];
  135. struct urb *rx_urb;
  136. __u8 *rx_buf;
  137. iobuff_t rx_unwrap_buff;
  138. struct usb_ctrlrequest *speed_setuprequest;
  139. struct urb *speed_urb;
  140. struct ksdazzle_speedparams speedparams;
  141. unsigned int new_speed;
  142. __u8 ep_in;
  143. __u8 ep_out;
  144. spinlock_t lock;
  145. int receiving;
  146. };
  147. /* Callback transmission routine */
  148. static void ksdazzle_speed_irq(struct urb *urb)
  149. {
  150. /* unlink, shutdown, unplug, other nasties */
  151. if (urb->status != 0) {
  152. err("ksdazzle_speed_irq: urb asynchronously failed - %d",
  153. urb->status);
  154. }
  155. }
  156. /* Send a control request to change speed of the dongle */
  157. static int ksdazzle_change_speed(struct ksdazzle_cb *kingsun, unsigned speed)
  158. {
  159. static unsigned int supported_speeds[] = { 2400, 9600, 19200, 38400,
  160. 57600, 115200, 576000, 1152000, 4000000, 0
  161. };
  162. int err;
  163. unsigned int i;
  164. if (kingsun->speed_setuprequest == NULL || kingsun->speed_urb == NULL)
  165. return -ENOMEM;
  166. /* Check that requested speed is among the supported ones */
  167. for (i = 0; supported_speeds[i] && supported_speeds[i] != speed; i++) ;
  168. if (supported_speeds[i] == 0)
  169. return -EOPNOTSUPP;
  170. memset(&(kingsun->speedparams), 0, sizeof(struct ksdazzle_speedparams));
  171. kingsun->speedparams.baudrate = cpu_to_le32(speed);
  172. kingsun->speedparams.flags = KS_DATA_8_BITS;
  173. /* speed_setuprequest pre-filled in ksdazzle_probe */
  174. usb_fill_control_urb(kingsun->speed_urb, kingsun->usbdev,
  175. usb_sndctrlpipe(kingsun->usbdev, 0),
  176. (unsigned char *)kingsun->speed_setuprequest,
  177. &(kingsun->speedparams),
  178. sizeof(struct ksdazzle_speedparams),
  179. ksdazzle_speed_irq, kingsun);
  180. kingsun->speed_urb->status = 0;
  181. err = usb_submit_urb(kingsun->speed_urb, GFP_ATOMIC);
  182. return err;
  183. }
  184. /* Submit one fragment of an IrDA frame to the dongle */
  185. static void ksdazzle_send_irq(struct urb *urb);
  186. static int ksdazzle_submit_tx_fragment(struct ksdazzle_cb *kingsun)
  187. {
  188. unsigned int wraplen;
  189. int ret;
  190. /* We can send at most 7 bytes of payload at a time */
  191. wraplen = 7;
  192. if (wraplen > kingsun->tx_buf_clear_used)
  193. wraplen = kingsun->tx_buf_clear_used;
  194. /* Prepare payload prefix with used length */
  195. memset(kingsun->tx_payload, 0, 8);
  196. kingsun->tx_payload[0] = (unsigned char)0xf8 + wraplen;
  197. memcpy(kingsun->tx_payload + 1, kingsun->tx_buf_clear, wraplen);
  198. usb_fill_int_urb(kingsun->tx_urb, kingsun->usbdev,
  199. usb_sndintpipe(kingsun->usbdev, kingsun->ep_out),
  200. kingsun->tx_payload, 8, ksdazzle_send_irq, kingsun, 1);
  201. kingsun->tx_urb->status = 0;
  202. ret = usb_submit_urb(kingsun->tx_urb, GFP_ATOMIC);
  203. /* Remember how much data was sent, in order to update at callback */
  204. kingsun->tx_buf_clear_sent = (ret == 0) ? wraplen : 0;
  205. return ret;
  206. }
  207. /* Callback transmission routine */
  208. static void ksdazzle_send_irq(struct urb *urb)
  209. {
  210. struct ksdazzle_cb *kingsun = urb->context;
  211. struct net_device *netdev = kingsun->netdev;
  212. int ret = 0;
  213. /* in process of stopping, just drop data */
  214. if (!netif_running(kingsun->netdev)) {
  215. err("ksdazzle_send_irq: Network not running!");
  216. return;
  217. }
  218. /* unlink, shutdown, unplug, other nasties */
  219. if (urb->status != 0) {
  220. err("ksdazzle_send_irq: urb asynchronously failed - %d",
  221. urb->status);
  222. return;
  223. }
  224. if (kingsun->tx_buf_clear_used > 0) {
  225. /* Update data remaining to be sent */
  226. if (kingsun->tx_buf_clear_sent < kingsun->tx_buf_clear_used) {
  227. memmove(kingsun->tx_buf_clear,
  228. kingsun->tx_buf_clear +
  229. kingsun->tx_buf_clear_sent,
  230. kingsun->tx_buf_clear_used -
  231. kingsun->tx_buf_clear_sent);
  232. }
  233. kingsun->tx_buf_clear_used -= kingsun->tx_buf_clear_sent;
  234. kingsun->tx_buf_clear_sent = 0;
  235. if (kingsun->tx_buf_clear_used > 0) {
  236. /* There is more data to be sent */
  237. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  238. err("ksdazzle_send_irq: failed tx_urb submit: %d", ret);
  239. switch (ret) {
  240. case -ENODEV:
  241. case -EPIPE:
  242. break;
  243. default:
  244. kingsun->stats.tx_errors++;
  245. netif_start_queue(netdev);
  246. }
  247. }
  248. } else {
  249. /* All data sent, send next speed && wake network queue */
  250. if (kingsun->new_speed != -1 &&
  251. cpu_to_le32(kingsun->new_speed) !=
  252. kingsun->speedparams.baudrate)
  253. ksdazzle_change_speed(kingsun,
  254. kingsun->new_speed);
  255. netif_wake_queue(netdev);
  256. }
  257. }
  258. }
  259. /*
  260. * Called from net/core when new frame is available.
  261. */
  262. static int ksdazzle_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
  263. {
  264. struct ksdazzle_cb *kingsun;
  265. unsigned int wraplen;
  266. int ret = 0;
  267. if (skb == NULL || netdev == NULL)
  268. return -EINVAL;
  269. netif_stop_queue(netdev);
  270. /* the IRDA wrapping routines don't deal with non linear skb */
  271. SKB_LINEAR_ASSERT(skb);
  272. kingsun = netdev_priv(netdev);
  273. spin_lock(&kingsun->lock);
  274. kingsun->new_speed = irda_get_next_speed(skb);
  275. /* Append data to the end of whatever data remains to be transmitted */
  276. wraplen =
  277. async_wrap_skb(skb, kingsun->tx_buf_clear, KINGSUN_SND_FIFO_SIZE);
  278. kingsun->tx_buf_clear_used = wraplen;
  279. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  280. err("ksdazzle_hard_xmit: failed tx_urb submit: %d", ret);
  281. switch (ret) {
  282. case -ENODEV:
  283. case -EPIPE:
  284. break;
  285. default:
  286. kingsun->stats.tx_errors++;
  287. netif_start_queue(netdev);
  288. }
  289. } else {
  290. kingsun->stats.tx_packets++;
  291. kingsun->stats.tx_bytes += skb->len;
  292. }
  293. dev_kfree_skb(skb);
  294. spin_unlock(&kingsun->lock);
  295. return ret;
  296. }
  297. /* Receive callback function */
  298. static void ksdazzle_rcv_irq(struct urb *urb)
  299. {
  300. struct ksdazzle_cb *kingsun = urb->context;
  301. /* in process of stopping, just drop data */
  302. if (!netif_running(kingsun->netdev)) {
  303. kingsun->receiving = 0;
  304. return;
  305. }
  306. /* unlink, shutdown, unplug, other nasties */
  307. if (urb->status != 0) {
  308. err("ksdazzle_rcv_irq: urb asynchronously failed - %d",
  309. urb->status);
  310. kingsun->receiving = 0;
  311. return;
  312. }
  313. if (urb->actual_length > 0) {
  314. __u8 *bytes = urb->transfer_buffer;
  315. unsigned int i;
  316. for (i = 0; i < urb->actual_length; i++) {
  317. async_unwrap_char(kingsun->netdev, &kingsun->stats,
  318. &kingsun->rx_unwrap_buff, bytes[i]);
  319. }
  320. kingsun->receiving =
  321. (kingsun->rx_unwrap_buff.state != OUTSIDE_FRAME) ? 1 : 0;
  322. }
  323. /* This urb has already been filled in ksdazzle_net_open. It is assumed that
  324. urb keeps the pointer to the payload buffer.
  325. */
  326. urb->status = 0;
  327. usb_submit_urb(urb, GFP_ATOMIC);
  328. }
  329. /*
  330. * Function ksdazzle_net_open (dev)
  331. *
  332. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  333. */
  334. static int ksdazzle_net_open(struct net_device *netdev)
  335. {
  336. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  337. int err = -ENOMEM;
  338. char hwname[16];
  339. /* At this point, urbs are NULL, and skb is NULL (see ksdazzle_probe) */
  340. kingsun->receiving = 0;
  341. /* Initialize for SIR to copy data directly into skb. */
  342. kingsun->rx_unwrap_buff.in_frame = FALSE;
  343. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  344. kingsun->rx_unwrap_buff.truesize = IRDA_SKB_MAX_MTU;
  345. kingsun->rx_unwrap_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
  346. if (!kingsun->rx_unwrap_buff.skb)
  347. goto free_mem;
  348. skb_reserve(kingsun->rx_unwrap_buff.skb, 1);
  349. kingsun->rx_unwrap_buff.head = kingsun->rx_unwrap_buff.skb->data;
  350. kingsun->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  351. if (!kingsun->rx_urb)
  352. goto free_mem;
  353. kingsun->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  354. if (!kingsun->tx_urb)
  355. goto free_mem;
  356. kingsun->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
  357. if (!kingsun->speed_urb)
  358. goto free_mem;
  359. /* Initialize speed for dongle */
  360. kingsun->new_speed = 9600;
  361. err = ksdazzle_change_speed(kingsun, 9600);
  362. if (err < 0)
  363. goto free_mem;
  364. /*
  365. * Now that everything should be initialized properly,
  366. * Open new IrLAP layer instance to take care of us...
  367. */
  368. sprintf(hwname, "usb#%d", kingsun->usbdev->devnum);
  369. kingsun->irlap = irlap_open(netdev, &kingsun->qos, hwname);
  370. if (!kingsun->irlap) {
  371. err("ksdazzle-sir: irlap_open failed");
  372. goto free_mem;
  373. }
  374. /* Start reception. */
  375. usb_fill_int_urb(kingsun->rx_urb, kingsun->usbdev,
  376. usb_rcvintpipe(kingsun->usbdev, kingsun->ep_in),
  377. kingsun->rx_buf, KINGSUN_RCV_MAX, ksdazzle_rcv_irq,
  378. kingsun, 1);
  379. kingsun->rx_urb->status = 0;
  380. err = usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
  381. if (err) {
  382. err("ksdazzle-sir: first urb-submit failed: %d", err);
  383. goto close_irlap;
  384. }
  385. netif_start_queue(netdev);
  386. /* Situation at this point:
  387. - all work buffers allocated
  388. - urbs allocated and ready to fill
  389. - max rx packet known (in max_rx)
  390. - unwrap state machine initialized, in state outside of any frame
  391. - receive request in progress
  392. - IrLAP layer started, about to hand over packets to send
  393. */
  394. return 0;
  395. close_irlap:
  396. irlap_close(kingsun->irlap);
  397. free_mem:
  398. usb_free_urb(kingsun->speed_urb);
  399. kingsun->speed_urb = NULL;
  400. usb_free_urb(kingsun->tx_urb);
  401. kingsun->tx_urb = NULL;
  402. usb_free_urb(kingsun->rx_urb);
  403. kingsun->rx_urb = NULL;
  404. if (kingsun->rx_unwrap_buff.skb) {
  405. kfree_skb(kingsun->rx_unwrap_buff.skb);
  406. kingsun->rx_unwrap_buff.skb = NULL;
  407. kingsun->rx_unwrap_buff.head = NULL;
  408. }
  409. return err;
  410. }
  411. /*
  412. * Function ksdazzle_net_close (dev)
  413. *
  414. * Network device is taken down. Usually this is done by
  415. * "ifconfig irda0 down"
  416. */
  417. static int ksdazzle_net_close(struct net_device *netdev)
  418. {
  419. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  420. /* Stop transmit processing */
  421. netif_stop_queue(netdev);
  422. /* Mop up receive && transmit urb's */
  423. usb_kill_urb(kingsun->tx_urb);
  424. usb_free_urb(kingsun->tx_urb);
  425. kingsun->tx_urb = NULL;
  426. usb_kill_urb(kingsun->speed_urb);
  427. usb_free_urb(kingsun->speed_urb);
  428. kingsun->speed_urb = NULL;
  429. usb_kill_urb(kingsun->rx_urb);
  430. usb_free_urb(kingsun->rx_urb);
  431. kingsun->rx_urb = NULL;
  432. kfree_skb(kingsun->rx_unwrap_buff.skb);
  433. kingsun->rx_unwrap_buff.skb = NULL;
  434. kingsun->rx_unwrap_buff.head = NULL;
  435. kingsun->rx_unwrap_buff.in_frame = FALSE;
  436. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  437. kingsun->receiving = 0;
  438. /* Stop and remove instance of IrLAP */
  439. irlap_close(kingsun->irlap);
  440. kingsun->irlap = NULL;
  441. return 0;
  442. }
  443. /*
  444. * IOCTLs : Extra out-of-band network commands...
  445. */
  446. static int ksdazzle_net_ioctl(struct net_device *netdev, struct ifreq *rq,
  447. int cmd)
  448. {
  449. struct if_irda_req *irq = (struct if_irda_req *)rq;
  450. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  451. int ret = 0;
  452. switch (cmd) {
  453. case SIOCSBANDWIDTH: /* Set bandwidth */
  454. if (!capable(CAP_NET_ADMIN))
  455. return -EPERM;
  456. /* Check if the device is still there */
  457. if (netif_device_present(kingsun->netdev))
  458. return ksdazzle_change_speed(kingsun,
  459. irq->ifr_baudrate);
  460. break;
  461. case SIOCSMEDIABUSY: /* Set media busy */
  462. if (!capable(CAP_NET_ADMIN))
  463. return -EPERM;
  464. /* Check if the IrDA stack is still there */
  465. if (netif_running(kingsun->netdev))
  466. irda_device_set_media_busy(kingsun->netdev, TRUE);
  467. break;
  468. case SIOCGRECEIVING:
  469. /* Only approximately true */
  470. irq->ifr_receiving = kingsun->receiving;
  471. break;
  472. default:
  473. ret = -EOPNOTSUPP;
  474. }
  475. return ret;
  476. }
  477. /*
  478. * Get device stats (for /proc/net/dev and ifconfig)
  479. */
  480. static struct net_device_stats *ksdazzle_net_get_stats(struct net_device
  481. *netdev)
  482. {
  483. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  484. return &kingsun->stats;
  485. }
  486. /*
  487. * This routine is called by the USB subsystem for each new device
  488. * in the system. We need to check if the device is ours, and in
  489. * this case start handling it.
  490. */
  491. static int ksdazzle_probe(struct usb_interface *intf,
  492. const struct usb_device_id *id)
  493. {
  494. struct usb_host_interface *interface;
  495. struct usb_endpoint_descriptor *endpoint;
  496. struct usb_device *dev = interface_to_usbdev(intf);
  497. struct ksdazzle_cb *kingsun = NULL;
  498. struct net_device *net = NULL;
  499. int ret = -ENOMEM;
  500. int pipe, maxp_in, maxp_out;
  501. __u8 ep_in;
  502. __u8 ep_out;
  503. /* Check that there really are two interrupt endpoints. Check based on the
  504. one in drivers/usb/input/usbmouse.c
  505. */
  506. interface = intf->cur_altsetting;
  507. if (interface->desc.bNumEndpoints != 2) {
  508. err("ksdazzle: expected 2 endpoints, found %d",
  509. interface->desc.bNumEndpoints);
  510. return -ENODEV;
  511. }
  512. endpoint = &interface->endpoint[KINGSUN_EP_IN].desc;
  513. if (!usb_endpoint_is_int_in(endpoint)) {
  514. err("ksdazzle: endpoint 0 is not interrupt IN");
  515. return -ENODEV;
  516. }
  517. ep_in = endpoint->bEndpointAddress;
  518. pipe = usb_rcvintpipe(dev, ep_in);
  519. maxp_in = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  520. if (maxp_in > 255 || maxp_in <= 1) {
  521. err("ksdazzle: endpoint 0 has max packet size %d not in range [2..255]", maxp_in);
  522. return -ENODEV;
  523. }
  524. endpoint = &interface->endpoint[KINGSUN_EP_OUT].desc;
  525. if (!usb_endpoint_is_int_out(endpoint)) {
  526. err("ksdazzle: endpoint 1 is not interrupt OUT");
  527. return -ENODEV;
  528. }
  529. ep_out = endpoint->bEndpointAddress;
  530. pipe = usb_sndintpipe(dev, ep_out);
  531. maxp_out = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  532. /* Allocate network device container. */
  533. net = alloc_irdadev(sizeof(*kingsun));
  534. if (!net)
  535. goto err_out1;
  536. SET_NETDEV_DEV(net, &intf->dev);
  537. kingsun = netdev_priv(net);
  538. kingsun->netdev = net;
  539. kingsun->usbdev = dev;
  540. kingsun->ep_in = ep_in;
  541. kingsun->ep_out = ep_out;
  542. kingsun->irlap = NULL;
  543. kingsun->tx_urb = NULL;
  544. kingsun->tx_buf_clear = NULL;
  545. kingsun->tx_buf_clear_used = 0;
  546. kingsun->tx_buf_clear_sent = 0;
  547. kingsun->rx_urb = NULL;
  548. kingsun->rx_buf = NULL;
  549. kingsun->rx_unwrap_buff.in_frame = FALSE;
  550. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  551. kingsun->rx_unwrap_buff.skb = NULL;
  552. kingsun->receiving = 0;
  553. spin_lock_init(&kingsun->lock);
  554. kingsun->speed_setuprequest = NULL;
  555. kingsun->speed_urb = NULL;
  556. kingsun->speedparams.baudrate = 0;
  557. /* Allocate input buffer */
  558. kingsun->rx_buf = kmalloc(KINGSUN_RCV_MAX, GFP_KERNEL);
  559. if (!kingsun->rx_buf)
  560. goto free_mem;
  561. /* Allocate output buffer */
  562. kingsun->tx_buf_clear = kmalloc(KINGSUN_SND_FIFO_SIZE, GFP_KERNEL);
  563. if (!kingsun->tx_buf_clear)
  564. goto free_mem;
  565. /* Allocate and initialize speed setup packet */
  566. kingsun->speed_setuprequest =
  567. kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  568. if (!kingsun->speed_setuprequest)
  569. goto free_mem;
  570. kingsun->speed_setuprequest->bRequestType =
  571. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  572. kingsun->speed_setuprequest->bRequest = KINGSUN_REQ_SEND;
  573. kingsun->speed_setuprequest->wValue = cpu_to_le16(0x0200);
  574. kingsun->speed_setuprequest->wIndex = cpu_to_le16(0x0001);
  575. kingsun->speed_setuprequest->wLength =
  576. cpu_to_le16(sizeof(struct ksdazzle_speedparams));
  577. printk(KERN_INFO "KingSun/Dazzle IRDA/USB found at address %d, "
  578. "Vendor: %x, Product: %x\n",
  579. dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
  580. le16_to_cpu(dev->descriptor.idProduct));
  581. /* Initialize QoS for this device */
  582. irda_init_max_qos_capabilies(&kingsun->qos);
  583. /* Baud rates known to be supported. Please uncomment if devices (other
  584. than a SonyEriccson K300 phone) can be shown to support higher speeds
  585. with this dongle.
  586. */
  587. kingsun->qos.baud_rate.bits =
  588. IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200;
  589. kingsun->qos.min_turn_time.bits &= KINGSUN_MTT;
  590. irda_qos_bits_to_value(&kingsun->qos);
  591. /* Override the network functions we need to use */
  592. net->hard_start_xmit = ksdazzle_hard_xmit;
  593. net->open = ksdazzle_net_open;
  594. net->stop = ksdazzle_net_close;
  595. net->get_stats = ksdazzle_net_get_stats;
  596. net->do_ioctl = ksdazzle_net_ioctl;
  597. ret = register_netdev(net);
  598. if (ret != 0)
  599. goto free_mem;
  600. dev_info(&net->dev, "IrDA: Registered KingSun/Dazzle device %s\n",
  601. net->name);
  602. usb_set_intfdata(intf, kingsun);
  603. /* Situation at this point:
  604. - all work buffers allocated
  605. - setup requests pre-filled
  606. - urbs not allocated, set to NULL
  607. - max rx packet known (is KINGSUN_FIFO_SIZE)
  608. - unwrap state machine (partially) initialized, but skb == NULL
  609. */
  610. return 0;
  611. free_mem:
  612. kfree(kingsun->speed_setuprequest);
  613. kfree(kingsun->tx_buf_clear);
  614. kfree(kingsun->rx_buf);
  615. free_netdev(net);
  616. err_out1:
  617. return ret;
  618. }
  619. /*
  620. * The current device is removed, the USB layer tell us to shut it down...
  621. */
  622. static void ksdazzle_disconnect(struct usb_interface *intf)
  623. {
  624. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  625. if (!kingsun)
  626. return;
  627. unregister_netdev(kingsun->netdev);
  628. /* Mop up receive && transmit urb's */
  629. usb_kill_urb(kingsun->speed_urb);
  630. usb_free_urb(kingsun->speed_urb);
  631. kingsun->speed_urb = NULL;
  632. usb_kill_urb(kingsun->tx_urb);
  633. usb_free_urb(kingsun->tx_urb);
  634. kingsun->tx_urb = NULL;
  635. usb_kill_urb(kingsun->rx_urb);
  636. usb_free_urb(kingsun->rx_urb);
  637. kingsun->rx_urb = NULL;
  638. kfree(kingsun->speed_setuprequest);
  639. kfree(kingsun->tx_buf_clear);
  640. kfree(kingsun->rx_buf);
  641. free_netdev(kingsun->netdev);
  642. usb_set_intfdata(intf, NULL);
  643. }
  644. #ifdef CONFIG_PM
  645. /* USB suspend, so power off the transmitter/receiver */
  646. static int ksdazzle_suspend(struct usb_interface *intf, pm_message_t message)
  647. {
  648. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  649. netif_device_detach(kingsun->netdev);
  650. if (kingsun->speed_urb != NULL)
  651. usb_kill_urb(kingsun->speed_urb);
  652. if (kingsun->tx_urb != NULL)
  653. usb_kill_urb(kingsun->tx_urb);
  654. if (kingsun->rx_urb != NULL)
  655. usb_kill_urb(kingsun->rx_urb);
  656. return 0;
  657. }
  658. /* Coming out of suspend, so reset hardware */
  659. static int ksdazzle_resume(struct usb_interface *intf)
  660. {
  661. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  662. if (kingsun->rx_urb != NULL) {
  663. /* Setup request already filled in ksdazzle_probe */
  664. usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
  665. }
  666. netif_device_attach(kingsun->netdev);
  667. return 0;
  668. }
  669. #endif
  670. /*
  671. * USB device callbacks
  672. */
  673. static struct usb_driver irda_driver = {
  674. .name = "ksdazzle-sir",
  675. .probe = ksdazzle_probe,
  676. .disconnect = ksdazzle_disconnect,
  677. .id_table = dongles,
  678. #ifdef CONFIG_PM
  679. .suspend = ksdazzle_suspend,
  680. .resume = ksdazzle_resume,
  681. #endif
  682. };
  683. /*
  684. * Module insertion
  685. */
  686. static int __init ksdazzle_init(void)
  687. {
  688. return usb_register(&irda_driver);
  689. }
  690. module_init(ksdazzle_init);
  691. /*
  692. * Module removal
  693. */
  694. static void __exit ksdazzle_cleanup(void)
  695. {
  696. /* Deregister the driver and remove all pending instances */
  697. usb_deregister(&irda_driver);
  698. }
  699. module_exit(ksdazzle_cleanup);
  700. MODULE_AUTHOR("Alex Villacís Lasso <a_villacis@palosanto.com>");
  701. MODULE_DESCRIPTION("IrDA-USB Dongle Driver for KingSun Dazzle");
  702. MODULE_LICENSE("GPL");