hso.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  9. * <ajb@spheresystems.co.uk>
  10. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  11. * Copyright (C) 2008 Novell, Inc.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  25. * USA
  26. *
  27. *
  28. *****************************************************************************/
  29. /******************************************************************************
  30. *
  31. * Description of the device:
  32. *
  33. * Interface 0: Contains the IP network interface on the bulk end points.
  34. * The multiplexed serial ports are using the interrupt and
  35. * control endpoints.
  36. * Interrupt contains a bitmap telling which multiplexed
  37. * serialport needs servicing.
  38. *
  39. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  40. * port is opened, as this have a huge impact on the network port
  41. * throughput.
  42. *
  43. * Interface 2: Standard modem interface - circuit switched interface, this
  44. * can be used to make a standard ppp connection however it
  45. * should not be used in conjunction with the IP network interface
  46. * enabled for USB performance reasons i.e. if using this set
  47. * ideally disable_net=1.
  48. *
  49. *****************************************************************************/
  50. #include <linux/sched.h>
  51. #include <linux/slab.h>
  52. #include <linux/init.h>
  53. #include <linux/delay.h>
  54. #include <linux/netdevice.h>
  55. #include <linux/module.h>
  56. #include <linux/ethtool.h>
  57. #include <linux/usb.h>
  58. #include <linux/timer.h>
  59. #include <linux/tty.h>
  60. #include <linux/tty_driver.h>
  61. #include <linux/tty_flip.h>
  62. #include <linux/kmod.h>
  63. #include <linux/rfkill.h>
  64. #include <linux/ip.h>
  65. #include <linux/uaccess.h>
  66. #include <linux/usb/cdc.h>
  67. #include <net/arp.h>
  68. #include <asm/byteorder.h>
  69. #include <linux/serial_core.h>
  70. #include <linux/serial.h>
  71. #define DRIVER_VERSION "1.2"
  72. #define MOD_AUTHOR "Option Wireless"
  73. #define MOD_DESCRIPTION "USB High Speed Option driver"
  74. #define MOD_LICENSE "GPL"
  75. #define HSO_MAX_NET_DEVICES 10
  76. #define HSO__MAX_MTU 2048
  77. #define DEFAULT_MTU 1500
  78. #define DEFAULT_MRU 1500
  79. #define CTRL_URB_RX_SIZE 1024
  80. #define CTRL_URB_TX_SIZE 64
  81. #define BULK_URB_RX_SIZE 4096
  82. #define BULK_URB_TX_SIZE 8192
  83. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  84. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_RX_BUF_COUNT 4
  86. #define USB_TYPE_OPTION_VENDOR 0x20
  87. /* These definitions are used with the struct hso_net flags element */
  88. /* - use *_bit operations on it. (bit indices not values.) */
  89. #define HSO_NET_RUNNING 0
  90. #define HSO_NET_TX_TIMEOUT (HZ*10)
  91. #define HSO_SERIAL_MAGIC 0x48534f31
  92. /* Number of ttys to handle */
  93. #define HSO_SERIAL_TTY_MINORS 256
  94. #define MAX_RX_URBS 2
  95. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  96. {
  97. if (tty)
  98. return tty->driver_data;
  99. return NULL;
  100. }
  101. /*****************************************************************************/
  102. /* Debugging functions */
  103. /*****************************************************************************/
  104. #define D__(lvl_, fmt, arg...) \
  105. do { \
  106. printk(lvl_ "[%d:%s]: " fmt "\n", \
  107. __LINE__, __func__, ## arg); \
  108. } while (0)
  109. #define D_(lvl, args...) \
  110. do { \
  111. if (lvl & debug) \
  112. D__(KERN_INFO, args); \
  113. } while (0)
  114. #define D1(args...) D_(0x01, ##args)
  115. #define D2(args...) D_(0x02, ##args)
  116. #define D3(args...) D_(0x04, ##args)
  117. #define D4(args...) D_(0x08, ##args)
  118. #define D5(args...) D_(0x10, ##args)
  119. /*****************************************************************************/
  120. /* Enumerators */
  121. /*****************************************************************************/
  122. enum pkt_parse_state {
  123. WAIT_IP,
  124. WAIT_DATA,
  125. WAIT_SYNC
  126. };
  127. /*****************************************************************************/
  128. /* Structs */
  129. /*****************************************************************************/
  130. struct hso_shared_int {
  131. struct usb_endpoint_descriptor *intr_endp;
  132. void *shared_intr_buf;
  133. struct urb *shared_intr_urb;
  134. struct usb_device *usb;
  135. int use_count;
  136. int ref_count;
  137. struct mutex shared_int_lock;
  138. };
  139. struct hso_net {
  140. struct hso_device *parent;
  141. struct net_device *net;
  142. struct rfkill *rfkill;
  143. struct usb_endpoint_descriptor *in_endp;
  144. struct usb_endpoint_descriptor *out_endp;
  145. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  146. struct urb *mux_bulk_tx_urb;
  147. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  148. void *mux_bulk_tx_buf;
  149. struct sk_buff *skb_rx_buf;
  150. struct sk_buff *skb_tx_buf;
  151. enum pkt_parse_state rx_parse_state;
  152. spinlock_t net_lock;
  153. unsigned short rx_buf_size;
  154. unsigned short rx_buf_missing;
  155. struct iphdr rx_ip_hdr;
  156. unsigned long flags;
  157. };
  158. enum rx_ctrl_state{
  159. RX_IDLE,
  160. RX_SENT,
  161. RX_PENDING
  162. };
  163. #define BM_REQUEST_TYPE (0xa1)
  164. #define B_NOTIFICATION (0x20)
  165. #define W_VALUE (0x0)
  166. #define W_INDEX (0x2)
  167. #define W_LENGTH (0x2)
  168. #define B_OVERRUN (0x1<<6)
  169. #define B_PARITY (0x1<<5)
  170. #define B_FRAMING (0x1<<4)
  171. #define B_RING_SIGNAL (0x1<<3)
  172. #define B_BREAK (0x1<<2)
  173. #define B_TX_CARRIER (0x1<<1)
  174. #define B_RX_CARRIER (0x1<<0)
  175. struct hso_serial_state_notification {
  176. u8 bmRequestType;
  177. u8 bNotification;
  178. u16 wValue;
  179. u16 wIndex;
  180. u16 wLength;
  181. u16 UART_state_bitmap;
  182. } __attribute__((packed));
  183. struct hso_tiocmget {
  184. struct mutex mutex;
  185. wait_queue_head_t waitq;
  186. int intr_completed;
  187. struct usb_endpoint_descriptor *endp;
  188. struct urb *urb;
  189. struct hso_serial_state_notification serial_state_notification;
  190. u16 prev_UART_state_bitmap;
  191. struct uart_icount icount;
  192. };
  193. struct hso_serial {
  194. struct hso_device *parent;
  195. int magic;
  196. u8 minor;
  197. struct hso_shared_int *shared_int;
  198. /* rx/tx urb could be either a bulk urb or a control urb depending
  199. on which serial port it is used on. */
  200. struct urb *rx_urb[MAX_RX_URBS];
  201. u8 num_rx_urbs;
  202. u8 *rx_data[MAX_RX_URBS];
  203. u16 rx_data_length; /* should contain allocated length */
  204. struct urb *tx_urb;
  205. u8 *tx_data;
  206. u8 *tx_buffer;
  207. u16 tx_data_length; /* should contain allocated length */
  208. u16 tx_data_count;
  209. u16 tx_buffer_count;
  210. struct usb_ctrlrequest ctrl_req_tx;
  211. struct usb_ctrlrequest ctrl_req_rx;
  212. struct usb_endpoint_descriptor *in_endp;
  213. struct usb_endpoint_descriptor *out_endp;
  214. enum rx_ctrl_state rx_state;
  215. u8 rts_state;
  216. u8 dtr_state;
  217. unsigned tx_urb_used:1;
  218. /* from usb_serial_port */
  219. struct tty_struct *tty;
  220. int open_count;
  221. spinlock_t serial_lock;
  222. int (*write_data) (struct hso_serial *serial);
  223. struct hso_tiocmget *tiocmget;
  224. /* Hacks required to get flow control
  225. * working on the serial receive buffers
  226. * so as not to drop characters on the floor.
  227. */
  228. int curr_rx_urb_idx;
  229. u16 curr_rx_urb_offset;
  230. u8 rx_urb_filled[MAX_RX_URBS];
  231. struct tasklet_struct unthrottle_tasklet;
  232. struct work_struct retry_unthrottle_workqueue;
  233. };
  234. struct hso_device {
  235. union {
  236. struct hso_serial *dev_serial;
  237. struct hso_net *dev_net;
  238. } port_data;
  239. u32 port_spec;
  240. u8 is_active;
  241. u8 usb_gone;
  242. struct work_struct async_get_intf;
  243. struct work_struct async_put_intf;
  244. struct usb_device *usb;
  245. struct usb_interface *interface;
  246. struct device *dev;
  247. struct kref ref;
  248. struct mutex mutex;
  249. };
  250. /* Type of interface */
  251. #define HSO_INTF_MASK 0xFF00
  252. #define HSO_INTF_MUX 0x0100
  253. #define HSO_INTF_BULK 0x0200
  254. /* Type of port */
  255. #define HSO_PORT_MASK 0xFF
  256. #define HSO_PORT_NO_PORT 0x0
  257. #define HSO_PORT_CONTROL 0x1
  258. #define HSO_PORT_APP 0x2
  259. #define HSO_PORT_GPS 0x3
  260. #define HSO_PORT_PCSC 0x4
  261. #define HSO_PORT_APP2 0x5
  262. #define HSO_PORT_GPS_CONTROL 0x6
  263. #define HSO_PORT_MSD 0x7
  264. #define HSO_PORT_VOICE 0x8
  265. #define HSO_PORT_DIAG2 0x9
  266. #define HSO_PORT_DIAG 0x10
  267. #define HSO_PORT_MODEM 0x11
  268. #define HSO_PORT_NETWORK 0x12
  269. /* Additional device info */
  270. #define HSO_INFO_MASK 0xFF000000
  271. #define HSO_INFO_CRC_BUG 0x01000000
  272. /*****************************************************************************/
  273. /* Prototypes */
  274. /*****************************************************************************/
  275. /* Serial driver functions */
  276. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  277. unsigned int set, unsigned int clear);
  278. static void ctrl_callback(struct urb *urb);
  279. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  280. static void hso_kick_transmit(struct hso_serial *serial);
  281. /* Helper functions */
  282. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  283. struct usb_device *usb, gfp_t gfp);
  284. static void log_usb_status(int status, const char *function);
  285. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  286. int type, int dir);
  287. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  288. static void hso_free_interface(struct usb_interface *intf);
  289. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  290. static int hso_stop_serial_device(struct hso_device *hso_dev);
  291. static int hso_start_net_device(struct hso_device *hso_dev);
  292. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  293. static int hso_stop_net_device(struct hso_device *hso_dev);
  294. static void hso_serial_ref_free(struct kref *ref);
  295. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  296. static int hso_mux_serial_read(struct hso_serial *serial);
  297. static void async_get_intf(struct work_struct *data);
  298. static void async_put_intf(struct work_struct *data);
  299. static int hso_put_activity(struct hso_device *hso_dev);
  300. static int hso_get_activity(struct hso_device *hso_dev);
  301. static void tiocmget_intr_callback(struct urb *urb);
  302. /*****************************************************************************/
  303. /* Helping functions */
  304. /*****************************************************************************/
  305. /* #define DEBUG */
  306. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  307. {
  308. return hso_dev->port_data.dev_net;
  309. }
  310. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  311. {
  312. return hso_dev->port_data.dev_serial;
  313. }
  314. /* Debugging functions */
  315. #ifdef DEBUG
  316. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  317. unsigned int len)
  318. {
  319. static char name[255];
  320. sprintf(name, "hso[%d:%s]", line_count, func_name);
  321. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  322. }
  323. #define DUMP(buf_, len_) \
  324. dbg_dump(__LINE__, __func__, buf_, len_)
  325. #define DUMP1(buf_, len_) \
  326. do { \
  327. if (0x01 & debug) \
  328. DUMP(buf_, len_); \
  329. } while (0)
  330. #else
  331. #define DUMP(buf_, len_)
  332. #define DUMP1(buf_, len_)
  333. #endif
  334. /* module parameters */
  335. static int debug;
  336. static int tty_major;
  337. static int disable_net;
  338. /* driver info */
  339. static const char driver_name[] = "hso";
  340. static const char tty_filename[] = "ttyHS";
  341. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  342. /* the usb driver itself (registered in hso_init) */
  343. static struct usb_driver hso_driver;
  344. /* serial structures */
  345. static struct tty_driver *tty_drv;
  346. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  347. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  348. static spinlock_t serial_table_lock;
  349. static const s32 default_port_spec[] = {
  350. HSO_INTF_MUX | HSO_PORT_NETWORK,
  351. HSO_INTF_BULK | HSO_PORT_DIAG,
  352. HSO_INTF_BULK | HSO_PORT_MODEM,
  353. 0
  354. };
  355. static const s32 icon321_port_spec[] = {
  356. HSO_INTF_MUX | HSO_PORT_NETWORK,
  357. HSO_INTF_BULK | HSO_PORT_DIAG2,
  358. HSO_INTF_BULK | HSO_PORT_MODEM,
  359. HSO_INTF_BULK | HSO_PORT_DIAG,
  360. 0
  361. };
  362. #define default_port_device(vendor, product) \
  363. USB_DEVICE(vendor, product), \
  364. .driver_info = (kernel_ulong_t)default_port_spec
  365. #define icon321_port_device(vendor, product) \
  366. USB_DEVICE(vendor, product), \
  367. .driver_info = (kernel_ulong_t)icon321_port_spec
  368. /* list of devices we support */
  369. static const struct usb_device_id hso_ids[] = {
  370. {default_port_device(0x0af0, 0x6711)},
  371. {default_port_device(0x0af0, 0x6731)},
  372. {default_port_device(0x0af0, 0x6751)},
  373. {default_port_device(0x0af0, 0x6771)},
  374. {default_port_device(0x0af0, 0x6791)},
  375. {default_port_device(0x0af0, 0x6811)},
  376. {default_port_device(0x0af0, 0x6911)},
  377. {default_port_device(0x0af0, 0x6951)},
  378. {default_port_device(0x0af0, 0x6971)},
  379. {default_port_device(0x0af0, 0x7011)},
  380. {default_port_device(0x0af0, 0x7031)},
  381. {default_port_device(0x0af0, 0x7051)},
  382. {default_port_device(0x0af0, 0x7071)},
  383. {default_port_device(0x0af0, 0x7111)},
  384. {default_port_device(0x0af0, 0x7211)},
  385. {default_port_device(0x0af0, 0x7251)},
  386. {default_port_device(0x0af0, 0x7271)},
  387. {default_port_device(0x0af0, 0x7311)},
  388. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  389. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  390. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  391. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  392. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  393. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  394. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  395. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  396. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  397. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  398. {USB_DEVICE(0x0af0, 0x7701)},
  399. {USB_DEVICE(0x0af0, 0x7801)},
  400. {USB_DEVICE(0x0af0, 0x7901)},
  401. {USB_DEVICE(0x0af0, 0x7361)},
  402. {USB_DEVICE(0x0af0, 0xd057)},
  403. {USB_DEVICE(0x0af0, 0xd055)},
  404. {}
  405. };
  406. MODULE_DEVICE_TABLE(usb, hso_ids);
  407. /* Sysfs attribute */
  408. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  409. struct device_attribute *attr,
  410. char *buf)
  411. {
  412. struct hso_device *hso_dev = dev->driver_data;
  413. char *port_name;
  414. if (!hso_dev)
  415. return 0;
  416. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  417. case HSO_PORT_CONTROL:
  418. port_name = "Control";
  419. break;
  420. case HSO_PORT_APP:
  421. port_name = "Application";
  422. break;
  423. case HSO_PORT_APP2:
  424. port_name = "Application2";
  425. break;
  426. case HSO_PORT_GPS:
  427. port_name = "GPS";
  428. break;
  429. case HSO_PORT_GPS_CONTROL:
  430. port_name = "GPS Control";
  431. break;
  432. case HSO_PORT_PCSC:
  433. port_name = "PCSC";
  434. break;
  435. case HSO_PORT_DIAG:
  436. port_name = "Diagnostic";
  437. break;
  438. case HSO_PORT_DIAG2:
  439. port_name = "Diagnostic2";
  440. break;
  441. case HSO_PORT_MODEM:
  442. port_name = "Modem";
  443. break;
  444. case HSO_PORT_NETWORK:
  445. port_name = "Network";
  446. break;
  447. default:
  448. port_name = "Unknown";
  449. break;
  450. }
  451. return sprintf(buf, "%s\n", port_name);
  452. }
  453. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  454. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  455. {
  456. int idx;
  457. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  458. if (serial->rx_urb[idx] == urb)
  459. return idx;
  460. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  461. return -1;
  462. }
  463. /* converts mux value to a port spec value */
  464. static u32 hso_mux_to_port(int mux)
  465. {
  466. u32 result;
  467. switch (mux) {
  468. case 0x1:
  469. result = HSO_PORT_CONTROL;
  470. break;
  471. case 0x2:
  472. result = HSO_PORT_APP;
  473. break;
  474. case 0x4:
  475. result = HSO_PORT_PCSC;
  476. break;
  477. case 0x8:
  478. result = HSO_PORT_GPS;
  479. break;
  480. case 0x10:
  481. result = HSO_PORT_APP2;
  482. break;
  483. default:
  484. result = HSO_PORT_NO_PORT;
  485. }
  486. return result;
  487. }
  488. /* converts port spec value to a mux value */
  489. static u32 hso_port_to_mux(int port)
  490. {
  491. u32 result;
  492. switch (port & HSO_PORT_MASK) {
  493. case HSO_PORT_CONTROL:
  494. result = 0x0;
  495. break;
  496. case HSO_PORT_APP:
  497. result = 0x1;
  498. break;
  499. case HSO_PORT_PCSC:
  500. result = 0x2;
  501. break;
  502. case HSO_PORT_GPS:
  503. result = 0x3;
  504. break;
  505. case HSO_PORT_APP2:
  506. result = 0x4;
  507. break;
  508. default:
  509. result = 0x0;
  510. }
  511. return result;
  512. }
  513. static struct hso_serial *get_serial_by_shared_int_and_type(
  514. struct hso_shared_int *shared_int,
  515. int mux)
  516. {
  517. int i, port;
  518. port = hso_mux_to_port(mux);
  519. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  520. if (serial_table[i]
  521. && (dev2ser(serial_table[i])->shared_int == shared_int)
  522. && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  523. return dev2ser(serial_table[i]);
  524. }
  525. }
  526. return NULL;
  527. }
  528. static struct hso_serial *get_serial_by_index(unsigned index)
  529. {
  530. struct hso_serial *serial = NULL;
  531. unsigned long flags;
  532. spin_lock_irqsave(&serial_table_lock, flags);
  533. if (serial_table[index])
  534. serial = dev2ser(serial_table[index]);
  535. spin_unlock_irqrestore(&serial_table_lock, flags);
  536. return serial;
  537. }
  538. static int get_free_serial_index(void)
  539. {
  540. int index;
  541. unsigned long flags;
  542. spin_lock_irqsave(&serial_table_lock, flags);
  543. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  544. if (serial_table[index] == NULL) {
  545. spin_unlock_irqrestore(&serial_table_lock, flags);
  546. return index;
  547. }
  548. }
  549. spin_unlock_irqrestore(&serial_table_lock, flags);
  550. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  551. return -1;
  552. }
  553. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  554. {
  555. unsigned long flags;
  556. spin_lock_irqsave(&serial_table_lock, flags);
  557. if (serial)
  558. serial_table[index] = serial->parent;
  559. else
  560. serial_table[index] = NULL;
  561. spin_unlock_irqrestore(&serial_table_lock, flags);
  562. }
  563. /* log a meaningful explanation of an USB status */
  564. static void log_usb_status(int status, const char *function)
  565. {
  566. char *explanation;
  567. switch (status) {
  568. case -ENODEV:
  569. explanation = "no device";
  570. break;
  571. case -ENOENT:
  572. explanation = "endpoint not enabled";
  573. break;
  574. case -EPIPE:
  575. explanation = "endpoint stalled";
  576. break;
  577. case -ENOSPC:
  578. explanation = "not enough bandwidth";
  579. break;
  580. case -ESHUTDOWN:
  581. explanation = "device disabled";
  582. break;
  583. case -EHOSTUNREACH:
  584. explanation = "device suspended";
  585. break;
  586. case -EINVAL:
  587. case -EAGAIN:
  588. case -EFBIG:
  589. case -EMSGSIZE:
  590. explanation = "internal error";
  591. break;
  592. default:
  593. explanation = "unknown status";
  594. break;
  595. }
  596. D1("%s: received USB status - %s (%d)", function, explanation, status);
  597. }
  598. /* Network interface functions */
  599. /* called when net interface is brought up by ifconfig */
  600. static int hso_net_open(struct net_device *net)
  601. {
  602. struct hso_net *odev = netdev_priv(net);
  603. unsigned long flags = 0;
  604. if (!odev) {
  605. dev_err(&net->dev, "No net device !\n");
  606. return -ENODEV;
  607. }
  608. odev->skb_tx_buf = NULL;
  609. /* setup environment */
  610. spin_lock_irqsave(&odev->net_lock, flags);
  611. odev->rx_parse_state = WAIT_IP;
  612. odev->rx_buf_size = 0;
  613. odev->rx_buf_missing = sizeof(struct iphdr);
  614. spin_unlock_irqrestore(&odev->net_lock, flags);
  615. /* We are up and running. */
  616. set_bit(HSO_NET_RUNNING, &odev->flags);
  617. hso_start_net_device(odev->parent);
  618. /* Tell the kernel we are ready to start receiving from it */
  619. netif_start_queue(net);
  620. return 0;
  621. }
  622. /* called when interface is brought down by ifconfig */
  623. static int hso_net_close(struct net_device *net)
  624. {
  625. struct hso_net *odev = netdev_priv(net);
  626. /* we don't need the queue anymore */
  627. netif_stop_queue(net);
  628. /* no longer running */
  629. clear_bit(HSO_NET_RUNNING, &odev->flags);
  630. hso_stop_net_device(odev->parent);
  631. /* done */
  632. return 0;
  633. }
  634. /* USB tells is xmit done, we should start the netqueue again */
  635. static void write_bulk_callback(struct urb *urb)
  636. {
  637. struct hso_net *odev = urb->context;
  638. int status = urb->status;
  639. /* Sanity check */
  640. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  641. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  642. return;
  643. }
  644. /* Do we still have a valid kernel network device? */
  645. if (!netif_device_present(odev->net)) {
  646. dev_err(&urb->dev->dev, "%s: net device not present\n",
  647. __func__);
  648. return;
  649. }
  650. /* log status, but don't act on it, we don't need to resubmit anything
  651. * anyhow */
  652. if (status)
  653. log_usb_status(status, __func__);
  654. hso_put_activity(odev->parent);
  655. /* Tell the network interface we are ready for another frame */
  656. netif_wake_queue(odev->net);
  657. }
  658. /* called by kernel when we need to transmit a packet */
  659. static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
  660. {
  661. struct hso_net *odev = netdev_priv(net);
  662. int result;
  663. /* Tell the kernel, "No more frames 'til we are done with this one." */
  664. netif_stop_queue(net);
  665. if (hso_get_activity(odev->parent) == -EAGAIN) {
  666. odev->skb_tx_buf = skb;
  667. return 0;
  668. }
  669. /* log if asked */
  670. DUMP1(skb->data, skb->len);
  671. /* Copy it from kernel memory to OUR memory */
  672. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  673. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  674. /* Fill in the URB for shipping it out. */
  675. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  676. odev->parent->usb,
  677. usb_sndbulkpipe(odev->parent->usb,
  678. odev->out_endp->
  679. bEndpointAddress & 0x7F),
  680. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  681. odev);
  682. /* Deal with the Zero Length packet problem, I hope */
  683. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  684. /* Send the URB on its merry way. */
  685. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  686. if (result) {
  687. dev_warn(&odev->parent->interface->dev,
  688. "failed mux_bulk_tx_urb %d", result);
  689. net->stats.tx_errors++;
  690. netif_start_queue(net);
  691. } else {
  692. net->stats.tx_packets++;
  693. net->stats.tx_bytes += skb->len;
  694. /* And tell the kernel when the last transmit started. */
  695. net->trans_start = jiffies;
  696. }
  697. dev_kfree_skb(skb);
  698. /* we're done */
  699. return result;
  700. }
  701. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  702. {
  703. struct hso_net *odev = netdev_priv(net);
  704. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  705. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  706. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  707. }
  708. static struct ethtool_ops ops = {
  709. .get_drvinfo = hso_get_drvinfo,
  710. .get_link = ethtool_op_get_link
  711. };
  712. /* called when a packet did not ack after watchdogtimeout */
  713. static void hso_net_tx_timeout(struct net_device *net)
  714. {
  715. struct hso_net *odev = netdev_priv(net);
  716. if (!odev)
  717. return;
  718. /* Tell syslog we are hosed. */
  719. dev_warn(&net->dev, "Tx timed out.\n");
  720. /* Tear the waiting frame off the list */
  721. if (odev->mux_bulk_tx_urb
  722. && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  723. usb_unlink_urb(odev->mux_bulk_tx_urb);
  724. /* Update statistics */
  725. net->stats.tx_errors++;
  726. }
  727. /* make a real packet from the received USB buffer */
  728. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  729. unsigned int count, unsigned char is_eop)
  730. {
  731. unsigned short temp_bytes;
  732. unsigned short buffer_offset = 0;
  733. unsigned short frame_len;
  734. unsigned char *tmp_rx_buf;
  735. /* log if needed */
  736. D1("Rx %d bytes", count);
  737. DUMP(ip_pkt, min(128, (int)count));
  738. while (count) {
  739. switch (odev->rx_parse_state) {
  740. case WAIT_IP:
  741. /* waiting for IP header. */
  742. /* wanted bytes - size of ip header */
  743. temp_bytes =
  744. (count <
  745. odev->rx_buf_missing) ? count : odev->
  746. rx_buf_missing;
  747. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  748. odev->rx_buf_size, ip_pkt + buffer_offset,
  749. temp_bytes);
  750. odev->rx_buf_size += temp_bytes;
  751. buffer_offset += temp_bytes;
  752. odev->rx_buf_missing -= temp_bytes;
  753. count -= temp_bytes;
  754. if (!odev->rx_buf_missing) {
  755. /* header is complete allocate an sk_buffer and
  756. * continue to WAIT_DATA */
  757. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  758. if ((frame_len > DEFAULT_MRU) ||
  759. (frame_len < sizeof(struct iphdr))) {
  760. dev_err(&odev->net->dev,
  761. "Invalid frame (%d) length\n",
  762. frame_len);
  763. odev->rx_parse_state = WAIT_SYNC;
  764. continue;
  765. }
  766. /* Allocate an sk_buff */
  767. odev->skb_rx_buf = dev_alloc_skb(frame_len);
  768. if (!odev->skb_rx_buf) {
  769. /* We got no receive buffer. */
  770. D1("could not allocate memory");
  771. odev->rx_parse_state = WAIT_SYNC;
  772. return;
  773. }
  774. /* Here's where it came from */
  775. odev->skb_rx_buf->dev = odev->net;
  776. /* Copy what we got so far. make room for iphdr
  777. * after tail. */
  778. tmp_rx_buf =
  779. skb_put(odev->skb_rx_buf,
  780. sizeof(struct iphdr));
  781. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  782. sizeof(struct iphdr));
  783. /* ETH_HLEN */
  784. odev->rx_buf_size = sizeof(struct iphdr);
  785. /* Filip actually use .tot_len */
  786. odev->rx_buf_missing =
  787. frame_len - sizeof(struct iphdr);
  788. odev->rx_parse_state = WAIT_DATA;
  789. }
  790. break;
  791. case WAIT_DATA:
  792. temp_bytes = (count < odev->rx_buf_missing)
  793. ? count : odev->rx_buf_missing;
  794. /* Copy the rest of the bytes that are left in the
  795. * buffer into the waiting sk_buf. */
  796. /* Make room for temp_bytes after tail. */
  797. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  798. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  799. odev->rx_buf_missing -= temp_bytes;
  800. count -= temp_bytes;
  801. buffer_offset += temp_bytes;
  802. odev->rx_buf_size += temp_bytes;
  803. if (!odev->rx_buf_missing) {
  804. /* Packet is complete. Inject into stack. */
  805. /* We have IP packet here */
  806. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  807. /* don't check it */
  808. odev->skb_rx_buf->ip_summed =
  809. CHECKSUM_UNNECESSARY;
  810. skb_reset_mac_header(odev->skb_rx_buf);
  811. /* Ship it off to the kernel */
  812. netif_rx(odev->skb_rx_buf);
  813. /* No longer our buffer. */
  814. odev->skb_rx_buf = NULL;
  815. /* update out statistics */
  816. odev->net->stats.rx_packets++;
  817. odev->net->stats.rx_bytes += odev->rx_buf_size;
  818. odev->rx_buf_size = 0;
  819. odev->rx_buf_missing = sizeof(struct iphdr);
  820. odev->rx_parse_state = WAIT_IP;
  821. }
  822. break;
  823. case WAIT_SYNC:
  824. D1(" W_S");
  825. count = 0;
  826. break;
  827. default:
  828. D1(" ");
  829. count--;
  830. break;
  831. }
  832. }
  833. /* Recovery mechanism for WAIT_SYNC state. */
  834. if (is_eop) {
  835. if (odev->rx_parse_state == WAIT_SYNC) {
  836. odev->rx_parse_state = WAIT_IP;
  837. odev->rx_buf_size = 0;
  838. odev->rx_buf_missing = sizeof(struct iphdr);
  839. }
  840. }
  841. }
  842. /* Moving data from usb to kernel (in interrupt state) */
  843. static void read_bulk_callback(struct urb *urb)
  844. {
  845. struct hso_net *odev = urb->context;
  846. struct net_device *net;
  847. int result;
  848. int status = urb->status;
  849. /* is al ok? (Filip: Who's Al ?) */
  850. if (status) {
  851. log_usb_status(status, __func__);
  852. return;
  853. }
  854. /* Sanity check */
  855. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  856. D1("BULK IN callback but driver is not active!");
  857. return;
  858. }
  859. usb_mark_last_busy(urb->dev);
  860. net = odev->net;
  861. if (!netif_device_present(net)) {
  862. /* Somebody killed our network interface... */
  863. return;
  864. }
  865. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  866. u32 rest;
  867. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  868. rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
  869. if (((rest == 5) || (rest == 6))
  870. && !memcmp(((u8 *) urb->transfer_buffer) +
  871. urb->actual_length - 4, crc_check, 4)) {
  872. urb->actual_length -= 4;
  873. }
  874. }
  875. /* do we even have a packet? */
  876. if (urb->actual_length) {
  877. /* Handle the IP stream, add header and push it onto network
  878. * stack if the packet is complete. */
  879. spin_lock(&odev->net_lock);
  880. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  881. (urb->transfer_buffer_length >
  882. urb->actual_length) ? 1 : 0);
  883. spin_unlock(&odev->net_lock);
  884. }
  885. /* We are done with this URB, resubmit it. Prep the USB to wait for
  886. * another frame. Reuse same as received. */
  887. usb_fill_bulk_urb(urb,
  888. odev->parent->usb,
  889. usb_rcvbulkpipe(odev->parent->usb,
  890. odev->in_endp->
  891. bEndpointAddress & 0x7F),
  892. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  893. read_bulk_callback, odev);
  894. /* Give this to the USB subsystem so it can tell us when more data
  895. * arrives. */
  896. result = usb_submit_urb(urb, GFP_ATOMIC);
  897. if (result)
  898. dev_warn(&odev->parent->interface->dev,
  899. "%s failed submit mux_bulk_rx_urb %d", __func__,
  900. result);
  901. }
  902. /* Serial driver functions */
  903. static void hso_init_termios(struct ktermios *termios)
  904. {
  905. /*
  906. * The default requirements for this device are:
  907. */
  908. termios->c_iflag &=
  909. ~(IGNBRK /* disable ignore break */
  910. | BRKINT /* disable break causes interrupt */
  911. | PARMRK /* disable mark parity errors */
  912. | ISTRIP /* disable clear high bit of input characters */
  913. | INLCR /* disable translate NL to CR */
  914. | IGNCR /* disable ignore CR */
  915. | ICRNL /* disable translate CR to NL */
  916. | IXON); /* disable enable XON/XOFF flow control */
  917. /* disable postprocess output characters */
  918. termios->c_oflag &= ~OPOST;
  919. termios->c_lflag &=
  920. ~(ECHO /* disable echo input characters */
  921. | ECHONL /* disable echo new line */
  922. | ICANON /* disable erase, kill, werase, and rprnt
  923. special characters */
  924. | ISIG /* disable interrupt, quit, and suspend special
  925. characters */
  926. | IEXTEN); /* disable non-POSIX special characters */
  927. termios->c_cflag &=
  928. ~(CSIZE /* no size */
  929. | PARENB /* disable parity bit */
  930. | CBAUD /* clear current baud rate */
  931. | CBAUDEX); /* clear current buad rate */
  932. termios->c_cflag |= CS8; /* character size 8 bits */
  933. /* baud rate 115200 */
  934. tty_termios_encode_baud_rate(termios, 115200, 115200);
  935. }
  936. static void _hso_serial_set_termios(struct tty_struct *tty,
  937. struct ktermios *old)
  938. {
  939. struct hso_serial *serial = get_serial_by_tty(tty);
  940. struct ktermios *termios;
  941. if (!serial) {
  942. printk(KERN_ERR "%s: no tty structures", __func__);
  943. return;
  944. }
  945. D4("port %d", serial->minor);
  946. /*
  947. * Fix up unsupported bits
  948. */
  949. termios = tty->termios;
  950. termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  951. termios->c_cflag &=
  952. ~(CSIZE /* no size */
  953. | PARENB /* disable parity bit */
  954. | CBAUD /* clear current baud rate */
  955. | CBAUDEX); /* clear current buad rate */
  956. termios->c_cflag |= CS8; /* character size 8 bits */
  957. /* baud rate 115200 */
  958. tty_encode_baud_rate(tty, 115200, 115200);
  959. }
  960. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  961. {
  962. int result;
  963. #ifdef CONFIG_HSO_AUTOPM
  964. usb_mark_last_busy(urb->dev);
  965. #endif
  966. /* We are done with this URB, resubmit it. Prep the USB to wait for
  967. * another frame */
  968. usb_fill_bulk_urb(urb, serial->parent->usb,
  969. usb_rcvbulkpipe(serial->parent->usb,
  970. serial->in_endp->
  971. bEndpointAddress & 0x7F),
  972. urb->transfer_buffer, serial->rx_data_length,
  973. hso_std_serial_read_bulk_callback, serial);
  974. /* Give this to the USB subsystem so it can tell us when more data
  975. * arrives. */
  976. result = usb_submit_urb(urb, GFP_ATOMIC);
  977. if (result) {
  978. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  979. __func__, result);
  980. }
  981. }
  982. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  983. {
  984. int count;
  985. struct urb *curr_urb;
  986. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  987. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  988. count = put_rxbuf_data(curr_urb, serial);
  989. if (count == -1)
  990. return;
  991. if (count == 0) {
  992. serial->curr_rx_urb_idx++;
  993. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  994. serial->curr_rx_urb_idx = 0;
  995. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  996. }
  997. }
  998. }
  999. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1000. {
  1001. int count = 0;
  1002. struct urb *urb;
  1003. urb = serial->rx_urb[0];
  1004. if (serial->open_count > 0) {
  1005. count = put_rxbuf_data(urb, serial);
  1006. if (count == -1)
  1007. return;
  1008. }
  1009. /* Re issue a read as long as we receive data. */
  1010. if (count == 0 && ((urb->actual_length != 0) ||
  1011. (serial->rx_state == RX_PENDING))) {
  1012. serial->rx_state = RX_SENT;
  1013. hso_mux_serial_read(serial);
  1014. } else
  1015. serial->rx_state = RX_IDLE;
  1016. }
  1017. /* read callback for Diag and CS port */
  1018. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1019. {
  1020. struct hso_serial *serial = urb->context;
  1021. int status = urb->status;
  1022. /* sanity check */
  1023. if (!serial) {
  1024. D1("serial == NULL");
  1025. return;
  1026. } else if (status) {
  1027. log_usb_status(status, __func__);
  1028. return;
  1029. }
  1030. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1031. D1("Actual length = %d\n", urb->actual_length);
  1032. DUMP1(urb->transfer_buffer, urb->actual_length);
  1033. /* Anyone listening? */
  1034. if (serial->open_count == 0)
  1035. return;
  1036. if (status == 0) {
  1037. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1038. u32 rest;
  1039. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1040. rest =
  1041. urb->actual_length %
  1042. serial->in_endp->wMaxPacketSize;
  1043. if (((rest == 5) || (rest == 6))
  1044. && !memcmp(((u8 *) urb->transfer_buffer) +
  1045. urb->actual_length - 4, crc_check, 4)) {
  1046. urb->actual_length -= 4;
  1047. }
  1048. }
  1049. /* Valid data, handle RX data */
  1050. spin_lock(&serial->serial_lock);
  1051. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1052. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1053. spin_unlock(&serial->serial_lock);
  1054. } else if (status == -ENOENT || status == -ECONNRESET) {
  1055. /* Unlinked - check for throttled port. */
  1056. D2("Port %d, successfully unlinked urb", serial->minor);
  1057. spin_lock(&serial->serial_lock);
  1058. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1059. hso_resubmit_rx_bulk_urb(serial, urb);
  1060. spin_unlock(&serial->serial_lock);
  1061. } else {
  1062. D2("Port %d, status = %d for read urb", serial->minor, status);
  1063. return;
  1064. }
  1065. }
  1066. /*
  1067. * This needs to be a tasklet otherwise we will
  1068. * end up recursively calling this function.
  1069. */
  1070. void hso_unthrottle_tasklet(struct hso_serial *serial)
  1071. {
  1072. unsigned long flags;
  1073. spin_lock_irqsave(&serial->serial_lock, flags);
  1074. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1075. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1076. else
  1077. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1078. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1079. }
  1080. static void hso_unthrottle(struct tty_struct *tty)
  1081. {
  1082. struct hso_serial *serial = get_serial_by_tty(tty);
  1083. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1084. }
  1085. void hso_unthrottle_workfunc(struct work_struct *work)
  1086. {
  1087. struct hso_serial *serial =
  1088. container_of(work, struct hso_serial,
  1089. retry_unthrottle_workqueue);
  1090. hso_unthrottle_tasklet(serial);
  1091. }
  1092. /* open the requested serial port */
  1093. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1094. {
  1095. struct hso_serial *serial = get_serial_by_index(tty->index);
  1096. int result;
  1097. /* sanity check */
  1098. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1099. WARN_ON(1);
  1100. tty->driver_data = NULL;
  1101. D1("Failed to open port");
  1102. return -ENODEV;
  1103. }
  1104. mutex_lock(&serial->parent->mutex);
  1105. result = usb_autopm_get_interface(serial->parent->interface);
  1106. if (result < 0)
  1107. goto err_out;
  1108. D1("Opening %d", serial->minor);
  1109. kref_get(&serial->parent->ref);
  1110. /* setup */
  1111. spin_lock_irq(&serial->serial_lock);
  1112. tty->driver_data = serial;
  1113. tty_kref_put(serial->tty);
  1114. serial->tty = tty_kref_get(tty);
  1115. spin_unlock_irq(&serial->serial_lock);
  1116. /* check for port already opened, if not set the termios */
  1117. serial->open_count++;
  1118. if (serial->open_count == 1) {
  1119. tty->low_latency = 1;
  1120. serial->rx_state = RX_IDLE;
  1121. /* Force default termio settings */
  1122. _hso_serial_set_termios(tty, NULL);
  1123. tasklet_init(&serial->unthrottle_tasklet,
  1124. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1125. (unsigned long)serial);
  1126. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1127. hso_unthrottle_workfunc);
  1128. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1129. if (result) {
  1130. hso_stop_serial_device(serial->parent);
  1131. serial->open_count--;
  1132. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1133. }
  1134. } else {
  1135. D1("Port was already open");
  1136. }
  1137. usb_autopm_put_interface(serial->parent->interface);
  1138. /* done */
  1139. if (result)
  1140. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1141. err_out:
  1142. mutex_unlock(&serial->parent->mutex);
  1143. return result;
  1144. }
  1145. /* close the requested serial port */
  1146. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1147. {
  1148. struct hso_serial *serial = tty->driver_data;
  1149. u8 usb_gone;
  1150. D1("Closing serial port");
  1151. /* Open failed, no close cleanup required */
  1152. if (serial == NULL)
  1153. return;
  1154. mutex_lock(&serial->parent->mutex);
  1155. usb_gone = serial->parent->usb_gone;
  1156. if (!usb_gone)
  1157. usb_autopm_get_interface(serial->parent->interface);
  1158. /* reset the rts and dtr */
  1159. /* do the actual close */
  1160. serial->open_count--;
  1161. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1162. if (serial->open_count <= 0) {
  1163. serial->open_count = 0;
  1164. spin_lock_irq(&serial->serial_lock);
  1165. if (serial->tty == tty) {
  1166. serial->tty->driver_data = NULL;
  1167. serial->tty = NULL;
  1168. tty_kref_put(tty);
  1169. }
  1170. spin_unlock_irq(&serial->serial_lock);
  1171. if (!usb_gone)
  1172. hso_stop_serial_device(serial->parent);
  1173. tasklet_kill(&serial->unthrottle_tasklet);
  1174. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1175. }
  1176. if (!usb_gone)
  1177. usb_autopm_put_interface(serial->parent->interface);
  1178. mutex_unlock(&serial->parent->mutex);
  1179. }
  1180. /* close the requested serial port */
  1181. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1182. int count)
  1183. {
  1184. struct hso_serial *serial = get_serial_by_tty(tty);
  1185. int space, tx_bytes;
  1186. unsigned long flags;
  1187. /* sanity check */
  1188. if (serial == NULL) {
  1189. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1190. return -ENODEV;
  1191. }
  1192. spin_lock_irqsave(&serial->serial_lock, flags);
  1193. space = serial->tx_data_length - serial->tx_buffer_count;
  1194. tx_bytes = (count < space) ? count : space;
  1195. if (!tx_bytes)
  1196. goto out;
  1197. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1198. serial->tx_buffer_count += tx_bytes;
  1199. out:
  1200. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1201. hso_kick_transmit(serial);
  1202. /* done */
  1203. return tx_bytes;
  1204. }
  1205. /* how much room is there for writing */
  1206. static int hso_serial_write_room(struct tty_struct *tty)
  1207. {
  1208. struct hso_serial *serial = get_serial_by_tty(tty);
  1209. int room;
  1210. unsigned long flags;
  1211. spin_lock_irqsave(&serial->serial_lock, flags);
  1212. room = serial->tx_data_length - serial->tx_buffer_count;
  1213. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1214. /* return free room */
  1215. return room;
  1216. }
  1217. /* setup the term */
  1218. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1219. {
  1220. struct hso_serial *serial = get_serial_by_tty(tty);
  1221. unsigned long flags;
  1222. if (old)
  1223. D5("Termios called with: cflags new[%d] - old[%d]",
  1224. tty->termios->c_cflag, old->c_cflag);
  1225. /* the actual setup */
  1226. spin_lock_irqsave(&serial->serial_lock, flags);
  1227. if (serial->open_count)
  1228. _hso_serial_set_termios(tty, old);
  1229. else
  1230. tty->termios = old;
  1231. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1232. /* done */
  1233. return;
  1234. }
  1235. /* how many characters in the buffer */
  1236. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1237. {
  1238. struct hso_serial *serial = get_serial_by_tty(tty);
  1239. int chars;
  1240. unsigned long flags;
  1241. /* sanity check */
  1242. if (serial == NULL)
  1243. return 0;
  1244. spin_lock_irqsave(&serial->serial_lock, flags);
  1245. chars = serial->tx_buffer_count;
  1246. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1247. return chars;
  1248. }
  1249. int tiocmget_submit_urb(struct hso_serial *serial,
  1250. struct hso_tiocmget *tiocmget,
  1251. struct usb_device *usb)
  1252. {
  1253. int result;
  1254. if (serial->parent->usb_gone)
  1255. return -ENODEV;
  1256. usb_fill_int_urb(tiocmget->urb, usb,
  1257. usb_rcvintpipe(usb,
  1258. tiocmget->endp->
  1259. bEndpointAddress & 0x7F),
  1260. &tiocmget->serial_state_notification,
  1261. sizeof(struct hso_serial_state_notification),
  1262. tiocmget_intr_callback, serial,
  1263. tiocmget->endp->bInterval);
  1264. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1265. if (result) {
  1266. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1267. result);
  1268. }
  1269. return result;
  1270. }
  1271. static void tiocmget_intr_callback(struct urb *urb)
  1272. {
  1273. struct hso_serial *serial = urb->context;
  1274. struct hso_tiocmget *tiocmget;
  1275. int status = urb->status;
  1276. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1277. struct uart_icount *icount;
  1278. struct hso_serial_state_notification *serial_state_notification;
  1279. struct usb_device *usb;
  1280. /* Sanity checks */
  1281. if (!serial)
  1282. return;
  1283. if (status) {
  1284. log_usb_status(status, __func__);
  1285. return;
  1286. }
  1287. tiocmget = serial->tiocmget;
  1288. if (!tiocmget)
  1289. return;
  1290. usb = serial->parent->usb;
  1291. serial_state_notification = &tiocmget->serial_state_notification;
  1292. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1293. serial_state_notification->bNotification != B_NOTIFICATION ||
  1294. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1295. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1296. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1297. dev_warn(&usb->dev,
  1298. "hso received invalid serial state notification\n");
  1299. DUMP(serial_state_notification,
  1300. sizeof(hso_serial_state_notifation))
  1301. } else {
  1302. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1303. UART_state_bitmap);
  1304. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1305. icount = &tiocmget->icount;
  1306. spin_lock(&serial->serial_lock);
  1307. if ((UART_state_bitmap & B_OVERRUN) !=
  1308. (prev_UART_state_bitmap & B_OVERRUN))
  1309. icount->parity++;
  1310. if ((UART_state_bitmap & B_PARITY) !=
  1311. (prev_UART_state_bitmap & B_PARITY))
  1312. icount->parity++;
  1313. if ((UART_state_bitmap & B_FRAMING) !=
  1314. (prev_UART_state_bitmap & B_FRAMING))
  1315. icount->frame++;
  1316. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1317. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1318. icount->rng++;
  1319. if ((UART_state_bitmap & B_BREAK) !=
  1320. (prev_UART_state_bitmap & B_BREAK))
  1321. icount->brk++;
  1322. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1323. (prev_UART_state_bitmap & B_TX_CARRIER))
  1324. icount->dsr++;
  1325. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1326. (prev_UART_state_bitmap & B_RX_CARRIER))
  1327. icount->dcd++;
  1328. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1329. spin_unlock(&serial->serial_lock);
  1330. tiocmget->intr_completed = 1;
  1331. wake_up_interruptible(&tiocmget->waitq);
  1332. }
  1333. memset(serial_state_notification, 0,
  1334. sizeof(struct hso_serial_state_notification));
  1335. tiocmget_submit_urb(serial,
  1336. tiocmget,
  1337. serial->parent->usb);
  1338. }
  1339. /*
  1340. * next few functions largely stolen from drivers/serial/serial_core.c
  1341. */
  1342. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1343. * - mask passed in arg for lines of interest
  1344. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1345. * Caller should use TIOCGICOUNT to see which one it was
  1346. */
  1347. static int
  1348. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1349. {
  1350. DECLARE_WAITQUEUE(wait, current);
  1351. struct uart_icount cprev, cnow;
  1352. struct hso_tiocmget *tiocmget;
  1353. int ret;
  1354. tiocmget = serial->tiocmget;
  1355. if (!tiocmget)
  1356. return -ENOENT;
  1357. /*
  1358. * note the counters on entry
  1359. */
  1360. spin_lock_irq(&serial->serial_lock);
  1361. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1362. spin_unlock_irq(&serial->serial_lock);
  1363. add_wait_queue(&tiocmget->waitq, &wait);
  1364. for (;;) {
  1365. spin_lock_irq(&serial->serial_lock);
  1366. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1367. spin_unlock_irq(&serial->serial_lock);
  1368. set_current_state(TASK_INTERRUPTIBLE);
  1369. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1370. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1371. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1372. ret = 0;
  1373. break;
  1374. }
  1375. schedule();
  1376. /* see if a signal did it */
  1377. if (signal_pending(current)) {
  1378. ret = -ERESTARTSYS;
  1379. break;
  1380. }
  1381. cprev = cnow;
  1382. }
  1383. current->state = TASK_RUNNING;
  1384. remove_wait_queue(&tiocmget->waitq, &wait);
  1385. return ret;
  1386. }
  1387. /*
  1388. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1389. * Return: write counters to the user passed counter struct
  1390. * NB: both 1->0 and 0->1 transitions are counted except for
  1391. * RI where only 0->1 is counted.
  1392. */
  1393. static int hso_get_count(struct hso_serial *serial,
  1394. struct serial_icounter_struct __user *icnt)
  1395. {
  1396. struct serial_icounter_struct icount;
  1397. struct uart_icount cnow;
  1398. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1399. if (!tiocmget)
  1400. return -ENOENT;
  1401. spin_lock_irq(&serial->serial_lock);
  1402. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1403. spin_unlock_irq(&serial->serial_lock);
  1404. icount.cts = cnow.cts;
  1405. icount.dsr = cnow.dsr;
  1406. icount.rng = cnow.rng;
  1407. icount.dcd = cnow.dcd;
  1408. icount.rx = cnow.rx;
  1409. icount.tx = cnow.tx;
  1410. icount.frame = cnow.frame;
  1411. icount.overrun = cnow.overrun;
  1412. icount.parity = cnow.parity;
  1413. icount.brk = cnow.brk;
  1414. icount.buf_overrun = cnow.buf_overrun;
  1415. return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
  1416. }
  1417. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1418. {
  1419. int retval;
  1420. struct hso_serial *serial = get_serial_by_tty(tty);
  1421. struct hso_tiocmget *tiocmget;
  1422. u16 UART_state_bitmap;
  1423. /* sanity check */
  1424. if (!serial) {
  1425. D1("no tty structures");
  1426. return -EINVAL;
  1427. }
  1428. spin_lock_irq(&serial->serial_lock);
  1429. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1430. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1431. tiocmget = serial->tiocmget;
  1432. if (tiocmget) {
  1433. UART_state_bitmap = le16_to_cpu(
  1434. tiocmget->prev_UART_state_bitmap);
  1435. if (UART_state_bitmap & B_RING_SIGNAL)
  1436. retval |= TIOCM_RNG;
  1437. if (UART_state_bitmap & B_RX_CARRIER)
  1438. retval |= TIOCM_CD;
  1439. if (UART_state_bitmap & B_TX_CARRIER)
  1440. retval |= TIOCM_DSR;
  1441. }
  1442. spin_unlock_irq(&serial->serial_lock);
  1443. return retval;
  1444. }
  1445. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1446. unsigned int set, unsigned int clear)
  1447. {
  1448. int val = 0;
  1449. unsigned long flags;
  1450. int if_num;
  1451. struct hso_serial *serial = get_serial_by_tty(tty);
  1452. /* sanity check */
  1453. if (!serial) {
  1454. D1("no tty structures");
  1455. return -EINVAL;
  1456. }
  1457. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1458. spin_lock_irqsave(&serial->serial_lock, flags);
  1459. if (set & TIOCM_RTS)
  1460. serial->rts_state = 1;
  1461. if (set & TIOCM_DTR)
  1462. serial->dtr_state = 1;
  1463. if (clear & TIOCM_RTS)
  1464. serial->rts_state = 0;
  1465. if (clear & TIOCM_DTR)
  1466. serial->dtr_state = 0;
  1467. if (serial->dtr_state)
  1468. val |= 0x01;
  1469. if (serial->rts_state)
  1470. val |= 0x02;
  1471. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1472. return usb_control_msg(serial->parent->usb,
  1473. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1474. 0x21, val, if_num, NULL, 0,
  1475. USB_CTRL_SET_TIMEOUT);
  1476. }
  1477. static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
  1478. unsigned int cmd, unsigned long arg)
  1479. {
  1480. struct hso_serial *serial = get_serial_by_tty(tty);
  1481. void __user *uarg = (void __user *)arg;
  1482. int ret = 0;
  1483. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1484. if (!serial)
  1485. return -ENODEV;
  1486. switch (cmd) {
  1487. case TIOCMIWAIT:
  1488. ret = hso_wait_modem_status(serial, arg);
  1489. break;
  1490. case TIOCGICOUNT:
  1491. ret = hso_get_count(serial, uarg);
  1492. break;
  1493. default:
  1494. ret = -ENOIOCTLCMD;
  1495. break;
  1496. }
  1497. return ret;
  1498. }
  1499. /* starts a transmit */
  1500. static void hso_kick_transmit(struct hso_serial *serial)
  1501. {
  1502. u8 *temp;
  1503. unsigned long flags;
  1504. int res;
  1505. spin_lock_irqsave(&serial->serial_lock, flags);
  1506. if (!serial->tx_buffer_count)
  1507. goto out;
  1508. if (serial->tx_urb_used)
  1509. goto out;
  1510. /* Wakeup USB interface if necessary */
  1511. if (hso_get_activity(serial->parent) == -EAGAIN)
  1512. goto out;
  1513. /* Switch pointers around to avoid memcpy */
  1514. temp = serial->tx_buffer;
  1515. serial->tx_buffer = serial->tx_data;
  1516. serial->tx_data = temp;
  1517. serial->tx_data_count = serial->tx_buffer_count;
  1518. serial->tx_buffer_count = 0;
  1519. /* If temp is set, it means we switched buffers */
  1520. if (temp && serial->write_data) {
  1521. res = serial->write_data(serial);
  1522. if (res >= 0)
  1523. serial->tx_urb_used = 1;
  1524. }
  1525. out:
  1526. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1527. }
  1528. /* make a request (for reading and writing data to muxed serial port) */
  1529. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1530. struct urb *ctrl_urb,
  1531. struct usb_ctrlrequest *ctrl_req,
  1532. u8 *ctrl_urb_data, u32 size)
  1533. {
  1534. int result;
  1535. int pipe;
  1536. /* Sanity check */
  1537. if (!serial || !ctrl_urb || !ctrl_req) {
  1538. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1539. return -EINVAL;
  1540. }
  1541. /* initialize */
  1542. ctrl_req->wValue = 0;
  1543. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1544. ctrl_req->wLength = cpu_to_le16(size);
  1545. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1546. /* Reading command */
  1547. ctrl_req->bRequestType = USB_DIR_IN |
  1548. USB_TYPE_OPTION_VENDOR |
  1549. USB_RECIP_INTERFACE;
  1550. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1551. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1552. } else {
  1553. /* Writing command */
  1554. ctrl_req->bRequestType = USB_DIR_OUT |
  1555. USB_TYPE_OPTION_VENDOR |
  1556. USB_RECIP_INTERFACE;
  1557. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1558. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1559. }
  1560. /* syslog */
  1561. D2("%s command (%02x) len: %d, port: %d",
  1562. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1563. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1564. /* Load ctrl urb */
  1565. ctrl_urb->transfer_flags = 0;
  1566. usb_fill_control_urb(ctrl_urb,
  1567. serial->parent->usb,
  1568. pipe,
  1569. (u8 *) ctrl_req,
  1570. ctrl_urb_data, size, ctrl_callback, serial);
  1571. /* Send it on merry way */
  1572. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1573. if (result) {
  1574. dev_err(&ctrl_urb->dev->dev,
  1575. "%s failed submit ctrl_urb %d type %d", __func__,
  1576. result, type);
  1577. return result;
  1578. }
  1579. /* done */
  1580. return size;
  1581. }
  1582. /* called by intr_callback when read occurs */
  1583. static int hso_mux_serial_read(struct hso_serial *serial)
  1584. {
  1585. if (!serial)
  1586. return -EINVAL;
  1587. /* clean data */
  1588. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1589. /* make the request */
  1590. if (serial->num_rx_urbs != 1) {
  1591. dev_err(&serial->parent->interface->dev,
  1592. "ERROR: mux'd reads with multiple buffers "
  1593. "not possible\n");
  1594. return 0;
  1595. }
  1596. return mux_device_request(serial,
  1597. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1598. serial->parent->port_spec & HSO_PORT_MASK,
  1599. serial->rx_urb[0],
  1600. &serial->ctrl_req_rx,
  1601. serial->rx_data[0], serial->rx_data_length);
  1602. }
  1603. /* used for muxed serial port callback (muxed serial read) */
  1604. static void intr_callback(struct urb *urb)
  1605. {
  1606. struct hso_shared_int *shared_int = urb->context;
  1607. struct hso_serial *serial;
  1608. unsigned char *port_req;
  1609. int status = urb->status;
  1610. int i;
  1611. usb_mark_last_busy(urb->dev);
  1612. /* sanity check */
  1613. if (!shared_int)
  1614. return;
  1615. /* status check */
  1616. if (status) {
  1617. log_usb_status(status, __func__);
  1618. return;
  1619. }
  1620. D4("\n--- Got intr callback 0x%02X ---", status);
  1621. /* what request? */
  1622. port_req = urb->transfer_buffer;
  1623. D4(" port_req = 0x%.2X\n", *port_req);
  1624. /* loop over all muxed ports to find the one sending this */
  1625. for (i = 0; i < 8; i++) {
  1626. /* max 8 channels on MUX */
  1627. if (*port_req & (1 << i)) {
  1628. serial = get_serial_by_shared_int_and_type(shared_int,
  1629. (1 << i));
  1630. if (serial != NULL) {
  1631. D1("Pending read interrupt on port %d\n", i);
  1632. spin_lock(&serial->serial_lock);
  1633. if (serial->rx_state == RX_IDLE) {
  1634. /* Setup and send a ctrl req read on
  1635. * port i */
  1636. if (!serial->rx_urb_filled[0]) {
  1637. serial->rx_state = RX_SENT;
  1638. hso_mux_serial_read(serial);
  1639. } else
  1640. serial->rx_state = RX_PENDING;
  1641. } else {
  1642. D1("Already pending a read on "
  1643. "port %d\n", i);
  1644. }
  1645. spin_unlock(&serial->serial_lock);
  1646. }
  1647. }
  1648. }
  1649. /* Resubmit interrupt urb */
  1650. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1651. }
  1652. /* called for writing to muxed serial port */
  1653. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1654. {
  1655. if (NULL == serial)
  1656. return -EINVAL;
  1657. return mux_device_request(serial,
  1658. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1659. serial->parent->port_spec & HSO_PORT_MASK,
  1660. serial->tx_urb,
  1661. &serial->ctrl_req_tx,
  1662. serial->tx_data, serial->tx_data_count);
  1663. }
  1664. /* write callback for Diag and CS port */
  1665. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1666. {
  1667. struct hso_serial *serial = urb->context;
  1668. int status = urb->status;
  1669. struct tty_struct *tty;
  1670. /* sanity check */
  1671. if (!serial) {
  1672. D1("serial == NULL");
  1673. return;
  1674. }
  1675. spin_lock(&serial->serial_lock);
  1676. serial->tx_urb_used = 0;
  1677. tty = tty_kref_get(serial->tty);
  1678. spin_unlock(&serial->serial_lock);
  1679. if (status) {
  1680. log_usb_status(status, __func__);
  1681. tty_kref_put(tty);
  1682. return;
  1683. }
  1684. hso_put_activity(serial->parent);
  1685. if (tty) {
  1686. tty_wakeup(tty);
  1687. tty_kref_put(tty);
  1688. }
  1689. hso_kick_transmit(serial);
  1690. D1(" ");
  1691. return;
  1692. }
  1693. /* called for writing diag or CS serial port */
  1694. static int hso_std_serial_write_data(struct hso_serial *serial)
  1695. {
  1696. int count = serial->tx_data_count;
  1697. int result;
  1698. usb_fill_bulk_urb(serial->tx_urb,
  1699. serial->parent->usb,
  1700. usb_sndbulkpipe(serial->parent->usb,
  1701. serial->out_endp->
  1702. bEndpointAddress & 0x7F),
  1703. serial->tx_data, serial->tx_data_count,
  1704. hso_std_serial_write_bulk_callback, serial);
  1705. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1706. if (result) {
  1707. dev_warn(&serial->parent->usb->dev,
  1708. "Failed to submit urb - res %d\n", result);
  1709. return result;
  1710. }
  1711. return count;
  1712. }
  1713. /* callback after read or write on muxed serial port */
  1714. static void ctrl_callback(struct urb *urb)
  1715. {
  1716. struct hso_serial *serial = urb->context;
  1717. struct usb_ctrlrequest *req;
  1718. int status = urb->status;
  1719. struct tty_struct *tty;
  1720. /* sanity check */
  1721. if (!serial)
  1722. return;
  1723. spin_lock(&serial->serial_lock);
  1724. serial->tx_urb_used = 0;
  1725. tty = tty_kref_get(serial->tty);
  1726. spin_unlock(&serial->serial_lock);
  1727. if (status) {
  1728. log_usb_status(status, __func__);
  1729. tty_kref_put(tty);
  1730. return;
  1731. }
  1732. /* what request? */
  1733. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1734. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1735. D4("Actual length of urb = %d\n", urb->actual_length);
  1736. DUMP1(urb->transfer_buffer, urb->actual_length);
  1737. if (req->bRequestType ==
  1738. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1739. /* response to a read command */
  1740. serial->rx_urb_filled[0] = 1;
  1741. spin_lock(&serial->serial_lock);
  1742. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1743. spin_unlock(&serial->serial_lock);
  1744. } else {
  1745. hso_put_activity(serial->parent);
  1746. if (tty)
  1747. tty_wakeup(tty);
  1748. /* response to a write command */
  1749. hso_kick_transmit(serial);
  1750. }
  1751. tty_kref_put(tty);
  1752. }
  1753. /* handle RX data for serial port */
  1754. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1755. {
  1756. struct tty_struct *tty;
  1757. int write_length_remaining = 0;
  1758. int curr_write_len;
  1759. /* Sanity check */
  1760. if (urb == NULL || serial == NULL) {
  1761. D1("serial = NULL");
  1762. return -2;
  1763. }
  1764. /* All callers to put_rxbuf_data hold serial_lock */
  1765. tty = tty_kref_get(serial->tty);
  1766. /* Push data to tty */
  1767. if (tty) {
  1768. write_length_remaining = urb->actual_length -
  1769. serial->curr_rx_urb_offset;
  1770. D1("data to push to tty");
  1771. while (write_length_remaining) {
  1772. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1773. tty_kref_put(tty);
  1774. return -1;
  1775. }
  1776. curr_write_len = tty_insert_flip_string
  1777. (tty, urb->transfer_buffer +
  1778. serial->curr_rx_urb_offset,
  1779. write_length_remaining);
  1780. serial->curr_rx_urb_offset += curr_write_len;
  1781. write_length_remaining -= curr_write_len;
  1782. tty_flip_buffer_push(tty);
  1783. }
  1784. }
  1785. if (write_length_remaining == 0) {
  1786. serial->curr_rx_urb_offset = 0;
  1787. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1788. }
  1789. tty_kref_put(tty);
  1790. return write_length_remaining;
  1791. }
  1792. /* Base driver functions */
  1793. static void hso_log_port(struct hso_device *hso_dev)
  1794. {
  1795. char *port_type;
  1796. char port_dev[20];
  1797. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1798. case HSO_PORT_CONTROL:
  1799. port_type = "Control";
  1800. break;
  1801. case HSO_PORT_APP:
  1802. port_type = "Application";
  1803. break;
  1804. case HSO_PORT_GPS:
  1805. port_type = "GPS";
  1806. break;
  1807. case HSO_PORT_GPS_CONTROL:
  1808. port_type = "GPS control";
  1809. break;
  1810. case HSO_PORT_APP2:
  1811. port_type = "Application2";
  1812. break;
  1813. case HSO_PORT_PCSC:
  1814. port_type = "PCSC";
  1815. break;
  1816. case HSO_PORT_DIAG:
  1817. port_type = "Diagnostic";
  1818. break;
  1819. case HSO_PORT_DIAG2:
  1820. port_type = "Diagnostic2";
  1821. break;
  1822. case HSO_PORT_MODEM:
  1823. port_type = "Modem";
  1824. break;
  1825. case HSO_PORT_NETWORK:
  1826. port_type = "Network";
  1827. break;
  1828. default:
  1829. port_type = "Unknown";
  1830. break;
  1831. }
  1832. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1833. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1834. } else
  1835. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1836. dev2ser(hso_dev)->minor);
  1837. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1838. port_type, port_dev);
  1839. }
  1840. static int hso_start_net_device(struct hso_device *hso_dev)
  1841. {
  1842. int i, result = 0;
  1843. struct hso_net *hso_net = dev2net(hso_dev);
  1844. if (!hso_net)
  1845. return -ENODEV;
  1846. /* send URBs for all read buffers */
  1847. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1848. /* Prep a receive URB */
  1849. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1850. hso_dev->usb,
  1851. usb_rcvbulkpipe(hso_dev->usb,
  1852. hso_net->in_endp->
  1853. bEndpointAddress & 0x7F),
  1854. hso_net->mux_bulk_rx_buf_pool[i],
  1855. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1856. hso_net);
  1857. /* Put it out there so the device can send us stuff */
  1858. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1859. GFP_NOIO);
  1860. if (result)
  1861. dev_warn(&hso_dev->usb->dev,
  1862. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1863. i, result);
  1864. }
  1865. return result;
  1866. }
  1867. static int hso_stop_net_device(struct hso_device *hso_dev)
  1868. {
  1869. int i;
  1870. struct hso_net *hso_net = dev2net(hso_dev);
  1871. if (!hso_net)
  1872. return -ENODEV;
  1873. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1874. if (hso_net->mux_bulk_rx_urb_pool[i])
  1875. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1876. }
  1877. if (hso_net->mux_bulk_tx_urb)
  1878. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1879. return 0;
  1880. }
  1881. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1882. {
  1883. int i, result = 0;
  1884. struct hso_serial *serial = dev2ser(hso_dev);
  1885. if (!serial)
  1886. return -ENODEV;
  1887. /* If it is not the MUX port fill in and submit a bulk urb (already
  1888. * allocated in hso_serial_start) */
  1889. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1890. for (i = 0; i < serial->num_rx_urbs; i++) {
  1891. usb_fill_bulk_urb(serial->rx_urb[i],
  1892. serial->parent->usb,
  1893. usb_rcvbulkpipe(serial->parent->usb,
  1894. serial->in_endp->
  1895. bEndpointAddress &
  1896. 0x7F),
  1897. serial->rx_data[i],
  1898. serial->rx_data_length,
  1899. hso_std_serial_read_bulk_callback,
  1900. serial);
  1901. result = usb_submit_urb(serial->rx_urb[i], flags);
  1902. if (result) {
  1903. dev_warn(&serial->parent->usb->dev,
  1904. "Failed to submit urb - res %d\n",
  1905. result);
  1906. break;
  1907. }
  1908. }
  1909. } else {
  1910. mutex_lock(&serial->shared_int->shared_int_lock);
  1911. if (!serial->shared_int->use_count) {
  1912. result =
  1913. hso_mux_submit_intr_urb(serial->shared_int,
  1914. hso_dev->usb, flags);
  1915. }
  1916. serial->shared_int->use_count++;
  1917. mutex_unlock(&serial->shared_int->shared_int_lock);
  1918. }
  1919. if (serial->tiocmget)
  1920. tiocmget_submit_urb(serial,
  1921. serial->tiocmget,
  1922. serial->parent->usb);
  1923. return result;
  1924. }
  1925. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1926. {
  1927. int i;
  1928. struct hso_serial *serial = dev2ser(hso_dev);
  1929. struct hso_tiocmget *tiocmget;
  1930. if (!serial)
  1931. return -ENODEV;
  1932. for (i = 0; i < serial->num_rx_urbs; i++) {
  1933. if (serial->rx_urb[i]) {
  1934. usb_kill_urb(serial->rx_urb[i]);
  1935. serial->rx_urb_filled[i] = 0;
  1936. }
  1937. }
  1938. serial->curr_rx_urb_idx = 0;
  1939. serial->curr_rx_urb_offset = 0;
  1940. if (serial->tx_urb)
  1941. usb_kill_urb(serial->tx_urb);
  1942. if (serial->shared_int) {
  1943. mutex_lock(&serial->shared_int->shared_int_lock);
  1944. if (serial->shared_int->use_count &&
  1945. (--serial->shared_int->use_count == 0)) {
  1946. struct urb *urb;
  1947. urb = serial->shared_int->shared_intr_urb;
  1948. if (urb)
  1949. usb_kill_urb(urb);
  1950. }
  1951. mutex_unlock(&serial->shared_int->shared_int_lock);
  1952. }
  1953. tiocmget = serial->tiocmget;
  1954. if (tiocmget) {
  1955. wake_up_interruptible(&tiocmget->waitq);
  1956. usb_kill_urb(tiocmget->urb);
  1957. }
  1958. return 0;
  1959. }
  1960. static void hso_serial_common_free(struct hso_serial *serial)
  1961. {
  1962. int i;
  1963. if (serial->parent->dev)
  1964. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1965. tty_unregister_device(tty_drv, serial->minor);
  1966. for (i = 0; i < serial->num_rx_urbs; i++) {
  1967. /* unlink and free RX URB */
  1968. usb_free_urb(serial->rx_urb[i]);
  1969. /* free the RX buffer */
  1970. kfree(serial->rx_data[i]);
  1971. }
  1972. /* unlink and free TX URB */
  1973. usb_free_urb(serial->tx_urb);
  1974. kfree(serial->tx_data);
  1975. }
  1976. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1977. int rx_size, int tx_size)
  1978. {
  1979. struct device *dev;
  1980. int minor;
  1981. int i;
  1982. minor = get_free_serial_index();
  1983. if (minor < 0)
  1984. goto exit;
  1985. /* register our minor number */
  1986. serial->parent->dev = tty_register_device(tty_drv, minor,
  1987. &serial->parent->interface->dev);
  1988. dev = serial->parent->dev;
  1989. dev->driver_data = serial->parent;
  1990. i = device_create_file(dev, &dev_attr_hsotype);
  1991. /* fill in specific data for later use */
  1992. serial->minor = minor;
  1993. serial->magic = HSO_SERIAL_MAGIC;
  1994. spin_lock_init(&serial->serial_lock);
  1995. serial->num_rx_urbs = num_urbs;
  1996. /* RX, allocate urb and initialize */
  1997. /* prepare our RX buffer */
  1998. serial->rx_data_length = rx_size;
  1999. for (i = 0; i < serial->num_rx_urbs; i++) {
  2000. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2001. if (!serial->rx_urb[i]) {
  2002. dev_err(dev, "Could not allocate urb?\n");
  2003. goto exit;
  2004. }
  2005. serial->rx_urb[i]->transfer_buffer = NULL;
  2006. serial->rx_urb[i]->transfer_buffer_length = 0;
  2007. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2008. GFP_KERNEL);
  2009. if (!serial->rx_data[i]) {
  2010. dev_err(dev, "%s - Out of memory\n", __func__);
  2011. goto exit;
  2012. }
  2013. }
  2014. /* TX, allocate urb and initialize */
  2015. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2016. if (!serial->tx_urb) {
  2017. dev_err(dev, "Could not allocate urb?\n");
  2018. goto exit;
  2019. }
  2020. serial->tx_urb->transfer_buffer = NULL;
  2021. serial->tx_urb->transfer_buffer_length = 0;
  2022. /* prepare our TX buffer */
  2023. serial->tx_data_count = 0;
  2024. serial->tx_buffer_count = 0;
  2025. serial->tx_data_length = tx_size;
  2026. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2027. if (!serial->tx_data) {
  2028. dev_err(dev, "%s - Out of memory", __func__);
  2029. goto exit;
  2030. }
  2031. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2032. if (!serial->tx_buffer) {
  2033. dev_err(dev, "%s - Out of memory", __func__);
  2034. goto exit;
  2035. }
  2036. return 0;
  2037. exit:
  2038. hso_serial_common_free(serial);
  2039. return -1;
  2040. }
  2041. /* Frees a general hso device */
  2042. static void hso_free_device(struct hso_device *hso_dev)
  2043. {
  2044. kfree(hso_dev);
  2045. }
  2046. /* Creates a general hso device */
  2047. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2048. int port_spec)
  2049. {
  2050. struct hso_device *hso_dev;
  2051. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2052. if (!hso_dev)
  2053. return NULL;
  2054. hso_dev->port_spec = port_spec;
  2055. hso_dev->usb = interface_to_usbdev(intf);
  2056. hso_dev->interface = intf;
  2057. kref_init(&hso_dev->ref);
  2058. mutex_init(&hso_dev->mutex);
  2059. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2060. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2061. return hso_dev;
  2062. }
  2063. /* Removes a network device in the network device table */
  2064. static int remove_net_device(struct hso_device *hso_dev)
  2065. {
  2066. int i;
  2067. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2068. if (network_table[i] == hso_dev) {
  2069. network_table[i] = NULL;
  2070. break;
  2071. }
  2072. }
  2073. if (i == HSO_MAX_NET_DEVICES)
  2074. return -1;
  2075. return 0;
  2076. }
  2077. /* Frees our network device */
  2078. static void hso_free_net_device(struct hso_device *hso_dev)
  2079. {
  2080. int i;
  2081. struct hso_net *hso_net = dev2net(hso_dev);
  2082. if (!hso_net)
  2083. return;
  2084. /* start freeing */
  2085. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2086. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2087. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2088. }
  2089. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2090. kfree(hso_net->mux_bulk_tx_buf);
  2091. remove_net_device(hso_net->parent);
  2092. if (hso_net->net) {
  2093. unregister_netdev(hso_net->net);
  2094. free_netdev(hso_net->net);
  2095. }
  2096. hso_free_device(hso_dev);
  2097. }
  2098. /* initialize the network interface */
  2099. static void hso_net_init(struct net_device *net)
  2100. {
  2101. struct hso_net *hso_net = netdev_priv(net);
  2102. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2103. /* fill in the other fields */
  2104. net->open = hso_net_open;
  2105. net->stop = hso_net_close;
  2106. net->hard_start_xmit = hso_net_start_xmit;
  2107. net->tx_timeout = hso_net_tx_timeout;
  2108. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2109. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2110. net->type = ARPHRD_NONE;
  2111. net->mtu = DEFAULT_MTU - 14;
  2112. net->tx_queue_len = 10;
  2113. SET_ETHTOOL_OPS(net, &ops);
  2114. /* and initialize the semaphore */
  2115. spin_lock_init(&hso_net->net_lock);
  2116. }
  2117. /* Adds a network device in the network device table */
  2118. static int add_net_device(struct hso_device *hso_dev)
  2119. {
  2120. int i;
  2121. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2122. if (network_table[i] == NULL) {
  2123. network_table[i] = hso_dev;
  2124. break;
  2125. }
  2126. }
  2127. if (i == HSO_MAX_NET_DEVICES)
  2128. return -1;
  2129. return 0;
  2130. }
  2131. static int hso_radio_toggle(void *data, enum rfkill_state state)
  2132. {
  2133. struct hso_device *hso_dev = data;
  2134. int enabled = (state == RFKILL_STATE_ON);
  2135. int rv;
  2136. mutex_lock(&hso_dev->mutex);
  2137. if (hso_dev->usb_gone)
  2138. rv = 0;
  2139. else
  2140. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2141. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2142. USB_CTRL_SET_TIMEOUT);
  2143. mutex_unlock(&hso_dev->mutex);
  2144. return rv;
  2145. }
  2146. /* Creates and sets up everything for rfkill */
  2147. static void hso_create_rfkill(struct hso_device *hso_dev,
  2148. struct usb_interface *interface)
  2149. {
  2150. struct hso_net *hso_net = dev2net(hso_dev);
  2151. struct device *dev = &hso_net->net->dev;
  2152. char *rfkn;
  2153. hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
  2154. RFKILL_TYPE_WWAN);
  2155. if (!hso_net->rfkill) {
  2156. dev_err(dev, "%s - Out of memory\n", __func__);
  2157. return;
  2158. }
  2159. rfkn = kzalloc(20, GFP_KERNEL);
  2160. if (!rfkn) {
  2161. rfkill_free(hso_net->rfkill);
  2162. hso_net->rfkill = NULL;
  2163. dev_err(dev, "%s - Out of memory\n", __func__);
  2164. return;
  2165. }
  2166. snprintf(rfkn, 20, "hso-%d",
  2167. interface->altsetting->desc.bInterfaceNumber);
  2168. hso_net->rfkill->name = rfkn;
  2169. hso_net->rfkill->state = RFKILL_STATE_ON;
  2170. hso_net->rfkill->data = hso_dev;
  2171. hso_net->rfkill->toggle_radio = hso_radio_toggle;
  2172. if (rfkill_register(hso_net->rfkill) < 0) {
  2173. kfree(rfkn);
  2174. hso_net->rfkill->name = NULL;
  2175. rfkill_free(hso_net->rfkill);
  2176. hso_net->rfkill = NULL;
  2177. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2178. return;
  2179. }
  2180. }
  2181. /* Creates our network device */
  2182. static struct hso_device *hso_create_net_device(struct usb_interface *interface)
  2183. {
  2184. int result, i;
  2185. struct net_device *net;
  2186. struct hso_net *hso_net;
  2187. struct hso_device *hso_dev;
  2188. hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
  2189. if (!hso_dev)
  2190. return NULL;
  2191. /* allocate our network device, then we can put in our private data */
  2192. /* call hso_net_init to do the basic initialization */
  2193. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2194. if (!net) {
  2195. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2196. goto exit;
  2197. }
  2198. hso_net = netdev_priv(net);
  2199. hso_dev->port_data.dev_net = hso_net;
  2200. hso_net->net = net;
  2201. hso_net->parent = hso_dev;
  2202. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2203. USB_DIR_IN);
  2204. if (!hso_net->in_endp) {
  2205. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2206. goto exit;
  2207. }
  2208. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2209. USB_DIR_OUT);
  2210. if (!hso_net->out_endp) {
  2211. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2212. goto exit;
  2213. }
  2214. SET_NETDEV_DEV(net, &interface->dev);
  2215. /* registering our net device */
  2216. result = register_netdev(net);
  2217. if (result) {
  2218. dev_err(&interface->dev, "Failed to register device\n");
  2219. goto exit;
  2220. }
  2221. /* start allocating */
  2222. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2223. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2224. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2225. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2226. goto exit;
  2227. }
  2228. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2229. GFP_KERNEL);
  2230. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2231. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2232. goto exit;
  2233. }
  2234. }
  2235. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2236. if (!hso_net->mux_bulk_tx_urb) {
  2237. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2238. goto exit;
  2239. }
  2240. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2241. if (!hso_net->mux_bulk_tx_buf) {
  2242. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2243. goto exit;
  2244. }
  2245. add_net_device(hso_dev);
  2246. hso_log_port(hso_dev);
  2247. hso_create_rfkill(hso_dev, interface);
  2248. return hso_dev;
  2249. exit:
  2250. hso_free_net_device(hso_dev);
  2251. return NULL;
  2252. }
  2253. static void hso_free_tiomget(struct hso_serial *serial)
  2254. {
  2255. struct hso_tiocmget *tiocmget = serial->tiocmget;
  2256. if (tiocmget) {
  2257. kfree(tiocmget);
  2258. if (tiocmget->urb) {
  2259. usb_free_urb(tiocmget->urb);
  2260. tiocmget->urb = NULL;
  2261. }
  2262. serial->tiocmget = NULL;
  2263. }
  2264. }
  2265. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2266. static void hso_free_serial_device(struct hso_device *hso_dev)
  2267. {
  2268. struct hso_serial *serial = dev2ser(hso_dev);
  2269. if (!serial)
  2270. return;
  2271. set_serial_by_index(serial->minor, NULL);
  2272. hso_serial_common_free(serial);
  2273. if (serial->shared_int) {
  2274. mutex_lock(&serial->shared_int->shared_int_lock);
  2275. if (--serial->shared_int->ref_count == 0)
  2276. hso_free_shared_int(serial->shared_int);
  2277. else
  2278. mutex_unlock(&serial->shared_int->shared_int_lock);
  2279. }
  2280. hso_free_tiomget(serial);
  2281. kfree(serial);
  2282. hso_free_device(hso_dev);
  2283. }
  2284. /* Creates a bulk AT channel */
  2285. static struct hso_device *hso_create_bulk_serial_device(
  2286. struct usb_interface *interface, int port)
  2287. {
  2288. struct hso_device *hso_dev;
  2289. struct hso_serial *serial;
  2290. int num_urbs;
  2291. struct hso_tiocmget *tiocmget;
  2292. hso_dev = hso_create_device(interface, port);
  2293. if (!hso_dev)
  2294. return NULL;
  2295. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2296. if (!serial)
  2297. goto exit;
  2298. serial->parent = hso_dev;
  2299. hso_dev->port_data.dev_serial = serial;
  2300. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2301. num_urbs = 2;
  2302. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2303. GFP_KERNEL);
  2304. /* it isn't going to break our heart if serial->tiocmget
  2305. * allocation fails don't bother checking this.
  2306. */
  2307. if (serial->tiocmget) {
  2308. tiocmget = serial->tiocmget;
  2309. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2310. if (tiocmget->urb) {
  2311. mutex_init(&tiocmget->mutex);
  2312. init_waitqueue_head(&tiocmget->waitq);
  2313. tiocmget->endp = hso_get_ep(
  2314. interface,
  2315. USB_ENDPOINT_XFER_INT,
  2316. USB_DIR_IN);
  2317. } else
  2318. hso_free_tiomget(serial);
  2319. }
  2320. }
  2321. else
  2322. num_urbs = 1;
  2323. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2324. BULK_URB_TX_SIZE))
  2325. goto exit;
  2326. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2327. USB_DIR_IN);
  2328. if (!serial->in_endp) {
  2329. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2330. goto exit2;
  2331. }
  2332. if (!
  2333. (serial->out_endp =
  2334. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2335. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2336. goto exit2;
  2337. }
  2338. serial->write_data = hso_std_serial_write_data;
  2339. /* and record this serial */
  2340. set_serial_by_index(serial->minor, serial);
  2341. /* setup the proc dirs and files if needed */
  2342. hso_log_port(hso_dev);
  2343. /* done, return it */
  2344. return hso_dev;
  2345. exit2:
  2346. hso_serial_common_free(serial);
  2347. exit:
  2348. hso_free_tiomget(serial);
  2349. kfree(serial);
  2350. hso_free_device(hso_dev);
  2351. return NULL;
  2352. }
  2353. /* Creates a multiplexed AT channel */
  2354. static
  2355. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2356. int port,
  2357. struct hso_shared_int *mux)
  2358. {
  2359. struct hso_device *hso_dev;
  2360. struct hso_serial *serial;
  2361. int port_spec;
  2362. port_spec = HSO_INTF_MUX;
  2363. port_spec &= ~HSO_PORT_MASK;
  2364. port_spec |= hso_mux_to_port(port);
  2365. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2366. return NULL;
  2367. hso_dev = hso_create_device(interface, port_spec);
  2368. if (!hso_dev)
  2369. return NULL;
  2370. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2371. if (!serial)
  2372. goto exit;
  2373. hso_dev->port_data.dev_serial = serial;
  2374. serial->parent = hso_dev;
  2375. if (hso_serial_common_create
  2376. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2377. goto exit;
  2378. serial->tx_data_length--;
  2379. serial->write_data = hso_mux_serial_write_data;
  2380. serial->shared_int = mux;
  2381. mutex_lock(&serial->shared_int->shared_int_lock);
  2382. serial->shared_int->ref_count++;
  2383. mutex_unlock(&serial->shared_int->shared_int_lock);
  2384. /* and record this serial */
  2385. set_serial_by_index(serial->minor, serial);
  2386. /* setup the proc dirs and files if needed */
  2387. hso_log_port(hso_dev);
  2388. /* done, return it */
  2389. return hso_dev;
  2390. exit:
  2391. if (serial) {
  2392. tty_unregister_device(tty_drv, serial->minor);
  2393. kfree(serial);
  2394. }
  2395. if (hso_dev)
  2396. hso_free_device(hso_dev);
  2397. return NULL;
  2398. }
  2399. static void hso_free_shared_int(struct hso_shared_int *mux)
  2400. {
  2401. usb_free_urb(mux->shared_intr_urb);
  2402. kfree(mux->shared_intr_buf);
  2403. mutex_unlock(&mux->shared_int_lock);
  2404. kfree(mux);
  2405. }
  2406. static
  2407. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2408. {
  2409. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2410. if (!mux)
  2411. return NULL;
  2412. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2413. USB_DIR_IN);
  2414. if (!mux->intr_endp) {
  2415. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2416. goto exit;
  2417. }
  2418. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2419. if (!mux->shared_intr_urb) {
  2420. dev_err(&interface->dev, "Could not allocate intr urb?");
  2421. goto exit;
  2422. }
  2423. mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
  2424. GFP_KERNEL);
  2425. if (!mux->shared_intr_buf) {
  2426. dev_err(&interface->dev, "Could not allocate intr buf?");
  2427. goto exit;
  2428. }
  2429. mutex_init(&mux->shared_int_lock);
  2430. return mux;
  2431. exit:
  2432. kfree(mux->shared_intr_buf);
  2433. usb_free_urb(mux->shared_intr_urb);
  2434. kfree(mux);
  2435. return NULL;
  2436. }
  2437. /* Gets the port spec for a certain interface */
  2438. static int hso_get_config_data(struct usb_interface *interface)
  2439. {
  2440. struct usb_device *usbdev = interface_to_usbdev(interface);
  2441. u8 config_data[17];
  2442. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2443. s32 result;
  2444. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2445. 0x86, 0xC0, 0, 0, config_data, 17,
  2446. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2447. return -EIO;
  2448. }
  2449. switch (config_data[if_num]) {
  2450. case 0x0:
  2451. result = 0;
  2452. break;
  2453. case 0x1:
  2454. result = HSO_PORT_DIAG;
  2455. break;
  2456. case 0x2:
  2457. result = HSO_PORT_GPS;
  2458. break;
  2459. case 0x3:
  2460. result = HSO_PORT_GPS_CONTROL;
  2461. break;
  2462. case 0x4:
  2463. result = HSO_PORT_APP;
  2464. break;
  2465. case 0x5:
  2466. result = HSO_PORT_APP2;
  2467. break;
  2468. case 0x6:
  2469. result = HSO_PORT_CONTROL;
  2470. break;
  2471. case 0x7:
  2472. result = HSO_PORT_NETWORK;
  2473. break;
  2474. case 0x8:
  2475. result = HSO_PORT_MODEM;
  2476. break;
  2477. case 0x9:
  2478. result = HSO_PORT_MSD;
  2479. break;
  2480. case 0xa:
  2481. result = HSO_PORT_PCSC;
  2482. break;
  2483. case 0xb:
  2484. result = HSO_PORT_VOICE;
  2485. break;
  2486. default:
  2487. result = 0;
  2488. }
  2489. if (result)
  2490. result |= HSO_INTF_BULK;
  2491. if (config_data[16] & 0x1)
  2492. result |= HSO_INFO_CRC_BUG;
  2493. return result;
  2494. }
  2495. /* called once for each interface upon device insertion */
  2496. static int hso_probe(struct usb_interface *interface,
  2497. const struct usb_device_id *id)
  2498. {
  2499. int mux, i, if_num, port_spec;
  2500. unsigned char port_mask;
  2501. struct hso_device *hso_dev = NULL;
  2502. struct hso_shared_int *shared_int;
  2503. struct hso_device *tmp_dev = NULL;
  2504. if_num = interface->altsetting->desc.bInterfaceNumber;
  2505. /* Get the interface/port specification from either driver_info or from
  2506. * the device itself */
  2507. if (id->driver_info)
  2508. port_spec = ((u32 *)(id->driver_info))[if_num];
  2509. else
  2510. port_spec = hso_get_config_data(interface);
  2511. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2512. dev_err(&interface->dev, "Not our interface\n");
  2513. return -ENODEV;
  2514. }
  2515. /* Check if we need to switch to alt interfaces prior to port
  2516. * configuration */
  2517. if (interface->num_altsetting > 1)
  2518. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2519. interface->needs_remote_wakeup = 1;
  2520. /* Allocate new hso device(s) */
  2521. switch (port_spec & HSO_INTF_MASK) {
  2522. case HSO_INTF_MUX:
  2523. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2524. /* Create the network device */
  2525. if (!disable_net) {
  2526. hso_dev = hso_create_net_device(interface);
  2527. if (!hso_dev)
  2528. goto exit;
  2529. tmp_dev = hso_dev;
  2530. }
  2531. }
  2532. if (hso_get_mux_ports(interface, &port_mask))
  2533. /* TODO: de-allocate everything */
  2534. goto exit;
  2535. shared_int = hso_create_shared_int(interface);
  2536. if (!shared_int)
  2537. goto exit;
  2538. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2539. if (port_mask & i) {
  2540. hso_dev = hso_create_mux_serial_device(
  2541. interface, i, shared_int);
  2542. if (!hso_dev)
  2543. goto exit;
  2544. }
  2545. }
  2546. if (tmp_dev)
  2547. hso_dev = tmp_dev;
  2548. break;
  2549. case HSO_INTF_BULK:
  2550. /* It's a regular bulk interface */
  2551. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
  2552. && !disable_net)
  2553. hso_dev = hso_create_net_device(interface);
  2554. else
  2555. hso_dev =
  2556. hso_create_bulk_serial_device(interface, port_spec);
  2557. if (!hso_dev)
  2558. goto exit;
  2559. break;
  2560. default:
  2561. goto exit;
  2562. }
  2563. usb_driver_claim_interface(&hso_driver, interface, hso_dev);
  2564. /* save our data pointer in this device */
  2565. usb_set_intfdata(interface, hso_dev);
  2566. /* done */
  2567. return 0;
  2568. exit:
  2569. hso_free_interface(interface);
  2570. return -ENODEV;
  2571. }
  2572. /* device removed, cleaning up */
  2573. static void hso_disconnect(struct usb_interface *interface)
  2574. {
  2575. hso_free_interface(interface);
  2576. /* remove reference of our private data */
  2577. usb_set_intfdata(interface, NULL);
  2578. usb_driver_release_interface(&hso_driver, interface);
  2579. }
  2580. static void async_get_intf(struct work_struct *data)
  2581. {
  2582. struct hso_device *hso_dev =
  2583. container_of(data, struct hso_device, async_get_intf);
  2584. usb_autopm_get_interface(hso_dev->interface);
  2585. }
  2586. static void async_put_intf(struct work_struct *data)
  2587. {
  2588. struct hso_device *hso_dev =
  2589. container_of(data, struct hso_device, async_put_intf);
  2590. usb_autopm_put_interface(hso_dev->interface);
  2591. }
  2592. static int hso_get_activity(struct hso_device *hso_dev)
  2593. {
  2594. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2595. if (!hso_dev->is_active) {
  2596. hso_dev->is_active = 1;
  2597. schedule_work(&hso_dev->async_get_intf);
  2598. }
  2599. }
  2600. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2601. return -EAGAIN;
  2602. usb_mark_last_busy(hso_dev->usb);
  2603. return 0;
  2604. }
  2605. static int hso_put_activity(struct hso_device *hso_dev)
  2606. {
  2607. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2608. if (hso_dev->is_active) {
  2609. hso_dev->is_active = 0;
  2610. schedule_work(&hso_dev->async_put_intf);
  2611. return -EAGAIN;
  2612. }
  2613. }
  2614. hso_dev->is_active = 0;
  2615. return 0;
  2616. }
  2617. /* called by kernel when we need to suspend device */
  2618. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2619. {
  2620. int i, result;
  2621. /* Stop all serial ports */
  2622. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2623. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2624. result = hso_stop_serial_device(serial_table[i]);
  2625. if (result)
  2626. goto out;
  2627. }
  2628. }
  2629. /* Stop all network ports */
  2630. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2631. if (network_table[i] &&
  2632. (network_table[i]->interface == iface)) {
  2633. result = hso_stop_net_device(network_table[i]);
  2634. if (result)
  2635. goto out;
  2636. }
  2637. }
  2638. out:
  2639. return 0;
  2640. }
  2641. /* called by kernel when we need to resume device */
  2642. static int hso_resume(struct usb_interface *iface)
  2643. {
  2644. int i, result = 0;
  2645. struct hso_net *hso_net;
  2646. /* Start all serial ports */
  2647. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2648. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2649. if (dev2ser(serial_table[i])->open_count) {
  2650. result =
  2651. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2652. hso_kick_transmit(dev2ser(serial_table[i]));
  2653. if (result)
  2654. goto out;
  2655. }
  2656. }
  2657. }
  2658. /* Start all network ports */
  2659. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2660. if (network_table[i] &&
  2661. (network_table[i]->interface == iface)) {
  2662. hso_net = dev2net(network_table[i]);
  2663. if (hso_net->flags & IFF_UP) {
  2664. /* First transmit any lingering data,
  2665. then restart the device. */
  2666. if (hso_net->skb_tx_buf) {
  2667. dev_dbg(&iface->dev,
  2668. "Transmitting"
  2669. " lingering data\n");
  2670. hso_net_start_xmit(hso_net->skb_tx_buf,
  2671. hso_net->net);
  2672. hso_net->skb_tx_buf = NULL;
  2673. }
  2674. result = hso_start_net_device(network_table[i]);
  2675. if (result)
  2676. goto out;
  2677. }
  2678. }
  2679. }
  2680. out:
  2681. return result;
  2682. }
  2683. static void hso_serial_ref_free(struct kref *ref)
  2684. {
  2685. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2686. hso_free_serial_device(hso_dev);
  2687. }
  2688. static void hso_free_interface(struct usb_interface *interface)
  2689. {
  2690. struct hso_serial *hso_dev;
  2691. struct tty_struct *tty;
  2692. int i;
  2693. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2694. if (serial_table[i]
  2695. && (serial_table[i]->interface == interface)) {
  2696. hso_dev = dev2ser(serial_table[i]);
  2697. spin_lock_irq(&hso_dev->serial_lock);
  2698. tty = tty_kref_get(hso_dev->tty);
  2699. spin_unlock_irq(&hso_dev->serial_lock);
  2700. if (tty)
  2701. tty_hangup(tty);
  2702. mutex_lock(&hso_dev->parent->mutex);
  2703. tty_kref_put(tty);
  2704. hso_dev->parent->usb_gone = 1;
  2705. mutex_unlock(&hso_dev->parent->mutex);
  2706. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2707. }
  2708. }
  2709. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2710. if (network_table[i]
  2711. && (network_table[i]->interface == interface)) {
  2712. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2713. /* hso_stop_net_device doesn't stop the net queue since
  2714. * traffic needs to start it again when suspended */
  2715. netif_stop_queue(dev2net(network_table[i])->net);
  2716. hso_stop_net_device(network_table[i]);
  2717. cancel_work_sync(&network_table[i]->async_put_intf);
  2718. cancel_work_sync(&network_table[i]->async_get_intf);
  2719. if (rfk)
  2720. rfkill_unregister(rfk);
  2721. hso_free_net_device(network_table[i]);
  2722. }
  2723. }
  2724. }
  2725. /* Helper functions */
  2726. /* Get the endpoint ! */
  2727. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2728. int type, int dir)
  2729. {
  2730. int i;
  2731. struct usb_host_interface *iface = intf->cur_altsetting;
  2732. struct usb_endpoint_descriptor *endp;
  2733. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2734. endp = &iface->endpoint[i].desc;
  2735. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2736. (usb_endpoint_type(endp) == type))
  2737. return endp;
  2738. }
  2739. return NULL;
  2740. }
  2741. /* Get the byte that describes which ports are enabled */
  2742. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2743. {
  2744. int i;
  2745. struct usb_host_interface *iface = intf->cur_altsetting;
  2746. if (iface->extralen == 3) {
  2747. *ports = iface->extra[2];
  2748. return 0;
  2749. }
  2750. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2751. if (iface->endpoint[i].extralen == 3) {
  2752. *ports = iface->endpoint[i].extra[2];
  2753. return 0;
  2754. }
  2755. }
  2756. return -1;
  2757. }
  2758. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2759. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2760. struct usb_device *usb, gfp_t gfp)
  2761. {
  2762. int result;
  2763. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2764. usb_rcvintpipe(usb,
  2765. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2766. shared_int->shared_intr_buf,
  2767. shared_int->intr_endp->wMaxPacketSize,
  2768. intr_callback, shared_int,
  2769. shared_int->intr_endp->bInterval);
  2770. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2771. if (result)
  2772. dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
  2773. result);
  2774. return result;
  2775. }
  2776. /* operations setup of the serial interface */
  2777. static const struct tty_operations hso_serial_ops = {
  2778. .open = hso_serial_open,
  2779. .close = hso_serial_close,
  2780. .write = hso_serial_write,
  2781. .write_room = hso_serial_write_room,
  2782. .ioctl = hso_serial_ioctl,
  2783. .set_termios = hso_serial_set_termios,
  2784. .chars_in_buffer = hso_serial_chars_in_buffer,
  2785. .tiocmget = hso_serial_tiocmget,
  2786. .tiocmset = hso_serial_tiocmset,
  2787. .unthrottle = hso_unthrottle
  2788. };
  2789. static struct usb_driver hso_driver = {
  2790. .name = driver_name,
  2791. .probe = hso_probe,
  2792. .disconnect = hso_disconnect,
  2793. .id_table = hso_ids,
  2794. .suspend = hso_suspend,
  2795. .resume = hso_resume,
  2796. .reset_resume = hso_resume,
  2797. .supports_autosuspend = 1,
  2798. };
  2799. static int __init hso_init(void)
  2800. {
  2801. int i;
  2802. int result;
  2803. /* put it in the log */
  2804. printk(KERN_INFO "hso: %s\n", version);
  2805. /* Initialise the serial table semaphore and table */
  2806. spin_lock_init(&serial_table_lock);
  2807. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2808. serial_table[i] = NULL;
  2809. /* allocate our driver using the proper amount of supported minors */
  2810. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2811. if (!tty_drv)
  2812. return -ENOMEM;
  2813. /* fill in all needed values */
  2814. tty_drv->magic = TTY_DRIVER_MAGIC;
  2815. tty_drv->owner = THIS_MODULE;
  2816. tty_drv->driver_name = driver_name;
  2817. tty_drv->name = tty_filename;
  2818. /* if major number is provided as parameter, use that one */
  2819. if (tty_major)
  2820. tty_drv->major = tty_major;
  2821. tty_drv->minor_start = 0;
  2822. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2823. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2824. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2825. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2826. tty_drv->init_termios = tty_std_termios;
  2827. hso_init_termios(&tty_drv->init_termios);
  2828. tty_set_operations(tty_drv, &hso_serial_ops);
  2829. /* register the tty driver */
  2830. result = tty_register_driver(tty_drv);
  2831. if (result) {
  2832. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2833. __func__, result);
  2834. return result;
  2835. }
  2836. /* register this module as an usb driver */
  2837. result = usb_register(&hso_driver);
  2838. if (result) {
  2839. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2840. result);
  2841. /* cleanup serial interface */
  2842. tty_unregister_driver(tty_drv);
  2843. return result;
  2844. }
  2845. /* done */
  2846. return 0;
  2847. }
  2848. static void __exit hso_exit(void)
  2849. {
  2850. printk(KERN_INFO "hso: unloaded\n");
  2851. tty_unregister_driver(tty_drv);
  2852. /* deregister the usb driver */
  2853. usb_deregister(&hso_driver);
  2854. }
  2855. /* Module definitions */
  2856. module_init(hso_init);
  2857. module_exit(hso_exit);
  2858. MODULE_AUTHOR(MOD_AUTHOR);
  2859. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2860. MODULE_LICENSE(MOD_LICENSE);
  2861. MODULE_INFO(Version, DRIVER_VERSION);
  2862. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2863. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2864. module_param(debug, int, S_IRUGO | S_IWUSR);
  2865. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2866. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2867. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2868. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2869. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2870. module_param(disable_net, int, S_IRUGO | S_IWUSR);