mos7720.c 42 KB

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