ksdazzle-sir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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 qos_info qos;
  129. struct urb *tx_urb;
  130. __u8 *tx_buf_clear;
  131. unsigned int tx_buf_clear_used;
  132. unsigned int tx_buf_clear_sent;
  133. __u8 tx_payload[8];
  134. struct urb *rx_urb;
  135. __u8 *rx_buf;
  136. iobuff_t rx_unwrap_buff;
  137. struct usb_ctrlrequest *speed_setuprequest;
  138. struct urb *speed_urb;
  139. struct ksdazzle_speedparams speedparams;
  140. unsigned int new_speed;
  141. __u8 ep_in;
  142. __u8 ep_out;
  143. spinlock_t lock;
  144. int receiving;
  145. };
  146. /* Callback transmission routine */
  147. static void ksdazzle_speed_irq(struct urb *urb)
  148. {
  149. /* unlink, shutdown, unplug, other nasties */
  150. if (urb->status != 0) {
  151. err("ksdazzle_speed_irq: urb asynchronously failed - %d",
  152. urb->status);
  153. }
  154. }
  155. /* Send a control request to change speed of the dongle */
  156. static int ksdazzle_change_speed(struct ksdazzle_cb *kingsun, unsigned speed)
  157. {
  158. static unsigned int supported_speeds[] = { 2400, 9600, 19200, 38400,
  159. 57600, 115200, 576000, 1152000, 4000000, 0
  160. };
  161. int err;
  162. unsigned int i;
  163. if (kingsun->speed_setuprequest == NULL || kingsun->speed_urb == NULL)
  164. return -ENOMEM;
  165. /* Check that requested speed is among the supported ones */
  166. for (i = 0; supported_speeds[i] && supported_speeds[i] != speed; i++) ;
  167. if (supported_speeds[i] == 0)
  168. return -EOPNOTSUPP;
  169. memset(&(kingsun->speedparams), 0, sizeof(struct ksdazzle_speedparams));
  170. kingsun->speedparams.baudrate = cpu_to_le32(speed);
  171. kingsun->speedparams.flags = KS_DATA_8_BITS;
  172. /* speed_setuprequest pre-filled in ksdazzle_probe */
  173. usb_fill_control_urb(kingsun->speed_urb, kingsun->usbdev,
  174. usb_sndctrlpipe(kingsun->usbdev, 0),
  175. (unsigned char *)kingsun->speed_setuprequest,
  176. &(kingsun->speedparams),
  177. sizeof(struct ksdazzle_speedparams),
  178. ksdazzle_speed_irq, kingsun);
  179. kingsun->speed_urb->status = 0;
  180. err = usb_submit_urb(kingsun->speed_urb, GFP_ATOMIC);
  181. return err;
  182. }
  183. /* Submit one fragment of an IrDA frame to the dongle */
  184. static void ksdazzle_send_irq(struct urb *urb);
  185. static int ksdazzle_submit_tx_fragment(struct ksdazzle_cb *kingsun)
  186. {
  187. unsigned int wraplen;
  188. int ret;
  189. /* We can send at most 7 bytes of payload at a time */
  190. wraplen = 7;
  191. if (wraplen > kingsun->tx_buf_clear_used)
  192. wraplen = kingsun->tx_buf_clear_used;
  193. /* Prepare payload prefix with used length */
  194. memset(kingsun->tx_payload, 0, 8);
  195. kingsun->tx_payload[0] = (unsigned char)0xf8 + wraplen;
  196. memcpy(kingsun->tx_payload + 1, kingsun->tx_buf_clear, wraplen);
  197. usb_fill_int_urb(kingsun->tx_urb, kingsun->usbdev,
  198. usb_sndintpipe(kingsun->usbdev, kingsun->ep_out),
  199. kingsun->tx_payload, 8, ksdazzle_send_irq, kingsun, 1);
  200. kingsun->tx_urb->status = 0;
  201. ret = usb_submit_urb(kingsun->tx_urb, GFP_ATOMIC);
  202. /* Remember how much data was sent, in order to update at callback */
  203. kingsun->tx_buf_clear_sent = (ret == 0) ? wraplen : 0;
  204. return ret;
  205. }
  206. /* Callback transmission routine */
  207. static void ksdazzle_send_irq(struct urb *urb)
  208. {
  209. struct ksdazzle_cb *kingsun = urb->context;
  210. struct net_device *netdev = kingsun->netdev;
  211. int ret = 0;
  212. /* in process of stopping, just drop data */
  213. if (!netif_running(kingsun->netdev)) {
  214. err("ksdazzle_send_irq: Network not running!");
  215. return;
  216. }
  217. /* unlink, shutdown, unplug, other nasties */
  218. if (urb->status != 0) {
  219. err("ksdazzle_send_irq: urb asynchronously failed - %d",
  220. urb->status);
  221. return;
  222. }
  223. if (kingsun->tx_buf_clear_used > 0) {
  224. /* Update data remaining to be sent */
  225. if (kingsun->tx_buf_clear_sent < kingsun->tx_buf_clear_used) {
  226. memmove(kingsun->tx_buf_clear,
  227. kingsun->tx_buf_clear +
  228. kingsun->tx_buf_clear_sent,
  229. kingsun->tx_buf_clear_used -
  230. kingsun->tx_buf_clear_sent);
  231. }
  232. kingsun->tx_buf_clear_used -= kingsun->tx_buf_clear_sent;
  233. kingsun->tx_buf_clear_sent = 0;
  234. if (kingsun->tx_buf_clear_used > 0) {
  235. /* There is more data to be sent */
  236. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  237. err("ksdazzle_send_irq: failed tx_urb submit: %d", ret);
  238. switch (ret) {
  239. case -ENODEV:
  240. case -EPIPE:
  241. break;
  242. default:
  243. netdev->stats.tx_errors++;
  244. netif_start_queue(netdev);
  245. }
  246. }
  247. } else {
  248. /* All data sent, send next speed && wake network queue */
  249. if (kingsun->new_speed != -1 &&
  250. cpu_to_le32(kingsun->new_speed) !=
  251. kingsun->speedparams.baudrate)
  252. ksdazzle_change_speed(kingsun,
  253. kingsun->new_speed);
  254. netif_wake_queue(netdev);
  255. }
  256. }
  257. }
  258. /*
  259. * Called from net/core when new frame is available.
  260. */
  261. static int ksdazzle_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
  262. {
  263. struct ksdazzle_cb *kingsun;
  264. unsigned int wraplen;
  265. int ret = 0;
  266. if (skb == NULL || netdev == NULL)
  267. return -EINVAL;
  268. netif_stop_queue(netdev);
  269. /* the IRDA wrapping routines don't deal with non linear skb */
  270. SKB_LINEAR_ASSERT(skb);
  271. kingsun = netdev_priv(netdev);
  272. spin_lock(&kingsun->lock);
  273. kingsun->new_speed = irda_get_next_speed(skb);
  274. /* Append data to the end of whatever data remains to be transmitted */
  275. wraplen =
  276. async_wrap_skb(skb, kingsun->tx_buf_clear, KINGSUN_SND_FIFO_SIZE);
  277. kingsun->tx_buf_clear_used = wraplen;
  278. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  279. err("ksdazzle_hard_xmit: failed tx_urb submit: %d", ret);
  280. switch (ret) {
  281. case -ENODEV:
  282. case -EPIPE:
  283. break;
  284. default:
  285. netdev->stats.tx_errors++;
  286. netif_start_queue(netdev);
  287. }
  288. } else {
  289. netdev->stats.tx_packets++;
  290. netdev->stats.tx_bytes += skb->len;
  291. }
  292. dev_kfree_skb(skb);
  293. spin_unlock(&kingsun->lock);
  294. return ret;
  295. }
  296. /* Receive callback function */
  297. static void ksdazzle_rcv_irq(struct urb *urb)
  298. {
  299. struct ksdazzle_cb *kingsun = urb->context;
  300. struct net_device *netdev = kingsun->netdev;
  301. /* in process of stopping, just drop data */
  302. if (!netif_running(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(netdev, &netdev->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. static const struct net_device_ops ksdazzle_ops = {
  478. .ndo_start_xmit = ksdazzle_hard_xmit,
  479. .ndo_open = ksdazzle_net_open,
  480. .ndo_stop = ksdazzle_net_close,
  481. .ndo_do_ioctl = ksdazzle_net_ioctl,
  482. };
  483. /*
  484. * This routine is called by the USB subsystem for each new device
  485. * in the system. We need to check if the device is ours, and in
  486. * this case start handling it.
  487. */
  488. static int ksdazzle_probe(struct usb_interface *intf,
  489. const struct usb_device_id *id)
  490. {
  491. struct usb_host_interface *interface;
  492. struct usb_endpoint_descriptor *endpoint;
  493. struct usb_device *dev = interface_to_usbdev(intf);
  494. struct ksdazzle_cb *kingsun = NULL;
  495. struct net_device *net = NULL;
  496. int ret = -ENOMEM;
  497. int pipe, maxp_in, maxp_out;
  498. __u8 ep_in;
  499. __u8 ep_out;
  500. /* Check that there really are two interrupt endpoints. Check based on the
  501. one in drivers/usb/input/usbmouse.c
  502. */
  503. interface = intf->cur_altsetting;
  504. if (interface->desc.bNumEndpoints != 2) {
  505. err("ksdazzle: expected 2 endpoints, found %d",
  506. interface->desc.bNumEndpoints);
  507. return -ENODEV;
  508. }
  509. endpoint = &interface->endpoint[KINGSUN_EP_IN].desc;
  510. if (!usb_endpoint_is_int_in(endpoint)) {
  511. err("ksdazzle: endpoint 0 is not interrupt IN");
  512. return -ENODEV;
  513. }
  514. ep_in = endpoint->bEndpointAddress;
  515. pipe = usb_rcvintpipe(dev, ep_in);
  516. maxp_in = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  517. if (maxp_in > 255 || maxp_in <= 1) {
  518. err("ksdazzle: endpoint 0 has max packet size %d not in range [2..255]", maxp_in);
  519. return -ENODEV;
  520. }
  521. endpoint = &interface->endpoint[KINGSUN_EP_OUT].desc;
  522. if (!usb_endpoint_is_int_out(endpoint)) {
  523. err("ksdazzle: endpoint 1 is not interrupt OUT");
  524. return -ENODEV;
  525. }
  526. ep_out = endpoint->bEndpointAddress;
  527. pipe = usb_sndintpipe(dev, ep_out);
  528. maxp_out = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  529. /* Allocate network device container. */
  530. net = alloc_irdadev(sizeof(*kingsun));
  531. if (!net)
  532. goto err_out1;
  533. SET_NETDEV_DEV(net, &intf->dev);
  534. kingsun = netdev_priv(net);
  535. kingsun->netdev = net;
  536. kingsun->usbdev = dev;
  537. kingsun->ep_in = ep_in;
  538. kingsun->ep_out = ep_out;
  539. kingsun->irlap = NULL;
  540. kingsun->tx_urb = NULL;
  541. kingsun->tx_buf_clear = NULL;
  542. kingsun->tx_buf_clear_used = 0;
  543. kingsun->tx_buf_clear_sent = 0;
  544. kingsun->rx_urb = NULL;
  545. kingsun->rx_buf = NULL;
  546. kingsun->rx_unwrap_buff.in_frame = FALSE;
  547. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  548. kingsun->rx_unwrap_buff.skb = NULL;
  549. kingsun->receiving = 0;
  550. spin_lock_init(&kingsun->lock);
  551. kingsun->speed_setuprequest = NULL;
  552. kingsun->speed_urb = NULL;
  553. kingsun->speedparams.baudrate = 0;
  554. /* Allocate input buffer */
  555. kingsun->rx_buf = kmalloc(KINGSUN_RCV_MAX, GFP_KERNEL);
  556. if (!kingsun->rx_buf)
  557. goto free_mem;
  558. /* Allocate output buffer */
  559. kingsun->tx_buf_clear = kmalloc(KINGSUN_SND_FIFO_SIZE, GFP_KERNEL);
  560. if (!kingsun->tx_buf_clear)
  561. goto free_mem;
  562. /* Allocate and initialize speed setup packet */
  563. kingsun->speed_setuprequest =
  564. kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  565. if (!kingsun->speed_setuprequest)
  566. goto free_mem;
  567. kingsun->speed_setuprequest->bRequestType =
  568. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  569. kingsun->speed_setuprequest->bRequest = KINGSUN_REQ_SEND;
  570. kingsun->speed_setuprequest->wValue = cpu_to_le16(0x0200);
  571. kingsun->speed_setuprequest->wIndex = cpu_to_le16(0x0001);
  572. kingsun->speed_setuprequest->wLength =
  573. cpu_to_le16(sizeof(struct ksdazzle_speedparams));
  574. printk(KERN_INFO "KingSun/Dazzle IRDA/USB found at address %d, "
  575. "Vendor: %x, Product: %x\n",
  576. dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
  577. le16_to_cpu(dev->descriptor.idProduct));
  578. /* Initialize QoS for this device */
  579. irda_init_max_qos_capabilies(&kingsun->qos);
  580. /* Baud rates known to be supported. Please uncomment if devices (other
  581. than a SonyEriccson K300 phone) can be shown to support higher speeds
  582. with this dongle.
  583. */
  584. kingsun->qos.baud_rate.bits =
  585. IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200;
  586. kingsun->qos.min_turn_time.bits &= KINGSUN_MTT;
  587. irda_qos_bits_to_value(&kingsun->qos);
  588. /* Override the network functions we need to use */
  589. net->netdev_ops = &ksdazzle_ops;
  590. ret = register_netdev(net);
  591. if (ret != 0)
  592. goto free_mem;
  593. dev_info(&net->dev, "IrDA: Registered KingSun/Dazzle device %s\n",
  594. net->name);
  595. usb_set_intfdata(intf, kingsun);
  596. /* Situation at this point:
  597. - all work buffers allocated
  598. - setup requests pre-filled
  599. - urbs not allocated, set to NULL
  600. - max rx packet known (is KINGSUN_FIFO_SIZE)
  601. - unwrap state machine (partially) initialized, but skb == NULL
  602. */
  603. return 0;
  604. free_mem:
  605. kfree(kingsun->speed_setuprequest);
  606. kfree(kingsun->tx_buf_clear);
  607. kfree(kingsun->rx_buf);
  608. free_netdev(net);
  609. err_out1:
  610. return ret;
  611. }
  612. /*
  613. * The current device is removed, the USB layer tell us to shut it down...
  614. */
  615. static void ksdazzle_disconnect(struct usb_interface *intf)
  616. {
  617. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  618. if (!kingsun)
  619. return;
  620. unregister_netdev(kingsun->netdev);
  621. /* Mop up receive && transmit urb's */
  622. usb_kill_urb(kingsun->speed_urb);
  623. usb_free_urb(kingsun->speed_urb);
  624. kingsun->speed_urb = NULL;
  625. usb_kill_urb(kingsun->tx_urb);
  626. usb_free_urb(kingsun->tx_urb);
  627. kingsun->tx_urb = NULL;
  628. usb_kill_urb(kingsun->rx_urb);
  629. usb_free_urb(kingsun->rx_urb);
  630. kingsun->rx_urb = NULL;
  631. kfree(kingsun->speed_setuprequest);
  632. kfree(kingsun->tx_buf_clear);
  633. kfree(kingsun->rx_buf);
  634. free_netdev(kingsun->netdev);
  635. usb_set_intfdata(intf, NULL);
  636. }
  637. #ifdef CONFIG_PM
  638. /* USB suspend, so power off the transmitter/receiver */
  639. static int ksdazzle_suspend(struct usb_interface *intf, pm_message_t message)
  640. {
  641. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  642. netif_device_detach(kingsun->netdev);
  643. if (kingsun->speed_urb != NULL)
  644. usb_kill_urb(kingsun->speed_urb);
  645. if (kingsun->tx_urb != NULL)
  646. usb_kill_urb(kingsun->tx_urb);
  647. if (kingsun->rx_urb != NULL)
  648. usb_kill_urb(kingsun->rx_urb);
  649. return 0;
  650. }
  651. /* Coming out of suspend, so reset hardware */
  652. static int ksdazzle_resume(struct usb_interface *intf)
  653. {
  654. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  655. if (kingsun->rx_urb != NULL) {
  656. /* Setup request already filled in ksdazzle_probe */
  657. usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
  658. }
  659. netif_device_attach(kingsun->netdev);
  660. return 0;
  661. }
  662. #endif
  663. /*
  664. * USB device callbacks
  665. */
  666. static struct usb_driver irda_driver = {
  667. .name = "ksdazzle-sir",
  668. .probe = ksdazzle_probe,
  669. .disconnect = ksdazzle_disconnect,
  670. .id_table = dongles,
  671. #ifdef CONFIG_PM
  672. .suspend = ksdazzle_suspend,
  673. .resume = ksdazzle_resume,
  674. #endif
  675. };
  676. /*
  677. * Module insertion
  678. */
  679. static int __init ksdazzle_init(void)
  680. {
  681. return usb_register(&irda_driver);
  682. }
  683. module_init(ksdazzle_init);
  684. /*
  685. * Module removal
  686. */
  687. static void __exit ksdazzle_cleanup(void)
  688. {
  689. /* Deregister the driver and remove all pending instances */
  690. usb_deregister(&irda_driver);
  691. }
  692. module_exit(ksdazzle_cleanup);
  693. MODULE_AUTHOR("Alex Villacís Lasso <a_villacis@palosanto.com>");
  694. MODULE_DESCRIPTION("IrDA-USB Dongle Driver for KingSun Dazzle");
  695. MODULE_LICENSE("GPL");