iuu_phoenix.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Infinity Unlimited USB Phoenix driver
  4. *
  5. * Copyright (C) 2010 James Courtier-Dutton (James@superbug.co.uk)
  6. * Copyright (C) 2007 Alain Degreffe (eczema@ecze.com)
  7. *
  8. * Original code taken from iuutool (Copyright (C) 2006 Juan Carlos Borrás)
  9. *
  10. * And tested with help of WB Electronics
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/slab.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_driver.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/serial.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/usb.h>
  24. #include <linux/usb/serial.h>
  25. #include "iuu_phoenix.h"
  26. #include <linux/random.h>
  27. #define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
  28. static const struct usb_device_id id_table[] = {
  29. {USB_DEVICE(IUU_USB_VENDOR_ID, IUU_USB_PRODUCT_ID)},
  30. {} /* Terminating entry */
  31. };
  32. MODULE_DEVICE_TABLE(usb, id_table);
  33. /* turbo parameter */
  34. static int boost = 100;
  35. static int clockmode = 1;
  36. static int cdmode = 1;
  37. static int iuu_cardin;
  38. static int iuu_cardout;
  39. static bool xmas;
  40. static int vcc_default = 5;
  41. static int iuu_create_sysfs_attrs(struct usb_serial_port *port);
  42. static int iuu_remove_sysfs_attrs(struct usb_serial_port *port);
  43. static void read_rxcmd_callback(struct urb *urb);
  44. struct iuu_private {
  45. spinlock_t lock; /* store irq state */
  46. u8 line_status;
  47. int tiostatus; /* store IUART SIGNAL for tiocmget call */
  48. u8 reset; /* if 1 reset is needed */
  49. int poll; /* number of poll */
  50. u8 *writebuf; /* buffer for writing to device */
  51. int writelen; /* num of byte to write to device */
  52. u8 *buf; /* used for initialize speed */
  53. u8 len;
  54. int vcc; /* vcc (either 3 or 5 V) */
  55. u32 boost;
  56. u32 clk;
  57. };
  58. static int iuu_port_probe(struct usb_serial_port *port)
  59. {
  60. struct iuu_private *priv;
  61. int ret;
  62. priv = kzalloc(sizeof(struct iuu_private), GFP_KERNEL);
  63. if (!priv)
  64. return -ENOMEM;
  65. priv->buf = kzalloc(256, GFP_KERNEL);
  66. if (!priv->buf) {
  67. kfree(priv);
  68. return -ENOMEM;
  69. }
  70. priv->writebuf = kzalloc(256, GFP_KERNEL);
  71. if (!priv->writebuf) {
  72. kfree(priv->buf);
  73. kfree(priv);
  74. return -ENOMEM;
  75. }
  76. priv->vcc = vcc_default;
  77. spin_lock_init(&priv->lock);
  78. usb_set_serial_port_data(port, priv);
  79. ret = iuu_create_sysfs_attrs(port);
  80. if (ret) {
  81. kfree(priv->writebuf);
  82. kfree(priv->buf);
  83. kfree(priv);
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. static int iuu_port_remove(struct usb_serial_port *port)
  89. {
  90. struct iuu_private *priv = usb_get_serial_port_data(port);
  91. iuu_remove_sysfs_attrs(port);
  92. kfree(priv->writebuf);
  93. kfree(priv->buf);
  94. kfree(priv);
  95. return 0;
  96. }
  97. static int iuu_tiocmset(struct tty_struct *tty,
  98. unsigned int set, unsigned int clear)
  99. {
  100. struct usb_serial_port *port = tty->driver_data;
  101. struct iuu_private *priv = usb_get_serial_port_data(port);
  102. unsigned long flags;
  103. /* FIXME: locking on tiomstatus */
  104. dev_dbg(&port->dev, "%s msg : SET = 0x%04x, CLEAR = 0x%04x\n",
  105. __func__, set, clear);
  106. spin_lock_irqsave(&priv->lock, flags);
  107. if ((set & TIOCM_RTS) && !(priv->tiostatus == TIOCM_RTS)) {
  108. dev_dbg(&port->dev, "%s TIOCMSET RESET called !!!\n", __func__);
  109. priv->reset = 1;
  110. }
  111. if (set & TIOCM_RTS)
  112. priv->tiostatus = TIOCM_RTS;
  113. spin_unlock_irqrestore(&priv->lock, flags);
  114. return 0;
  115. }
  116. /* This is used to provide a carrier detect mechanism
  117. * When a card is present, the response is 0x00
  118. * When no card , the reader respond with TIOCM_CD
  119. * This is known as CD autodetect mechanism
  120. */
  121. static int iuu_tiocmget(struct tty_struct *tty)
  122. {
  123. struct usb_serial_port *port = tty->driver_data;
  124. struct iuu_private *priv = usb_get_serial_port_data(port);
  125. unsigned long flags;
  126. int rc;
  127. spin_lock_irqsave(&priv->lock, flags);
  128. rc = priv->tiostatus;
  129. spin_unlock_irqrestore(&priv->lock, flags);
  130. return rc;
  131. }
  132. static void iuu_rxcmd(struct urb *urb)
  133. {
  134. struct usb_serial_port *port = urb->context;
  135. int result;
  136. int status = urb->status;
  137. if (status) {
  138. dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
  139. /* error stop all */
  140. return;
  141. }
  142. memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
  143. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  144. usb_sndbulkpipe(port->serial->dev,
  145. port->bulk_out_endpointAddress),
  146. port->write_urb->transfer_buffer, 1,
  147. read_rxcmd_callback, port);
  148. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  149. }
  150. static int iuu_reset(struct usb_serial_port *port, u8 wt)
  151. {
  152. struct iuu_private *priv = usb_get_serial_port_data(port);
  153. int result;
  154. char *buf_ptr = port->write_urb->transfer_buffer;
  155. /* Prepare the reset sequence */
  156. *buf_ptr++ = IUU_RST_SET;
  157. *buf_ptr++ = IUU_DELAY_MS;
  158. *buf_ptr++ = wt;
  159. *buf_ptr = IUU_RST_CLEAR;
  160. /* send the sequence */
  161. usb_fill_bulk_urb(port->write_urb,
  162. port->serial->dev,
  163. usb_sndbulkpipe(port->serial->dev,
  164. port->bulk_out_endpointAddress),
  165. port->write_urb->transfer_buffer, 4, iuu_rxcmd, port);
  166. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  167. priv->reset = 0;
  168. return result;
  169. }
  170. /* Status Function
  171. * Return value is
  172. * 0x00 = no card
  173. * 0x01 = smartcard
  174. * 0x02 = sim card
  175. */
  176. static void iuu_update_status_callback(struct urb *urb)
  177. {
  178. struct usb_serial_port *port = urb->context;
  179. struct iuu_private *priv = usb_get_serial_port_data(port);
  180. u8 *st;
  181. int status = urb->status;
  182. if (status) {
  183. dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
  184. /* error stop all */
  185. return;
  186. }
  187. st = urb->transfer_buffer;
  188. dev_dbg(&port->dev, "%s - enter\n", __func__);
  189. if (urb->actual_length == 1) {
  190. switch (st[0]) {
  191. case 0x1:
  192. priv->tiostatus = iuu_cardout;
  193. break;
  194. case 0x0:
  195. priv->tiostatus = iuu_cardin;
  196. break;
  197. default:
  198. priv->tiostatus = iuu_cardin;
  199. }
  200. }
  201. iuu_rxcmd(urb);
  202. }
  203. static void iuu_status_callback(struct urb *urb)
  204. {
  205. struct usb_serial_port *port = urb->context;
  206. int result;
  207. int status = urb->status;
  208. dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
  209. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  210. usb_rcvbulkpipe(port->serial->dev,
  211. port->bulk_in_endpointAddress),
  212. port->read_urb->transfer_buffer, 256,
  213. iuu_update_status_callback, port);
  214. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  215. }
  216. static int iuu_status(struct usb_serial_port *port)
  217. {
  218. int result;
  219. memset(port->write_urb->transfer_buffer, IUU_GET_STATE_REGISTER, 1);
  220. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  221. usb_sndbulkpipe(port->serial->dev,
  222. port->bulk_out_endpointAddress),
  223. port->write_urb->transfer_buffer, 1,
  224. iuu_status_callback, port);
  225. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  226. return result;
  227. }
  228. static int bulk_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
  229. {
  230. int status;
  231. struct usb_serial *serial = port->serial;
  232. int actual = 0;
  233. /* send the data out the bulk port */
  234. status =
  235. usb_bulk_msg(serial->dev,
  236. usb_sndbulkpipe(serial->dev,
  237. port->bulk_out_endpointAddress), buf,
  238. count, &actual, 1000);
  239. if (status != IUU_OPERATION_OK)
  240. dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
  241. else
  242. dev_dbg(&port->dev, "%s - write OK !\n", __func__);
  243. return status;
  244. }
  245. static int read_immediate(struct usb_serial_port *port, u8 *buf, u8 count)
  246. {
  247. int status;
  248. struct usb_serial *serial = port->serial;
  249. int actual = 0;
  250. /* send the data out the bulk port */
  251. status =
  252. usb_bulk_msg(serial->dev,
  253. usb_rcvbulkpipe(serial->dev,
  254. port->bulk_in_endpointAddress), buf,
  255. count, &actual, 1000);
  256. if (status != IUU_OPERATION_OK)
  257. dev_dbg(&port->dev, "%s - error = %2x\n", __func__, status);
  258. else
  259. dev_dbg(&port->dev, "%s - read OK !\n", __func__);
  260. return status;
  261. }
  262. static int iuu_led(struct usb_serial_port *port, unsigned int R,
  263. unsigned int G, unsigned int B, u8 f)
  264. {
  265. int status;
  266. u8 *buf;
  267. buf = kmalloc(8, GFP_KERNEL);
  268. if (!buf)
  269. return -ENOMEM;
  270. buf[0] = IUU_SET_LED;
  271. buf[1] = R & 0xFF;
  272. buf[2] = (R >> 8) & 0xFF;
  273. buf[3] = G & 0xFF;
  274. buf[4] = (G >> 8) & 0xFF;
  275. buf[5] = B & 0xFF;
  276. buf[6] = (B >> 8) & 0xFF;
  277. buf[7] = f;
  278. status = bulk_immediate(port, buf, 8);
  279. kfree(buf);
  280. if (status != IUU_OPERATION_OK)
  281. dev_dbg(&port->dev, "%s - led error status = %2x\n", __func__, status);
  282. else
  283. dev_dbg(&port->dev, "%s - led OK !\n", __func__);
  284. return IUU_OPERATION_OK;
  285. }
  286. static void iuu_rgbf_fill_buffer(u8 *buf, u8 r1, u8 r2, u8 g1, u8 g2, u8 b1,
  287. u8 b2, u8 freq)
  288. {
  289. *buf++ = IUU_SET_LED;
  290. *buf++ = r1;
  291. *buf++ = r2;
  292. *buf++ = g1;
  293. *buf++ = g2;
  294. *buf++ = b1;
  295. *buf++ = b2;
  296. *buf = freq;
  297. }
  298. static void iuu_led_activity_on(struct urb *urb)
  299. {
  300. struct usb_serial_port *port = urb->context;
  301. int result;
  302. char *buf_ptr = port->write_urb->transfer_buffer;
  303. *buf_ptr++ = IUU_SET_LED;
  304. if (xmas) {
  305. get_random_bytes(buf_ptr, 6);
  306. *(buf_ptr+7) = 1;
  307. } else {
  308. iuu_rgbf_fill_buffer(buf_ptr, 255, 255, 0, 0, 0, 0, 255);
  309. }
  310. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  311. usb_sndbulkpipe(port->serial->dev,
  312. port->bulk_out_endpointAddress),
  313. port->write_urb->transfer_buffer, 8 ,
  314. iuu_rxcmd, port);
  315. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  316. }
  317. static void iuu_led_activity_off(struct urb *urb)
  318. {
  319. struct usb_serial_port *port = urb->context;
  320. int result;
  321. char *buf_ptr = port->write_urb->transfer_buffer;
  322. if (xmas) {
  323. iuu_rxcmd(urb);
  324. return;
  325. } else {
  326. *buf_ptr++ = IUU_SET_LED;
  327. iuu_rgbf_fill_buffer(buf_ptr, 0, 0, 255, 255, 0, 0, 255);
  328. }
  329. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  330. usb_sndbulkpipe(port->serial->dev,
  331. port->bulk_out_endpointAddress),
  332. port->write_urb->transfer_buffer, 8 ,
  333. iuu_rxcmd, port);
  334. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  335. }
  336. static int iuu_clk(struct usb_serial_port *port, int dwFrq)
  337. {
  338. int status;
  339. struct iuu_private *priv = usb_get_serial_port_data(port);
  340. int Count = 0;
  341. u8 FrqGenAdr = 0x69;
  342. u8 DIV = 0; /* 8bit */
  343. u8 XDRV = 0; /* 8bit */
  344. u8 PUMP = 0; /* 3bit */
  345. u8 PBmsb = 0; /* 2bit */
  346. u8 PBlsb = 0; /* 8bit */
  347. u8 PO = 0; /* 1bit */
  348. u8 Q = 0; /* 7bit */
  349. /* 24bit = 3bytes */
  350. unsigned int P = 0;
  351. unsigned int P2 = 0;
  352. int frq = (int)dwFrq;
  353. if (frq == 0) {
  354. priv->buf[Count++] = IUU_UART_WRITE_I2C;
  355. priv->buf[Count++] = FrqGenAdr << 1;
  356. priv->buf[Count++] = 0x09;
  357. priv->buf[Count++] = 0x00;
  358. status = bulk_immediate(port, (u8 *) priv->buf, Count);
  359. if (status != 0) {
  360. dev_dbg(&port->dev, "%s - write error\n", __func__);
  361. return status;
  362. }
  363. } else if (frq == 3579000) {
  364. DIV = 100;
  365. P = 1193;
  366. Q = 40;
  367. XDRV = 0;
  368. } else if (frq == 3680000) {
  369. DIV = 105;
  370. P = 161;
  371. Q = 5;
  372. XDRV = 0;
  373. } else if (frq == 6000000) {
  374. DIV = 66;
  375. P = 66;
  376. Q = 2;
  377. XDRV = 0x28;
  378. } else {
  379. unsigned int result = 0;
  380. unsigned int tmp = 0;
  381. unsigned int check;
  382. unsigned int check2;
  383. char found = 0x00;
  384. unsigned int lQ = 2;
  385. unsigned int lP = 2055;
  386. unsigned int lDiv = 4;
  387. for (lQ = 2; lQ <= 47 && !found; lQ++)
  388. for (lP = 2055; lP >= 8 && !found; lP--)
  389. for (lDiv = 4; lDiv <= 127 && !found; lDiv++) {
  390. tmp = (12000000 / lDiv) * (lP / lQ);
  391. if (abs((int)(tmp - frq)) <
  392. abs((int)(frq - result))) {
  393. check2 = (12000000 / lQ);
  394. if (check2 < 250000)
  395. continue;
  396. check = (12000000 / lQ) * lP;
  397. if (check > 400000000)
  398. continue;
  399. if (check < 100000000)
  400. continue;
  401. if (lDiv < 4 || lDiv > 127)
  402. continue;
  403. result = tmp;
  404. P = lP;
  405. DIV = lDiv;
  406. Q = lQ;
  407. if (result == frq)
  408. found = 0x01;
  409. }
  410. }
  411. }
  412. P2 = ((P - PO) / 2) - 4;
  413. PUMP = 0x04;
  414. PBmsb = (P2 >> 8 & 0x03);
  415. PBlsb = P2 & 0xFF;
  416. PO = (P >> 10) & 0x01;
  417. Q = Q - 2;
  418. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  419. priv->buf[Count++] = FrqGenAdr << 1;
  420. priv->buf[Count++] = 0x09;
  421. priv->buf[Count++] = 0x20; /* Adr = 0x09 */
  422. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  423. priv->buf[Count++] = FrqGenAdr << 1;
  424. priv->buf[Count++] = 0x0C;
  425. priv->buf[Count++] = DIV; /* Adr = 0x0C */
  426. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  427. priv->buf[Count++] = FrqGenAdr << 1;
  428. priv->buf[Count++] = 0x12;
  429. priv->buf[Count++] = XDRV; /* Adr = 0x12 */
  430. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  431. priv->buf[Count++] = FrqGenAdr << 1;
  432. priv->buf[Count++] = 0x13;
  433. priv->buf[Count++] = 0x6B; /* Adr = 0x13 */
  434. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  435. priv->buf[Count++] = FrqGenAdr << 1;
  436. priv->buf[Count++] = 0x40;
  437. priv->buf[Count++] = (0xC0 | ((PUMP & 0x07) << 2)) |
  438. (PBmsb & 0x03); /* Adr = 0x40 */
  439. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  440. priv->buf[Count++] = FrqGenAdr << 1;
  441. priv->buf[Count++] = 0x41;
  442. priv->buf[Count++] = PBlsb; /* Adr = 0x41 */
  443. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  444. priv->buf[Count++] = FrqGenAdr << 1;
  445. priv->buf[Count++] = 0x42;
  446. priv->buf[Count++] = Q | (((PO & 0x01) << 7)); /* Adr = 0x42 */
  447. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  448. priv->buf[Count++] = FrqGenAdr << 1;
  449. priv->buf[Count++] = 0x44;
  450. priv->buf[Count++] = (char)0xFF; /* Adr = 0x44 */
  451. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  452. priv->buf[Count++] = FrqGenAdr << 1;
  453. priv->buf[Count++] = 0x45;
  454. priv->buf[Count++] = (char)0xFE; /* Adr = 0x45 */
  455. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  456. priv->buf[Count++] = FrqGenAdr << 1;
  457. priv->buf[Count++] = 0x46;
  458. priv->buf[Count++] = 0x7F; /* Adr = 0x46 */
  459. priv->buf[Count++] = IUU_UART_WRITE_I2C; /* 0x4C */
  460. priv->buf[Count++] = FrqGenAdr << 1;
  461. priv->buf[Count++] = 0x47;
  462. priv->buf[Count++] = (char)0x84; /* Adr = 0x47 */
  463. status = bulk_immediate(port, (u8 *) priv->buf, Count);
  464. if (status != IUU_OPERATION_OK)
  465. dev_dbg(&port->dev, "%s - write error\n", __func__);
  466. return status;
  467. }
  468. static int iuu_uart_flush(struct usb_serial_port *port)
  469. {
  470. struct device *dev = &port->dev;
  471. int i;
  472. int status;
  473. u8 rxcmd = IUU_UART_RX;
  474. struct iuu_private *priv = usb_get_serial_port_data(port);
  475. if (iuu_led(port, 0xF000, 0, 0, 0xFF) < 0)
  476. return -EIO;
  477. for (i = 0; i < 2; i++) {
  478. status = bulk_immediate(port, &rxcmd, 1);
  479. if (status != IUU_OPERATION_OK) {
  480. dev_dbg(dev, "%s - uart_flush_write error\n", __func__);
  481. return status;
  482. }
  483. status = read_immediate(port, &priv->len, 1);
  484. if (status != IUU_OPERATION_OK) {
  485. dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
  486. return status;
  487. }
  488. if (priv->len > 0) {
  489. dev_dbg(dev, "%s - uart_flush datalen is : %i\n", __func__, priv->len);
  490. status = read_immediate(port, priv->buf, priv->len);
  491. if (status != IUU_OPERATION_OK) {
  492. dev_dbg(dev, "%s - uart_flush_read error\n", __func__);
  493. return status;
  494. }
  495. }
  496. }
  497. dev_dbg(dev, "%s - uart_flush_read OK!\n", __func__);
  498. iuu_led(port, 0, 0xF000, 0, 0xFF);
  499. return status;
  500. }
  501. static void read_buf_callback(struct urb *urb)
  502. {
  503. struct usb_serial_port *port = urb->context;
  504. unsigned char *data = urb->transfer_buffer;
  505. int status = urb->status;
  506. if (status) {
  507. if (status == -EPROTO) {
  508. /* reschedule needed */
  509. }
  510. return;
  511. }
  512. dev_dbg(&port->dev, "%s - %i chars to write\n", __func__, urb->actual_length);
  513. if (urb->actual_length) {
  514. tty_insert_flip_string(&port->port, data, urb->actual_length);
  515. tty_flip_buffer_push(&port->port);
  516. }
  517. iuu_led_activity_on(urb);
  518. }
  519. static int iuu_bulk_write(struct usb_serial_port *port)
  520. {
  521. struct iuu_private *priv = usb_get_serial_port_data(port);
  522. unsigned long flags;
  523. int result;
  524. int buf_len;
  525. char *buf_ptr = port->write_urb->transfer_buffer;
  526. spin_lock_irqsave(&priv->lock, flags);
  527. *buf_ptr++ = IUU_UART_ESC;
  528. *buf_ptr++ = IUU_UART_TX;
  529. *buf_ptr++ = priv->writelen;
  530. memcpy(buf_ptr, priv->writebuf, priv->writelen);
  531. buf_len = priv->writelen;
  532. priv->writelen = 0;
  533. spin_unlock_irqrestore(&priv->lock, flags);
  534. dev_dbg(&port->dev, "%s - writing %i chars : %*ph\n", __func__,
  535. buf_len, buf_len, buf_ptr);
  536. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  537. usb_sndbulkpipe(port->serial->dev,
  538. port->bulk_out_endpointAddress),
  539. port->write_urb->transfer_buffer, buf_len + 3,
  540. iuu_rxcmd, port);
  541. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  542. usb_serial_port_softint(port);
  543. return result;
  544. }
  545. static int iuu_read_buf(struct usb_serial_port *port, int len)
  546. {
  547. int result;
  548. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  549. usb_rcvbulkpipe(port->serial->dev,
  550. port->bulk_in_endpointAddress),
  551. port->read_urb->transfer_buffer, len,
  552. read_buf_callback, port);
  553. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  554. return result;
  555. }
  556. static void iuu_uart_read_callback(struct urb *urb)
  557. {
  558. struct usb_serial_port *port = urb->context;
  559. struct iuu_private *priv = usb_get_serial_port_data(port);
  560. unsigned long flags;
  561. int status = urb->status;
  562. int error = 0;
  563. int len = 0;
  564. unsigned char *data = urb->transfer_buffer;
  565. priv->poll++;
  566. if (status) {
  567. dev_dbg(&port->dev, "%s - status = %d\n", __func__, status);
  568. /* error stop all */
  569. return;
  570. }
  571. if (urb->actual_length == 1)
  572. len = (int) data[0];
  573. if (urb->actual_length > 1) {
  574. dev_dbg(&port->dev, "%s - urb->actual_length = %i\n", __func__,
  575. urb->actual_length);
  576. error = 1;
  577. return;
  578. }
  579. /* if len > 0 call readbuf */
  580. if (len > 0 && error == 0) {
  581. dev_dbg(&port->dev, "%s - call read buf - len to read is %i\n",
  582. __func__, len);
  583. status = iuu_read_buf(port, len);
  584. return;
  585. }
  586. /* need to update status ? */
  587. if (priv->poll > 99) {
  588. status = iuu_status(port);
  589. priv->poll = 0;
  590. return;
  591. }
  592. /* reset waiting ? */
  593. if (priv->reset == 1) {
  594. status = iuu_reset(port, 0xC);
  595. return;
  596. }
  597. /* Writebuf is waiting */
  598. spin_lock_irqsave(&priv->lock, flags);
  599. if (priv->writelen > 0) {
  600. spin_unlock_irqrestore(&priv->lock, flags);
  601. status = iuu_bulk_write(port);
  602. return;
  603. }
  604. spin_unlock_irqrestore(&priv->lock, flags);
  605. /* if nothing to write call again rxcmd */
  606. dev_dbg(&port->dev, "%s - rxcmd recall\n", __func__);
  607. iuu_led_activity_off(urb);
  608. }
  609. static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
  610. const u8 *buf, int count)
  611. {
  612. struct iuu_private *priv = usb_get_serial_port_data(port);
  613. unsigned long flags;
  614. if (count > 256)
  615. return -ENOMEM;
  616. spin_lock_irqsave(&priv->lock, flags);
  617. /* fill the buffer */
  618. memcpy(priv->writebuf + priv->writelen, buf, count);
  619. priv->writelen += count;
  620. spin_unlock_irqrestore(&priv->lock, flags);
  621. return count;
  622. }
  623. static void read_rxcmd_callback(struct urb *urb)
  624. {
  625. struct usb_serial_port *port = urb->context;
  626. int result;
  627. int status = urb->status;
  628. if (status) {
  629. /* error stop all */
  630. return;
  631. }
  632. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  633. usb_rcvbulkpipe(port->serial->dev,
  634. port->bulk_in_endpointAddress),
  635. port->read_urb->transfer_buffer, 256,
  636. iuu_uart_read_callback, port);
  637. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  638. dev_dbg(&port->dev, "%s - submit result = %d\n", __func__, result);
  639. }
  640. static int iuu_uart_on(struct usb_serial_port *port)
  641. {
  642. int status;
  643. u8 *buf;
  644. buf = kmalloc(4, GFP_KERNEL);
  645. if (!buf)
  646. return -ENOMEM;
  647. buf[0] = IUU_UART_ENABLE;
  648. buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
  649. buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
  650. buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
  651. status = bulk_immediate(port, buf, 4);
  652. if (status != IUU_OPERATION_OK) {
  653. dev_dbg(&port->dev, "%s - uart_on error\n", __func__);
  654. goto uart_enable_failed;
  655. }
  656. /* iuu_reset() the card after iuu_uart_on() */
  657. status = iuu_uart_flush(port);
  658. if (status != IUU_OPERATION_OK)
  659. dev_dbg(&port->dev, "%s - uart_flush error\n", __func__);
  660. uart_enable_failed:
  661. kfree(buf);
  662. return status;
  663. }
  664. /* Disables the IUU UART (a.k.a. the Phoenix voiderface) */
  665. static int iuu_uart_off(struct usb_serial_port *port)
  666. {
  667. int status;
  668. u8 *buf;
  669. buf = kmalloc(1, GFP_KERNEL);
  670. if (!buf)
  671. return -ENOMEM;
  672. buf[0] = IUU_UART_DISABLE;
  673. status = bulk_immediate(port, buf, 1);
  674. if (status != IUU_OPERATION_OK)
  675. dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
  676. kfree(buf);
  677. return status;
  678. }
  679. static int iuu_uart_baud(struct usb_serial_port *port, u32 baud_base,
  680. u32 *actual, u8 parity)
  681. {
  682. int status;
  683. u32 baud;
  684. u8 *dataout;
  685. u8 DataCount = 0;
  686. u8 T1Frekvens = 0;
  687. u8 T1reload = 0;
  688. unsigned int T1FrekvensHZ = 0;
  689. dev_dbg(&port->dev, "%s - enter baud_base=%d\n", __func__, baud_base);
  690. dataout = kmalloc(5, GFP_KERNEL);
  691. if (!dataout)
  692. return -ENOMEM;
  693. /*baud = (((priv->clk / 35) * baud_base) / 100000); */
  694. baud = baud_base;
  695. if (baud < 1200 || baud > 230400) {
  696. kfree(dataout);
  697. return IUU_INVALID_PARAMETER;
  698. }
  699. if (baud > 977) {
  700. T1Frekvens = 3;
  701. T1FrekvensHZ = 500000;
  702. }
  703. if (baud > 3906) {
  704. T1Frekvens = 2;
  705. T1FrekvensHZ = 2000000;
  706. }
  707. if (baud > 11718) {
  708. T1Frekvens = 1;
  709. T1FrekvensHZ = 6000000;
  710. }
  711. if (baud > 46875) {
  712. T1Frekvens = 0;
  713. T1FrekvensHZ = 24000000;
  714. }
  715. T1reload = 256 - (u8) (T1FrekvensHZ / (baud * 2));
  716. /* magic number here: ENTER_FIRMWARE_UPDATE; */
  717. dataout[DataCount++] = IUU_UART_ESC;
  718. /* magic number here: CHANGE_BAUD; */
  719. dataout[DataCount++] = IUU_UART_CHANGE;
  720. dataout[DataCount++] = T1Frekvens;
  721. dataout[DataCount++] = T1reload;
  722. *actual = (T1FrekvensHZ / (256 - T1reload)) / 2;
  723. switch (parity & 0x0F) {
  724. case IUU_PARITY_NONE:
  725. dataout[DataCount++] = 0x00;
  726. break;
  727. case IUU_PARITY_EVEN:
  728. dataout[DataCount++] = 0x01;
  729. break;
  730. case IUU_PARITY_ODD:
  731. dataout[DataCount++] = 0x02;
  732. break;
  733. case IUU_PARITY_MARK:
  734. dataout[DataCount++] = 0x03;
  735. break;
  736. case IUU_PARITY_SPACE:
  737. dataout[DataCount++] = 0x04;
  738. break;
  739. default:
  740. kfree(dataout);
  741. return IUU_INVALID_PARAMETER;
  742. break;
  743. }
  744. switch (parity & 0xF0) {
  745. case IUU_ONE_STOP_BIT:
  746. dataout[DataCount - 1] |= IUU_ONE_STOP_BIT;
  747. break;
  748. case IUU_TWO_STOP_BITS:
  749. dataout[DataCount - 1] |= IUU_TWO_STOP_BITS;
  750. break;
  751. default:
  752. kfree(dataout);
  753. return IUU_INVALID_PARAMETER;
  754. break;
  755. }
  756. status = bulk_immediate(port, dataout, DataCount);
  757. if (status != IUU_OPERATION_OK)
  758. dev_dbg(&port->dev, "%s - uart_off error\n", __func__);
  759. kfree(dataout);
  760. return status;
  761. }
  762. static void iuu_set_termios(struct tty_struct *tty,
  763. struct usb_serial_port *port, struct ktermios *old_termios)
  764. {
  765. const u32 supported_mask = CMSPAR|PARENB|PARODD;
  766. struct iuu_private *priv = usb_get_serial_port_data(port);
  767. unsigned int cflag = tty->termios.c_cflag;
  768. int status;
  769. u32 actual;
  770. u32 parity;
  771. int csize = CS7;
  772. int baud;
  773. u32 newval = cflag & supported_mask;
  774. /* Just use the ospeed. ispeed should be the same. */
  775. baud = tty->termios.c_ospeed;
  776. dev_dbg(&port->dev, "%s - enter c_ospeed or baud=%d\n", __func__, baud);
  777. /* compute the parity parameter */
  778. parity = 0;
  779. if (cflag & CMSPAR) { /* Using mark space */
  780. if (cflag & PARODD)
  781. parity |= IUU_PARITY_SPACE;
  782. else
  783. parity |= IUU_PARITY_MARK;
  784. } else if (!(cflag & PARENB)) {
  785. parity |= IUU_PARITY_NONE;
  786. csize = CS8;
  787. } else if (cflag & PARODD)
  788. parity |= IUU_PARITY_ODD;
  789. else
  790. parity |= IUU_PARITY_EVEN;
  791. parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
  792. /* set it */
  793. status = iuu_uart_baud(port,
  794. baud * priv->boost / 100,
  795. &actual, parity);
  796. /* set the termios value to the real one, so the user now what has
  797. * changed. We support few fields so its easies to copy the old hw
  798. * settings back over and then adjust them
  799. */
  800. if (old_termios)
  801. tty_termios_copy_hw(&tty->termios, old_termios);
  802. if (status != 0) /* Set failed - return old bits */
  803. return;
  804. /* Re-encode speed, parity and csize */
  805. tty_encode_baud_rate(tty, baud, baud);
  806. tty->termios.c_cflag &= ~(supported_mask|CSIZE);
  807. tty->termios.c_cflag |= newval | csize;
  808. }
  809. static void iuu_close(struct usb_serial_port *port)
  810. {
  811. /* iuu_led (port,255,0,0,0); */
  812. iuu_uart_off(port);
  813. usb_kill_urb(port->write_urb);
  814. usb_kill_urb(port->read_urb);
  815. iuu_led(port, 0, 0, 0xF000, 0xFF);
  816. }
  817. static void iuu_init_termios(struct tty_struct *tty)
  818. {
  819. tty->termios = tty_std_termios;
  820. tty->termios.c_cflag = CLOCAL | CREAD | CS8 | B9600
  821. | TIOCM_CTS | CSTOPB | PARENB;
  822. tty->termios.c_ispeed = 9600;
  823. tty->termios.c_ospeed = 9600;
  824. tty->termios.c_lflag = 0;
  825. tty->termios.c_oflag = 0;
  826. tty->termios.c_iflag = 0;
  827. }
  828. static int iuu_open(struct tty_struct *tty, struct usb_serial_port *port)
  829. {
  830. struct usb_serial *serial = port->serial;
  831. struct device *dev = &port->dev;
  832. int result;
  833. int baud;
  834. u32 actual;
  835. struct iuu_private *priv = usb_get_serial_port_data(port);
  836. baud = tty->termios.c_ospeed;
  837. dev_dbg(dev, "%s - baud %d\n", __func__, baud);
  838. usb_clear_halt(serial->dev, port->write_urb->pipe);
  839. usb_clear_halt(serial->dev, port->read_urb->pipe);
  840. priv->poll = 0;
  841. #define SOUP(a, b, c, d) do { \
  842. result = usb_control_msg(port->serial->dev, \
  843. usb_sndctrlpipe(port->serial->dev, 0), \
  844. b, a, c, d, NULL, 0, 1000); \
  845. dev_dbg(dev, "0x%x:0x%x:0x%x:0x%x %d\n", a, b, c, d, result); } while (0)
  846. /* This is not UART related but IUU USB driver related or something */
  847. /* like that. Basically no IUU will accept any commands from the USB */
  848. /* host unless it has received the following message */
  849. /* sprintf(buf ,"%c%c%c%c",0x03,0x02,0x02,0x0); */
  850. SOUP(0x03, 0x02, 0x02, 0x0);
  851. iuu_led(port, 0xF000, 0xF000, 0, 0xFF);
  852. iuu_uart_on(port);
  853. if (boost < 100)
  854. boost = 100;
  855. priv->boost = boost;
  856. switch (clockmode) {
  857. case 2: /* 3.680 Mhz */
  858. priv->clk = IUU_CLK_3680000;
  859. iuu_clk(port, IUU_CLK_3680000 * boost / 100);
  860. result =
  861. iuu_uart_baud(port, baud * boost / 100, &actual,
  862. IUU_PARITY_EVEN);
  863. break;
  864. case 3: /* 6.00 Mhz */
  865. iuu_clk(port, IUU_CLK_6000000 * boost / 100);
  866. priv->clk = IUU_CLK_6000000;
  867. /* Ratio of 6000000 to 3500000 for baud 9600 */
  868. result =
  869. iuu_uart_baud(port, 16457 * boost / 100, &actual,
  870. IUU_PARITY_EVEN);
  871. break;
  872. default: /* 3.579 Mhz */
  873. iuu_clk(port, IUU_CLK_3579000 * boost / 100);
  874. priv->clk = IUU_CLK_3579000;
  875. result =
  876. iuu_uart_baud(port, baud * boost / 100, &actual,
  877. IUU_PARITY_EVEN);
  878. }
  879. /* set the cardin cardout signals */
  880. switch (cdmode) {
  881. case 0:
  882. iuu_cardin = 0;
  883. iuu_cardout = 0;
  884. break;
  885. case 1:
  886. iuu_cardin = TIOCM_CD;
  887. iuu_cardout = 0;
  888. break;
  889. case 2:
  890. iuu_cardin = 0;
  891. iuu_cardout = TIOCM_CD;
  892. break;
  893. case 3:
  894. iuu_cardin = TIOCM_DSR;
  895. iuu_cardout = 0;
  896. break;
  897. case 4:
  898. iuu_cardin = 0;
  899. iuu_cardout = TIOCM_DSR;
  900. break;
  901. case 5:
  902. iuu_cardin = TIOCM_CTS;
  903. iuu_cardout = 0;
  904. break;
  905. case 6:
  906. iuu_cardin = 0;
  907. iuu_cardout = TIOCM_CTS;
  908. break;
  909. case 7:
  910. iuu_cardin = TIOCM_RNG;
  911. iuu_cardout = 0;
  912. break;
  913. case 8:
  914. iuu_cardin = 0;
  915. iuu_cardout = TIOCM_RNG;
  916. }
  917. iuu_uart_flush(port);
  918. dev_dbg(dev, "%s - initialization done\n", __func__);
  919. memset(port->write_urb->transfer_buffer, IUU_UART_RX, 1);
  920. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  921. usb_sndbulkpipe(port->serial->dev,
  922. port->bulk_out_endpointAddress),
  923. port->write_urb->transfer_buffer, 1,
  924. read_rxcmd_callback, port);
  925. result = usb_submit_urb(port->write_urb, GFP_KERNEL);
  926. if (result) {
  927. dev_err(dev, "%s - failed submitting read urb, error %d\n", __func__, result);
  928. iuu_close(port);
  929. } else {
  930. dev_dbg(dev, "%s - rxcmd OK\n", __func__);
  931. }
  932. return result;
  933. }
  934. /* how to change VCC */
  935. static int iuu_vcc_set(struct usb_serial_port *port, unsigned int vcc)
  936. {
  937. int status;
  938. u8 *buf;
  939. buf = kmalloc(5, GFP_KERNEL);
  940. if (!buf)
  941. return -ENOMEM;
  942. buf[0] = IUU_SET_VCC;
  943. buf[1] = vcc & 0xFF;
  944. buf[2] = (vcc >> 8) & 0xFF;
  945. buf[3] = (vcc >> 16) & 0xFF;
  946. buf[4] = (vcc >> 24) & 0xFF;
  947. status = bulk_immediate(port, buf, 5);
  948. kfree(buf);
  949. if (status != IUU_OPERATION_OK)
  950. dev_dbg(&port->dev, "%s - vcc error status = %2x\n", __func__, status);
  951. else
  952. dev_dbg(&port->dev, "%s - vcc OK !\n", __func__);
  953. return status;
  954. }
  955. /*
  956. * Sysfs Attributes
  957. */
  958. static ssize_t vcc_mode_show(struct device *dev,
  959. struct device_attribute *attr, char *buf)
  960. {
  961. struct usb_serial_port *port = to_usb_serial_port(dev);
  962. struct iuu_private *priv = usb_get_serial_port_data(port);
  963. return sprintf(buf, "%d\n", priv->vcc);
  964. }
  965. static ssize_t vcc_mode_store(struct device *dev,
  966. struct device_attribute *attr, const char *buf, size_t count)
  967. {
  968. struct usb_serial_port *port = to_usb_serial_port(dev);
  969. struct iuu_private *priv = usb_get_serial_port_data(port);
  970. unsigned long v;
  971. if (kstrtoul(buf, 10, &v)) {
  972. dev_err(dev, "%s - vcc_mode: %s is not a unsigned long\n",
  973. __func__, buf);
  974. goto fail_store_vcc_mode;
  975. }
  976. dev_dbg(dev, "%s: setting vcc_mode = %ld\n", __func__, v);
  977. if ((v != 3) && (v != 5)) {
  978. dev_err(dev, "%s - vcc_mode %ld is invalid\n", __func__, v);
  979. } else {
  980. iuu_vcc_set(port, v);
  981. priv->vcc = v;
  982. }
  983. fail_store_vcc_mode:
  984. return count;
  985. }
  986. static DEVICE_ATTR_RW(vcc_mode);
  987. static int iuu_create_sysfs_attrs(struct usb_serial_port *port)
  988. {
  989. return device_create_file(&port->dev, &dev_attr_vcc_mode);
  990. }
  991. static int iuu_remove_sysfs_attrs(struct usb_serial_port *port)
  992. {
  993. device_remove_file(&port->dev, &dev_attr_vcc_mode);
  994. return 0;
  995. }
  996. /*
  997. * End Sysfs Attributes
  998. */
  999. static struct usb_serial_driver iuu_device = {
  1000. .driver = {
  1001. .owner = THIS_MODULE,
  1002. .name = "iuu_phoenix",
  1003. },
  1004. .id_table = id_table,
  1005. .num_ports = 1,
  1006. .num_bulk_in = 1,
  1007. .num_bulk_out = 1,
  1008. .bulk_in_size = 512,
  1009. .bulk_out_size = 512,
  1010. .open = iuu_open,
  1011. .close = iuu_close,
  1012. .write = iuu_uart_write,
  1013. .read_bulk_callback = iuu_uart_read_callback,
  1014. .tiocmget = iuu_tiocmget,
  1015. .tiocmset = iuu_tiocmset,
  1016. .set_termios = iuu_set_termios,
  1017. .init_termios = iuu_init_termios,
  1018. .port_probe = iuu_port_probe,
  1019. .port_remove = iuu_port_remove,
  1020. };
  1021. static struct usb_serial_driver * const serial_drivers[] = {
  1022. &iuu_device, NULL
  1023. };
  1024. module_usb_serial_driver(serial_drivers, id_table);
  1025. MODULE_AUTHOR("Alain Degreffe eczema@ecze.com");
  1026. MODULE_DESCRIPTION(DRIVER_DESC);
  1027. MODULE_LICENSE("GPL");
  1028. module_param(xmas, bool, S_IRUGO | S_IWUSR);
  1029. MODULE_PARM_DESC(xmas, "Xmas colors enabled or not");
  1030. module_param(boost, int, S_IRUGO | S_IWUSR);
  1031. MODULE_PARM_DESC(boost, "Card overclock boost (in percent 100-500)");
  1032. module_param(clockmode, int, S_IRUGO | S_IWUSR);
  1033. MODULE_PARM_DESC(clockmode, "Card clock mode (1=3.579 MHz, 2=3.680 MHz, "
  1034. "3=6 Mhz)");
  1035. module_param(cdmode, int, S_IRUGO | S_IWUSR);
  1036. MODULE_PARM_DESC(cdmode, "Card detect mode (0=none, 1=CD, 2=!CD, 3=DSR, "
  1037. "4=!DSR, 5=CTS, 6=!CTS, 7=RING, 8=!RING)");
  1038. module_param(vcc_default, int, S_IRUGO | S_IWUSR);
  1039. MODULE_PARM_DESC(vcc_default, "Set default VCC (either 3 for 3.3V or 5 "
  1040. "for 5V). Default to 5.");