mos7720.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. /*
  2. * mos7720.c
  3. * Controls the Moschip 7720 usb to dual port serial convertor
  4. *
  5. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * Developed by:
  12. * VijayaKumar.G.N. <vijaykumar@aspirecom.net>
  13. * AjayKumar <ajay@aspirecom.net>
  14. * Gurudeva.N. <gurudev@aspirecom.net>
  15. *
  16. * Cleaned up from the original by:
  17. * Greg Kroah-Hartman <gregkh@suse.de>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <asm/uaccess.h>
  37. /*
  38. * Version Information
  39. */
  40. #define DRIVER_VERSION "1.0.0.4F"
  41. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  42. #define DRIVER_DESC "Moschip USB Serial Driver"
  43. /* default urb timeout */
  44. #define MOS_WDR_TIMEOUT (HZ * 5)
  45. #define MOS_PORT1 0x0200
  46. #define MOS_PORT2 0x0300
  47. #define MOS_VENREG 0x0000
  48. #define MOS_MAX_PORT 0x02
  49. #define MOS_WRITE 0x0E
  50. #define MOS_READ 0x0D
  51. /* Interrupt Rotinue Defines */
  52. #define SERIAL_IIR_RLS 0x06
  53. #define SERIAL_IIR_RDA 0x04
  54. #define SERIAL_IIR_CTI 0x0c
  55. #define SERIAL_IIR_THR 0x02
  56. #define SERIAL_IIR_MS 0x00
  57. #define NUM_URBS 16 /* URB Count */
  58. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  59. /* This structure holds all of the local port information */
  60. struct moschip_port
  61. {
  62. __u8 shadowLCR; /* last LCR value received */
  63. __u8 shadowMCR; /* last MCR value received */
  64. __u8 shadowMSR; /* last MSR value received */
  65. char open;
  66. struct async_icount icount;
  67. struct usb_serial_port *port; /* loop back to the owner */
  68. struct urb *write_urb_pool[NUM_URBS];
  69. };
  70. /* This structure holds all of the individual serial device information */
  71. struct moschip_serial
  72. {
  73. int interrupt_started;
  74. };
  75. static int debug;
  76. #define USB_VENDOR_ID_MOSCHIP 0x9710
  77. #define MOSCHIP_DEVICE_ID_7720 0x7720
  78. #define MOSCHIP_DEVICE_ID_7715 0x7715
  79. static struct usb_device_id moschip_port_id_table [] = {
  80. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP,MOSCHIP_DEVICE_ID_7720) },
  81. { } /* terminating entry */
  82. };
  83. MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
  84. /*
  85. * mos7720_interrupt_callback
  86. * this is the callback function for when we have received data on the
  87. * interrupt endpoint.
  88. */
  89. static void mos7720_interrupt_callback(struct urb *urb)
  90. {
  91. int result;
  92. int length;
  93. __u32 *data;
  94. unsigned int status;
  95. __u8 sp1;
  96. __u8 sp2;
  97. __u8 st;
  98. dbg("%s"," : Entering\n");
  99. if (!urb) {
  100. dbg("%s","Invalid Pointer !!!!:\n");
  101. return;
  102. }
  103. switch (urb->status) {
  104. case 0:
  105. /* success */
  106. break;
  107. case -ECONNRESET:
  108. case -ENOENT:
  109. case -ESHUTDOWN:
  110. /* this urb is terminated, clean up */
  111. dbg("%s - urb shutting down with status: %d", __FUNCTION__,
  112. urb->status);
  113. return;
  114. default:
  115. dbg("%s - nonzero urb status received: %d", __FUNCTION__,
  116. urb->status);
  117. goto exit;
  118. }
  119. length = urb->actual_length;
  120. data = urb->transfer_buffer;
  121. /* Moschip get 4 bytes
  122. * Byte 1 IIR Port 1 (port.number is 0)
  123. * Byte 2 IIR Port 2 (port.number is 1)
  124. * Byte 3 --------------
  125. * Byte 4 FIFO status for both */
  126. if (length && length > 4) {
  127. dbg("Wrong data !!!");
  128. return;
  129. }
  130. status = *data;
  131. sp1 = (status & 0xff000000)>>24;
  132. sp2 = (status & 0x00ff0000)>>16;
  133. st = status & 0x000000ff;
  134. if ((sp1 & 0x01) || (sp2 & 0x01)) {
  135. /* No Interrupt Pending in both the ports */
  136. dbg("No Interrupt !!!");
  137. } else {
  138. switch (sp1 & 0x0f) {
  139. case SERIAL_IIR_RLS:
  140. dbg("Serial Port 1: Receiver status error or address "
  141. "bit detected in 9-bit mode\n");
  142. break;
  143. case SERIAL_IIR_CTI:
  144. dbg("Serial Port 1: Receiver time out");
  145. break;
  146. case SERIAL_IIR_MS:
  147. dbg("Serial Port 1: Modem status change");
  148. break;
  149. }
  150. switch (sp2 & 0x0f) {
  151. case SERIAL_IIR_RLS:
  152. dbg("Serial Port 2: Receiver status error or address "
  153. "bit detected in 9-bit mode");
  154. break;
  155. case SERIAL_IIR_CTI:
  156. dbg("Serial Port 2: Receiver time out");
  157. break;
  158. case SERIAL_IIR_MS:
  159. dbg("Serial Port 2: Modem status change");
  160. break;
  161. }
  162. }
  163. exit:
  164. result = usb_submit_urb(urb, GFP_ATOMIC);
  165. if (result)
  166. dev_err(&urb->dev->dev,
  167. "%s - Error %d submitting control urb\n",
  168. __FUNCTION__, result);
  169. return;
  170. }
  171. /*
  172. * mos7720_bulk_in_callback
  173. * this is the callback function for when we have received data on the
  174. * bulk in endpoint.
  175. */
  176. static void mos7720_bulk_in_callback(struct urb *urb)
  177. {
  178. int status;
  179. unsigned char *data ;
  180. struct usb_serial_port *port;
  181. struct moschip_port *mos7720_port;
  182. struct tty_struct *tty;
  183. if (urb->status) {
  184. dbg("nonzero read bulk status received: %d",urb->status);
  185. return;
  186. }
  187. mos7720_port = urb->context;
  188. if (!mos7720_port) {
  189. dbg("%s","NULL mos7720_port pointer \n");
  190. return ;
  191. }
  192. port = mos7720_port->port;
  193. dbg("Entering...%s", __FUNCTION__);
  194. data = urb->transfer_buffer;
  195. tty = port->tty;
  196. if (tty && urb->actual_length) {
  197. tty_buffer_request_room(tty, urb->actual_length);
  198. tty_insert_flip_string(tty, data, urb->actual_length);
  199. tty_flip_buffer_push(tty);
  200. }
  201. if (!port->read_urb) {
  202. dbg("URB KILLED !!!");
  203. return;
  204. }
  205. if (port->read_urb->status != -EINPROGRESS) {
  206. port->read_urb->dev = port->serial->dev;
  207. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  208. if (status)
  209. dbg("usb_submit_urb(read bulk) failed, status = %d",
  210. status);
  211. }
  212. }
  213. /*
  214. * mos7720_bulk_out_data_callback
  215. * this is the callback function for when we have finished sending serial
  216. * data on the bulk out endpoint.
  217. */
  218. static void mos7720_bulk_out_data_callback(struct urb *urb)
  219. {
  220. struct moschip_port *mos7720_port;
  221. struct tty_struct *tty;
  222. if (urb->status) {
  223. dbg("nonzero write bulk status received:%d", urb->status);
  224. return;
  225. }
  226. mos7720_port = urb->context;
  227. if (!mos7720_port) {
  228. dbg("NULL mos7720_port pointer");
  229. return ;
  230. }
  231. dbg("Entering .........");
  232. tty = mos7720_port->port->tty;
  233. if (tty && mos7720_port->open)
  234. tty_wakeup(tty);
  235. }
  236. /*
  237. * send_mos_cmd
  238. * this function will be used for sending command to device
  239. */
  240. static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
  241. __u16 index, void *data)
  242. {
  243. int status;
  244. unsigned int pipe;
  245. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  246. __u8 requesttype;
  247. __u16 size = 0x0000;
  248. if (value < MOS_MAX_PORT) {
  249. if (product == MOSCHIP_DEVICE_ID_7715) {
  250. value = value*0x100+0x100;
  251. } else {
  252. value = value*0x100+0x200;
  253. }
  254. } else {
  255. value = 0x0000;
  256. if ((product == MOSCHIP_DEVICE_ID_7715) &&
  257. (index != 0x08)) {
  258. dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
  259. //index = 0x01 ;
  260. }
  261. }
  262. if (request == MOS_WRITE) {
  263. request = (__u8)MOS_WRITE;
  264. requesttype = (__u8)0x40;
  265. value = value + (__u16)*((unsigned char *)data);
  266. data = NULL;
  267. pipe = usb_sndctrlpipe(serial->dev, 0);
  268. } else {
  269. request = (__u8)MOS_READ;
  270. requesttype = (__u8)0xC0;
  271. size = 0x01;
  272. pipe = usb_rcvctrlpipe(serial->dev,0);
  273. }
  274. status = usb_control_msg(serial->dev, pipe, request, requesttype,
  275. value, index, data, size, MOS_WDR_TIMEOUT);
  276. if (status < 0)
  277. dbg("Command Write failed Value %x index %x\n",value,index);
  278. return status;
  279. }
  280. static int mos7720_open(struct usb_serial_port *port, struct file * filp)
  281. {
  282. struct usb_serial *serial;
  283. struct usb_serial_port *port0;
  284. struct urb *urb;
  285. struct moschip_serial *mos7720_serial;
  286. struct moschip_port *mos7720_port;
  287. int response;
  288. int port_number;
  289. char data;
  290. int allocated_urbs = 0;
  291. int j;
  292. serial = port->serial;
  293. mos7720_port = usb_get_serial_port_data(port);
  294. if (mos7720_port == NULL)
  295. return -ENODEV;
  296. port0 = serial->port[0];
  297. mos7720_serial = usb_get_serial_data(serial);
  298. if (mos7720_serial == NULL || port0 == NULL)
  299. return -ENODEV;
  300. usb_clear_halt(serial->dev, port->write_urb->pipe);
  301. usb_clear_halt(serial->dev, port->read_urb->pipe);
  302. /* Initialising the write urb pool */
  303. for (j = 0; j < NUM_URBS; ++j) {
  304. urb = usb_alloc_urb(0,GFP_KERNEL);
  305. mos7720_port->write_urb_pool[j] = urb;
  306. if (urb == NULL) {
  307. err("No more urbs???");
  308. continue;
  309. }
  310. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  311. GFP_KERNEL);
  312. if (!urb->transfer_buffer) {
  313. err("%s-out of memory for urb buffers.", __FUNCTION__);
  314. usb_free_urb(mos7720_port->write_urb_pool[j]);
  315. mos7720_port->write_urb_pool[j] = NULL;
  316. continue;
  317. }
  318. allocated_urbs++;
  319. }
  320. if (!allocated_urbs)
  321. return -ENOMEM;
  322. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  323. *
  324. * Register Index
  325. * 1 : IER
  326. * 2 : FCR
  327. * 3 : LCR
  328. * 4 : MCR
  329. *
  330. * 0x08 : SP1/2 Control Reg
  331. */
  332. port_number = port->number - port->serial->minor;
  333. send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
  334. dbg("SS::%p LSR:%x\n",mos7720_port, data);
  335. dbg("Check:Sending Command ..........");
  336. data = 0x02;
  337. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
  338. data = 0x02;
  339. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
  340. data = 0x00;
  341. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  342. data = 0x00;
  343. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  344. data = 0xCF;
  345. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  346. data = 0x03;
  347. mos7720_port->shadowLCR = data;
  348. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  349. data = 0x0b;
  350. mos7720_port->shadowMCR = data;
  351. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  352. data = 0x0b;
  353. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  354. data = 0x00;
  355. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  356. data = 0x00;
  357. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  358. /* data = 0x00;
  359. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
  360. data = 0x03;
  361. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  362. data = 0x00;
  363. send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  364. */
  365. data = 0x00;
  366. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  367. data = data | (port->number - port->serial->minor + 1);
  368. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  369. data = 0x83;
  370. mos7720_port->shadowLCR = data;
  371. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  372. data = 0x0c;
  373. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  374. data = 0x00;
  375. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  376. data = 0x03;
  377. mos7720_port->shadowLCR = data;
  378. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  379. data = 0x0c;
  380. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  381. data = 0x0c;
  382. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  383. //Matrix
  384. /* force low_latency on so that our tty_push actually forces *
  385. * the data through,otherwise it is scheduled, and with *
  386. * high data rates (like with OHCI) data can get lost. */
  387. if (port->tty)
  388. port->tty->low_latency = 1;
  389. /* see if we've set up our endpoint info yet *
  390. * (can't set it up in mos7720_startup as the *
  391. * structures were not set up at that time.) */
  392. if (!mos7720_serial->interrupt_started) {
  393. dbg("Interrupt buffer NULL !!!");
  394. /* not set up yet, so do it now */
  395. mos7720_serial->interrupt_started = 1;
  396. dbg("To Submit URB !!!");
  397. /* set up our interrupt urb */
  398. usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
  399. usb_rcvintpipe(serial->dev,
  400. port->interrupt_in_endpointAddress),
  401. port0->interrupt_in_buffer,
  402. port0->interrupt_in_urb->transfer_buffer_length,
  403. mos7720_interrupt_callback, mos7720_port,
  404. port0->interrupt_in_urb->interval);
  405. /* start interrupt read for this mos7720 this interrupt *
  406. * will continue as long as the mos7720 is connected */
  407. dbg("Submit URB over !!!");
  408. response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
  409. if (response)
  410. dev_err(&port->dev,
  411. "%s - Error %d submitting control urb",
  412. __FUNCTION__, response);
  413. }
  414. /* set up our bulk in urb */
  415. usb_fill_bulk_urb(port->read_urb, serial->dev,
  416. usb_rcvbulkpipe(serial->dev,
  417. port->bulk_in_endpointAddress),
  418. port->bulk_in_buffer,
  419. port->read_urb->transfer_buffer_length,
  420. mos7720_bulk_in_callback, mos7720_port);
  421. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  422. if (response)
  423. dev_err(&port->dev,
  424. "%s - Error %d submitting read urb", __FUNCTION__, response);
  425. /* initialize our icount structure */
  426. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  427. /* initialize our port settings */
  428. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  429. /* send a open port command */
  430. mos7720_port->open = 1;
  431. return 0;
  432. }
  433. /*
  434. * mos7720_chars_in_buffer
  435. * this function is called by the tty driver when it wants to know how many
  436. * bytes of data we currently have outstanding in the port (data that has
  437. * been written, but hasn't made it out the port yet)
  438. * If successful, we return the number of bytes left to be written in the
  439. * system,
  440. * Otherwise we return a negative error number.
  441. */
  442. static int mos7720_chars_in_buffer(struct usb_serial_port *port)
  443. {
  444. int i;
  445. int chars = 0;
  446. struct moschip_port *mos7720_port;
  447. dbg("%s:entering ...........", __FUNCTION__);
  448. mos7720_port = usb_get_serial_port_data(port);
  449. if (mos7720_port == NULL) {
  450. dbg("%s:leaving ...........", __FUNCTION__);
  451. return -ENODEV;
  452. }
  453. for (i = 0; i < NUM_URBS; ++i) {
  454. if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  455. chars += URB_TRANSFER_BUFFER_SIZE;
  456. }
  457. dbg("%s - returns %d", __FUNCTION__, chars);
  458. return chars;
  459. }
  460. static void mos7720_close(struct usb_serial_port *port, struct file *filp)
  461. {
  462. struct usb_serial *serial;
  463. struct moschip_port *mos7720_port;
  464. char data;
  465. int j;
  466. dbg("mos7720_close:entering...");
  467. serial = port->serial;
  468. mos7720_port = usb_get_serial_port_data(port);
  469. if (mos7720_port == NULL)
  470. return;
  471. for (j = 0; j < NUM_URBS; ++j)
  472. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  473. /* Freeing Write URBs */
  474. for (j = 0; j < NUM_URBS; ++j) {
  475. if (mos7720_port->write_urb_pool[j]) {
  476. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  477. usb_free_urb(mos7720_port->write_urb_pool[j]);
  478. }
  479. }
  480. /* While closing port, shutdown all bulk read, write *
  481. * and interrupt read if they exists */
  482. if (serial->dev) {
  483. dbg("Shutdown bulk write");
  484. usb_kill_urb(port->write_urb);
  485. dbg("Shutdown bulk read");
  486. usb_kill_urb(port->read_urb);
  487. }
  488. data = 0x00;
  489. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  490. 0x04, &data);
  491. data = 0x00;
  492. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  493. 0x01, &data);
  494. mos7720_port->open = 0;
  495. dbg("Leaving %s", __FUNCTION__);
  496. }
  497. static void mos7720_break(struct usb_serial_port *port, int break_state)
  498. {
  499. unsigned char data;
  500. struct usb_serial *serial;
  501. struct moschip_port *mos7720_port;
  502. dbg("Entering %s", __FUNCTION__);
  503. serial = port->serial;
  504. mos7720_port = usb_get_serial_port_data(port);
  505. if (mos7720_port == NULL)
  506. return;
  507. if (break_state == -1)
  508. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  509. else
  510. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  511. mos7720_port->shadowLCR = data;
  512. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  513. 0x03, &data);
  514. return;
  515. }
  516. /*
  517. * mos7720_write_room
  518. * this function is called by the tty driver when it wants to know how many
  519. * bytes of data we can accept for a specific port.
  520. * If successful, we return the amount of room that we have for this port
  521. * Otherwise we return a negative error number.
  522. */
  523. static int mos7720_write_room(struct usb_serial_port *port)
  524. {
  525. struct moschip_port *mos7720_port;
  526. int room = 0;
  527. int i;
  528. dbg("%s:entering ...........", __FUNCTION__);
  529. mos7720_port = usb_get_serial_port_data(port);
  530. if (mos7720_port == NULL) {
  531. dbg("%s:leaving ...........", __FUNCTION__);
  532. return -ENODEV;
  533. }
  534. for (i = 0; i < NUM_URBS; ++i) {
  535. if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  536. room += URB_TRANSFER_BUFFER_SIZE;
  537. }
  538. dbg("%s - returns %d", __FUNCTION__, room);
  539. return room;
  540. }
  541. static int mos7720_write(struct usb_serial_port *port,
  542. const unsigned char *data, int count)
  543. {
  544. int status;
  545. int i;
  546. int bytes_sent = 0;
  547. int transfer_size;
  548. struct moschip_port *mos7720_port;
  549. struct usb_serial *serial;
  550. struct urb *urb;
  551. const unsigned char *current_position = data;
  552. dbg("%s:entering ...........", __FUNCTION__);
  553. serial = port->serial;
  554. mos7720_port = usb_get_serial_port_data(port);
  555. if (mos7720_port == NULL) {
  556. dbg("mos7720_port is NULL");
  557. return -ENODEV;
  558. }
  559. /* try to find a free urb in the list */
  560. urb = NULL;
  561. for (i = 0; i < NUM_URBS; ++i) {
  562. if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  563. urb = mos7720_port->write_urb_pool[i];
  564. dbg("URB:%d",i);
  565. break;
  566. }
  567. }
  568. if (urb == NULL) {
  569. dbg("%s - no more free urbs", __FUNCTION__);
  570. goto exit;
  571. }
  572. if (urb->transfer_buffer == NULL) {
  573. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  574. GFP_KERNEL);
  575. if (urb->transfer_buffer == NULL) {
  576. err("%s no more kernel memory...", __FUNCTION__);
  577. goto exit;
  578. }
  579. }
  580. transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
  581. memcpy(urb->transfer_buffer, current_position, transfer_size);
  582. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size,
  583. urb->transfer_buffer);
  584. /* fill urb with data and submit */
  585. usb_fill_bulk_urb(urb, serial->dev,
  586. usb_sndbulkpipe(serial->dev,
  587. port->bulk_out_endpointAddress),
  588. urb->transfer_buffer, transfer_size,
  589. mos7720_bulk_out_data_callback, mos7720_port);
  590. /* send it down the pipe */
  591. status = usb_submit_urb(urb,GFP_ATOMIC);
  592. if (status) {
  593. err("%s - usb_submit_urb(write bulk) failed with status = %d",
  594. __FUNCTION__, status);
  595. bytes_sent = status;
  596. goto exit;
  597. }
  598. bytes_sent = transfer_size;
  599. exit:
  600. return bytes_sent;
  601. }
  602. static void mos7720_throttle(struct usb_serial_port *port)
  603. {
  604. struct moschip_port *mos7720_port;
  605. struct tty_struct *tty;
  606. int status;
  607. dbg("%s- port %d\n", __FUNCTION__, port->number);
  608. mos7720_port = usb_get_serial_port_data(port);
  609. if (mos7720_port == NULL)
  610. return;
  611. if (!mos7720_port->open) {
  612. dbg("port not opened");
  613. return;
  614. }
  615. dbg("%s: Entering ..........", __FUNCTION__);
  616. tty = port->tty;
  617. if (!tty) {
  618. dbg("%s - no tty available", __FUNCTION__);
  619. return;
  620. }
  621. /* if we are implementing XON/XOFF, send the stop character */
  622. if (I_IXOFF(tty)) {
  623. unsigned char stop_char = STOP_CHAR(tty);
  624. status = mos7720_write(port, &stop_char, 1);
  625. if (status <= 0)
  626. return;
  627. }
  628. /* if we are implementing RTS/CTS, toggle that line */
  629. if (tty->termios->c_cflag & CRTSCTS) {
  630. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  631. status = send_mos_cmd(port->serial, MOS_WRITE,
  632. port->number - port->serial->minor,
  633. UART_MCR, &mos7720_port->shadowMCR);
  634. if (status != 0)
  635. return;
  636. }
  637. }
  638. static void mos7720_unthrottle(struct usb_serial_port *port)
  639. {
  640. struct tty_struct *tty;
  641. int status;
  642. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  643. if (mos7720_port == NULL)
  644. return;
  645. if (!mos7720_port->open) {
  646. dbg("%s - port not opened", __FUNCTION__);
  647. return;
  648. }
  649. dbg("%s: Entering ..........", __FUNCTION__);
  650. tty = port->tty;
  651. if (!tty) {
  652. dbg("%s - no tty available", __FUNCTION__);
  653. return;
  654. }
  655. /* if we are implementing XON/XOFF, send the start character */
  656. if (I_IXOFF(tty)) {
  657. unsigned char start_char = START_CHAR(tty);
  658. status = mos7720_write(port, &start_char, 1);
  659. if (status <= 0)
  660. return;
  661. }
  662. /* if we are implementing RTS/CTS, toggle that line */
  663. if (tty->termios->c_cflag & CRTSCTS) {
  664. mos7720_port->shadowMCR |= UART_MCR_RTS;
  665. status = send_mos_cmd(port->serial, MOS_WRITE,
  666. port->number - port->serial->minor,
  667. UART_MCR, &mos7720_port->shadowMCR);
  668. if (status != 0)
  669. return;
  670. }
  671. }
  672. static int set_higher_rates(struct moschip_port *mos7720_port,
  673. unsigned int baud)
  674. {
  675. unsigned char data;
  676. struct usb_serial_port *port;
  677. struct usb_serial *serial;
  678. int port_number;
  679. if (mos7720_port == NULL)
  680. return -EINVAL;
  681. port = mos7720_port->port;
  682. serial = port->serial;
  683. /***********************************************
  684. * Init Sequence for higher rates
  685. ***********************************************/
  686. dbg("Sending Setting Commands ..........");
  687. port_number = port->number - port->serial->minor;
  688. data = 0x000;
  689. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  690. data = 0x000;
  691. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  692. data = 0x0CF;
  693. send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
  694. data = 0x00b;
  695. mos7720_port->shadowMCR = data;
  696. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  697. data = 0x00b;
  698. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  699. data = 0x000;
  700. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  701. data = 0x000;
  702. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  703. /***********************************************
  704. * Set for higher rates *
  705. ***********************************************/
  706. data = baud * 0x10;
  707. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1,&data);
  708. data = 0x003;
  709. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  710. data = 0x003;
  711. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  712. data = 0x02b;
  713. mos7720_port->shadowMCR = data;
  714. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  715. data = 0x02b;
  716. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  717. /***********************************************
  718. * Set DLL/DLM
  719. ***********************************************/
  720. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  721. mos7720_port->shadowLCR = data;
  722. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  723. data = 0x001; /* DLL */
  724. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  725. data = 0x000; /* DLM */
  726. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  727. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  728. mos7720_port->shadowLCR = data;
  729. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  730. return 0;
  731. }
  732. /* baud rate information */
  733. struct divisor_table_entry
  734. {
  735. __u32 baudrate;
  736. __u16 divisor;
  737. };
  738. /* Define table of divisors for moschip 7720 hardware *
  739. * These assume a 3.6864MHz crystal, the standard /16, and *
  740. * MCR.7 = 0. */
  741. static struct divisor_table_entry divisor_table[] = {
  742. { 50, 2304},
  743. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  744. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  745. { 150, 768},
  746. { 300, 384},
  747. { 600, 192},
  748. { 1200, 96},
  749. { 1800, 64},
  750. { 2400, 48},
  751. { 4800, 24},
  752. { 7200, 16},
  753. { 9600, 12},
  754. { 19200, 6},
  755. { 38400, 3},
  756. { 57600, 2},
  757. { 115200, 1},
  758. };
  759. /*****************************************************************************
  760. * calc_baud_rate_divisor
  761. * this function calculates the proper baud rate divisor for the specified
  762. * baud rate.
  763. *****************************************************************************/
  764. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  765. {
  766. int i;
  767. __u16 custom;
  768. __u16 round1;
  769. __u16 round;
  770. dbg("%s - %d", __FUNCTION__, baudrate);
  771. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  772. if (divisor_table[i].baudrate == baudrate) {
  773. *divisor = divisor_table[i].divisor;
  774. return 0;
  775. }
  776. }
  777. /* After trying for all the standard baud rates *
  778. * Try calculating the divisor for this baud rate */
  779. if (baudrate > 75 && baudrate < 230400) {
  780. /* get the divisor */
  781. custom = (__u16)(230400L / baudrate);
  782. /* Check for round off */
  783. round1 = (__u16)(2304000L / baudrate);
  784. round = (__u16)(round1 - (custom * 10));
  785. if (round > 4)
  786. custom++;
  787. *divisor = custom;
  788. dbg("Baud %d = %d",baudrate, custom);
  789. return 0;
  790. }
  791. dbg("Baud calculation Failed...");
  792. return -EINVAL;
  793. }
  794. /*
  795. * send_cmd_write_baud_rate
  796. * this function sends the proper command to change the baud rate of the
  797. * specified port.
  798. */
  799. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  800. int baudrate)
  801. {
  802. struct usb_serial_port *port;
  803. struct usb_serial *serial;
  804. int divisor;
  805. int status;
  806. unsigned char data;
  807. unsigned char number;
  808. if (mos7720_port == NULL)
  809. return -1;
  810. port = mos7720_port->port;
  811. serial = port->serial;
  812. dbg("%s: Entering ..........", __FUNCTION__);
  813. number = port->number - port->serial->minor;
  814. dbg("%s - port = %d, baud = %d", __FUNCTION__, port->number, baudrate);
  815. /* Calculate the Divisor */
  816. status = calc_baud_rate_divisor(baudrate, &divisor);
  817. if (status) {
  818. err("%s - bad baud rate", __FUNCTION__);
  819. return status;
  820. }
  821. /* Enable access to divisor latch */
  822. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  823. mos7720_port->shadowLCR = data;
  824. send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
  825. /* Write the divisor */
  826. data = ((unsigned char)(divisor & 0xff));
  827. send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
  828. data = ((unsigned char)((divisor & 0xff00) >> 8));
  829. send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
  830. /* Disable access to divisor latch */
  831. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  832. mos7720_port->shadowLCR = data;
  833. send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
  834. return status;
  835. }
  836. /*
  837. * change_port_settings
  838. * This routine is called to set the UART on the device to match
  839. * the specified new settings.
  840. */
  841. static void change_port_settings(struct moschip_port *mos7720_port,
  842. struct ktermios *old_termios)
  843. {
  844. struct usb_serial_port *port;
  845. struct usb_serial *serial;
  846. struct tty_struct *tty;
  847. int baud;
  848. unsigned cflag;
  849. unsigned iflag;
  850. __u8 mask = 0xff;
  851. __u8 lData;
  852. __u8 lParity;
  853. __u8 lStop;
  854. int status;
  855. int port_number;
  856. char data;
  857. if (mos7720_port == NULL)
  858. return ;
  859. port = mos7720_port->port;
  860. serial = port->serial;
  861. port_number = port->number - port->serial->minor;
  862. dbg("%s - port %d", __FUNCTION__, port->number);
  863. if (!mos7720_port->open) {
  864. dbg("%s - port not opened", __FUNCTION__);
  865. return;
  866. }
  867. tty = mos7720_port->port->tty;
  868. if ((!tty) || (!tty->termios)) {
  869. dbg("%s - no tty structures", __FUNCTION__);
  870. return;
  871. }
  872. dbg("%s: Entering ..........", __FUNCTION__);
  873. lData = UART_LCR_WLEN8;
  874. lStop = 0x00; /* 1 stop bit */
  875. lParity = 0x00; /* No parity */
  876. cflag = tty->termios->c_cflag;
  877. iflag = tty->termios->c_iflag;
  878. /* Change the number of bits */
  879. switch (cflag & CSIZE) {
  880. case CS5:
  881. lData = UART_LCR_WLEN5;
  882. mask = 0x1f;
  883. break;
  884. case CS6:
  885. lData = UART_LCR_WLEN6;
  886. mask = 0x3f;
  887. break;
  888. case CS7:
  889. lData = UART_LCR_WLEN7;
  890. mask = 0x7f;
  891. break;
  892. default:
  893. case CS8:
  894. lData = UART_LCR_WLEN8;
  895. break;
  896. }
  897. /* Change the Parity bit */
  898. if (cflag & PARENB) {
  899. if (cflag & PARODD) {
  900. lParity = UART_LCR_PARITY;
  901. dbg("%s - parity = odd", __FUNCTION__);
  902. } else {
  903. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  904. dbg("%s - parity = even", __FUNCTION__);
  905. }
  906. } else {
  907. dbg("%s - parity = none", __FUNCTION__);
  908. }
  909. if (cflag & CMSPAR)
  910. lParity = lParity | 0x20;
  911. /* Change the Stop bit */
  912. if (cflag & CSTOPB) {
  913. lStop = UART_LCR_STOP;
  914. dbg("%s - stop bits = 2", __FUNCTION__);
  915. } else {
  916. lStop = 0x00;
  917. dbg("%s - stop bits = 1", __FUNCTION__);
  918. }
  919. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  920. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  921. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  922. /* Update the LCR with the correct value */
  923. mos7720_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  924. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  925. /* Disable Interrupts */
  926. data = 0x00;
  927. send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
  928. data = 0x00;
  929. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  930. data = 0xcf;
  931. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  932. /* Send the updated LCR value to the mos7720 */
  933. data = mos7720_port->shadowLCR;
  934. send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
  935. data = 0x00b;
  936. mos7720_port->shadowMCR = data;
  937. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  938. data = 0x00b;
  939. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  940. /* set up the MCR register and send it to the mos7720 */
  941. mos7720_port->shadowMCR = UART_MCR_OUT2;
  942. if (cflag & CBAUD)
  943. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  944. if (cflag & CRTSCTS) {
  945. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  946. /* To set hardware flow control to the specified *
  947. * serial port, in SP1/2_CONTROL_REG */
  948. if (port->number) {
  949. data = 0x001;
  950. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  951. 0x08, &data);
  952. } else {
  953. data = 0x002;
  954. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  955. 0x08, &data);
  956. }
  957. } else {
  958. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  959. }
  960. data = mos7720_port->shadowMCR;
  961. send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
  962. /* Determine divisor based on baud rate */
  963. baud = tty_get_baud_rate(tty);
  964. if (!baud) {
  965. /* pick a default, any default... */
  966. dbg("Picked default baud...");
  967. baud = 9600;
  968. }
  969. if (baud >= 230400) {
  970. set_higher_rates(mos7720_port, baud);
  971. /* Enable Interrupts */
  972. data = 0x0c;
  973. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  974. return;
  975. }
  976. dbg("%s - baud rate = %d", __FUNCTION__, baud);
  977. status = send_cmd_write_baud_rate(mos7720_port, baud);
  978. /* Enable Interrupts */
  979. data = 0x0c;
  980. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  981. if (port->read_urb->status != -EINPROGRESS) {
  982. port->read_urb->dev = serial->dev;
  983. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  984. if (status)
  985. dbg("usb_submit_urb(read bulk) failed, status = %d",
  986. status);
  987. }
  988. return;
  989. }
  990. /*
  991. * mos7720_set_termios
  992. * this function is called by the tty driver when it wants to change the
  993. * termios structure.
  994. */
  995. static void mos7720_set_termios(struct usb_serial_port *port,
  996. struct ktermios *old_termios)
  997. {
  998. int status;
  999. unsigned int cflag;
  1000. struct usb_serial *serial;
  1001. struct moschip_port *mos7720_port;
  1002. struct tty_struct *tty;
  1003. serial = port->serial;
  1004. mos7720_port = usb_get_serial_port_data(port);
  1005. if (mos7720_port == NULL)
  1006. return;
  1007. tty = port->tty;
  1008. if (!port->tty || !port->tty->termios) {
  1009. dbg("%s - no tty or termios", __FUNCTION__);
  1010. return;
  1011. }
  1012. if (!mos7720_port->open) {
  1013. dbg("%s - port not opened", __FUNCTION__);
  1014. return;
  1015. }
  1016. dbg("%s\n","setting termios - ASPIRE");
  1017. cflag = tty->termios->c_cflag;
  1018. if (!cflag) {
  1019. printk("%s %s\n",__FUNCTION__,"cflag is NULL");
  1020. return;
  1021. }
  1022. /* check that they really want us to change something */
  1023. if (old_termios) {
  1024. if ((cflag == old_termios->c_cflag) &&
  1025. (RELEVANT_IFLAG(tty->termios->c_iflag) ==
  1026. RELEVANT_IFLAG(old_termios->c_iflag))) {
  1027. dbg("Nothing to change");
  1028. return;
  1029. }
  1030. }
  1031. dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
  1032. tty->termios->c_cflag,
  1033. RELEVANT_IFLAG(tty->termios->c_iflag));
  1034. if (old_termios)
  1035. dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
  1036. old_termios->c_cflag,
  1037. RELEVANT_IFLAG(old_termios->c_iflag));
  1038. dbg("%s - port %d", __FUNCTION__, port->number);
  1039. /* change the port settings to the new ones specified */
  1040. change_port_settings(mos7720_port, old_termios);
  1041. if(!port->read_urb) {
  1042. dbg("%s","URB KILLED !!!!!\n");
  1043. return;
  1044. }
  1045. if(port->read_urb->status != -EINPROGRESS) {
  1046. port->read_urb->dev = serial->dev;
  1047. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1048. if (status)
  1049. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1050. status);
  1051. }
  1052. return;
  1053. }
  1054. /*
  1055. * get_lsr_info - get line status register info
  1056. *
  1057. * Purpose: Let user call ioctl() to get info when the UART physically
  1058. * is emptied. On bus types like RS485, the transmitter must
  1059. * release the bus after transmitting. This must be done when
  1060. * the transmit shift register is empty, not be done when the
  1061. * transmit holding register is empty. This functionality
  1062. * allows an RS485 driver to be written in user space.
  1063. */
  1064. static int get_lsr_info(struct moschip_port *mos7720_port,
  1065. unsigned int __user *value)
  1066. {
  1067. int count;
  1068. unsigned int result = 0;
  1069. count = mos7720_chars_in_buffer(mos7720_port->port);
  1070. if (count == 0) {
  1071. dbg("%s -- Empty", __FUNCTION__);
  1072. result = TIOCSER_TEMT;
  1073. }
  1074. if (copy_to_user(value, &result, sizeof(int)))
  1075. return -EFAULT;
  1076. return 0;
  1077. }
  1078. /*
  1079. * get_number_bytes_avail - get number of bytes available
  1080. *
  1081. * Purpose: Let user call ioctl to get the count of number of bytes available.
  1082. */
  1083. static int get_number_bytes_avail(struct moschip_port *mos7720_port,
  1084. unsigned int __user *value)
  1085. {
  1086. unsigned int result = 0;
  1087. struct tty_struct *tty = mos7720_port->port->tty;
  1088. if (!tty)
  1089. return -ENOIOCTLCMD;
  1090. result = tty->read_cnt;
  1091. dbg("%s(%d) = %d", __FUNCTION__, mos7720_port->port->number, result);
  1092. if (copy_to_user(value, &result, sizeof(int)))
  1093. return -EFAULT;
  1094. return -ENOIOCTLCMD;
  1095. }
  1096. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1097. unsigned int __user *value)
  1098. {
  1099. unsigned int mcr ;
  1100. unsigned int arg;
  1101. unsigned char data;
  1102. struct usb_serial_port *port;
  1103. if (mos7720_port == NULL)
  1104. return -1;
  1105. port = (struct usb_serial_port*)mos7720_port->port;
  1106. mcr = mos7720_port->shadowMCR;
  1107. if (copy_from_user(&arg, value, sizeof(int)))
  1108. return -EFAULT;
  1109. switch (cmd) {
  1110. case TIOCMBIS:
  1111. if (arg & TIOCM_RTS)
  1112. mcr |= UART_MCR_RTS;
  1113. if (arg & TIOCM_DTR)
  1114. mcr |= UART_MCR_RTS;
  1115. if (arg & TIOCM_LOOP)
  1116. mcr |= UART_MCR_LOOP;
  1117. break;
  1118. case TIOCMBIC:
  1119. if (arg & TIOCM_RTS)
  1120. mcr &= ~UART_MCR_RTS;
  1121. if (arg & TIOCM_DTR)
  1122. mcr &= ~UART_MCR_RTS;
  1123. if (arg & TIOCM_LOOP)
  1124. mcr &= ~UART_MCR_LOOP;
  1125. break;
  1126. case TIOCMSET:
  1127. /* turn off the RTS and DTR and LOOPBACK
  1128. * and then only turn on what was asked to */
  1129. mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
  1130. mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
  1131. mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
  1132. mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
  1133. break;
  1134. }
  1135. mos7720_port->shadowMCR = mcr;
  1136. data = mos7720_port->shadowMCR;
  1137. send_mos_cmd(port->serial, MOS_WRITE,
  1138. port->number - port->serial->minor, UART_MCR, &data);
  1139. return 0;
  1140. }
  1141. static int get_modem_info(struct moschip_port *mos7720_port,
  1142. unsigned int __user *value)
  1143. {
  1144. unsigned int result = 0;
  1145. unsigned int msr = mos7720_port->shadowMSR;
  1146. unsigned int mcr = mos7720_port->shadowMCR;
  1147. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1148. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1149. | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1150. | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
  1151. | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1152. | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1153. dbg("%s -- %x", __FUNCTION__, result);
  1154. if (copy_to_user(value, &result, sizeof(int)))
  1155. return -EFAULT;
  1156. return 0;
  1157. }
  1158. static int get_serial_info(struct moschip_port *mos7720_port,
  1159. struct serial_struct __user *retinfo)
  1160. {
  1161. struct serial_struct tmp;
  1162. if (!retinfo)
  1163. return -EFAULT;
  1164. memset(&tmp, 0, sizeof(tmp));
  1165. tmp.type = PORT_16550A;
  1166. tmp.line = mos7720_port->port->serial->minor;
  1167. tmp.port = mos7720_port->port->number;
  1168. tmp.irq = 0;
  1169. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1170. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1171. tmp.baud_base = 9600;
  1172. tmp.close_delay = 5*HZ;
  1173. tmp.closing_wait = 30*HZ;
  1174. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1175. return -EFAULT;
  1176. return 0;
  1177. }
  1178. static int mos7720_ioctl(struct usb_serial_port *port, struct file *file,
  1179. unsigned int cmd, unsigned long arg)
  1180. {
  1181. struct moschip_port *mos7720_port;
  1182. struct async_icount cnow;
  1183. struct async_icount cprev;
  1184. struct serial_icounter_struct icount;
  1185. mos7720_port = usb_get_serial_port_data(port);
  1186. if (mos7720_port == NULL)
  1187. return -ENODEV;
  1188. dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
  1189. switch (cmd) {
  1190. case TIOCINQ:
  1191. /* return number of bytes available */
  1192. dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
  1193. return get_number_bytes_avail(mos7720_port,
  1194. (unsigned int __user *)arg);
  1195. break;
  1196. case TIOCSERGETLSR:
  1197. dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
  1198. return get_lsr_info(mos7720_port, (unsigned int __user *)arg);
  1199. return 0;
  1200. case TIOCMBIS:
  1201. case TIOCMBIC:
  1202. case TIOCMSET:
  1203. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
  1204. port->number);
  1205. return set_modem_info(mos7720_port, cmd,
  1206. (unsigned int __user *)arg);
  1207. case TIOCMGET:
  1208. dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
  1209. return get_modem_info(mos7720_port,
  1210. (unsigned int __user *)arg);
  1211. case TIOCGSERIAL:
  1212. dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
  1213. return get_serial_info(mos7720_port,
  1214. (struct serial_struct __user *)arg);
  1215. case TIOCSSERIAL:
  1216. dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
  1217. break;
  1218. case TIOCMIWAIT:
  1219. dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
  1220. cprev = mos7720_port->icount;
  1221. while (1) {
  1222. if (signal_pending(current))
  1223. return -ERESTARTSYS;
  1224. cnow = mos7720_port->icount;
  1225. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1226. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1227. return -EIO; /* no change => error */
  1228. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1229. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1230. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1231. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1232. return 0;
  1233. }
  1234. cprev = cnow;
  1235. }
  1236. /* NOTREACHED */
  1237. break;
  1238. case TIOCGICOUNT:
  1239. cnow = mos7720_port->icount;
  1240. icount.cts = cnow.cts;
  1241. icount.dsr = cnow.dsr;
  1242. icount.rng = cnow.rng;
  1243. icount.dcd = cnow.dcd;
  1244. icount.rx = cnow.rx;
  1245. icount.tx = cnow.tx;
  1246. icount.frame = cnow.frame;
  1247. icount.overrun = cnow.overrun;
  1248. icount.parity = cnow.parity;
  1249. icount.brk = cnow.brk;
  1250. icount.buf_overrun = cnow.buf_overrun;
  1251. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
  1252. port->number, icount.rx, icount.tx );
  1253. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1254. return -EFAULT;
  1255. return 0;
  1256. }
  1257. return -ENOIOCTLCMD;
  1258. }
  1259. static int mos7720_startup(struct usb_serial *serial)
  1260. {
  1261. struct moschip_serial *mos7720_serial;
  1262. struct moschip_port *mos7720_port;
  1263. struct usb_device *dev;
  1264. int i;
  1265. char data;
  1266. dbg("%s: Entering ..........", __FUNCTION__);
  1267. if (!serial) {
  1268. dbg("Invalid Handler");
  1269. return -ENODEV;
  1270. }
  1271. dev = serial->dev;
  1272. /* create our private serial structure */
  1273. mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
  1274. if (mos7720_serial == NULL) {
  1275. err("%s - Out of memory", __FUNCTION__);
  1276. return -ENOMEM;
  1277. }
  1278. usb_set_serial_data(serial, mos7720_serial);
  1279. /* we set up the pointers to the endpoints in the mos7720_open *
  1280. * function, as the structures aren't created yet. */
  1281. /* set up port private structures */
  1282. for (i = 0; i < serial->num_ports; ++i) {
  1283. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1284. if (mos7720_port == NULL) {
  1285. err("%s - Out of memory", __FUNCTION__);
  1286. usb_set_serial_data(serial, NULL);
  1287. kfree(mos7720_serial);
  1288. return -ENOMEM;
  1289. }
  1290. /* Initialize all port interrupt end point to port 0 int
  1291. * endpoint. Our device has only one interrupt endpoint
  1292. * comman to all ports */
  1293. serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
  1294. mos7720_port->port = serial->port[i];
  1295. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1296. dbg("port number is %d", serial->port[i]->number);
  1297. dbg("serial number is %d", serial->minor);
  1298. }
  1299. /* setting configuration feature to one */
  1300. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1301. (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
  1302. send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data); // LSR For Port 1
  1303. dbg("LSR:%x",data);
  1304. send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data); // LSR For Port 2
  1305. dbg("LSR:%x",data);
  1306. return 0;
  1307. }
  1308. static void mos7720_shutdown(struct usb_serial *serial)
  1309. {
  1310. int i;
  1311. /* free private structure allocated for serial port */
  1312. for (i=0; i < serial->num_ports; ++i) {
  1313. kfree(usb_get_serial_port_data(serial->port[i]));
  1314. usb_set_serial_port_data(serial->port[i], NULL);
  1315. }
  1316. /* free private structure allocated for serial device */
  1317. kfree(usb_get_serial_data(serial));
  1318. usb_set_serial_data(serial, NULL);
  1319. }
  1320. static struct usb_driver usb_driver = {
  1321. .name = "moschip7720",
  1322. .probe = usb_serial_probe,
  1323. .disconnect = usb_serial_disconnect,
  1324. .id_table = moschip_port_id_table,
  1325. .no_dynamic_id = 1,
  1326. };
  1327. static struct usb_serial_driver moschip7720_2port_driver = {
  1328. .driver = {
  1329. .owner = THIS_MODULE,
  1330. .name = "moschip7720",
  1331. },
  1332. .description = "Moschip 2 port adapter",
  1333. .usb_driver = &usb_driver,
  1334. .id_table = moschip_port_id_table,
  1335. .num_interrupt_in = 1,
  1336. .num_bulk_in = 2,
  1337. .num_bulk_out = 2,
  1338. .num_ports = 2,
  1339. .open = mos7720_open,
  1340. .close = mos7720_close,
  1341. .throttle = mos7720_throttle,
  1342. .unthrottle = mos7720_unthrottle,
  1343. .attach = mos7720_startup,
  1344. .shutdown = mos7720_shutdown,
  1345. .ioctl = mos7720_ioctl,
  1346. .set_termios = mos7720_set_termios,
  1347. .write = mos7720_write,
  1348. .write_room = mos7720_write_room,
  1349. .chars_in_buffer = mos7720_chars_in_buffer,
  1350. .break_ctl = mos7720_break,
  1351. .read_bulk_callback = mos7720_bulk_in_callback,
  1352. .read_int_callback = mos7720_interrupt_callback,
  1353. };
  1354. static int __init moschip7720_init(void)
  1355. {
  1356. int retval;
  1357. dbg("%s: Entering ..........", __FUNCTION__);
  1358. /* Register with the usb serial */
  1359. retval = usb_serial_register(&moschip7720_2port_driver);
  1360. if (retval)
  1361. goto failed_port_device_register;
  1362. info(DRIVER_DESC " " DRIVER_VERSION);
  1363. /* Register with the usb */
  1364. retval = usb_register(&usb_driver);
  1365. if (retval)
  1366. goto failed_usb_register;
  1367. return 0;
  1368. failed_usb_register:
  1369. usb_serial_deregister(&moschip7720_2port_driver);
  1370. failed_port_device_register:
  1371. return retval;
  1372. }
  1373. static void __exit moschip7720_exit(void)
  1374. {
  1375. usb_deregister(&usb_driver);
  1376. usb_serial_deregister(&moschip7720_2port_driver);
  1377. }
  1378. module_init(moschip7720_init);
  1379. module_exit(moschip7720_exit);
  1380. /* Module information */
  1381. MODULE_AUTHOR( DRIVER_AUTHOR );
  1382. MODULE_DESCRIPTION( DRIVER_DESC );
  1383. MODULE_LICENSE("GPL");
  1384. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1385. MODULE_PARM_DESC(debug, "Debug enabled or not");